source: josm/trunk/src/org/openstreetmap/josm/gui/conflict/tags/MultiValueCellEditor.java@ 6883

Last change on this file since 6883 was 6883, checked in by Don-vip, 10 years ago

fix some Sonar issues

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