Merge pull request #1556 from SiboVG/issue-1552

[#1552] Use tab and arrow keys to traverse sim table
This commit is contained in:
SiboVG 2022-08-02 22:51:04 +02:00 committed by GitHub
commit 04409e51a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,8 @@ import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
@ -18,6 +20,8 @@ import java.util.Arrays;
import java.util.Comparator;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
@ -153,8 +157,6 @@ public class SimulationPanel extends JPanel {
//////// The simulation table
simulationTableModel = new SimulationTableModel();
// Override processKeyBinding so that the JTable does not catch
// key bindings used in menu accelerators
simulationTable = new ColumnTable(simulationTableModel) {
private static final long serialVersionUID = -5799340181229735630L;
};
@ -173,6 +175,17 @@ public class SimulationPanel extends JPanel {
pm.add(runSimulationAction);
pm.add(plotSimulationAction);
// The normal left/right and tab/shift-tab key action traverses each cell/column of the table instead of going to the next row.
InputMap im = simulationTable.getInputMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "Action.NextRowCycle");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "Action.NextRow");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK), "Action.PreviousRowCycle");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "Action.PreviousRow");
ActionMap am = simulationTable.getActionMap();
am.put("Action.NextRow", new NextRowAction(simulationTable, false));
am.put("Action.NextRowCycle", new NextRowAction(simulationTable, true));
am.put("Action.PreviousRow", new PreviousRowAction(simulationTable, false));
am.put("Action.PreviousRowCycle", new PreviousRowAction(simulationTable, true));
// Mouse listener to act on double-clicks
simulationTable.addMouseListener(new MouseAdapter() {
@ -973,4 +986,68 @@ public class SimulationPanel extends JPanel {
return document.getSimulationCount();
}
}
private static class NextRowAction extends AbstractAction {
private final JTable table;
private final boolean cycle;
/**
* Action for cycling through the next row of the table.
* @param table table for which the action is intended
* @param cycle whether to go back to the first row if the end is reached.
*/
public NextRowAction(JTable table, boolean cycle) {
this.table = table;
this.cycle = cycle;
}
@Override
public void actionPerformed(ActionEvent e) {
int nextRow = table.getSelectedRow() + 1;
if (nextRow >= table.getRowCount()) {
if (cycle) {
nextRow = 0;
} else {
return;
}
}
table.getSelectionModel().setSelectionInterval(nextRow, nextRow);
table.getColumnModel().getSelectionModel().setSelectionInterval(0, 0);
}
}
private static class PreviousRowAction extends AbstractAction {
private final JTable table;
private final boolean cycle;
/**
* Action for cycling through the previous row of the table.
* @param table table for which the action is intended
* @param cycle whether to go back to the last row if the current row is the first one of the table.
*/
public PreviousRowAction(JTable table, boolean cycle) {
this.table = table;
this.cycle = cycle;
}
@Override
public void actionPerformed(ActionEvent e) {
int nextRow = table.getSelectedRow() - 1;
if (nextRow < 0) {
if (cycle) {
nextRow = table.getRowCount() - 1;
} else {
return;
}
}
table.getSelectionModel().setSelectionInterval(nextRow, nextRow);
table.getColumnModel().getSelectionModel().setSelectionInterval(0, 0);
}
}
}