Skip to content

Commit

Permalink
Refactor youth training view (#2183)
Browse files Browse the repository at this point in the history
* refactor youth training view

* Fix HOTableModel.moveColumn illegal argument exception

* Fix player selection in lineup panel

* Fix editable user columns

* Fix editable user columns
  • Loading branch information
wsbrenk authored Nov 10, 2024
1 parent 40220fc commit e589a2e
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 61 deletions.
6 changes: 6 additions & 0 deletions src/main/java/core/gui/comp/table/HOTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/core/gui/comp/table/UserColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,6 @@ public void setSortPriority(Integer sortPriority) {
public void setSortOrder(SortOrder sortOrder) {
this.sortOrder = sortOrder;
}

public boolean isEditable() {return false;}
}
16 changes: 16 additions & 0 deletions src/main/java/core/gui/model/PlayerOverviewTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/module/lineup/LineupPlayersTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/module/playerOverview/PlayerOverviewTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/module/youth/YouthPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

public class YouthPanel extends JPanel {

private YouthPlayerView youthPlayerView;
private YouthTrainingView youthTrainingView;
private final YouthPlayerView youthPlayerView;
private final YouthTrainingView youthTrainingView;

public YouthPanel() {
setLayout(new BorderLayout());
youthPlayerView = new YouthPlayerView();
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);
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/module/youth/YouthTrainingColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ public boolean canBeDisabled() {
return false;
}


}
50 changes: 14 additions & 36 deletions src/main/java/module/youth/YouthTrainingView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -59,8 +38,7 @@ public void refresh() {

@Override
public void reInit() {
initModel();
repaint();
refresh();
}

public void storeUserSettings() {
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/module/youth/YouthTrainingViewTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@

## 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
* Sebastian Reddig 12
* Achilles 10
* Billy Dikkanen 10

Total 294
Total 559

0 comments on commit e589a2e

Please sign in to comment.