Skip to content

Commit

Permalink
Fix youthplayer table (#2179)
Browse files Browse the repository at this point in the history
* Fix youth player table

* HOTableModel rowSorterListener

* Refactor YouthPlayerView

* Refactor YouthPlayerView

* remove TODOs from LineupPlayersTable.java

* remove comments form YouthPlayerView.java
  • Loading branch information
wsbrenk authored Nov 7, 2024
1 parent 4ae9a7b commit 5ea0938
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 51 deletions.
19 changes: 18 additions & 1 deletion src/main/java/core/gui/comp/table/FixedColumnsTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/core/gui/comp/table/HOTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public abstract class HOTableModel extends AbstractTableModel {
/** Table component **/
protected JTable table;

int selectedRow = -1;
/**
* Constructor
*
Expand Down Expand Up @@ -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());
Expand Down
1 change: 0 additions & 1 deletion src/main/java/module/lineup/LineupPlayersTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
68 changes: 20 additions & 48 deletions src/main/java/module/youth/YouthPlayerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -63,15 +70,15 @@ 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());
var topLinePanel = new JPanel(new BorderLayout());
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
Expand Down Expand Up @@ -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());
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -324,7 +296,7 @@ private YouthPlayer getSelectedPlayer() {
private void setSelectedPlayer(YouthPlayer selectedPlayer) {
var currentPlayers = HOVerwaltung.instance().getModel().getCurrentYouthPlayers();
for (int row=0; row<currentPlayers.size(); row++){
var index = playerOverviewTableSorter.modelIndex(row);
var index = playerOverviewTable.getSelectedModelIndex();
var player = currentPlayers.get(index);
if ( player != null && player.getId() == selectedPlayer.getId()){
this.playerOverviewTable.setRowSelectionInterval(row,row);
Expand Down

0 comments on commit 5ea0938

Please sign in to comment.