From e589a2e3764abfda3551bb308ebff6bc9ee4c666 Mon Sep 17 00:00:00 2001 From: wsbrenk Date: Sun, 10 Nov 2024 14:06:43 +0100 Subject: [PATCH] Refactor youth training view (#2183) * refactor youth training view * Fix HOTableModel.moveColumn illegal argument exception * Fix player selection in lineup panel * Fix editable user columns * Fix editable user columns --- .../core/gui/comp/table/HOTableModel.java | 6 +++ .../java/core/gui/comp/table/UserColumn.java | 2 + .../gui/model/PlayerOverviewTableModel.java | 16 ++++++ .../module/lineup/LineupPlayersTable.java | 10 ++-- .../playerOverview/PlayerOverviewTable.java | 6 +-- src/main/java/module/youth/YouthPanel.java | 6 +-- .../module/youth/YouthTrainingColumn.java | 1 - .../java/module/youth/YouthTrainingView.java | 50 ++++++------------- .../youth/YouthTrainingViewTableModel.java | 10 +--- src/main/resources/release_notes.md | 5 +- 10 files changed, 51 insertions(+), 61 deletions(-) diff --git a/src/main/java/core/gui/comp/table/HOTableModel.java b/src/main/java/core/gui/comp/table/HOTableModel.java index 092c70eb8..8e1b6403a 100644 --- a/src/main/java/core/gui/comp/table/HOTableModel.java +++ b/src/main/java/core/gui/comp/table/HOTableModel.java @@ -177,6 +177,11 @@ public final Object getValueAt(int row, int column) { return null; } + @Override + public boolean isCellEditable(int row, int column) { + return columns[column].isEditable(); + } + /** * Return row count * @return int @@ -356,6 +361,7 @@ public void initTable(JTable table) { var rowSorter = new TableRowSorter<>(this); rowSorter.addRowSorterListener(e -> { + // Restore the previous selection when table rows were sorted // Sorting changed switch (e.getType()){ case SORT_ORDER_CHANGED -> selectedRow = table.getSelectedRow(); diff --git a/src/main/java/core/gui/comp/table/UserColumn.java b/src/main/java/core/gui/comp/table/UserColumn.java index ef620fc8a..c1c687065 100644 --- a/src/main/java/core/gui/comp/table/UserColumn.java +++ b/src/main/java/core/gui/comp/table/UserColumn.java @@ -189,4 +189,6 @@ public void setSortPriority(Integer sortPriority) { public void setSortOrder(SortOrder sortOrder) { this.sortOrder = sortOrder; } + + public boolean isEditable() {return false;} } diff --git a/src/main/java/core/gui/model/PlayerOverviewTableModel.java b/src/main/java/core/gui/model/PlayerOverviewTableModel.java index d419f7152..f5198c3ee 100644 --- a/src/main/java/core/gui/model/PlayerOverviewTableModel.java +++ b/src/main/java/core/gui/model/PlayerOverviewTableModel.java @@ -101,6 +101,22 @@ public int getRowIndexOfPlayer(int playerId){ return -1; } + public Player getSelectedPlayer(){ + var rowIndex = this.table.getSelectedRow(); + if (rowIndex >= 0) { + return getPlayers().get(this.table.convertRowIndexToModel(rowIndex)); + } + return null; + + } + + public void selectPlayer(int playerId){ + var row = getRowIndexOfPlayer(playerId); + if ( row > -1 ) { + this.table.setRowSelectionInterval(row, row); + } + } + public Player getPlayerAtRow(int tableRow) { if (tableRow > -1 ) return m_vPlayers.get(this.table.convertRowIndexToModel(tableRow)); return null; diff --git a/src/main/java/module/lineup/LineupPlayersTable.java b/src/main/java/module/lineup/LineupPlayersTable.java index b64c5cf1d..9cc2b5f4d 100644 --- a/src/main/java/module/lineup/LineupPlayersTable.java +++ b/src/main/java/module/lineup/LineupPlayersTable.java @@ -39,10 +39,7 @@ public final class LineupPlayersTable extends FixedColumnsTable implements core. @Override public void setPlayer(int iPlayerID) { - var rowIndex = tableModel.getRowIndexOfPlayer(iPlayerID); - if (rowIndex >= 0) { - this.setRowSelectionInterval(rowIndex, rowIndex); - } + tableModel.selectPlayer(iPlayerID); } @Override @@ -52,14 +49,15 @@ public void setPlayer(int iPlayerID) { @Override public void reInit() { + var selectedPlayer = tableModel.getSelectedPlayer(); resetPlayers(); repaint(); + if ( selectedPlayer != null ) {tableModel.selectPlayer(selectedPlayer.getPlayerId());} } @Override public void refresh() { - resetPlayers(); - repaint(); + reInit(); } public PlayerOverviewTableModel getTableModel() { diff --git a/src/main/java/module/playerOverview/PlayerOverviewTable.java b/src/main/java/module/playerOverview/PlayerOverviewTable.java index ea4ac6a6d..0a4911f0a 100644 --- a/src/main/java/module/playerOverview/PlayerOverviewTable.java +++ b/src/main/java/module/playerOverview/PlayerOverviewTable.java @@ -85,11 +85,7 @@ public Player getSelectedPlayer(){ } public final void selectPlayer(int playerId) { - var index = tableModel.getPlayerIndex(playerId); - if (index >= 0) { - index = convertRowIndexToView(index); - this.setRowSelectionInterval(index, index); - } + tableModel.selectPlayer(playerId); } @Override diff --git a/src/main/java/module/youth/YouthPanel.java b/src/main/java/module/youth/YouthPanel.java index dab22b83e..0af62b454 100644 --- a/src/main/java/module/youth/YouthPanel.java +++ b/src/main/java/module/youth/YouthPanel.java @@ -7,8 +7,8 @@ public class YouthPanel extends JPanel { - private YouthPlayerView youthPlayerView; - private YouthTrainingView youthTrainingView; + private final YouthPlayerView youthPlayerView; + private final YouthTrainingView youthTrainingView; public YouthPanel() { setLayout(new BorderLayout()); @@ -16,7 +16,7 @@ public YouthPanel() { var tabbedPane = new JTabbedPane(); tabbedPane.addTab(TranslationFacility.tr("ls.youth.player"), this.youthPlayerView); youthTrainingView = new YouthTrainingView(); - tabbedPane.addTab(TranslationFacility.tr("ls.youth.training"), this.youthTrainingView); + tabbedPane.addTab(TranslationFacility.tr("ls.youth.training"), this.youthTrainingView.getContainerComponent()); add(tabbedPane, BorderLayout.CENTER); } diff --git a/src/main/java/module/youth/YouthTrainingColumn.java b/src/main/java/module/youth/YouthTrainingColumn.java index 1f74f3373..f3d24c02e 100644 --- a/src/main/java/module/youth/YouthTrainingColumn.java +++ b/src/main/java/module/youth/YouthTrainingColumn.java @@ -32,5 +32,4 @@ public boolean canBeDisabled() { return false; } - } diff --git a/src/main/java/module/youth/YouthTrainingView.java b/src/main/java/module/youth/YouthTrainingView.java index 3507416b7..3464dc85a 100644 --- a/src/main/java/module/youth/YouthTrainingView.java +++ b/src/main/java/module/youth/YouthTrainingView.java @@ -2,53 +2,32 @@ import core.gui.RefreshManager; import core.gui.Refreshable; -import core.gui.comp.renderer.HODefaultTableCellRenderer; +import core.gui.comp.table.FixedColumnsTable; import core.gui.model.UserColumnController; import javax.swing.*; -import javax.swing.table.TableColumnModel; -public class YouthTrainingView extends JScrollPane implements Refreshable { +public class YouthTrainingView extends FixedColumnsTable implements Refreshable { - private final JTable table; - private YouthTrainingViewTableModel tableModel; + private final YouthTrainingViewTableModel tableModel; public YouthTrainingView() { - table = new JTable(); - this.setViewportView(table); - initModel(); - RefreshManager.instance().registerRefreshable(this); - table.setDefaultRenderer(Object.class, new HODefaultTableCellRenderer()); - } - - private void initModel() { - setOpaque(false); - if (tableModel == null) { - tableModel = UserColumnController.instance().getYouthTrainingViewColumnModel(); - table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); - table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - table.setRowSelectionAllowed(true); - - table.setModel(tableModel); - TableColumnModel tableColumnModel = table.getColumnModel(); - for (int i = 0; i < tableModel.getColumnCount(); i++) { - tableColumnModel.getColumn(i).setIdentifier(i); - } - - for (var c : tableModel.getColumns()) { - if (c.canBeDisabled()) { - var tablecol = table.getColumn(c.getIndex()); - if (tablecol != null) { + super(UserColumnController.instance().getYouthTrainingViewColumnModel()); + tableModel = (YouthTrainingViewTableModel) this.getModel(); + for (var c : tableModel.getColumns()) { + if (c instanceof YouthTrainingColumn youthTrainingColumn) { + if (youthTrainingColumn.isEditable()) { + var tableColumn = getColumn(c.getId()); + if (tableColumn != null) { var cb = new JComboBox<>(new YouthTrainingTableEntry.ComboBoxModel()); var editor = new DefaultCellEditor(cb); - editor.addCellEditorListener(table); - tablecol.setCellEditor(editor); + editor.addCellEditorListener(this); + tableColumn.setCellEditor(editor); } } } - - tableModel.initTable(table); } tableModel.initData(); + RefreshManager.instance().registerRefreshable(this); } @Override @@ -59,8 +38,7 @@ public void refresh() { @Override public void reInit() { - initModel(); - repaint(); + refresh(); } public void storeUserSettings() { diff --git a/src/main/java/module/youth/YouthTrainingViewTableModel.java b/src/main/java/module/youth/YouthTrainingViewTableModel.java index d97650a89..079eb8b15 100644 --- a/src/main/java/module/youth/YouthTrainingViewTableModel.java +++ b/src/main/java/module/youth/YouthTrainingViewTableModel.java @@ -53,15 +53,14 @@ public IHOTableEntry getTableEntry(YouthTraining youthTraining){ return new YouthTrainingTableEntry(youthTraining.getTraining(YouthTraining.Priority.Primary)); } @Override - public boolean canBeDisabled(){return true;} + public boolean isEditable() {return true;} }, new YouthTrainingColumn(4, "ls.youth.training.secondary", 200){ @Override public IHOTableEntry getTableEntry(YouthTraining youthTraining){ return new YouthTrainingTableEntry(youthTraining.getTraining(YouthTraining.Priority.Secondary)); } - @Override - public boolean canBeDisabled(){return true;} + public boolean isEditable() {return true;} }, new YouthTrainingColumn(99, "ls.training.id", 0) { @@ -77,11 +76,6 @@ public boolean isDisplay() { }; } - @Override - public boolean isCellEditable(int row, int column) { - return columns[column].canBeDisabled(); - } - @Override protected void initData() { youthTrainings = HOVerwaltung.instance().getModel().getYouthTrainings() diff --git a/src/main/resources/release_notes.md b/src/main/resources/release_notes.md index 9c8cc4a74..2eec14c08 100644 --- a/src/main/resources/release_notes.md +++ b/src/main/resources/release_notes.md @@ -53,8 +53,9 @@ ## Translations -Reports by Contributors - June 23, 2024 - November 02, 2024 +Reports by Contributors - June 23, 2024 - November 09, 2024 +* Kristaps 265 * Tavaro 175 * wsbrenk 58 * Lidegang 29 @@ -62,4 +63,4 @@ Reports by Contributors - June 23, 2024 - November 02, 2024 * Achilles 10 * Billy Dikkanen 10 -Total 294 \ No newline at end of file +Total 559 \ No newline at end of file