source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/JosmComboBox.java@ 8005

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

fix #11064 - disable undo/redo feature for table cell editors

  • Property svn:eol-style set to native
File size: 9.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.Component;
5import java.awt.Dimension;
6import java.awt.Toolkit;
7import java.awt.event.MouseAdapter;
8import java.awt.event.MouseEvent;
9import java.beans.PropertyChangeEvent;
10import java.beans.PropertyChangeListener;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Collection;
14import java.util.List;
15
16import javax.accessibility.Accessible;
17import javax.swing.ComboBoxModel;
18import javax.swing.DefaultComboBoxModel;
19import javax.swing.JComboBox;
20import javax.swing.JList;
21import javax.swing.plaf.basic.ComboPopup;
22import javax.swing.text.JTextComponent;
23
24/**
25 * Class overriding each {@link JComboBox} in JOSM to control consistently the number of displayed items at once.<br>
26 * This is needed because of the default Java behaviour that may display the top-down list off the screen (see #7917).
27 * @param <E> the type of the elements of this combo box
28 *
29 * @since 5429 (creation)
30 * @since 7015 (generics for Java 7)
31 */
32public class JosmComboBox<E> extends JComboBox<E> {
33
34 /**
35 * Creates a <code>JosmComboBox</code> with a default data model.
36 * The default data model is an empty list of objects.
37 * Use <code>addItem</code> to add items. By default the first item
38 * in the data model becomes selected.
39 *
40 * @see DefaultComboBoxModel
41 */
42 public JosmComboBox() {
43 init(null);
44 }
45
46 /**
47 * Creates a <code>JosmComboBox</code> with a default data model and
48 * the specified prototype display value.
49 * The default data model is an empty list of objects.
50 * Use <code>addItem</code> to add items. By default the first item
51 * in the data model becomes selected.
52 *
53 * @param prototypeDisplayValue the <code>Object</code> used to compute
54 * the maximum number of elements to be displayed at once before
55 * displaying a scroll bar
56 *
57 * @see DefaultComboBoxModel
58 * @since 5450
59 */
60 public JosmComboBox(E prototypeDisplayValue) {
61 init(prototypeDisplayValue);
62 }
63
64 /**
65 * Creates a <code>JosmComboBox</code> that takes its items from an
66 * existing <code>ComboBoxModel</code>. Since the
67 * <code>ComboBoxModel</code> is provided, a combo box created using
68 * this constructor does not create a default combo box model and
69 * may impact how the insert, remove and add methods behave.
70 *
71 * @param aModel the <code>ComboBoxModel</code> that provides the
72 * displayed list of items
73 * @see DefaultComboBoxModel
74 */
75 public JosmComboBox(ComboBoxModel<E> aModel) {
76 super(aModel);
77 List<E> list = new ArrayList<>(aModel.getSize());
78 for (int i = 0; i<aModel.getSize(); i++) {
79 list.add(aModel.getElementAt(i));
80 }
81 init(findPrototypeDisplayValue(list));
82 }
83
84 /**
85 * Creates a <code>JosmComboBox</code> that contains the elements
86 * in the specified array. By default the first item in the array
87 * (and therefore the data model) becomes selected.
88 *
89 * @param items an array of objects to insert into the combo box
90 * @see DefaultComboBoxModel
91 */
92 public JosmComboBox(E[] items) {
93 super(items);
94 init(findPrototypeDisplayValue(Arrays.asList(items)));
95 }
96
97 /**
98 * Finds the prototype display value to use among the given possible candidates.
99 * @param possibleValues The possible candidates that will be iterated.
100 * @return The value that needs the largest display height on screen.
101 * @since 5558
102 */
103 protected final E findPrototypeDisplayValue(Collection<E> possibleValues) {
104 E result = null;
105 int maxHeight = -1;
106 if (possibleValues != null) {
107 // Remind old prototype to restore it later
108 E oldPrototype = getPrototypeDisplayValue();
109 // Get internal JList to directly call the renderer
110 JList<E> list = getList();
111 try {
112 // Index to give to renderer
113 int i = 0;
114 for (E value : possibleValues) {
115 if (value != null) {
116 // With a "classic" renderer, we could call setPrototypeDisplayValue(value) + getPreferredSize()
117 // but not with TaggingPreset custom renderer that return a dummy height if index is equal to -1
118 // So we explicitely call the renderer by simulating a correct index for the current value
119 Component c = getRenderer().getListCellRendererComponent(list, value, i, true, true);
120 if (c != null) {
121 // Get the real preferred size for the current value
122 Dimension dim = c.getPreferredSize();
123 if (dim.height > maxHeight) {
124 // Larger ? This is our new prototype
125 maxHeight = dim.height;
126 result = value;
127 }
128 }
129 }
130 i++;
131 }
132 } finally {
133 // Restore original prototype
134 setPrototypeDisplayValue(oldPrototype);
135 }
136 }
137 return result;
138 }
139
140 @SuppressWarnings("unchecked")
141 protected final JList<E> getList() {
142 for (int i = 0; i < getUI().getAccessibleChildrenCount(this); i++) {
143 Accessible child = getUI().getAccessibleChild(this, i);
144 if (child instanceof ComboPopup) {
145 return ((ComboPopup)child).getList();
146 }
147 }
148 return null;
149 }
150
151 protected final void init(E prototype) {
152 if (prototype != null) {
153 setPrototypeDisplayValue(prototype);
154 int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
155 // Compute maximum number of visible items based on the preferred size of the combo box.
156 // This assumes that items have the same height as the combo box, which is not granted by the look and feel
157 int maxsize = (screenHeight/getPreferredSize().height) / 2;
158 // If possible, adjust the maximum number of items with the real height of items
159 // It is not granted this works on every platform (tested OK on Windows)
160 JList<E> list = getList();
161 if (list != null) {
162 if (!prototype.equals(list.getPrototypeCellValue())) {
163 list.setPrototypeCellValue(prototype);
164 }
165 int height = list.getFixedCellHeight();
166 if (height > 0) {
167 maxsize = (screenHeight/height) / 2;
168 }
169 }
170 setMaximumRowCount(Math.max(getMaximumRowCount(), maxsize));
171 }
172 // Handle text contextual menus for editable comboboxes
173 ContextMenuHandler handler = new ContextMenuHandler();
174 addPropertyChangeListener("editable", handler);
175 addPropertyChangeListener("editor", handler);
176 }
177
178 protected class ContextMenuHandler extends MouseAdapter implements PropertyChangeListener {
179
180 private JTextComponent component;
181 private PopupMenuLauncher launcher;
182
183 @Override
184 public void propertyChange(PropertyChangeEvent evt) {
185 if ("editable".equals(evt.getPropertyName())) {
186 if (evt.getNewValue().equals(true)) {
187 enableMenu();
188 } else {
189 disableMenu();
190 }
191 } else if ("editor".equals(evt.getPropertyName())) {
192 disableMenu();
193 if (isEditable()) {
194 enableMenu();
195 }
196 }
197 }
198
199 private void enableMenu() {
200 if (launcher == null && editor != null) {
201 Component editorComponent = editor.getEditorComponent();
202 if (editorComponent instanceof JTextComponent) {
203 component = (JTextComponent) editorComponent;
204 component.addMouseListener(this);
205 launcher = TextContextualPopupMenu.enableMenuFor(component, true);
206 }
207 }
208 }
209
210 private void disableMenu() {
211 if (launcher != null) {
212 TextContextualPopupMenu.disableMenuFor(component, launcher);
213 launcher = null;
214 component.removeMouseListener(this);
215 component = null;
216 }
217 }
218
219 @Override
220 public void mousePressed(MouseEvent e) {
221 processEvent(e);
222 }
223
224 @Override
225 public void mouseReleased(MouseEvent e) {
226 processEvent(e);
227 }
228
229 private void processEvent(MouseEvent e) {
230 if (launcher != null && !e.isPopupTrigger() && launcher.getMenu().isShowing()) {
231 launcher.getMenu().setVisible(false);
232 }
233 }
234 }
235
236 /**
237 * Reinitializes this {@link JosmComboBox} to the specified values. This may needed if a custom renderer is used.
238 * @param values The values displayed in the combo box.
239 * @since 5558
240 */
241 public final void reinitialize(Collection<E> values) {
242 init(findPrototypeDisplayValue(values));
243 }
244}
Note: See TracBrowser for help on using the repository browser.