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

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

see #8465 - use diamond operator where applicable

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