1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.KeyboardFocusManager;
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.awt.event.KeyEvent;
|
---|
7 |
|
---|
8 | import javax.swing.AbstractAction;
|
---|
9 | import javax.swing.JComponent;
|
---|
10 | import javax.swing.JTable;
|
---|
11 | import javax.swing.KeyStroke;
|
---|
12 | import javax.swing.ListSelectionModel;
|
---|
13 | import javax.swing.table.TableColumnModel;
|
---|
14 | import javax.swing.table.TableModel;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Generic table offering custom cell navigation features.
|
---|
18 | * @since 9497
|
---|
19 | */
|
---|
20 | public abstract class JosmTable extends JTable {
|
---|
21 |
|
---|
22 | private int colEnd;
|
---|
23 |
|
---|
24 | protected SelectNextColumnCellAction selectNextColumnCellAction;
|
---|
25 | protected SelectPreviousColumnCellAction selectPreviousColumnCellAction;
|
---|
26 |
|
---|
27 | protected JosmTable(TableModel dm, TableColumnModel cm) {
|
---|
28 | this(dm, cm, null);
|
---|
29 | }
|
---|
30 |
|
---|
31 | protected JosmTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm) {
|
---|
32 | super(dm, cm, sm);
|
---|
33 | }
|
---|
34 |
|
---|
35 | protected void installCustomNavigation(int colEnd) {
|
---|
36 | // make ENTER behave like TAB
|
---|
37 | getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
|
---|
38 | KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "selectNextColumnCell");
|
---|
39 |
|
---|
40 | // install custom navigation actions
|
---|
41 | this.colEnd = colEnd;
|
---|
42 | selectNextColumnCellAction = new SelectNextColumnCellAction();
|
---|
43 | selectPreviousColumnCellAction = new SelectPreviousColumnCellAction();
|
---|
44 | getActionMap().put("selectNextColumnCell", selectNextColumnCellAction);
|
---|
45 | getActionMap().put("selectPreviousColumnCell", selectPreviousColumnCellAction);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Action to be run when the user navigates to the next cell in the table, for instance by
|
---|
50 | * pressing TAB or ENTER. The action alters the standard navigation path from cell to cell: <ul>
|
---|
51 | * <li>it jumps over cells in the first column</li>
|
---|
52 | * <li>it automatically add a new empty row when the user leaves the last cell in the table</li></ul>
|
---|
53 | */
|
---|
54 | protected class SelectNextColumnCellAction extends AbstractAction {
|
---|
55 | @Override
|
---|
56 | public void actionPerformed(ActionEvent e) {
|
---|
57 | int col = getSelectedColumn();
|
---|
58 | int row = getSelectedRow();
|
---|
59 | if (getCellEditor() != null) {
|
---|
60 | getCellEditor().stopCellEditing();
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (col == colEnd && row < getRowCount() - 1) {
|
---|
64 | row++;
|
---|
65 | } else if (row < getRowCount() - 1) {
|
---|
66 | col = colEnd;
|
---|
67 | row++;
|
---|
68 | } else {
|
---|
69 | // go to next component, no more rows in this table
|
---|
70 | KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
|
---|
71 | manager.focusNextComponent();
|
---|
72 | return;
|
---|
73 | }
|
---|
74 | changeSelection(row, col, false, false);
|
---|
75 | if (editCellAt(getSelectedRow(), getSelectedColumn())) {
|
---|
76 | getEditorComponent().requestFocusInWindow();
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Action to be run when the user navigates to the previous cell in the table, for instance by
|
---|
83 | * pressing Shift-TAB
|
---|
84 | */
|
---|
85 | protected class SelectPreviousColumnCellAction extends AbstractAction {
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public void actionPerformed(ActionEvent e) {
|
---|
89 | int col = getSelectedColumn();
|
---|
90 | int row = getSelectedRow();
|
---|
91 | if (getCellEditor() != null) {
|
---|
92 | getCellEditor().stopCellEditing();
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (col <= 0 && row <= 0) {
|
---|
96 | // change nothing
|
---|
97 | } else if (row > 0) {
|
---|
98 | col = colEnd;
|
---|
99 | row--;
|
---|
100 | }
|
---|
101 | changeSelection(row, col, false, false);
|
---|
102 | if (editCellAt(getSelectedRow(), getSelectedColumn())) {
|
---|
103 | getEditorComponent().requestFocusInWindow();
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|