diff --git a/swing/src/net/sf/openrocket/gui/dialogs/preset/ComponentPresetChooserDialog.java b/swing/src/net/sf/openrocket/gui/dialogs/preset/ComponentPresetChooserDialog.java index 36b42640b..a71535a32 100644 --- a/swing/src/net/sf/openrocket/gui/dialogs/preset/ComponentPresetChooserDialog.java +++ b/swing/src/net/sf/openrocket/gui/dialogs/preset/ComponentPresetChooserDialog.java @@ -40,6 +40,7 @@ import net.sf.openrocket.rocketcomponent.SymmetricComponent; import net.sf.openrocket.startup.Application; import net.sf.openrocket.util.Chars; import net.sf.openrocket.gui.widgets.SelectColorButton; +import net.sf.openrocket.utils.TableRowTraversalPolicy; /** * Dialog shown for selecting a preset component. @@ -157,6 +158,9 @@ public class ComponentPresetChooserDialog extends JDialog { tc.setMaxWidth(w); tc.setMinWidth(w); + // The normal left/right and tab/shift-tab key action traverses each cell/column of the table instead of going to the next row. + TableRowTraversalPolicy.setTableRowTraversalPolicy(componentSelectionTable); + panel.add(getFilterCheckboxes(tm, legacyColumnIndex), "wrap para"); JScrollPane scrollpane = new JScrollPane(); diff --git a/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java b/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java index bd6ad592e..5d8cd2e53 100644 --- a/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java +++ b/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java @@ -10,7 +10,6 @@ import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.ActionEvent; -import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; @@ -19,8 +18,6 @@ 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; @@ -38,6 +35,7 @@ import javax.swing.event.ListSelectionListener; import javax.swing.table.DefaultTableCellRenderer; import net.sf.openrocket.gui.widgets.IconButton; +import net.sf.openrocket.utils.TableRowTraversalPolicy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -178,16 +176,7 @@ public class SimulationPanel extends JPanel { 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)); + TableRowTraversalPolicy.setTableRowTraversalPolicy(simulationTable); // Mouse listener to act on double-clicks simulationTable.addMouseListener(new MouseAdapter() { @@ -1029,67 +1018,4 @@ public class SimulationPanel extends JPanel { } } } - - 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); - } - } } diff --git a/swing/src/net/sf/openrocket/utils/TableRowTraversalPolicy.java b/swing/src/net/sf/openrocket/utils/TableRowTraversalPolicy.java new file mode 100644 index 000000000..77623f881 --- /dev/null +++ b/swing/src/net/sf/openrocket/utils/TableRowTraversalPolicy.java @@ -0,0 +1,100 @@ +package net.sf.openrocket.utils; + +import net.sf.openrocket.gui.main.SimulationPanel; + +import javax.swing.AbstractAction; +import javax.swing.ActionMap; +import javax.swing.InputMap; +import javax.swing.JTable; +import javax.swing.KeyStroke; +import java.awt.event.ActionEvent; +import java.awt.event.InputEvent; +import java.awt.event.KeyEvent; + +/** + * Helper class for setting a JTable to traverse rows rather than columns, when the tab/shift-tab, or right-arrow/left-arrow + * key is pressed. + * + * @author Sibo Van Gool + */ +public abstract class TableRowTraversalPolicy { + /** + * Applies the row traversal policy to the given table. + * @param table table to apply the row traversal policy to + */ + public static void setTableRowTraversalPolicy(final JTable table) { + InputMap im = table.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 = table.getActionMap(); + am.put("Action.NextRow", new NextRowAction(table, false)); + am.put("Action.NextRowCycle", new NextRowAction(table, true)); + am.put("Action.PreviousRow", new PreviousRowAction(table, false)); + am.put("Action.PreviousRowCycle", new PreviousRowAction(table, true)); + } + + 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); + } + } +}