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