source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/JosmTable.java@ 12892

Last change on this file since 12892 was 10070, checked in by Don-vip, 8 years ago

refactor duplicated code

File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.Container;
5import java.awt.Dimension;
6import java.awt.KeyboardFocusManager;
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9
10import javax.swing.AbstractAction;
11import javax.swing.JComponent;
12import javax.swing.JTable;
13import javax.swing.JViewport;
14import javax.swing.KeyStroke;
15import javax.swing.ListSelectionModel;
16import javax.swing.table.TableColumnModel;
17import javax.swing.table.TableModel;
18
19/**
20 * Generic table offering custom cell navigation features.
21 * @since 9497
22 */
23public 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}
Note: See TracBrowser for help on using the repository browser.