From 297ed6c188978ad1ebfbac775a8057ada02295cb Mon Sep 17 00:00:00 2001 From: lodici Date: Sat, 4 Feb 2017 17:02:27 +0000 Subject: [PATCH] consolidate duplicate code into super class. --- .../widget/cards/table/CardTablePanelA.java | 30 +++++++++---------- .../widget/cards/table/CardTablePanelB.java | 28 ++++++++--------- .../widget/cards/table/CardsTablePanel.java | 12 +++++++- .../ui/widget/cards/table/DeckTablePanel.java | 30 +++++++++---------- 4 files changed, 52 insertions(+), 48 deletions(-) diff --git a/src/magic/ui/widget/cards/table/CardTablePanelA.java b/src/magic/ui/widget/cards/table/CardTablePanelA.java index a08d7555a1..1b7345229b 100644 --- a/src/magic/ui/widget/cards/table/CardTablePanelA.java +++ b/src/magic/ui/widget/cards/table/CardTablePanelA.java @@ -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 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; } diff --git a/src/magic/ui/widget/cards/table/CardTablePanelB.java b/src/magic/ui/widget/cards/table/CardTablePanelB.java index 942ec03d60..0fc9a54746 100644 --- a/src/magic/ui/widget/cards/table/CardTablePanelB.java +++ b/src/magic/ui/widget/cards/table/CardTablePanelB.java @@ -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 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); } diff --git a/src/magic/ui/widget/cards/table/CardsTablePanel.java b/src/magic/ui/widget/cards/table/CardsTablePanel.java index 144b758a21..65a00c4306 100644 --- a/src/magic/ui/widget/cards/table/CardsTablePanel.java +++ b/src/magic/ui/widget/cards/table/CardsTablePanel.java @@ -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 lastSelectedCards = new ArrayList<>(); + protected abstract MouseAdapter getRowMouseOverListener(); + public CardsTablePanel(List 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(); } diff --git a/src/magic/ui/widget/cards/table/DeckTablePanel.java b/src/magic/ui/widget/cards/table/DeckTablePanel.java index eb1a2636e8..996242b54b 100644 --- a/src/magic/ui/widget/cards/table/DeckTablePanel.java +++ b/src/magic/ui/widget/cards/table/DeckTablePanel.java @@ -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 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);