move duplicate ColumnListener code into super class.

master
lodici 2017-02-04 15:34:18 +00:00
parent 761e2e8240
commit f5483c8c6e
4 changed files with 51 additions and 141 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 javax.swing.table.TableColumnModel;
import magic.data.GeneralConfig;
import magic.model.MagicCardDefinition;
import magic.ui.FontsAndBorders;
@ -134,30 +133,6 @@ public class CardTablePanelA extends CardsTablePanel {
return selectedCards;
}
private void reselectLastCards() {
// select previous card if possible
if (lastSelectedCards.size() > 0) {
final List<MagicCardDefinition> newSelectedCards = new ArrayList<>();
for (final MagicCardDefinition card : lastSelectedCards) {
final int index = tableModel.findCardIndex(card);
if (index != -1) {
// previous card still in list
table.getSelectionModel().addSelectionInterval(index,index);
newSelectedCards.add(card);
}
}
lastSelectedCards = newSelectedCards;
} else {
setSelectedRow();
}
}
private void setSelectedRow() {
if (table.getRowCount() > 0) {
table.setRowSelectionInterval(0, 0);
}
}
public void setCards(final List<MagicCardDefinition> defs) {
final boolean isRowSelected = table.getSelectedRow() != -1;
tableModel.setCards(defs);
@ -194,28 +169,6 @@ public class CardTablePanelA extends CardsTablePanel {
return table;
}
private class ColumnListener extends MouseAdapter {
@Override
public void mouseClicked(final MouseEvent e) {
final TableColumnModel colModel = table.getColumnModel();
final int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
final int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();
if (modelIndex < 0) {
return;
}
// sort
tableModel.sort(modelIndex);
// redraw
table.tableChanged(new TableModelEvent(tableModel));
table.repaint();
reselectLastCards();
}
}
private class RowMouseOverListener extends MouseAdapter {
@Override
public void mouseMoved(final MouseEvent e) {

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 javax.swing.table.TableColumnModel;
import magic.data.GeneralConfig;
import magic.model.MagicCardDefinition;
import magic.model.MagicRandom;
@ -98,30 +97,6 @@ public class CardTablePanelB extends CardsTablePanel
return selectedCards;
}
private void reselectLastCards() {
// select previous card if possible
if (lastSelectedCards.size() > 0) {
final List<MagicCardDefinition> newSelectedCards = new ArrayList<>();
for (final MagicCardDefinition card : lastSelectedCards) {
final int index = tableModel.findCardIndex(card);
if (index != -1) {
// previous card still in list
table.getSelectionModel().addSelectionInterval(index,index);
newSelectedCards.add(card);
}
}
lastSelectedCards = newSelectedCards;
} else {
setSelectedRow();
}
}
private void setSelectedRow() {
if (table.getRowCount() > 0) {
table.setRowSelectionInterval(0, 0);
}
}
public void setCards(final List<MagicCardDefinition> defs) {
tableModel.setCards(defs);
table.tableChanged(new TableModelEvent(tableModel));
@ -187,28 +162,6 @@ public class CardTablePanelB extends CardsTablePanel
setEmptyBackgroundColor();
}
private class ColumnListener extends MouseAdapter {
@Override
public void mouseClicked(final MouseEvent e) {
final TableColumnModel colModel = table.getColumnModel();
final int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
final int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();
if (modelIndex < 0) {
return;
}
// sort
tableModel.sort(modelIndex);
// redraw
table.tableChanged(new TableModelEvent(tableModel));
table.repaint();
reselectLastCards();
}
}
private class RowMouseOverListener extends MouseAdapter {
@Override
public void mouseMoved(final MouseEvent e) {

View File

@ -1,6 +1,11 @@
package magic.ui.widget.cards.table;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.table.TableColumnModel;
import magic.model.MagicCardDefinition;
import magic.ui.widget.M.MScrollPane;
import magic.ui.widget.TexturedPanel;
@ -19,4 +24,50 @@ class CardsTablePanel extends TexturedPanel {
public CardsTablePanel(List<MagicCardDefinition> defs) {
tableModel = new CardTableModel(defs);
}
private void setSelectedRow() {
if (table.getRowCount() > 0) {
table.setRowSelectionInterval(0, 0);
}
}
protected void reselectLastCards() {
// select previous card if possible
if (lastSelectedCards.size() > 0) {
final List<MagicCardDefinition> newSelectedCards = new ArrayList<>();
for (final MagicCardDefinition card : lastSelectedCards) {
final int index = tableModel.findCardIndex(card);
if (index != -1) {
// previous card still in list
table.getSelectionModel().addSelectionInterval(index,index);
newSelectedCards.add(card);
}
}
lastSelectedCards = newSelectedCards;
} else {
setSelectedRow();
}
}
protected class ColumnListener extends MouseAdapter {
@Override
public void mouseClicked(final MouseEvent e) {
final TableColumnModel colModel = table.getColumnModel();
final int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
final int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();
if (modelIndex < 0) {
return;
}
// sort
tableModel.sort(modelIndex);
// redraw
table.tableChanged(new TableModelEvent(tableModel));
table.repaint();
reselectLastCards();
}
}
}

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 javax.swing.table.TableColumnModel;
import magic.data.GeneralConfig;
import magic.model.MagicCardDefinition;
import magic.model.MagicDeck;
@ -138,30 +137,6 @@ public class DeckTablePanel extends CardsTablePanel {
return selectedCards;
}
private void reselectLastCards() {
// select previous card if possible
if (lastSelectedCards.size() > 0) {
final List<MagicCardDefinition> newSelectedCards = new ArrayList<>();
for (final MagicCardDefinition card : lastSelectedCards) {
final int index = tableModel.findCardIndex(card);
if (index != -1) {
// previous card still in list
table.getSelectionModel().addSelectionInterval(index,index);
newSelectedCards.add(card);
}
}
lastSelectedCards = newSelectedCards;
} else {
setSelectedRow();
}
}
private void setSelectedRow() {
if (table.getRowCount() > 0) {
table.setRowSelectionInterval(0, 0);
}
}
public void setDeck(MagicDeck aDeck) {
tableModel.setCards(aDeck);
table.tableChanged(new TableModelEvent(tableModel));
@ -233,26 +208,4 @@ public class DeckTablePanel extends CardsTablePanel {
tableModel.showCardCount(b);
}
private class ColumnListener extends MouseAdapter {
@Override
public void mouseClicked(final MouseEvent e) {
final TableColumnModel colModel = table.getColumnModel();
final int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
final int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();
if (modelIndex < 0) {
return;
}
// sort
tableModel.sort(modelIndex);
// redraw
table.tableChanged(new TableModelEvent(tableModel));
table.repaint();
reselectLastCards();
}
}
}