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