consolidate duplicate code into super class.

master
lodici 2017-02-04 17:02:27 +00:00
parent d658abc70a
commit 297ed6c188
4 changed files with 52 additions and 48 deletions

View File

@ -11,7 +11,6 @@ import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.JTableHeader;
import magic.data.GeneralConfig;
import magic.model.MagicCardDefinition;
import magic.ui.FontsAndBorders;
@ -31,10 +30,6 @@ public class CardTablePanelA extends CardsTablePanel {
public CardTablePanelA(final List<MagicCardDefinition> defs, final String title) {
super(defs, title);
if (!GeneralConfig.getInstance().isPreviewCardOnSelect()) {
table.addMouseMotionListener(new RowMouseOverListener());
}
// listener to change card image on selection
table.getSelectionModel().addListSelectionListener(getTableListSelectionListener());
@ -57,6 +52,20 @@ public class CardTablePanelA extends CardsTablePanel {
this(defs, "");
}
@Override
protected MouseAdapter getRowMouseOverListener() {
return new MouseAdapter() {
@Override
public void mouseMoved(final MouseEvent e) {
final Point p = e.getPoint();
final int row = table.rowAtPoint(p);
if (row != lastSelectedRow) {
lastSelectedRow = row;
}
}
};
}
private void setEmptyBackgroundColor() {
setBackground(CardsTableStyle.getStyle().getEmptyBackgroundColor());
}
@ -141,17 +150,6 @@ public class CardTablePanelA extends CardsTablePanel {
return table;
}
private class RowMouseOverListener extends MouseAdapter {
@Override
public void mouseMoved(final MouseEvent e) {
final Point p = e.getPoint();
final int row = table.rowAtPoint(p);
if (row != lastSelectedRow) {
lastSelectedRow = row;
}
}
}
private boolean hasDoubleClickListeners() {
return getPropertyChangeListeners(CP_CARD_DCLICKED).length > 0;
}

View File

@ -13,7 +13,6 @@ import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.JTableHeader;
import magic.data.GeneralConfig;
import magic.model.MagicCardDefinition;
import magic.model.MagicRandom;
import magic.ui.FontsAndBorders;
@ -30,10 +29,6 @@ public class CardTablePanelB extends CardsTablePanel
this.isDeck = isDeck;
if (!GeneralConfig.getInstance().isPreviewCardOnSelect()) {
table.addMouseMotionListener(new RowMouseOverListener());
}
// listener to change card image on selection
table.getSelectionModel().addListSelectionListener(this);
@ -49,6 +44,19 @@ public class CardTablePanelB extends CardsTablePanel
setEmptyBackgroundColor();
}
@Override
protected MouseAdapter getRowMouseOverListener() {
return new MouseAdapter() {
@Override
public void mouseMoved(final MouseEvent e) {
final Point p = e.getPoint();
final int row = table.rowAtPoint(p);
final MagicCardDefinition card = tableModel.getCardDef(row);
notifyCardSelectionListeners(card);
}
};
}
public CardTablePanelB(List<MagicCardDefinition> defs, boolean isDeck) {
this(defs, "", isDeck);
}
@ -129,16 +137,6 @@ public class CardTablePanelB extends CardsTablePanel
setEmptyBackgroundColor();
}
private class RowMouseOverListener extends MouseAdapter {
@Override
public void mouseMoved(final MouseEvent e) {
final Point p = e.getPoint();
final int row = table.rowAtPoint(p);
final MagicCardDefinition card = tableModel.getCardDef(row);
notifyCardSelectionListeners(card);
}
}
public void addCardSelectionListener(final ICardSelectionListener listener) {
cardSelectionListeners.add(listener);
}

View File

@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.table.TableColumnModel;
import magic.data.GeneralConfig;
import magic.model.MagicCardDefinition;
import magic.ui.widget.M.MScrollPane;
import magic.ui.widget.TexturedPanel;
@ -13,7 +14,7 @@ import magic.ui.widget.TitleBar;
import net.miginfocom.swing.MigLayout;
@SuppressWarnings("serial")
class CardsTablePanel extends TexturedPanel {
abstract class CardsTablePanel extends TexturedPanel {
protected final MScrollPane scrollpane = new MScrollPane();
private final TitleBar titleBar;
@ -23,11 +24,20 @@ class CardsTablePanel extends TexturedPanel {
protected final CardTableModel tableModel;
protected List<MagicCardDefinition> lastSelectedCards = new ArrayList<>();
protected abstract MouseAdapter getRowMouseOverListener();
public CardsTablePanel(List<MagicCardDefinition> defs, String title) {
tableModel = new CardTableModel(defs);
table = new CardsJTable(tableModel);
titleBar = new TitleBar(title);
setTitle(title);
if (!GeneralConfig.getInstance().isPreviewCardOnSelect()) {
table.addMouseMotionListener(getRowMouseOverListener());
}
setLayout(migLayout);
refreshLayout();
}

View File

@ -11,7 +11,6 @@ import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.JTableHeader;
import magic.data.GeneralConfig;
import magic.model.MagicCardDefinition;
import magic.model.MagicDeck;
import magic.ui.FontsAndBorders;
@ -32,10 +31,6 @@ public class DeckTablePanel extends CardsTablePanel {
public DeckTablePanel(final List<MagicCardDefinition> defs) {
super(defs);
if (!GeneralConfig.getInstance().isPreviewCardOnSelect()) {
table.addMouseMotionListener(new RowMouseOverListener());
}
// listener to change card image on selection
this.listSelListener = getTableListSelectionListener();
table.getSelectionModel().addListSelectionListener(listSelListener);
@ -55,6 +50,20 @@ public class DeckTablePanel extends CardsTablePanel {
setEmptyBackgroundColor();
}
@Override
protected MouseAdapter getRowMouseOverListener() {
return new MouseAdapter() {
@Override
public void mouseMoved(final MouseEvent e) {
final Point p = e.getPoint();
final int row = table.rowAtPoint(p);
if (row != lastSelectedRow) {
lastSelectedRow = row;
}
}
};
}
private void setEmptyBackgroundColor() {
setBackground(CardsTableStyle.getStyle().getEmptyBackgroundColor());
}
@ -147,17 +156,6 @@ public class DeckTablePanel extends CardsTablePanel {
scrollpane.setViewportView(table);
}
private class RowMouseOverListener extends MouseAdapter {
@Override
public void mouseMoved(final MouseEvent e) {
final Point p = e.getPoint();
final int row = table.rowAtPoint(p);
if (row != lastSelectedRow) {
lastSelectedRow = row;
}
}
}
public void selectFirstRow() {
if (table.getRowCount() > 0) {
table.setRowSelectionInterval(0, 0);