1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.conflict.tags;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Font;
|
---|
8 | import java.awt.event.FocusAdapter;
|
---|
9 | import java.awt.event.FocusEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractCellEditor;
|
---|
14 | import javax.swing.DefaultComboBoxModel;
|
---|
15 | import javax.swing.JComboBox;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JList;
|
---|
18 | import javax.swing.JTable;
|
---|
19 | import javax.swing.ListCellRenderer;
|
---|
20 | import javax.swing.UIManager;
|
---|
21 | import javax.swing.table.TableCellEditor;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * This is a table cell editor for selecting a possible tag value from a list of
|
---|
25 | * proposed tag values. The editor also allows to select all proposed valued or
|
---|
26 | * to remove the tag.
|
---|
27 | *
|
---|
28 | * The editor responds intercepts some keys and interprets them as navigation keys. It
|
---|
29 | * forwards navigation events to {@see NavigationListener}s registred with this editor.
|
---|
30 | * You should register the parent table using this editor as {@see NavigationListener}.
|
---|
31 | *
|
---|
32 | * {@see KeyEvent#VK_ENTER} and {@see KeyEvent#VK_TAB} trigger a {@see NavigationListener#gotoNextDecision()}.
|
---|
33 | */
|
---|
34 | public class MultiValueCellEditor extends AbstractCellEditor implements TableCellEditor{
|
---|
35 |
|
---|
36 | public static interface NavigationListener {
|
---|
37 | void gotoNextDecision();
|
---|
38 | void gotoPreviousDecision();
|
---|
39 | }
|
---|
40 |
|
---|
41 | /** the combo box used as editor */
|
---|
42 | private JComboBox editor;
|
---|
43 | private DefaultComboBoxModel editorModel;
|
---|
44 | private CopyOnWriteArrayList<NavigationListener> listeners;
|
---|
45 |
|
---|
46 | public void addNavigationListeners(NavigationListener listener) {
|
---|
47 | if (listener != null && ! (listeners.contains(listener))) {
|
---|
48 | listeners.add(listener);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void removeavigationListeners(NavigationListener listener) {
|
---|
53 | if (listener != null && listeners.contains(listener)) {
|
---|
54 | listeners.remove(listener);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected void fireGotoNextDecision() {
|
---|
59 | for (NavigationListener l: listeners) {
|
---|
60 | l.gotoNextDecision();
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected void fireGotoPreviousDecision() {
|
---|
65 | for (NavigationListener l: listeners) {
|
---|
66 | l.gotoPreviousDecision();
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public MultiValueCellEditor() {
|
---|
71 | editorModel = new DefaultComboBoxModel();
|
---|
72 | editor = new JComboBox(editorModel) {
|
---|
73 | @Override
|
---|
74 | public void processKeyEvent(KeyEvent e) {
|
---|
75 | if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_ENTER) {
|
---|
76 | fireGotoNextDecision();
|
---|
77 | } if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_TAB) {
|
---|
78 | if (e.isShiftDown()) {
|
---|
79 | fireGotoPreviousDecision();
|
---|
80 | } else {
|
---|
81 | fireGotoNextDecision();
|
---|
82 | }
|
---|
83 | } else if ( e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_DELETE || e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
|
---|
84 | if (editorModel.getIndexOf(MultiValueDecisionType.KEEP_NONE) > 0) {
|
---|
85 | editorModel.setSelectedItem(MultiValueDecisionType.KEEP_NONE);
|
---|
86 | fireGotoNextDecision();
|
---|
87 | }
|
---|
88 | } else if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
---|
89 | cancelCellEditing();
|
---|
90 | }
|
---|
91 | super.processKeyEvent(e);
|
---|
92 | }
|
---|
93 | };
|
---|
94 | editor.addFocusListener(
|
---|
95 | new FocusAdapter() {
|
---|
96 | @Override
|
---|
97 | public void focusGained(FocusEvent e) {
|
---|
98 | editor.showPopup();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | );
|
---|
102 | editor.setRenderer(new EditorCellRenderer());
|
---|
103 | listeners = new CopyOnWriteArrayList<NavigationListener>();
|
---|
104 | }
|
---|
105 |
|
---|
106 | protected void initEditor(MultiValueResolutionDecision decision) {
|
---|
107 | editorModel.removeAllElements();
|
---|
108 | for (String value: decision.getValues()) {
|
---|
109 | editorModel.addElement(value);
|
---|
110 | }
|
---|
111 | if (decision.canKeepNone()) {
|
---|
112 | editorModel.addElement(MultiValueDecisionType.KEEP_NONE);
|
---|
113 | }
|
---|
114 | if (decision.canKeepAll()) {
|
---|
115 | editorModel.addElement(MultiValueDecisionType.KEEP_ALL);
|
---|
116 | }
|
---|
117 | switch(decision.getDecisionType()) {
|
---|
118 | case UNDECIDED:
|
---|
119 | editor.setSelectedIndex(0);
|
---|
120 | break;
|
---|
121 | case KEEP_ONE:
|
---|
122 | editor.setSelectedItem(decision.getChosenValue());
|
---|
123 | break;
|
---|
124 | case KEEP_NONE:
|
---|
125 | editor.setSelectedItem(MultiValueDecisionType.KEEP_NONE);
|
---|
126 | break;
|
---|
127 | case KEEP_ALL:
|
---|
128 | editor.setSelectedItem(MultiValueDecisionType.KEEP_ALL);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
133 | MultiValueResolutionDecision decision = (MultiValueResolutionDecision)value;
|
---|
134 | initEditor(decision);
|
---|
135 | editor.requestFocus();
|
---|
136 | return editor;
|
---|
137 | }
|
---|
138 |
|
---|
139 | public Object getCellEditorValue() {
|
---|
140 | return editor.getSelectedItem();
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * The cell renderer used in the combo box
|
---|
145 | *
|
---|
146 | */
|
---|
147 | static private class EditorCellRenderer extends JLabel implements ListCellRenderer {
|
---|
148 |
|
---|
149 | public EditorCellRenderer() {
|
---|
150 | setOpaque(true);
|
---|
151 | }
|
---|
152 |
|
---|
153 | protected void renderColors(boolean selected) {
|
---|
154 | if (selected) {
|
---|
155 | setForeground( UIManager.getColor("ComboBox.selectionForeground"));
|
---|
156 | setBackground(UIManager.getColor("ComboBox.selectionBackground"));
|
---|
157 | } else {
|
---|
158 | setForeground( UIManager.getColor("ComboBox.foreground"));
|
---|
159 | setBackground(UIManager.getColor("ComboBox.background"));
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | protected void renderValue(Object value) {
|
---|
164 | setFont(UIManager.getFont("ComboBox.font"));
|
---|
165 | if (String.class.isInstance(value)) {
|
---|
166 | setText(String.class.cast(value));
|
---|
167 | } else if (MultiValueDecisionType.class.isInstance(value)) {
|
---|
168 | switch(MultiValueDecisionType.class.cast(value)) {
|
---|
169 | case KEEP_NONE:
|
---|
170 | setText(tr("none"));
|
---|
171 | setFont(UIManager.getFont("ComboBox.font").deriveFont(Font.ITALIC + Font.BOLD));
|
---|
172 | break;
|
---|
173 | case KEEP_ALL:
|
---|
174 | setText(tr("all"));
|
---|
175 | setFont(UIManager.getFont("ComboBox.font").deriveFont(Font.ITALIC + Font.BOLD));
|
---|
176 | break;
|
---|
177 | default:
|
---|
178 | // don't display other values
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
|
---|
184 | boolean cellHasFocus) {
|
---|
185 | renderColors(isSelected);
|
---|
186 | renderValue(value);
|
---|
187 | return this;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|