diff --git a/src/main/java/core/gui/comp/table/FixedColumnsTable.java b/src/main/java/core/gui/comp/table/FixedColumnsTable.java index 36f96f9d9..10acff452 100644 --- a/src/main/java/core/gui/comp/table/FixedColumnsTable.java +++ b/src/main/java/core/gui/comp/table/FixedColumnsTable.java @@ -8,7 +8,6 @@ import javax.swing.event.ListSelectionListener; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; -import javax.swing.table.TableColumnModel; import javax.swing.table.TableModel; import java.awt.*; import java.awt.event.AdjustmentListener; @@ -136,6 +135,9 @@ public FixedColumnsTable(HOTableModel tableModel, int fixedColumns) { }); scrollPane = new JScrollPane(); scrollPane.setViewportView(splitPane); + + tableModel.initTable(this); + } else { // No fixed columns fixed = null; @@ -165,6 +167,21 @@ public void setDefaultRenderer(Class columnClass, TableCellRenderer renderer) if ( fixed != null ) fixed.setDefaultRenderer(columnClass, renderer); } + public int getSelectedModelIndex(){ + var viewRowIndex = getSelectedRow(); + if (viewRowIndex > -1){ + return convertRowIndexToModel(viewRowIndex); + } + return -1; + } + + public void selectModelIndex(int modelIndex){ + if ( modelIndex > -1 ) { + var viewRowIndex = convertRowIndexToView(modelIndex); + setRowSelectionInterval(viewRowIndex, viewRowIndex); + } + } + /** * Add a list selection listener * @param listener ListSelectionListener diff --git a/src/main/java/core/gui/comp/table/HOTableModel.java b/src/main/java/core/gui/comp/table/HOTableModel.java index ebfbda86f..9c1e9d4a0 100644 --- a/src/main/java/core/gui/comp/table/HOTableModel.java +++ b/src/main/java/core/gui/comp/table/HOTableModel.java @@ -50,6 +50,7 @@ public abstract class HOTableModel extends AbstractTableModel { /** Table component **/ protected JTable table; + int selectedRow = -1; /** * Constructor * @@ -345,7 +346,23 @@ public void initTable(JTable table) { tableColumn.setIdentifier(userColumn.getId()); } getUserColumnSettings(table); + var rowSorter = new TableRowSorter<>(this); + rowSorter.addRowSorterListener(e -> { + // Sorting changed + switch (e.getType()){ + case SORT_ORDER_CHANGED -> selectedRow = table.getSelectedRow(); + case SORTED -> { + if ( selectedRow > -1) { + var modelIndex = e.convertPreviousRowIndexToModel(selectedRow); + if ( modelIndex > -1) { + var newSelectedRow = table.convertRowIndexToView(modelIndex); + table.setRowSelectionInterval(newSelectedRow, newSelectedRow); + } + } + } + } + }); getRowOrderSettings(rowSorter); table.setRowSorter(rowSorter); table.setDefaultRenderer(Object.class, new HODefaultTableCellRenderer()); diff --git a/src/main/java/module/lineup/LineupPlayersTable.java b/src/main/java/module/lineup/LineupPlayersTable.java index 81702b872..b64c5cf1d 100644 --- a/src/main/java/module/lineup/LineupPlayersTable.java +++ b/src/main/java/module/lineup/LineupPlayersTable.java @@ -31,7 +31,6 @@ public final class LineupPlayersTable extends FixedColumnsTable implements core. super(UserColumnController.instance().getLineupModel()); tableModel = (PlayerOverviewTableModel) this.getModel(); tableModel.setValues(HOVerwaltung.instance().getModel().getCurrentPlayers()); - tableModel.initTable(this); setDefaultRenderer(Object.class, new HODefaultTableCellRenderer()); setDefaultRenderer(Boolean.class, new BooleanTableCellRenderer()); RefreshManager.instance().registerRefreshable(this); diff --git a/src/main/java/module/playerOverview/PlayerOverviewTable.java b/src/main/java/module/playerOverview/PlayerOverviewTable.java index 7a3df6e23..ea4ac6a6d 100644 --- a/src/main/java/module/playerOverview/PlayerOverviewTable.java +++ b/src/main/java/module/playerOverview/PlayerOverviewTable.java @@ -44,7 +44,6 @@ public PlayerOverviewTable() { super(UserColumnController.instance().getPlayerOverviewModel()); tableModel = (PlayerOverviewTableModel)this.getModel(); tableModel.setValues(HOVerwaltung.instance().getModel().getCurrentPlayers()); - tableModel.initTable(this); setOpaque(false); RefreshManager.instance().registerRefreshable(this); diff --git a/src/main/java/module/youth/YouthPlayerView.java b/src/main/java/module/youth/YouthPlayerView.java index cfaad671f..1eb7a98cd 100644 --- a/src/main/java/module/youth/YouthPlayerView.java +++ b/src/main/java/module/youth/YouthPlayerView.java @@ -5,6 +5,7 @@ import core.gui.Refreshable; import core.gui.comp.panel.ImagePanel; import core.gui.comp.renderer.HODefaultTableCellRenderer; +import core.gui.comp.table.FixedColumnsTable; import core.gui.model.UserColumnController; import core.model.HOVerwaltung; import core.model.TranslationFacility; @@ -30,20 +31,26 @@ public class YouthPlayerView extends JPanel implements Refreshable, ListSelectio public static final String HORIZONTALSPLIT_POSITION = "YouthPlayerView.HorizontalSplitPosition"; private final HOLinesChart youthSkillChart; - private final JTable playerOverviewTable; - private YouthPlayerOverviewTableModel playerOverviewTableModel; - private YouthTableSorter playerOverviewTableSorter; + private final FixedColumnsTable playerOverviewTable; + private final YouthPlayerOverviewTableModel playerOverviewTableModel; private final JLabel playerNameLabel; private final YouthSkillInfoEditor[] playerSkillInfoEditors; private final JEditorPane playerScoutCommentField; - private final JTable playerDetailsTable; - private YouthPlayerDetailsTableModel playerDetailsTableModel; + private final YouthPlayerDetailsTableModel playerDetailsTableModel; public YouthPlayerView() { super(); - playerOverviewTable = new JTable(); - playerDetailsTable = new JTable(); + + setLayout(new BorderLayout()); + this.playerOverviewTableModel = UserColumnController.instance().getYouthPlayerOverviewColumnModel(); + playerOverviewTable = new FixedColumnsTable(this.playerOverviewTableModel); + var selectionModel = playerOverviewTable.getSelectionModel(); + selectionModel.addListSelectionListener(this); + + this.playerDetailsTableModel = UserColumnController.instance().getYouthPlayerDetailsColumnModel(); + FixedColumnsTable playerDetailsTable = new FixedColumnsTable(this.playerDetailsTableModel); + playerNameLabel = new JLabel(); playerScoutCommentField = new JEditorPane(); @@ -63,7 +70,7 @@ public YouthPlayerView() { setDividerLocation(split3, HORIZONTALSPLIT_POSITION, 800); // First section - split1.setLeftComponent(new JScrollPane(playerOverviewTable)); + split1.setLeftComponent(new JScrollPane(playerOverviewTable.getContainerComponent())); // Second section var developmentPanel = new JPanel(new BorderLayout()); @@ -71,7 +78,7 @@ public YouthPlayerView() { topLinePanel.add(playerNameLabel, BorderLayout.NORTH); topLinePanel.add(new JLabel(TranslationFacility.tr("ls.youth.player.trainingdevelopment"))); developmentPanel.add(topLinePanel, BorderLayout.NORTH); - developmentPanel.add(new JScrollPane(playerDetailsTable)); + developmentPanel.add(new JScrollPane(playerDetailsTable.getContainerComponent())); split1.setRightComponent(developmentPanel); // Third section @@ -126,7 +133,6 @@ public YouthPlayerView() { split3.setLeftComponent(new JScrollPane(scoutAndChartPanel)); split3.setRightComponent(new JScrollPane(skillEditorPanel)); - initModel(); RefreshManager.instance().registerRefreshable(this); playerOverviewTable.setDefaultRenderer(Object.class, new YouthPlayerOverviewTableCellRenderer()); playerDetailsTable.setDefaultRenderer(Object.class, new HODefaultTableCellRenderer()); @@ -144,54 +150,20 @@ public final void reInit() { refresh(); } - private void initModel() { - setLayout(new BorderLayout()); - initPlayerOverview(); - initPlayerDetails(); - } - - private void initPlayerOverview() { - playerOverviewTable.setOpaque(false); - if (playerOverviewTableModel == null) { - playerOverviewTableModel = UserColumnController.instance().getYouthPlayerOverviewColumnModel(); - playerOverviewTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); - playerOverviewTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - playerOverviewTable.setRowSelectionAllowed(true); - var selectionModel = playerOverviewTable.getSelectionModel(); - selectionModel.addListSelectionListener(this); - playerOverviewTableSorter = new YouthTableSorter(playerOverviewTableModel, playerOverviewTable); - playerOverviewTable.setModel(playerOverviewTableSorter); - playerOverviewTableModel.initTable(playerOverviewTable); - } - } - private boolean isRefreshingPlayerOverview=false; private void refreshPlayerOverview() { if ( isRefreshingPlayerOverview) return; try { isRefreshingPlayerOverview = true; - var selection = this.playerOverviewTableSorter.getSelectedModelIndex(); + var selection = this.playerOverviewTable.getSelectedModelIndex(); playerOverviewTableModel.initData(); - this.playerOverviewTableSorter.setSelectedModelIndex(selection); + this.playerOverviewTable.selectModelIndex(selection); } finally { isRefreshingPlayerOverview=false; } } - private void initPlayerDetails() { - if (playerDetailsTableModel == null) { - playerDetailsTableModel = UserColumnController.instance().getYouthPlayerDetailsColumnModel(); - playerDetailsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); - playerDetailsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - playerDetailsTable.setRowSelectionAllowed(true); - - YouthTableSorter playerDetailsTableSorter = new YouthTableSorter(playerDetailsTableModel, playerDetailsTable); - playerDetailsTable.setModel(playerDetailsTableSorter); - playerDetailsTableModel.initTable(playerDetailsTable); - } - } - private class CurrentValueChangeListener implements ChangeListener { @Override @@ -312,7 +284,7 @@ private YouthPlayer getSelectedPlayer() { initSelection(row); } if ( row > -1) { - var index = playerOverviewTableSorter.modelIndex(row); + var index = playerOverviewTable.getSelectedModelIndex(); var currentPlayers = HOVerwaltung.instance().getModel().getCurrentYouthPlayers(); if (currentPlayers != null && currentPlayers.size() > index) { return currentPlayers.get(index); @@ -324,7 +296,7 @@ private YouthPlayer getSelectedPlayer() { private void setSelectedPlayer(YouthPlayer selectedPlayer) { var currentPlayers = HOVerwaltung.instance().getModel().getCurrentYouthPlayers(); for (int row=0; row