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