author | alitvinov |
Mon, 03 Jun 2013 16:37:13 +0400 | |
changeset 17902 | 14a0ae3ca973 |
parent 15637 | 2c226ebab6a6 |
child 20098 | 7b11536bf6b3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
15637
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing.plaf.basic; |
|
27 |
||
28 |
import java.awt.*; |
|
29 |
import java.awt.event.*; |
|
30 |
import javax.swing.*; |
|
31 |
import javax.accessibility.*; |
|
32 |
import javax.swing.plaf.*; |
|
33 |
import javax.swing.text.*; |
|
34 |
import javax.swing.event.*; |
|
35 |
import java.beans.PropertyChangeListener; |
|
36 |
import java.beans.PropertyChangeEvent; |
|
37 |
import sun.awt.AppContext; |
|
38 |
import sun.swing.DefaultLookup; |
|
39 |
import sun.swing.UIAction; |
|
40 |
||
41 |
/** |
|
42 |
* Basic UI implementation for JComboBox. |
|
43 |
* <p> |
|
44 |
* The combo box is a compound component which means that it is an agregate of |
|
45 |
* many simpler components. This class creates and manages the listeners |
|
46 |
* on the combo box and the combo box model. These listeners update the user |
|
47 |
* interface in response to changes in the properties and state of the combo box. |
|
48 |
* <p> |
|
49 |
* All event handling is handled by listener classes created with the |
|
50 |
* <code>createxxxListener()</code> methods and internal classes. |
|
51 |
* You can change the behavior of this class by overriding the |
|
52 |
* <code>createxxxListener()</code> methods and supplying your own |
|
53 |
* event listeners or subclassing from the ones supplied in this class. |
|
54 |
* <p> |
|
55 |
* For adding specific actions, |
|
56 |
* overide <code>installKeyboardActions</code> to add actions in response to |
|
57 |
* KeyStroke bindings. See the article <a href="http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html">Keyboard Bindings in Swing</a> |
|
58 |
* at <a href="http://java.sun.com/products/jfc/tsc"><em>The Swing Connection</em></a>. |
|
59 |
* |
|
60 |
* @author Arnaud Weber |
|
61 |
* @author Tom Santos |
|
62 |
* @author Mark Davidson |
|
63 |
*/ |
|
64 |
public class BasicComboBoxUI extends ComboBoxUI { |
|
65 |
protected JComboBox comboBox; |
|
66 |
/** |
|
67 |
* This protected field is implementation specific. Do not access directly |
|
68 |
* or override. |
|
69 |
*/ |
|
70 |
protected boolean hasFocus = false; |
|
71 |
||
72 |
// Control the selection behavior of the JComboBox when it is used |
|
73 |
// in the JTable DefaultCellEditor. |
|
74 |
private boolean isTableCellEditor = false; |
|
75 |
private static final String IS_TABLE_CELL_EDITOR = "JComboBox.isTableCellEditor"; |
|
76 |
||
77 |
// This list is for drawing the current item in the combo box. |
|
78 |
protected JList listBox; |
|
79 |
||
80 |
// Used to render the currently selected item in the combo box. |
|
81 |
// It doesn't have anything to do with the popup's rendering. |
|
82 |
protected CellRendererPane currentValuePane = new CellRendererPane(); |
|
83 |
||
84 |
// The implementation of ComboPopup that is used to show the popup. |
|
85 |
protected ComboPopup popup; |
|
86 |
||
87 |
// The Component that the ComboBoxEditor uses for editing |
|
88 |
protected Component editor; |
|
89 |
||
90 |
// The arrow button that invokes the popup. |
|
91 |
protected JButton arrowButton; |
|
92 |
||
93 |
// Listeners that are attached to the JComboBox |
|
94 |
/** |
|
95 |
* This protected field is implementation specific. Do not access directly |
|
96 |
* or override. Override the listener construction method instead. |
|
97 |
* |
|
98 |
* @see #createKeyListener |
|
99 |
*/ |
|
100 |
protected KeyListener keyListener; |
|
101 |
/** |
|
102 |
* This protected field is implementation specific. Do not access directly |
|
103 |
* or override. Override the listener construction method instead. |
|
104 |
* |
|
105 |
* @see #createFocusListener |
|
106 |
*/ |
|
107 |
protected FocusListener focusListener; |
|
108 |
/** |
|
109 |
* This protected field is implementation specific. Do not access directly |
|
110 |
* or override. Override the listener construction method instead. |
|
111 |
* |
|
112 |
* @see #createPropertyChangeListener |
|
113 |
*/ |
|
114 |
protected PropertyChangeListener propertyChangeListener; |
|
115 |
||
116 |
/** |
|
117 |
* This protected field is implementation specific. Do not access directly |
|
118 |
* or override. Override the listener construction method instead. |
|
119 |
* |
|
120 |
* @see #createItemListener |
|
121 |
*/ |
|
122 |
protected ItemListener itemListener; |
|
123 |
||
124 |
// Listeners that the ComboPopup produces. |
|
125 |
protected MouseListener popupMouseListener; |
|
126 |
protected MouseMotionListener popupMouseMotionListener; |
|
127 |
protected KeyListener popupKeyListener; |
|
128 |
||
129 |
// This is used for knowing when to cache the minimum preferred size. |
|
130 |
// If the data in the list changes, the cached value get marked for recalc. |
|
131 |
// Added to the current JComboBox model |
|
132 |
/** |
|
133 |
* This protected field is implementation specific. Do not access directly |
|
134 |
* or override. Override the listener construction method instead. |
|
135 |
* |
|
136 |
* @see #createListDataListener |
|
137 |
*/ |
|
138 |
protected ListDataListener listDataListener; |
|
139 |
||
140 |
/** |
|
141 |
* Implements all the Listeners needed by this class, all existing |
|
142 |
* listeners redirect to it. |
|
143 |
*/ |
|
144 |
private Handler handler; |
|
145 |
||
146 |
/** |
|
147 |
* The time factor to treate the series of typed alphanumeric key |
|
148 |
* as prefix for first letter navigation. |
|
149 |
*/ |
|
150 |
private long timeFactor = 1000L; |
|
151 |
||
152 |
/** |
|
153 |
* This is tricky, this variables is needed for DefaultKeySelectionManager |
|
154 |
* to take into account time factor. |
|
155 |
*/ |
|
156 |
private long lastTime = 0L; |
|
157 |
private long time = 0L; |
|
158 |
||
159 |
/** |
|
160 |
* The default key selection manager |
|
161 |
*/ |
|
162 |
JComboBox.KeySelectionManager keySelectionManager; |
|
163 |
||
164 |
// Flag for recalculating the minimum preferred size. |
|
165 |
protected boolean isMinimumSizeDirty = true; |
|
166 |
||
167 |
// Cached minimum preferred size. |
|
168 |
protected Dimension cachedMinimumSize = new Dimension( 0, 0 ); |
|
169 |
||
170 |
// Flag for calculating the display size |
|
171 |
private boolean isDisplaySizeDirty = true; |
|
172 |
||
173 |
// Cached the size that the display needs to render the largest item |
|
174 |
private Dimension cachedDisplaySize = new Dimension( 0, 0 ); |
|
175 |
||
176 |
// Key used for lookup of the DefaultListCellRenderer in the AppContext. |
|
177 |
private static final Object COMBO_UI_LIST_CELL_RENDERER_KEY = |
|
178 |
new StringBuffer("DefaultListCellRendererKey"); |
|
179 |
||
180 |
static final StringBuffer HIDE_POPUP_KEY |
|
181 |
= new StringBuffer("HidePopupKey"); |
|
182 |
||
183 |
/** |
|
184 |
* Whether or not all cells have the same baseline. |
|
185 |
*/ |
|
186 |
private boolean sameBaseline; |
|
187 |
||
2658 | 188 |
/** |
189 |
* Indicates whether or not the combo box button should be square. |
|
190 |
* If square, then the width and height are equal, and are both set to |
|
4394 | 191 |
* the height of the combo minus appropriate insets. |
192 |
* |
|
193 |
* @since 1.7 |
|
2658 | 194 |
*/ |
4394 | 195 |
protected boolean squareButton = true; |
2658 | 196 |
|
197 |
/** |
|
4394 | 198 |
* If specified, these insets act as padding around the cell renderer when |
199 |
* laying out and painting the "selected" item in the combo box. These |
|
200 |
* insets add to those specified by the cell renderer. |
|
201 |
* |
|
202 |
* @since 1.7 |
|
2658 | 203 |
*/ |
4394 | 204 |
protected Insets padding; |
2658 | 205 |
|
2 | 206 |
// Used for calculating the default size. |
207 |
private static ListCellRenderer getDefaultListCellRenderer() { |
|
208 |
ListCellRenderer renderer = (ListCellRenderer)AppContext. |
|
209 |
getAppContext().get(COMBO_UI_LIST_CELL_RENDERER_KEY); |
|
210 |
||
211 |
if (renderer == null) { |
|
212 |
renderer = new DefaultListCellRenderer(); |
|
213 |
AppContext.getAppContext().put(COMBO_UI_LIST_CELL_RENDERER_KEY, |
|
214 |
new DefaultListCellRenderer()); |
|
215 |
} |
|
216 |
return renderer; |
|
217 |
} |
|
218 |
||
219 |
/** |
|
220 |
* Populates ComboBox's actions. |
|
221 |
*/ |
|
222 |
static void loadActionMap(LazyActionMap map) { |
|
223 |
map.put(new Actions(Actions.HIDE)); |
|
224 |
map.put(new Actions(Actions.PAGE_DOWN)); |
|
225 |
map.put(new Actions(Actions.PAGE_UP)); |
|
226 |
map.put(new Actions(Actions.HOME)); |
|
227 |
map.put(new Actions(Actions.END)); |
|
228 |
map.put(new Actions(Actions.DOWN)); |
|
229 |
map.put(new Actions(Actions.DOWN_2)); |
|
230 |
map.put(new Actions(Actions.TOGGLE)); |
|
231 |
map.put(new Actions(Actions.TOGGLE_2)); |
|
232 |
map.put(new Actions(Actions.UP)); |
|
233 |
map.put(new Actions(Actions.UP_2)); |
|
234 |
map.put(new Actions(Actions.ENTER)); |
|
235 |
} |
|
236 |
||
237 |
//======================== |
|
238 |
// begin UI Initialization |
|
239 |
// |
|
240 |
||
241 |
public static ComponentUI createUI(JComponent c) { |
|
242 |
return new BasicComboBoxUI(); |
|
243 |
} |
|
244 |
||
2658 | 245 |
@Override |
2 | 246 |
public void installUI( JComponent c ) { |
247 |
isMinimumSizeDirty = true; |
|
248 |
||
249 |
comboBox = (JComboBox)c; |
|
250 |
installDefaults(); |
|
251 |
popup = createPopup(); |
|
252 |
listBox = popup.getList(); |
|
253 |
||
254 |
// Is this combo box a cell editor? |
|
255 |
Boolean inTable = (Boolean)c.getClientProperty(IS_TABLE_CELL_EDITOR ); |
|
256 |
if (inTable != null) { |
|
257 |
isTableCellEditor = inTable.equals(Boolean.TRUE) ? true : false; |
|
258 |
} |
|
259 |
||
260 |
if ( comboBox.getRenderer() == null || comboBox.getRenderer() instanceof UIResource ) { |
|
261 |
comboBox.setRenderer( createRenderer() ); |
|
262 |
} |
|
263 |
||
264 |
if ( comboBox.getEditor() == null || comboBox.getEditor() instanceof UIResource ) { |
|
265 |
comboBox.setEditor( createEditor() ); |
|
266 |
} |
|
267 |
||
268 |
installListeners(); |
|
269 |
installComponents(); |
|
270 |
||
271 |
comboBox.setLayout( createLayoutManager() ); |
|
272 |
||
273 |
comboBox.setRequestFocusEnabled( true ); |
|
274 |
||
275 |
installKeyboardActions(); |
|
276 |
||
277 |
comboBox.putClientProperty("doNotCancelPopup", HIDE_POPUP_KEY); |
|
278 |
||
279 |
if (keySelectionManager == null || keySelectionManager instanceof UIResource) { |
|
280 |
keySelectionManager = new DefaultKeySelectionManager(); |
|
281 |
} |
|
282 |
comboBox.setKeySelectionManager(keySelectionManager); |
|
283 |
} |
|
284 |
||
2658 | 285 |
@Override |
2 | 286 |
public void uninstallUI( JComponent c ) { |
287 |
setPopupVisible( comboBox, false); |
|
288 |
popup.uninstallingUI(); |
|
289 |
||
290 |
uninstallKeyboardActions(); |
|
291 |
||
292 |
comboBox.setLayout( null ); |
|
293 |
||
294 |
uninstallComponents(); |
|
295 |
uninstallListeners(); |
|
296 |
uninstallDefaults(); |
|
297 |
||
298 |
if ( comboBox.getRenderer() == null || comboBox.getRenderer() instanceof UIResource ) { |
|
299 |
comboBox.setRenderer( null ); |
|
300 |
} |
|
301 |
||
302 |
ComboBoxEditor comboBoxEditor = comboBox.getEditor(); |
|
303 |
if (comboBoxEditor instanceof UIResource ) { |
|
304 |
if (comboBoxEditor.getEditorComponent().hasFocus()) { |
|
305 |
// Leave focus in JComboBox. |
|
306 |
comboBox.requestFocusInWindow(); |
|
307 |
} |
|
308 |
comboBox.setEditor( null ); |
|
309 |
} |
|
310 |
||
311 |
if (keySelectionManager instanceof UIResource) { |
|
312 |
comboBox.setKeySelectionManager(null); |
|
313 |
} |
|
314 |
||
315 |
handler = null; |
|
316 |
keyListener = null; |
|
317 |
focusListener = null; |
|
318 |
listDataListener = null; |
|
319 |
propertyChangeListener = null; |
|
320 |
popup = null; |
|
321 |
listBox = null; |
|
322 |
comboBox = null; |
|
323 |
} |
|
324 |
||
325 |
/** |
|
326 |
* Installs the default colors, default font, default renderer, and default |
|
327 |
* editor into the JComboBox. |
|
328 |
*/ |
|
329 |
protected void installDefaults() { |
|
330 |
LookAndFeel.installColorsAndFont( comboBox, |
|
331 |
"ComboBox.background", |
|
332 |
"ComboBox.foreground", |
|
333 |
"ComboBox.font" ); |
|
334 |
LookAndFeel.installBorder( comboBox, "ComboBox.border" ); |
|
335 |
LookAndFeel.installProperty( comboBox, "opaque", Boolean.TRUE); |
|
336 |
||
337 |
Long l = (Long)UIManager.get("ComboBox.timeFactor"); |
|
2658 | 338 |
timeFactor = l == null ? 1000L : l.longValue(); |
339 |
||
340 |
//NOTE: this needs to default to true if not specified |
|
341 |
Boolean b = (Boolean)UIManager.get("ComboBox.squareButton"); |
|
342 |
squareButton = b == null ? true : b; |
|
343 |
||
344 |
padding = UIManager.getInsets("ComboBox.padding"); |
|
2 | 345 |
} |
346 |
||
347 |
/** |
|
4394 | 348 |
* Creates and installs listeners for the combo box and its model. |
2 | 349 |
* This method is called when the UI is installed. |
350 |
*/ |
|
351 |
protected void installListeners() { |
|
352 |
if ( (itemListener = createItemListener()) != null) { |
|
353 |
comboBox.addItemListener( itemListener ); |
|
354 |
} |
|
355 |
if ( (propertyChangeListener = createPropertyChangeListener()) != null ) { |
|
356 |
comboBox.addPropertyChangeListener( propertyChangeListener ); |
|
357 |
} |
|
358 |
if ( (keyListener = createKeyListener()) != null ) { |
|
359 |
comboBox.addKeyListener( keyListener ); |
|
360 |
} |
|
361 |
if ( (focusListener = createFocusListener()) != null ) { |
|
362 |
comboBox.addFocusListener( focusListener ); |
|
363 |
} |
|
364 |
if ((popupMouseListener = popup.getMouseListener()) != null) { |
|
365 |
comboBox.addMouseListener( popupMouseListener ); |
|
366 |
} |
|
367 |
if ((popupMouseMotionListener = popup.getMouseMotionListener()) != null) { |
|
368 |
comboBox.addMouseMotionListener( popupMouseMotionListener ); |
|
369 |
} |
|
370 |
if ((popupKeyListener = popup.getKeyListener()) != null) { |
|
371 |
comboBox.addKeyListener(popupKeyListener); |
|
372 |
} |
|
373 |
||
374 |
if ( comboBox.getModel() != null ) { |
|
375 |
if ( (listDataListener = createListDataListener()) != null ) { |
|
376 |
comboBox.getModel().addListDataListener( listDataListener ); |
|
377 |
} |
|
378 |
} |
|
379 |
} |
|
380 |
||
381 |
/** |
|
4394 | 382 |
* Uninstalls the default colors, default font, default renderer, |
383 |
* and default editor from the combo box. |
|
2 | 384 |
*/ |
385 |
protected void uninstallDefaults() { |
|
386 |
LookAndFeel.installColorsAndFont( comboBox, |
|
387 |
"ComboBox.background", |
|
388 |
"ComboBox.foreground", |
|
389 |
"ComboBox.font" ); |
|
390 |
LookAndFeel.uninstallBorder( comboBox ); |
|
391 |
} |
|
392 |
||
393 |
/** |
|
4394 | 394 |
* Removes the installed listeners from the combo box and its model. |
2 | 395 |
* The number and types of listeners removed and in this method should be |
396 |
* the same that was added in <code>installListeners</code> |
|
397 |
*/ |
|
398 |
protected void uninstallListeners() { |
|
399 |
if ( keyListener != null ) { |
|
400 |
comboBox.removeKeyListener( keyListener ); |
|
401 |
} |
|
402 |
if ( itemListener != null) { |
|
403 |
comboBox.removeItemListener( itemListener ); |
|
404 |
} |
|
405 |
if ( propertyChangeListener != null ) { |
|
406 |
comboBox.removePropertyChangeListener( propertyChangeListener ); |
|
407 |
} |
|
408 |
if ( focusListener != null) { |
|
409 |
comboBox.removeFocusListener( focusListener ); |
|
410 |
} |
|
411 |
if ( popupMouseListener != null) { |
|
412 |
comboBox.removeMouseListener( popupMouseListener ); |
|
413 |
} |
|
414 |
if ( popupMouseMotionListener != null) { |
|
415 |
comboBox.removeMouseMotionListener( popupMouseMotionListener ); |
|
416 |
} |
|
417 |
if (popupKeyListener != null) { |
|
418 |
comboBox.removeKeyListener(popupKeyListener); |
|
419 |
} |
|
420 |
if ( comboBox.getModel() != null ) { |
|
421 |
if ( listDataListener != null ) { |
|
422 |
comboBox.getModel().removeListDataListener( listDataListener ); |
|
423 |
} |
|
424 |
} |
|
425 |
} |
|
426 |
||
427 |
/** |
|
428 |
* Creates the popup portion of the combo box. |
|
429 |
* |
|
430 |
* @return an instance of <code>ComboPopup</code> |
|
431 |
* @see ComboPopup |
|
432 |
*/ |
|
433 |
protected ComboPopup createPopup() { |
|
434 |
return new BasicComboPopup( comboBox ); |
|
435 |
} |
|
436 |
||
437 |
/** |
|
438 |
* Creates a <code>KeyListener</code> which will be added to the |
|
439 |
* combo box. If this method returns null then it will not be added |
|
440 |
* to the combo box. |
|
441 |
* |
|
442 |
* @return an instance <code>KeyListener</code> or null |
|
443 |
*/ |
|
444 |
protected KeyListener createKeyListener() { |
|
445 |
return getHandler(); |
|
446 |
} |
|
447 |
||
448 |
/** |
|
449 |
* Creates a <code>FocusListener</code> which will be added to the combo box. |
|
450 |
* If this method returns null then it will not be added to the combo box. |
|
451 |
* |
|
452 |
* @return an instance of a <code>FocusListener</code> or null |
|
453 |
*/ |
|
454 |
protected FocusListener createFocusListener() { |
|
455 |
return getHandler(); |
|
456 |
} |
|
457 |
||
458 |
/** |
|
459 |
* Creates a list data listener which will be added to the |
|
460 |
* <code>ComboBoxModel</code>. If this method returns null then |
|
461 |
* it will not be added to the combo box model. |
|
462 |
* |
|
463 |
* @return an instance of a <code>ListDataListener</code> or null |
|
464 |
*/ |
|
465 |
protected ListDataListener createListDataListener() { |
|
466 |
return getHandler(); |
|
467 |
} |
|
468 |
||
469 |
/** |
|
470 |
* Creates an <code>ItemListener</code> which will be added to the |
|
471 |
* combo box. If this method returns null then it will not |
|
472 |
* be added to the combo box. |
|
473 |
* <p> |
|
474 |
* Subclasses may override this method to return instances of their own |
|
475 |
* ItemEvent handlers. |
|
476 |
* |
|
477 |
* @return an instance of an <code>ItemListener</code> or null |
|
478 |
*/ |
|
479 |
protected ItemListener createItemListener() { |
|
480 |
return null; |
|
481 |
} |
|
482 |
||
483 |
/** |
|
484 |
* Creates a <code>PropertyChangeListener</code> which will be added to |
|
485 |
* the combo box. If this method returns null then it will not |
|
486 |
* be added to the combo box. |
|
487 |
* |
|
488 |
* @return an instance of a <code>PropertyChangeListener</code> or null |
|
489 |
*/ |
|
490 |
protected PropertyChangeListener createPropertyChangeListener() { |
|
491 |
return getHandler(); |
|
492 |
} |
|
493 |
||
494 |
/** |
|
495 |
* Creates a layout manager for managing the components which make up the |
|
496 |
* combo box. |
|
497 |
* |
|
498 |
* @return an instance of a layout manager |
|
499 |
*/ |
|
500 |
protected LayoutManager createLayoutManager() { |
|
501 |
return getHandler(); |
|
502 |
} |
|
503 |
||
504 |
/** |
|
505 |
* Creates the default renderer that will be used in a non-editiable combo |
|
506 |
* box. A default renderer will used only if a renderer has not been |
|
507 |
* explicitly set with <code>setRenderer</code>. |
|
508 |
* |
|
509 |
* @return a <code>ListCellRender</code> used for the combo box |
|
510 |
* @see javax.swing.JComboBox#setRenderer |
|
511 |
*/ |
|
512 |
protected ListCellRenderer createRenderer() { |
|
513 |
return new BasicComboBoxRenderer.UIResource(); |
|
514 |
} |
|
515 |
||
516 |
/** |
|
517 |
* Creates the default editor that will be used in editable combo boxes. |
|
518 |
* A default editor will be used only if an editor has not been |
|
519 |
* explicitly set with <code>setEditor</code>. |
|
520 |
* |
|
521 |
* @return a <code>ComboBoxEditor</code> used for the combo box |
|
522 |
* @see javax.swing.JComboBox#setEditor |
|
523 |
*/ |
|
524 |
protected ComboBoxEditor createEditor() { |
|
525 |
return new BasicComboBoxEditor.UIResource(); |
|
526 |
} |
|
527 |
||
528 |
/** |
|
529 |
* Returns the shared listener. |
|
530 |
*/ |
|
531 |
private Handler getHandler() { |
|
532 |
if (handler == null) { |
|
533 |
handler = new Handler(); |
|
534 |
} |
|
535 |
return handler; |
|
536 |
} |
|
537 |
||
538 |
// |
|
539 |
// end UI Initialization |
|
540 |
//====================== |
|
541 |
||
542 |
||
543 |
//====================== |
|
544 |
// begin Inner classes |
|
545 |
// |
|
546 |
||
547 |
/** |
|
548 |
* This listener checks to see if the key event isn't a navigation key. If |
|
549 |
* it finds a key event that wasn't a navigation key it dispatches it to |
|
550 |
* JComboBox.selectWithKeyChar() so that it can do type-ahead. |
|
551 |
* |
|
552 |
* This public inner class should be treated as protected. |
|
553 |
* Instantiate it only within subclasses of |
|
554 |
* <code>BasicComboBoxUI</code>. |
|
555 |
*/ |
|
556 |
public class KeyHandler extends KeyAdapter { |
|
2658 | 557 |
@Override |
2 | 558 |
public void keyPressed( KeyEvent e ) { |
559 |
getHandler().keyPressed(e); |
|
560 |
} |
|
561 |
} |
|
562 |
||
563 |
/** |
|
564 |
* This listener hides the popup when the focus is lost. It also repaints |
|
565 |
* when focus is gained or lost. |
|
566 |
* |
|
567 |
* This public inner class should be treated as protected. |
|
568 |
* Instantiate it only within subclasses of |
|
569 |
* <code>BasicComboBoxUI</code>. |
|
570 |
*/ |
|
571 |
public class FocusHandler implements FocusListener { |
|
572 |
public void focusGained( FocusEvent e ) { |
|
573 |
getHandler().focusGained(e); |
|
574 |
} |
|
575 |
||
576 |
public void focusLost( FocusEvent e ) { |
|
577 |
getHandler().focusLost(e); |
|
578 |
} |
|
579 |
} |
|
580 |
||
581 |
/** |
|
582 |
* This listener watches for changes in the |
|
583 |
* <code>ComboBoxModel</code>. |
|
584 |
* <p> |
|
585 |
* This public inner class should be treated as protected. |
|
586 |
* Instantiate it only within subclasses of |
|
587 |
* <code>BasicComboBoxUI</code>. |
|
588 |
* |
|
589 |
* @see #createListDataListener |
|
590 |
*/ |
|
591 |
public class ListDataHandler implements ListDataListener { |
|
592 |
public void contentsChanged( ListDataEvent e ) { |
|
593 |
getHandler().contentsChanged(e); |
|
594 |
} |
|
595 |
||
596 |
public void intervalAdded( ListDataEvent e ) { |
|
597 |
getHandler().intervalAdded(e); |
|
598 |
} |
|
599 |
||
600 |
public void intervalRemoved( ListDataEvent e ) { |
|
601 |
getHandler().intervalRemoved(e); |
|
602 |
} |
|
603 |
} |
|
604 |
||
605 |
/** |
|
606 |
* This listener watches for changes to the selection in the |
|
607 |
* combo box. |
|
608 |
* <p> |
|
609 |
* This public inner class should be treated as protected. |
|
610 |
* Instantiate it only within subclasses of |
|
611 |
* <code>BasicComboBoxUI</code>. |
|
612 |
* |
|
613 |
* @see #createItemListener |
|
614 |
*/ |
|
615 |
public class ItemHandler implements ItemListener { |
|
616 |
// This class used to implement behavior which is now redundant. |
|
617 |
public void itemStateChanged(ItemEvent e) {} |
|
618 |
} |
|
619 |
||
620 |
/** |
|
621 |
* This listener watches for bound properties that have changed in the |
|
622 |
* combo box. |
|
623 |
* <p> |
|
624 |
* Subclasses which wish to listen to combo box property changes should |
|
625 |
* call the superclass methods to ensure that the combo box ui correctly |
|
626 |
* handles property changes. |
|
627 |
* <p> |
|
628 |
* This public inner class should be treated as protected. |
|
629 |
* Instantiate it only within subclasses of |
|
630 |
* <code>BasicComboBoxUI</code>. |
|
631 |
* |
|
632 |
* @see #createPropertyChangeListener |
|
633 |
*/ |
|
634 |
public class PropertyChangeHandler implements PropertyChangeListener { |
|
635 |
public void propertyChange(PropertyChangeEvent e) { |
|
636 |
getHandler().propertyChange(e); |
|
637 |
} |
|
638 |
} |
|
639 |
||
640 |
||
641 |
// Syncronizes the ToolTip text for the components within the combo box to be the |
|
642 |
// same value as the combo box ToolTip text. |
|
643 |
private void updateToolTipTextForChildren() { |
|
644 |
Component[] children = comboBox.getComponents(); |
|
645 |
for ( int i = 0; i < children.length; ++i ) { |
|
646 |
if ( children[i] instanceof JComponent ) { |
|
647 |
((JComponent)children[i]).setToolTipText( comboBox.getToolTipText() ); |
|
648 |
} |
|
649 |
} |
|
650 |
} |
|
651 |
||
652 |
/** |
|
653 |
* This layout manager handles the 'standard' layout of combo boxes. It puts |
|
654 |
* the arrow button to the right and the editor to the left. If there is no |
|
655 |
* editor it still keeps the arrow button to the right. |
|
656 |
* |
|
657 |
* This public inner class should be treated as protected. |
|
658 |
* Instantiate it only within subclasses of |
|
659 |
* <code>BasicComboBoxUI</code>. |
|
660 |
*/ |
|
661 |
public class ComboBoxLayoutManager implements LayoutManager { |
|
662 |
public void addLayoutComponent(String name, Component comp) {} |
|
663 |
||
664 |
public void removeLayoutComponent(Component comp) {} |
|
665 |
||
666 |
public Dimension preferredLayoutSize(Container parent) { |
|
667 |
return getHandler().preferredLayoutSize(parent); |
|
668 |
} |
|
669 |
||
670 |
public Dimension minimumLayoutSize(Container parent) { |
|
671 |
return getHandler().minimumLayoutSize(parent); |
|
672 |
} |
|
673 |
||
674 |
public void layoutContainer(Container parent) { |
|
675 |
getHandler().layoutContainer(parent); |
|
676 |
} |
|
677 |
} |
|
678 |
||
679 |
// |
|
680 |
// end Inner classes |
|
681 |
//==================== |
|
682 |
||
683 |
||
684 |
//=============================== |
|
685 |
// begin Sub-Component Management |
|
686 |
// |
|
687 |
||
688 |
/** |
|
689 |
* Creates and initializes the components which make up the |
|
690 |
* aggregate combo box. This method is called as part of the UI |
|
691 |
* installation process. |
|
692 |
*/ |
|
693 |
protected void installComponents() { |
|
694 |
arrowButton = createArrowButton(); |
|
695 |
||
696 |
if (arrowButton != null) { |
|
17902
14a0ae3ca973
6337518: Null Arrow Button Throws Exception in BasicComboBoxUI
alitvinov
parents:
15637
diff
changeset
|
697 |
comboBox.add(arrowButton); |
2 | 698 |
configureArrowButton(); |
699 |
} |
|
700 |
||
701 |
if ( comboBox.isEditable() ) { |
|
702 |
addEditor(); |
|
703 |
} |
|
704 |
||
705 |
comboBox.add( currentValuePane ); |
|
706 |
} |
|
707 |
||
708 |
/** |
|
709 |
* The aggregate components which compise the combo box are |
|
710 |
* unregistered and uninitialized. This method is called as part of the |
|
711 |
* UI uninstallation process. |
|
712 |
*/ |
|
713 |
protected void uninstallComponents() { |
|
714 |
if ( arrowButton != null ) { |
|
715 |
unconfigureArrowButton(); |
|
716 |
} |
|
717 |
if ( editor != null ) { |
|
718 |
unconfigureEditor(); |
|
719 |
} |
|
720 |
comboBox.removeAll(); // Just to be safe. |
|
721 |
arrowButton = null; |
|
722 |
} |
|
723 |
||
724 |
/** |
|
725 |
* This public method is implementation specific and should be private. |
|
726 |
* do not call or override. To implement a specific editor create a |
|
727 |
* custom <code>ComboBoxEditor</code> |
|
728 |
* |
|
729 |
* @see #createEditor |
|
730 |
* @see javax.swing.JComboBox#setEditor |
|
731 |
* @see javax.swing.ComboBoxEditor |
|
732 |
*/ |
|
733 |
public void addEditor() { |
|
734 |
removeEditor(); |
|
735 |
editor = comboBox.getEditor().getEditorComponent(); |
|
736 |
if ( editor != null ) { |
|
737 |
configureEditor(); |
|
738 |
comboBox.add(editor); |
|
739 |
if(comboBox.isFocusOwner()) { |
|
740 |
// Switch focus to the editor component |
|
741 |
editor.requestFocusInWindow(); |
|
742 |
} |
|
743 |
} |
|
744 |
} |
|
745 |
||
746 |
/** |
|
747 |
* This public method is implementation specific and should be private. |
|
748 |
* do not call or override. |
|
749 |
* |
|
750 |
* @see #addEditor |
|
751 |
*/ |
|
752 |
public void removeEditor() { |
|
753 |
if ( editor != null ) { |
|
754 |
unconfigureEditor(); |
|
755 |
comboBox.remove( editor ); |
|
756 |
editor = null; |
|
757 |
} |
|
758 |
} |
|
759 |
||
760 |
/** |
|
761 |
* This protected method is implementation specific and should be private. |
|
762 |
* do not call or override. |
|
763 |
* |
|
764 |
* @see #addEditor |
|
765 |
*/ |
|
766 |
protected void configureEditor() { |
|
767 |
// Should be in the same state as the combobox |
|
768 |
editor.setEnabled(comboBox.isEnabled()); |
|
769 |
||
770 |
editor.setFocusable(comboBox.isFocusable()); |
|
771 |
||
772 |
editor.setFont( comboBox.getFont() ); |
|
773 |
||
774 |
if (focusListener != null) { |
|
775 |
editor.addFocusListener(focusListener); |
|
776 |
} |
|
777 |
||
778 |
editor.addFocusListener( getHandler() ); |
|
779 |
||
780 |
comboBox.getEditor().addActionListener(getHandler()); |
|
781 |
||
782 |
if(editor instanceof JComponent) { |
|
783 |
((JComponent)editor).putClientProperty("doNotCancelPopup", |
|
784 |
HIDE_POPUP_KEY); |
|
785 |
((JComponent)editor).setInheritsPopupMenu(true); |
|
786 |
} |
|
787 |
||
788 |
comboBox.configureEditor(comboBox.getEditor(),comboBox.getSelectedItem()); |
|
2658 | 789 |
|
790 |
editor.addPropertyChangeListener(propertyChangeListener); |
|
2 | 791 |
} |
792 |
||
793 |
/** |
|
794 |
* This protected method is implementation specific and should be private. |
|
795 |
* Do not call or override. |
|
796 |
* |
|
797 |
* @see #addEditor |
|
798 |
*/ |
|
799 |
protected void unconfigureEditor() { |
|
800 |
if (focusListener != null) { |
|
801 |
editor.removeFocusListener(focusListener); |
|
802 |
} |
|
803 |
||
2658 | 804 |
editor.removePropertyChangeListener(propertyChangeListener); |
2 | 805 |
editor.removeFocusListener(getHandler()); |
806 |
comboBox.getEditor().removeActionListener(getHandler()); |
|
807 |
} |
|
808 |
||
809 |
/** |
|
810 |
* This public method is implementation specific and should be private. Do |
|
811 |
* not call or override. |
|
812 |
* |
|
813 |
* @see #createArrowButton |
|
814 |
*/ |
|
815 |
public void configureArrowButton() { |
|
816 |
if ( arrowButton != null ) { |
|
817 |
arrowButton.setEnabled( comboBox.isEnabled() ); |
|
818 |
arrowButton.setFocusable(comboBox.isFocusable()); |
|
819 |
arrowButton.setRequestFocusEnabled(false); |
|
820 |
arrowButton.addMouseListener( popup.getMouseListener() ); |
|
821 |
arrowButton.addMouseMotionListener( popup.getMouseMotionListener() ); |
|
822 |
arrowButton.resetKeyboardActions(); |
|
823 |
arrowButton.putClientProperty("doNotCancelPopup", HIDE_POPUP_KEY); |
|
824 |
arrowButton.setInheritsPopupMenu(true); |
|
825 |
} |
|
826 |
} |
|
827 |
||
828 |
/** |
|
829 |
* This public method is implementation specific and should be private. Do |
|
830 |
* not call or override. |
|
831 |
* |
|
832 |
* @see #createArrowButton |
|
833 |
*/ |
|
834 |
public void unconfigureArrowButton() { |
|
835 |
if ( arrowButton != null ) { |
|
836 |
arrowButton.removeMouseListener( popup.getMouseListener() ); |
|
837 |
arrowButton.removeMouseMotionListener( popup.getMouseMotionListener() ); |
|
838 |
} |
|
839 |
} |
|
840 |
||
841 |
/** |
|
4394 | 842 |
* Creates a button which will be used as the control to show or hide |
2 | 843 |
* the popup portion of the combo box. |
844 |
* |
|
845 |
* @return a button which represents the popup control |
|
846 |
*/ |
|
847 |
protected JButton createArrowButton() { |
|
848 |
JButton button = new BasicArrowButton(BasicArrowButton.SOUTH, |
|
849 |
UIManager.getColor("ComboBox.buttonBackground"), |
|
850 |
UIManager.getColor("ComboBox.buttonShadow"), |
|
851 |
UIManager.getColor("ComboBox.buttonDarkShadow"), |
|
852 |
UIManager.getColor("ComboBox.buttonHighlight")); |
|
853 |
button.setName("ComboBox.arrowButton"); |
|
854 |
return button; |
|
855 |
} |
|
856 |
||
857 |
// |
|
858 |
// end Sub-Component Management |
|
859 |
//=============================== |
|
860 |
||
861 |
||
862 |
//================================ |
|
863 |
// begin ComboBoxUI Implementation |
|
864 |
// |
|
865 |
||
866 |
/** |
|
867 |
* Tells if the popup is visible or not. |
|
868 |
*/ |
|
869 |
public boolean isPopupVisible( JComboBox c ) { |
|
870 |
return popup.isVisible(); |
|
871 |
} |
|
872 |
||
873 |
/** |
|
874 |
* Hides the popup. |
|
875 |
*/ |
|
876 |
public void setPopupVisible( JComboBox c, boolean v ) { |
|
877 |
if ( v ) { |
|
878 |
popup.show(); |
|
879 |
} else { |
|
880 |
popup.hide(); |
|
881 |
} |
|
882 |
} |
|
883 |
||
884 |
/** |
|
885 |
* Determines if the JComboBox is focus traversable. If the JComboBox is editable |
|
886 |
* this returns false, otherwise it returns true. |
|
887 |
*/ |
|
888 |
public boolean isFocusTraversable( JComboBox c ) { |
|
889 |
return !comboBox.isEditable(); |
|
890 |
} |
|
891 |
||
892 |
// |
|
893 |
// end ComboBoxUI Implementation |
|
894 |
//============================== |
|
895 |
||
896 |
||
897 |
//================================= |
|
898 |
// begin ComponentUI Implementation |
|
2658 | 899 |
@Override |
2 | 900 |
public void paint( Graphics g, JComponent c ) { |
901 |
hasFocus = comboBox.hasFocus(); |
|
902 |
if ( !comboBox.isEditable() ) { |
|
903 |
Rectangle r = rectangleForCurrentValue(); |
|
904 |
paintCurrentValueBackground(g,r,hasFocus); |
|
905 |
paintCurrentValue(g,r,hasFocus); |
|
906 |
} |
|
907 |
} |
|
908 |
||
2658 | 909 |
@Override |
2 | 910 |
public Dimension getPreferredSize( JComponent c ) { |
911 |
return getMinimumSize(c); |
|
912 |
} |
|
913 |
||
914 |
/** |
|
915 |
* The minumum size is the size of the display area plus insets plus the button. |
|
916 |
*/ |
|
2658 | 917 |
@Override |
2 | 918 |
public Dimension getMinimumSize( JComponent c ) { |
919 |
if ( !isMinimumSizeDirty ) { |
|
920 |
return new Dimension(cachedMinimumSize); |
|
921 |
} |
|
922 |
Dimension size = getDisplaySize(); |
|
923 |
Insets insets = getInsets(); |
|
2658 | 924 |
//calculate the width and height of the button |
925 |
int buttonHeight = size.height; |
|
926 |
int buttonWidth = squareButton ? buttonHeight : arrowButton.getPreferredSize().width; |
|
927 |
//adjust the size based on the button width |
|
2 | 928 |
size.height += insets.top + insets.bottom; |
2658 | 929 |
size.width += insets.left + insets.right + buttonWidth; |
2 | 930 |
|
931 |
cachedMinimumSize.setSize( size.width, size.height ); |
|
932 |
isMinimumSizeDirty = false; |
|
933 |
||
934 |
return new Dimension(size); |
|
935 |
} |
|
936 |
||
2658 | 937 |
@Override |
2 | 938 |
public Dimension getMaximumSize( JComponent c ) { |
939 |
return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE); |
|
940 |
} |
|
941 |
||
942 |
/** |
|
943 |
* Returns the baseline. |
|
944 |
* |
|
945 |
* @throws NullPointerException {@inheritDoc} |
|
946 |
* @throws IllegalArgumentException {@inheritDoc} |
|
947 |
* @see javax.swing.JComponent#getBaseline(int, int) |
|
948 |
* @since 1.6 |
|
949 |
*/ |
|
2658 | 950 |
@Override |
2 | 951 |
public int getBaseline(JComponent c, int width, int height) { |
952 |
super.getBaseline(c, width, height); |
|
953 |
int baseline = -1; |
|
954 |
// force sameBaseline to be updated. |
|
955 |
getDisplaySize(); |
|
956 |
if (sameBaseline) { |
|
957 |
Insets insets = c.getInsets(); |
|
958 |
height = height - insets.top - insets.bottom; |
|
959 |
if (!comboBox.isEditable()) { |
|
960 |
ListCellRenderer renderer = comboBox.getRenderer(); |
|
961 |
if (renderer == null) { |
|
962 |
renderer = new DefaultListCellRenderer(); |
|
963 |
} |
|
964 |
Object value = null; |
|
965 |
Object prototypeValue = comboBox.getPrototypeDisplayValue(); |
|
966 |
if (prototypeValue != null) { |
|
967 |
value = prototypeValue; |
|
968 |
} |
|
969 |
else if (comboBox.getModel().getSize() > 0) { |
|
970 |
// Note, we're assuming the baseline is the same for all |
|
971 |
// cells, if not, this needs to loop through all. |
|
972 |
value = comboBox.getModel().getElementAt(0); |
|
973 |
} |
|
974 |
if (value == null) { |
|
975 |
value = " "; |
|
976 |
} else if (value instanceof String && "".equals(value)) { |
|
977 |
value = " "; |
|
978 |
} |
|
979 |
Component component = renderer. |
|
980 |
getListCellRendererComponent(listBox, value, -1, |
|
981 |
false, false); |
|
982 |
if (component instanceof JComponent) { |
|
983 |
component.setFont(comboBox.getFont()); |
|
984 |
} |
|
985 |
baseline = component.getBaseline(width, height); |
|
986 |
} |
|
987 |
else { |
|
988 |
baseline = editor.getBaseline(width, height); |
|
989 |
} |
|
990 |
if (baseline > 0) { |
|
991 |
baseline += insets.top; |
|
992 |
} |
|
993 |
} |
|
994 |
return baseline; |
|
995 |
} |
|
996 |
||
997 |
/** |
|
998 |
* Returns an enum indicating how the baseline of the component |
|
999 |
* changes as the size changes. |
|
1000 |
* |
|
1001 |
* @throws NullPointerException {@inheritDoc} |
|
1002 |
* @see javax.swing.JComponent#getBaseline(int, int) |
|
1003 |
* @since 1.6 |
|
1004 |
*/ |
|
2658 | 1005 |
@Override |
2 | 1006 |
public Component.BaselineResizeBehavior getBaselineResizeBehavior( |
1007 |
JComponent c) { |
|
1008 |
super.getBaselineResizeBehavior(c); |
|
1009 |
// Force sameBaseline to be updated. |
|
1010 |
getDisplaySize(); |
|
1011 |
if (comboBox.isEditable()) { |
|
1012 |
return editor.getBaselineResizeBehavior(); |
|
1013 |
} |
|
1014 |
else if (sameBaseline) { |
|
1015 |
ListCellRenderer renderer = comboBox.getRenderer(); |
|
1016 |
if (renderer == null) { |
|
1017 |
renderer = new DefaultListCellRenderer(); |
|
1018 |
} |
|
1019 |
Object value = null; |
|
1020 |
Object prototypeValue = comboBox.getPrototypeDisplayValue(); |
|
1021 |
if (prototypeValue != null) { |
|
1022 |
value = prototypeValue; |
|
1023 |
} |
|
1024 |
else if (comboBox.getModel().getSize() > 0) { |
|
1025 |
// Note, we're assuming the baseline is the same for all |
|
1026 |
// cells, if not, this needs to loop through all. |
|
1027 |
value = comboBox.getModel().getElementAt(0); |
|
1028 |
} |
|
1029 |
if (value != null) { |
|
1030 |
Component component = renderer. |
|
1031 |
getListCellRendererComponent(listBox, value, -1, |
|
1032 |
false, false); |
|
1033 |
return component.getBaselineResizeBehavior(); |
|
1034 |
} |
|
1035 |
} |
|
1036 |
return Component.BaselineResizeBehavior.OTHER; |
|
1037 |
} |
|
1038 |
||
1039 |
// This is currently hacky... |
|
2658 | 1040 |
@Override |
2 | 1041 |
public int getAccessibleChildrenCount(JComponent c) { |
1042 |
if ( comboBox.isEditable() ) { |
|
1043 |
return 2; |
|
1044 |
} |
|
1045 |
else { |
|
1046 |
return 1; |
|
1047 |
} |
|
1048 |
} |
|
1049 |
||
1050 |
// This is currently hacky... |
|
2658 | 1051 |
@Override |
2 | 1052 |
public Accessible getAccessibleChild(JComponent c, int i) { |
1053 |
// 0 = the popup |
|
1054 |
// 1 = the editor |
|
1055 |
switch ( i ) { |
|
1056 |
case 0: |
|
1057 |
if ( popup instanceof Accessible ) { |
|
1058 |
AccessibleContext ac = ((Accessible) popup).getAccessibleContext(); |
|
1059 |
ac.setAccessibleParent(comboBox); |
|
1060 |
return(Accessible) popup; |
|
1061 |
} |
|
1062 |
break; |
|
1063 |
case 1: |
|
1064 |
if ( comboBox.isEditable() |
|
1065 |
&& (editor instanceof Accessible) ) { |
|
1066 |
AccessibleContext ac = ((Accessible) editor).getAccessibleContext(); |
|
1067 |
ac.setAccessibleParent(comboBox); |
|
1068 |
return(Accessible) editor; |
|
1069 |
} |
|
1070 |
break; |
|
1071 |
} |
|
1072 |
return null; |
|
1073 |
} |
|
1074 |
||
1075 |
// |
|
1076 |
// end ComponentUI Implementation |
|
1077 |
//=============================== |
|
1078 |
||
1079 |
||
1080 |
//====================== |
|
1081 |
// begin Utility Methods |
|
1082 |
// |
|
1083 |
||
1084 |
/** |
|
1085 |
* Returns whether or not the supplied keyCode maps to a key that is used for |
|
1086 |
* navigation. This is used for optimizing key input by only passing non- |
|
1087 |
* navigation keys to the type-ahead mechanism. Subclasses should override this |
|
1088 |
* if they change the navigation keys. |
|
1089 |
*/ |
|
1090 |
protected boolean isNavigationKey( int keyCode ) { |
|
1091 |
return keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN || |
|
1092 |
keyCode == KeyEvent.VK_KP_UP || keyCode == KeyEvent.VK_KP_DOWN; |
|
1093 |
} |
|
1094 |
||
1095 |
private boolean isNavigationKey(int keyCode, int modifiers) { |
|
1096 |
InputMap inputMap = comboBox.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); |
|
1097 |
KeyStroke key = KeyStroke.getKeyStroke(keyCode, modifiers); |
|
1098 |
||
1099 |
if (inputMap != null && inputMap.get(key) != null) { |
|
1100 |
return true; |
|
1101 |
} |
|
1102 |
return false; |
|
1103 |
} |
|
1104 |
||
1105 |
/** |
|
1106 |
* Selects the next item in the list. It won't change the selection if the |
|
1107 |
* currently selected item is already the last item. |
|
1108 |
*/ |
|
1109 |
protected void selectNextPossibleValue() { |
|
1110 |
int si; |
|
1111 |
||
1112 |
if ( comboBox.isPopupVisible() ) { |
|
1113 |
si = listBox.getSelectedIndex(); |
|
1114 |
} |
|
1115 |
else { |
|
1116 |
si = comboBox.getSelectedIndex(); |
|
1117 |
} |
|
1118 |
||
1119 |
if ( si < comboBox.getModel().getSize() - 1 ) { |
|
1120 |
listBox.setSelectedIndex( si + 1 ); |
|
1121 |
listBox.ensureIndexIsVisible( si + 1 ); |
|
1122 |
if ( !isTableCellEditor ) { |
|
15637
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1123 |
if (!(UIManager.getBoolean("ComboBox.noActionOnKeyNavigation") && comboBox.isPopupVisible())) { |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1124 |
comboBox.setSelectedIndex(si+1); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1125 |
} |
2 | 1126 |
} |
1127 |
comboBox.repaint(); |
|
1128 |
} |
|
1129 |
} |
|
1130 |
||
1131 |
/** |
|
1132 |
* Selects the previous item in the list. It won't change the selection if the |
|
1133 |
* currently selected item is already the first item. |
|
1134 |
*/ |
|
1135 |
protected void selectPreviousPossibleValue() { |
|
1136 |
int si; |
|
1137 |
||
1138 |
if ( comboBox.isPopupVisible() ) { |
|
1139 |
si = listBox.getSelectedIndex(); |
|
1140 |
} |
|
1141 |
else { |
|
1142 |
si = comboBox.getSelectedIndex(); |
|
1143 |
} |
|
1144 |
||
1145 |
if ( si > 0 ) { |
|
1146 |
listBox.setSelectedIndex( si - 1 ); |
|
1147 |
listBox.ensureIndexIsVisible( si - 1 ); |
|
1148 |
if ( !isTableCellEditor ) { |
|
15637
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1149 |
if (!(UIManager.getBoolean("ComboBox.noActionOnKeyNavigation") && comboBox.isPopupVisible())) { |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1150 |
comboBox.setSelectedIndex(si-1); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1151 |
} |
2 | 1152 |
} |
1153 |
comboBox.repaint(); |
|
1154 |
} |
|
1155 |
} |
|
1156 |
||
1157 |
/** |
|
1158 |
* Hides the popup if it is showing and shows the popup if it is hidden. |
|
1159 |
*/ |
|
1160 |
protected void toggleOpenClose() { |
|
1161 |
setPopupVisible(comboBox, !isPopupVisible(comboBox)); |
|
1162 |
} |
|
1163 |
||
1164 |
/** |
|
1165 |
* Returns the area that is reserved for drawing the currently selected item. |
|
1166 |
*/ |
|
1167 |
protected Rectangle rectangleForCurrentValue() { |
|
1168 |
int width = comboBox.getWidth(); |
|
1169 |
int height = comboBox.getHeight(); |
|
1170 |
Insets insets = getInsets(); |
|
1171 |
int buttonSize = height - (insets.top + insets.bottom); |
|
1172 |
if ( arrowButton != null ) { |
|
1173 |
buttonSize = arrowButton.getWidth(); |
|
1174 |
} |
|
1175 |
if(BasicGraphicsUtils.isLeftToRight(comboBox)) { |
|
1176 |
return new Rectangle(insets.left, insets.top, |
|
1177 |
width - (insets.left + insets.right + buttonSize), |
|
1178 |
height - (insets.top + insets.bottom)); |
|
1179 |
} |
|
1180 |
else { |
|
1181 |
return new Rectangle(insets.left + buttonSize, insets.top, |
|
1182 |
width - (insets.left + insets.right + buttonSize), |
|
1183 |
height - (insets.top + insets.bottom)); |
|
1184 |
} |
|
1185 |
} |
|
1186 |
||
1187 |
/** |
|
1188 |
* Gets the insets from the JComboBox. |
|
1189 |
*/ |
|
1190 |
protected Insets getInsets() { |
|
1191 |
return comboBox.getInsets(); |
|
1192 |
} |
|
1193 |
||
1194 |
// |
|
1195 |
// end Utility Methods |
|
1196 |
//==================== |
|
1197 |
||
1198 |
||
1199 |
//=============================== |
|
1200 |
// begin Painting Utility Methods |
|
1201 |
// |
|
1202 |
||
1203 |
/** |
|
1204 |
* Paints the currently selected item. |
|
1205 |
*/ |
|
1206 |
public void paintCurrentValue(Graphics g,Rectangle bounds,boolean hasFocus) { |
|
1207 |
ListCellRenderer renderer = comboBox.getRenderer(); |
|
1208 |
Component c; |
|
1209 |
||
1210 |
if ( hasFocus && !isPopupVisible(comboBox) ) { |
|
1211 |
c = renderer.getListCellRendererComponent( listBox, |
|
1212 |
comboBox.getSelectedItem(), |
|
1213 |
-1, |
|
1214 |
true, |
|
1215 |
false ); |
|
1216 |
} |
|
1217 |
else { |
|
1218 |
c = renderer.getListCellRendererComponent( listBox, |
|
1219 |
comboBox.getSelectedItem(), |
|
1220 |
-1, |
|
1221 |
false, |
|
1222 |
false ); |
|
1223 |
c.setBackground(UIManager.getColor("ComboBox.background")); |
|
1224 |
} |
|
1225 |
c.setFont(comboBox.getFont()); |
|
1226 |
if ( hasFocus && !isPopupVisible(comboBox) ) { |
|
1227 |
c.setForeground(listBox.getSelectionForeground()); |
|
1228 |
c.setBackground(listBox.getSelectionBackground()); |
|
1229 |
} |
|
1230 |
else { |
|
1231 |
if ( comboBox.isEnabled() ) { |
|
1232 |
c.setForeground(comboBox.getForeground()); |
|
1233 |
c.setBackground(comboBox.getBackground()); |
|
1234 |
} |
|
1235 |
else { |
|
1236 |
c.setForeground(DefaultLookup.getColor( |
|
1237 |
comboBox, this, "ComboBox.disabledForeground", null)); |
|
1238 |
c.setBackground(DefaultLookup.getColor( |
|
1239 |
comboBox, this, "ComboBox.disabledBackground", null)); |
|
1240 |
} |
|
1241 |
} |
|
1242 |
||
1243 |
// Fix for 4238829: should lay out the JPanel. |
|
1244 |
boolean shouldValidate = false; |
|
1245 |
if (c instanceof JPanel) { |
|
1246 |
shouldValidate = true; |
|
1247 |
} |
|
1248 |
||
2658 | 1249 |
int x = bounds.x, y = bounds.y, w = bounds.width, h = bounds.height; |
1250 |
if (padding != null) { |
|
1251 |
x = bounds.x + padding.left; |
|
1252 |
y = bounds.y + padding.top; |
|
1253 |
w = bounds.width - (padding.left + padding.right); |
|
1254 |
h = bounds.height - (padding.top + padding.bottom); |
|
1255 |
} |
|
1256 |
||
1257 |
currentValuePane.paintComponent(g,c,comboBox,x,y,w,h,shouldValidate); |
|
2 | 1258 |
} |
1259 |
||
1260 |
/** |
|
1261 |
* Paints the background of the currently selected item. |
|
1262 |
*/ |
|
1263 |
public void paintCurrentValueBackground(Graphics g,Rectangle bounds,boolean hasFocus) { |
|
1264 |
Color t = g.getColor(); |
|
1265 |
if ( comboBox.isEnabled() ) |
|
1266 |
g.setColor(DefaultLookup.getColor(comboBox, this, |
|
1267 |
"ComboBox.background", null)); |
|
1268 |
else |
|
1269 |
g.setColor(DefaultLookup.getColor(comboBox, this, |
|
1270 |
"ComboBox.disabledBackground", null)); |
|
1271 |
g.fillRect(bounds.x,bounds.y,bounds.width,bounds.height); |
|
1272 |
g.setColor(t); |
|
1273 |
} |
|
1274 |
||
1275 |
/** |
|
1276 |
* Repaint the currently selected item. |
|
1277 |
*/ |
|
1278 |
void repaintCurrentValue() { |
|
1279 |
Rectangle r = rectangleForCurrentValue(); |
|
1280 |
comboBox.repaint(r.x,r.y,r.width,r.height); |
|
1281 |
} |
|
1282 |
||
1283 |
// |
|
1284 |
// end Painting Utility Methods |
|
1285 |
//============================= |
|
1286 |
||
1287 |
||
1288 |
//=============================== |
|
1289 |
// begin Size Utility Methods |
|
1290 |
// |
|
1291 |
||
1292 |
/** |
|
1293 |
* Return the default size of an empty display area of the combo box using |
|
1294 |
* the current renderer and font. |
|
1295 |
* |
|
1296 |
* @return the size of an empty display area |
|
1297 |
* @see #getDisplaySize |
|
1298 |
*/ |
|
1299 |
protected Dimension getDefaultSize() { |
|
1300 |
// Calculates the height and width using the default text renderer |
|
1301 |
Dimension d = getSizeForComponent(getDefaultListCellRenderer().getListCellRendererComponent(listBox, " ", -1, false, false)); |
|
1302 |
||
1303 |
return new Dimension(d.width, d.height); |
|
1304 |
} |
|
1305 |
||
1306 |
/** |
|
1307 |
* Returns the calculated size of the display area. The display area is the |
|
1308 |
* portion of the combo box in which the selected item is displayed. This |
|
1309 |
* method will use the prototype display value if it has been set. |
|
1310 |
* <p> |
|
1311 |
* For combo boxes with a non trivial number of items, it is recommended to |
|
1312 |
* use a prototype display value to significantly speed up the display |
|
1313 |
* size calculation. |
|
1314 |
* |
|
1315 |
* @return the size of the display area calculated from the combo box items |
|
1316 |
* @see javax.swing.JComboBox#setPrototypeDisplayValue |
|
1317 |
*/ |
|
1318 |
protected Dimension getDisplaySize() { |
|
1319 |
if (!isDisplaySizeDirty) { |
|
1320 |
return new Dimension(cachedDisplaySize); |
|
1321 |
} |
|
1322 |
Dimension result = new Dimension(); |
|
1323 |
||
1324 |
ListCellRenderer renderer = comboBox.getRenderer(); |
|
1325 |
if (renderer == null) { |
|
1326 |
renderer = new DefaultListCellRenderer(); |
|
1327 |
} |
|
1328 |
||
1329 |
sameBaseline = true; |
|
1330 |
||
1331 |
Object prototypeValue = comboBox.getPrototypeDisplayValue(); |
|
1332 |
if (prototypeValue != null) { |
|
1333 |
// Calculates the dimension based on the prototype value |
|
1334 |
result = getSizeForComponent(renderer.getListCellRendererComponent(listBox, |
|
1335 |
prototypeValue, |
|
1336 |
-1, false, false)); |
|
1337 |
} else { |
|
1338 |
// Calculate the dimension by iterating over all the elements in the combo |
|
1339 |
// box list. |
|
1340 |
ComboBoxModel model = comboBox.getModel(); |
|
1341 |
int modelSize = model.getSize(); |
|
1342 |
int baseline = -1; |
|
1343 |
Dimension d; |
|
1344 |
||
1345 |
Component cpn; |
|
1346 |
||
1347 |
if (modelSize > 0 ) { |
|
1348 |
for (int i = 0; i < modelSize ; i++ ) { |
|
1349 |
// Calculates the maximum height and width based on the largest |
|
1350 |
// element |
|
1351 |
Object value = model.getElementAt(i); |
|
1352 |
Component c = renderer.getListCellRendererComponent( |
|
1353 |
listBox, value, -1, false, false); |
|
1354 |
d = getSizeForComponent(c); |
|
1355 |
if (sameBaseline && value != null && |
|
1356 |
(!(value instanceof String) || !"".equals(value))) { |
|
1357 |
int newBaseline = c.getBaseline(d.width, d.height); |
|
1358 |
if (newBaseline == -1) { |
|
1359 |
sameBaseline = false; |
|
1360 |
} |
|
1361 |
else if (baseline == -1) { |
|
1362 |
baseline = newBaseline; |
|
1363 |
} |
|
1364 |
else if (baseline != newBaseline) { |
|
1365 |
sameBaseline = false; |
|
1366 |
} |
|
1367 |
} |
|
1368 |
result.width = Math.max(result.width,d.width); |
|
1369 |
result.height = Math.max(result.height,d.height); |
|
1370 |
} |
|
1371 |
} else { |
|
1372 |
result = getDefaultSize(); |
|
1373 |
if (comboBox.isEditable()) { |
|
1374 |
result.width = 100; |
|
1375 |
} |
|
1376 |
} |
|
1377 |
} |
|
1378 |
||
1379 |
if ( comboBox.isEditable() ) { |
|
1380 |
Dimension d = editor.getPreferredSize(); |
|
1381 |
result.width = Math.max(result.width,d.width); |
|
1382 |
result.height = Math.max(result.height,d.height); |
|
1383 |
} |
|
1384 |
||
2658 | 1385 |
// calculate in the padding |
1386 |
if (padding != null) { |
|
1387 |
result.width += padding.left + padding.right; |
|
1388 |
result.height += padding.top + padding.bottom; |
|
1389 |
} |
|
1390 |
||
2 | 1391 |
// Set the cached value |
1392 |
cachedDisplaySize.setSize(result.width, result.height); |
|
1393 |
isDisplaySizeDirty = false; |
|
1394 |
||
1395 |
return result; |
|
1396 |
} |
|
1397 |
||
1398 |
/** |
|
4394 | 1399 |
* Returns the size a component would have if used as a cell renderer. |
1400 |
* |
|
1401 |
* @param comp a {@code Component} to check |
|
1402 |
* @return size of the component |
|
1403 |
* @since 1.7 |
|
2 | 1404 |
*/ |
4394 | 1405 |
protected Dimension getSizeForComponent(Component comp) { |
1406 |
// This has been refactored out in hopes that it may be investigated and |
|
1407 |
// simplified for the next major release. adding/removing |
|
1408 |
// the component to the currentValuePane and changing the font may be |
|
1409 |
// redundant operations. |
|
2 | 1410 |
currentValuePane.add(comp); |
1411 |
comp.setFont(comboBox.getFont()); |
|
1412 |
Dimension d = comp.getPreferredSize(); |
|
1413 |
currentValuePane.remove(comp); |
|
1414 |
return d; |
|
1415 |
} |
|
1416 |
||
1417 |
||
1418 |
// |
|
1419 |
// end Size Utility Methods |
|
1420 |
//============================= |
|
1421 |
||
1422 |
||
1423 |
//================================= |
|
1424 |
// begin Keyboard Action Management |
|
1425 |
// |
|
1426 |
||
1427 |
/** |
|
1428 |
* Adds keyboard actions to the JComboBox. Actions on enter and esc are already |
|
1429 |
* supplied. Add more actions as you need them. |
|
1430 |
*/ |
|
1431 |
protected void installKeyboardActions() { |
|
1432 |
InputMap km = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); |
|
1433 |
SwingUtilities.replaceUIInputMap(comboBox, JComponent. |
|
1434 |
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, km); |
|
1435 |
||
1436 |
||
1437 |
LazyActionMap.installLazyActionMap(comboBox, BasicComboBoxUI.class, |
|
1438 |
"ComboBox.actionMap"); |
|
1439 |
} |
|
1440 |
||
1441 |
InputMap getInputMap(int condition) { |
|
1442 |
if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { |
|
1443 |
return (InputMap)DefaultLookup.get(comboBox, this, |
|
1444 |
"ComboBox.ancestorInputMap"); |
|
1445 |
} |
|
1446 |
return null; |
|
1447 |
} |
|
1448 |
||
1449 |
boolean isTableCellEditor() { |
|
1450 |
return isTableCellEditor; |
|
1451 |
} |
|
1452 |
||
1453 |
/** |
|
1454 |
* Removes the focus InputMap and ActionMap. |
|
1455 |
*/ |
|
1456 |
protected void uninstallKeyboardActions() { |
|
1457 |
SwingUtilities.replaceUIInputMap(comboBox, JComponent. |
|
1458 |
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); |
|
1459 |
SwingUtilities.replaceUIActionMap(comboBox, null); |
|
1460 |
} |
|
1461 |
||
1462 |
||
1463 |
// |
|
1464 |
// Actions |
|
1465 |
// |
|
1466 |
private static class Actions extends UIAction { |
|
1467 |
private static final String HIDE = "hidePopup"; |
|
1468 |
private static final String DOWN = "selectNext"; |
|
1469 |
private static final String DOWN_2 = "selectNext2"; |
|
1470 |
private static final String TOGGLE = "togglePopup"; |
|
1471 |
private static final String TOGGLE_2 = "spacePopup"; |
|
1472 |
private static final String UP = "selectPrevious"; |
|
1473 |
private static final String UP_2 = "selectPrevious2"; |
|
1474 |
private static final String ENTER = "enterPressed"; |
|
1475 |
private static final String PAGE_DOWN = "pageDownPassThrough"; |
|
1476 |
private static final String PAGE_UP = "pageUpPassThrough"; |
|
1477 |
private static final String HOME = "homePassThrough"; |
|
1478 |
private static final String END = "endPassThrough"; |
|
1479 |
||
1480 |
Actions(String name) { |
|
1481 |
super(name); |
|
1482 |
} |
|
1483 |
||
1484 |
public void actionPerformed( ActionEvent e ) { |
|
1485 |
String key = getName(); |
|
1486 |
JComboBox comboBox = (JComboBox)e.getSource(); |
|
1487 |
BasicComboBoxUI ui = (BasicComboBoxUI)BasicLookAndFeel.getUIOfType( |
|
1488 |
comboBox.getUI(), BasicComboBoxUI.class); |
|
1489 |
if (key == HIDE) { |
|
1490 |
comboBox.firePopupMenuCanceled(); |
|
1491 |
comboBox.setPopupVisible(false); |
|
1492 |
} |
|
1493 |
else if (key == PAGE_DOWN || key == PAGE_UP || |
|
1494 |
key == HOME || key == END) { |
|
1495 |
int index = getNextIndex(comboBox, key); |
|
1496 |
if (index >= 0 && index < comboBox.getItemCount()) { |
|
15637
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1497 |
if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation") && comboBox.isPopupVisible()) { |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1498 |
ui.listBox.setSelectedIndex(index); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1499 |
ui.listBox.ensureIndexIsVisible(index); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1500 |
comboBox.repaint(); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1501 |
} else { |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1502 |
comboBox.setSelectedIndex(index); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1503 |
} |
2 | 1504 |
} |
1505 |
} |
|
1506 |
else if (key == DOWN) { |
|
1507 |
if (comboBox.isShowing() ) { |
|
1508 |
if ( comboBox.isPopupVisible() ) { |
|
1509 |
if (ui != null) { |
|
1510 |
ui.selectNextPossibleValue(); |
|
1511 |
} |
|
1512 |
} else { |
|
1513 |
comboBox.setPopupVisible(true); |
|
1514 |
} |
|
1515 |
} |
|
1516 |
} |
|
1517 |
else if (key == DOWN_2) { |
|
1518 |
// Special case in which pressing the arrow keys will not |
|
1519 |
// make the popup appear - except for editable combo boxes |
|
1520 |
// and combo boxes inside a table. |
|
1521 |
if (comboBox.isShowing() ) { |
|
1522 |
if ( (comboBox.isEditable() || |
|
1523 |
(ui != null && ui.isTableCellEditor())) |
|
1524 |
&& !comboBox.isPopupVisible() ) { |
|
1525 |
comboBox.setPopupVisible(true); |
|
1526 |
} else { |
|
1527 |
if (ui != null) { |
|
1528 |
ui.selectNextPossibleValue(); |
|
1529 |
} |
|
1530 |
} |
|
1531 |
} |
|
1532 |
} |
|
1533 |
else if (key == TOGGLE || key == TOGGLE_2) { |
|
1534 |
if (ui != null && (key == TOGGLE || !comboBox.isEditable())) { |
|
1535 |
if ( ui.isTableCellEditor() ) { |
|
1536 |
// Forces the selection of the list item if the |
|
1537 |
// combo box is in a JTable. |
|
1538 |
comboBox.setSelectedIndex(ui.popup.getList(). |
|
1539 |
getSelectedIndex()); |
|
1540 |
} |
|
1541 |
else { |
|
1542 |
comboBox.setPopupVisible(!comboBox.isPopupVisible()); |
|
1543 |
} |
|
1544 |
} |
|
1545 |
} |
|
1546 |
else if (key == UP) { |
|
1547 |
if (ui != null) { |
|
1548 |
if (ui.isPopupVisible(comboBox)) { |
|
1549 |
ui.selectPreviousPossibleValue(); |
|
1550 |
} |
|
1551 |
else if (DefaultLookup.getBoolean(comboBox, ui, |
|
1552 |
"ComboBox.showPopupOnNavigation", false)) { |
|
1553 |
ui.setPopupVisible(comboBox, true); |
|
1554 |
} |
|
1555 |
} |
|
1556 |
} |
|
1557 |
else if (key == UP_2) { |
|
1558 |
// Special case in which pressing the arrow keys will not |
|
1559 |
// make the popup appear - except for editable combo boxes. |
|
1560 |
if (comboBox.isShowing() && ui != null) { |
|
1561 |
if ( comboBox.isEditable() && !comboBox.isPopupVisible()) { |
|
1562 |
comboBox.setPopupVisible(true); |
|
1563 |
} else { |
|
1564 |
ui.selectPreviousPossibleValue(); |
|
1565 |
} |
|
1566 |
} |
|
1567 |
} |
|
1568 |
||
1569 |
else if (key == ENTER) { |
|
1570 |
if (comboBox.isPopupVisible()) { |
|
15637
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1571 |
// If ComboBox.noActionOnKeyNavigation is set, |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1572 |
// forse selection of list item |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1573 |
if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation")) { |
2 | 1574 |
Object listItem = ui.popup.getList().getSelectedValue(); |
1575 |
if (listItem != null) { |
|
1576 |
comboBox.getEditor().setItem(listItem); |
|
1284
39d9a711aad4
6607130: REGRESSION: JComboBox cell editor isn't hidden if the same value is selected with keyboard
mlapshin
parents:
2
diff
changeset
|
1577 |
comboBox.setSelectedItem(listItem); |
2 | 1578 |
} |
15637
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1579 |
comboBox.setPopupVisible(false); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1580 |
} else { |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1581 |
// Forces the selection of the list item |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1582 |
boolean isEnterSelectablePopup = |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1583 |
UIManager.getBoolean("ComboBox.isEnterSelectablePopup"); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1584 |
if (!comboBox.isEditable() || isEnterSelectablePopup |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1585 |
|| ui.isTableCellEditor) { |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1586 |
Object listItem = ui.popup.getList().getSelectedValue(); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1587 |
if (listItem != null) { |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1588 |
// Use the selected value from popup |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1589 |
// to set the selected item in combo box, |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1590 |
// but ensure before that JComboBox.actionPerformed() |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1591 |
// won't use editor's value to set the selected item |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1592 |
comboBox.getEditor().setItem(listItem); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1593 |
comboBox.setSelectedItem(listItem); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1594 |
} |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1595 |
} |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1596 |
comboBox.setPopupVisible(false); |
2 | 1597 |
} |
1598 |
} |
|
1599 |
else { |
|
1284
39d9a711aad4
6607130: REGRESSION: JComboBox cell editor isn't hidden if the same value is selected with keyboard
mlapshin
parents:
2
diff
changeset
|
1600 |
// Hide combo box if it is a table cell editor |
39d9a711aad4
6607130: REGRESSION: JComboBox cell editor isn't hidden if the same value is selected with keyboard
mlapshin
parents:
2
diff
changeset
|
1601 |
if (ui.isTableCellEditor && !comboBox.isEditable()) { |
39d9a711aad4
6607130: REGRESSION: JComboBox cell editor isn't hidden if the same value is selected with keyboard
mlapshin
parents:
2
diff
changeset
|
1602 |
comboBox.setSelectedItem(comboBox.getSelectedItem()); |
39d9a711aad4
6607130: REGRESSION: JComboBox cell editor isn't hidden if the same value is selected with keyboard
mlapshin
parents:
2
diff
changeset
|
1603 |
} |
2 | 1604 |
// Call the default button binding. |
1605 |
// This is a pretty messy way of passing an event through |
|
1606 |
// to the root pane. |
|
1607 |
JRootPane root = SwingUtilities.getRootPane(comboBox); |
|
1608 |
if (root != null) { |
|
1609 |
InputMap im = root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); |
|
1610 |
ActionMap am = root.getActionMap(); |
|
1611 |
if (im != null && am != null) { |
|
1612 |
Object obj = im.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0)); |
|
1613 |
if (obj != null) { |
|
1614 |
Action action = am.get(obj); |
|
1615 |
if (action != null) { |
|
1616 |
action.actionPerformed(new ActionEvent( |
|
1617 |
root, e.getID(), e.getActionCommand(), |
|
1618 |
e.getWhen(), e.getModifiers())); |
|
1619 |
} |
|
1620 |
} |
|
1621 |
} |
|
1622 |
} |
|
1623 |
} |
|
1624 |
} |
|
1625 |
} |
|
1626 |
||
1627 |
private int getNextIndex(JComboBox comboBox, String key) { |
|
15637
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1628 |
int listHeight = comboBox.getMaximumRowCount(); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1629 |
|
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1630 |
int selectedIndex = comboBox.getSelectedIndex(); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1631 |
if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation") |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1632 |
&& (comboBox.getUI() instanceof BasicComboBoxUI)) { |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1633 |
selectedIndex = ((BasicComboBoxUI) comboBox.getUI()).listBox.getSelectedIndex(); |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1634 |
} |
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1635 |
|
2 | 1636 |
if (key == PAGE_UP) { |
15637
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1637 |
int index = selectedIndex - listHeight; |
2 | 1638 |
return (index < 0 ? 0: index); |
1639 |
} |
|
1640 |
else if (key == PAGE_DOWN) { |
|
15637
2c226ebab6a6
4199622: RFE: JComboBox shouldn't sending ActionEvents for keyboard navigation
vkarnauk
parents:
9035
diff
changeset
|
1641 |
int index = selectedIndex + listHeight; |
2 | 1642 |
int max = comboBox.getItemCount(); |
1643 |
return (index < max ? index: max-1); |
|
1644 |
} |
|
1645 |
else if (key == HOME) { |
|
1646 |
return 0; |
|
1647 |
} |
|
1648 |
else if (key == END) { |
|
1649 |
return comboBox.getItemCount() - 1; |
|
1650 |
} |
|
1651 |
return comboBox.getSelectedIndex(); |
|
1652 |
} |
|
1653 |
||
1654 |
public boolean isEnabled(Object c) { |
|
1655 |
if (getName() == HIDE) { |
|
1656 |
return (c != null && ((JComboBox)c).isPopupVisible()); |
|
1657 |
} |
|
1658 |
return true; |
|
1659 |
} |
|
1660 |
} |
|
1661 |
// |
|
1662 |
// end Keyboard Action Management |
|
1663 |
//=============================== |
|
1664 |
||
1665 |
||
1666 |
// |
|
1667 |
// Shared Handler, implements all listeners |
|
1668 |
// |
|
1669 |
private class Handler implements ActionListener, FocusListener, |
|
1670 |
KeyListener, LayoutManager, |
|
1671 |
ListDataListener, PropertyChangeListener { |
|
1672 |
// |
|
1673 |
// PropertyChangeListener |
|
1674 |
// |
|
1675 |
public void propertyChange(PropertyChangeEvent e) { |
|
1676 |
String propertyName = e.getPropertyName(); |
|
2658 | 1677 |
if (e.getSource() == editor){ |
1678 |
// If the border of the editor changes then this can effect |
|
1679 |
// the size of the editor which can cause the combo's size to |
|
1680 |
// become invalid so we need to clear size caches |
|
1681 |
if ("border".equals(propertyName)){ |
|
1682 |
isMinimumSizeDirty = true; |
|
1683 |
isDisplaySizeDirty = true; |
|
1684 |
comboBox.revalidate(); |
|
2 | 1685 |
} |
2658 | 1686 |
} else { |
1687 |
JComboBox comboBox = (JComboBox)e.getSource(); |
|
1688 |
if ( propertyName == "model" ) { |
|
1689 |
ComboBoxModel newModel = (ComboBoxModel)e.getNewValue(); |
|
1690 |
ComboBoxModel oldModel = (ComboBoxModel)e.getOldValue(); |
|
2 | 1691 |
|
2658 | 1692 |
if ( oldModel != null && listDataListener != null ) { |
1693 |
oldModel.removeListDataListener( listDataListener ); |
|
1694 |
} |
|
1695 |
||
1696 |
if ( newModel != null && listDataListener != null ) { |
|
1697 |
newModel.addListDataListener( listDataListener ); |
|
1698 |
} |
|
2 | 1699 |
|
2658 | 1700 |
if ( editor != null ) { |
1701 |
comboBox.configureEditor( comboBox.getEditor(), comboBox.getSelectedItem() ); |
|
1702 |
} |
|
1703 |
isMinimumSizeDirty = true; |
|
1704 |
isDisplaySizeDirty = true; |
|
1705 |
comboBox.revalidate(); |
|
1706 |
comboBox.repaint(); |
|
1707 |
} |
|
1708 |
else if ( propertyName == "editor" && comboBox.isEditable() ) { |
|
1709 |
addEditor(); |
|
1710 |
comboBox.revalidate(); |
|
1711 |
} |
|
1712 |
else if ( propertyName == "editable" ) { |
|
1713 |
if ( comboBox.isEditable() ) { |
|
1714 |
comboBox.setRequestFocusEnabled( false ); |
|
1715 |
addEditor(); |
|
1716 |
} else { |
|
1717 |
comboBox.setRequestFocusEnabled( true ); |
|
1718 |
removeEditor(); |
|
1719 |
} |
|
1720 |
updateToolTipTextForChildren(); |
|
1721 |
comboBox.revalidate(); |
|
1722 |
} |
|
1723 |
else if ( propertyName == "enabled" ) { |
|
1724 |
boolean enabled = comboBox.isEnabled(); |
|
1725 |
if ( editor != null ) |
|
1726 |
editor.setEnabled(enabled); |
|
1727 |
if ( arrowButton != null ) |
|
1728 |
arrowButton.setEnabled(enabled); |
|
1729 |
comboBox.repaint(); |
|
2 | 1730 |
} |
2658 | 1731 |
else if ( propertyName == "focusable" ) { |
1732 |
boolean focusable = comboBox.isFocusable(); |
|
1733 |
if ( editor != null ) |
|
1734 |
editor.setFocusable(focusable); |
|
1735 |
if ( arrowButton != null ) |
|
1736 |
arrowButton.setFocusable(focusable); |
|
1737 |
comboBox.repaint(); |
|
1738 |
} |
|
1739 |
else if ( propertyName == "maximumRowCount" ) { |
|
1740 |
if ( isPopupVisible( comboBox ) ) { |
|
1741 |
setPopupVisible(comboBox, false); |
|
1742 |
setPopupVisible(comboBox, true); |
|
1743 |
} |
|
2 | 1744 |
} |
2658 | 1745 |
else if ( propertyName == "font" ) { |
1746 |
listBox.setFont( comboBox.getFont() ); |
|
1747 |
if ( editor != null ) { |
|
1748 |
editor.setFont( comboBox.getFont() ); |
|
1749 |
} |
|
1750 |
isMinimumSizeDirty = true; |
|
8143
1b0feb422660
6988168: Press the "Toggle Font" button.The size of the combo box didn't change.
rupashka
parents:
5506
diff
changeset
|
1751 |
isDisplaySizeDirty = true; |
2658 | 1752 |
comboBox.validate(); |
1753 |
} |
|
1754 |
else if ( propertyName == JComponent.TOOL_TIP_TEXT_KEY ) { |
|
1755 |
updateToolTipTextForChildren(); |
|
1756 |
} |
|
1757 |
else if ( propertyName == BasicComboBoxUI.IS_TABLE_CELL_EDITOR ) { |
|
1758 |
Boolean inTable = (Boolean)e.getNewValue(); |
|
1759 |
isTableCellEditor = inTable.equals(Boolean.TRUE) ? true : false; |
|
1760 |
} |
|
1761 |
else if (propertyName == "prototypeDisplayValue") { |
|
1762 |
isMinimumSizeDirty = true; |
|
1763 |
isDisplaySizeDirty = true; |
|
1764 |
comboBox.revalidate(); |
|
1765 |
} |
|
1766 |
else if (propertyName == "renderer") { |
|
1767 |
isMinimumSizeDirty = true; |
|
1768 |
isDisplaySizeDirty = true; |
|
1769 |
comboBox.revalidate(); |
|
1770 |
} |
|
2 | 1771 |
} |
1772 |
} |
|
1773 |
||
1774 |
||
1775 |
// |
|
1776 |
// KeyListener |
|
1777 |
// |
|
1778 |
||
1779 |
// This listener checks to see if the key event isn't a navigation |
|
1780 |
// key. If it finds a key event that wasn't a navigation key it |
|
1781 |
// dispatches it to JComboBox.selectWithKeyChar() so that it can do |
|
1782 |
// type-ahead. |
|
1783 |
public void keyPressed( KeyEvent e ) { |
|
1784 |
if ( isNavigationKey(e.getKeyCode(), e.getModifiers()) ) { |
|
1785 |
lastTime = 0L; |
|
1786 |
} else if ( comboBox.isEnabled() && comboBox.getModel().getSize()!=0 && |
|
1787 |
isTypeAheadKey( e ) && e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) { |
|
1788 |
time = e.getWhen(); |
|
1789 |
if ( comboBox.selectWithKeyChar(e.getKeyChar()) ) { |
|
1790 |
e.consume(); |
|
1791 |
} |
|
1792 |
} |
|
1793 |
} |
|
1794 |
||
1795 |
public void keyTyped(KeyEvent e) { |
|
1796 |
} |
|
1797 |
||
1798 |
public void keyReleased(KeyEvent e) { |
|
1799 |
} |
|
1800 |
||
1801 |
private boolean isTypeAheadKey( KeyEvent e ) { |
|
3977
0da8e3baf0b5
4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents:
2658
diff
changeset
|
1802 |
return !e.isAltDown() && !BasicGraphicsUtils.isMenuShortcutKeyDown(e); |
2 | 1803 |
} |
1804 |
||
1805 |
// |
|
1806 |
// FocusListener |
|
1807 |
// |
|
1808 |
// NOTE: The class is added to both the Editor and ComboBox. |
|
1809 |
// The combo box listener hides the popup when the focus is lost. |
|
1810 |
// It also repaints when focus is gained or lost. |
|
1811 |
||
1812 |
public void focusGained( FocusEvent e ) { |
|
1813 |
ComboBoxEditor comboBoxEditor = comboBox.getEditor(); |
|
1814 |
||
1815 |
if ( (comboBoxEditor != null) && |
|
1816 |
(e.getSource() == comboBoxEditor.getEditorComponent()) ) { |
|
1817 |
return; |
|
1818 |
} |
|
1819 |
hasFocus = true; |
|
1820 |
comboBox.repaint(); |
|
1821 |
||
1822 |
if (comboBox.isEditable() && editor != null) { |
|
1823 |
editor.requestFocus(); |
|
1824 |
} |
|
1825 |
} |
|
1826 |
||
1827 |
public void focusLost( FocusEvent e ) { |
|
1828 |
ComboBoxEditor editor = comboBox.getEditor(); |
|
1829 |
if ( (editor != null) && |
|
1830 |
(e.getSource() == editor.getEditorComponent()) ) { |
|
1831 |
Object item = editor.getItem(); |
|
1832 |
||
1833 |
Object selectedItem = comboBox.getSelectedItem(); |
|
1834 |
if (!e.isTemporary() && item != null && |
|
1835 |
!item.equals((selectedItem == null) ? "" : selectedItem )) { |
|
1836 |
comboBox.actionPerformed |
|
1837 |
(new ActionEvent(editor, 0, "", |
|
1838 |
EventQueue.getMostRecentEventTime(), 0)); |
|
1839 |
} |
|
1840 |
} |
|
1841 |
||
1842 |
hasFocus = false; |
|
1843 |
if (!e.isTemporary()) { |
|
1844 |
setPopupVisible(comboBox, false); |
|
1845 |
} |
|
1846 |
comboBox.repaint(); |
|
1847 |
} |
|
1848 |
||
1849 |
// |
|
1850 |
// ListDataListener |
|
1851 |
// |
|
1852 |
||
1853 |
// This listener watches for changes in the ComboBoxModel |
|
1854 |
public void contentsChanged( ListDataEvent e ) { |
|
1855 |
if ( !(e.getIndex0() == -1 && e.getIndex1() == -1) ) { |
|
1856 |
isMinimumSizeDirty = true; |
|
1857 |
comboBox.revalidate(); |
|
1858 |
} |
|
1859 |
||
1860 |
// set the editor with the selected item since this |
|
1861 |
// is the event handler for a selected item change. |
|
1862 |
if (comboBox.isEditable() && editor != null) { |
|
1863 |
comboBox.configureEditor( comboBox.getEditor(), |
|
1864 |
comboBox.getSelectedItem() ); |
|
1865 |
} |
|
1866 |
||
1867 |
isDisplaySizeDirty = true; |
|
1868 |
comboBox.repaint(); |
|
1869 |
} |
|
1870 |
||
1871 |
public void intervalAdded( ListDataEvent e ) { |
|
1872 |
contentsChanged( e ); |
|
1873 |
} |
|
1874 |
||
1875 |
public void intervalRemoved( ListDataEvent e ) { |
|
1876 |
contentsChanged( e ); |
|
1877 |
} |
|
1878 |
||
1879 |
// |
|
1880 |
// LayoutManager |
|
1881 |
// |
|
1882 |
||
1883 |
// This layout manager handles the 'standard' layout of combo boxes. |
|
1884 |
// It puts the arrow button to the right and the editor to the left. |
|
1885 |
// If there is no editor it still keeps the arrow button to the right. |
|
1886 |
public void addLayoutComponent(String name, Component comp) {} |
|
1887 |
||
1888 |
public void removeLayoutComponent(Component comp) {} |
|
1889 |
||
1890 |
public Dimension preferredLayoutSize(Container parent) { |
|
1891 |
return parent.getPreferredSize(); |
|
1892 |
} |
|
1893 |
||
1894 |
public Dimension minimumLayoutSize(Container parent) { |
|
1895 |
return parent.getMinimumSize(); |
|
1896 |
} |
|
1897 |
||
1898 |
public void layoutContainer(Container parent) { |
|
1899 |
JComboBox cb = (JComboBox)parent; |
|
1900 |
int width = cb.getWidth(); |
|
1901 |
int height = cb.getHeight(); |
|
1902 |
||
1903 |
Insets insets = getInsets(); |
|
2658 | 1904 |
int buttonHeight = height - (insets.top + insets.bottom); |
1905 |
int buttonWidth = buttonHeight; |
|
1906 |
if (arrowButton != null) { |
|
1907 |
Insets arrowInsets = arrowButton.getInsets(); |
|
1908 |
buttonWidth = squareButton ? |
|
1909 |
buttonHeight : |
|
1910 |
arrowButton.getPreferredSize().width + arrowInsets.left + arrowInsets.right; |
|
1911 |
} |
|
2 | 1912 |
Rectangle cvb; |
1913 |
||
2658 | 1914 |
if (arrowButton != null) { |
1915 |
if (BasicGraphicsUtils.isLeftToRight(cb)) { |
|
1916 |
arrowButton.setBounds(width - (insets.right + buttonWidth), |
|
1917 |
insets.top, buttonWidth, buttonHeight); |
|
1918 |
} else { |
|
1919 |
arrowButton.setBounds(insets.left, insets.top, |
|
1920 |
buttonWidth, buttonHeight); |
|
2 | 1921 |
} |
1922 |
} |
|
1923 |
if ( editor != null ) { |
|
1924 |
cvb = rectangleForCurrentValue(); |
|
1925 |
editor.setBounds(cvb); |
|
1926 |
} |
|
1927 |
} |
|
1928 |
||
1929 |
// |
|
1930 |
// ActionListener |
|
1931 |
// |
|
1932 |
// Fix for 4515752: Forward the Enter pressed on the |
|
1933 |
// editable combo box to the default button |
|
1934 |
||
1935 |
// Note: This could depend on event ordering. The first ActionEvent |
|
1936 |
// from the editor may be handled by the JComboBox in which case, the |
|
1937 |
// enterPressed action will always be invoked. |
|
1938 |
public void actionPerformed(ActionEvent evt) { |
|
1939 |
Object item = comboBox.getEditor().getItem(); |
|
1940 |
if (item != null) { |
|
1941 |
if(!comboBox.isPopupVisible() && !item.equals(comboBox.getSelectedItem())) { |
|
1942 |
comboBox.setSelectedItem(comboBox.getEditor().getItem()); |
|
1943 |
} |
|
1944 |
ActionMap am = comboBox.getActionMap(); |
|
1945 |
if (am != null) { |
|
1946 |
Action action = am.get("enterPressed"); |
|
1947 |
if (action != null) { |
|
1948 |
action.actionPerformed(new ActionEvent(comboBox, evt.getID(), |
|
1949 |
evt.getActionCommand(), |
|
1950 |
evt.getModifiers())); |
|
1951 |
} |
|
1952 |
} |
|
1953 |
} |
|
1954 |
} |
|
1955 |
} |
|
1956 |
||
1957 |
class DefaultKeySelectionManager implements JComboBox.KeySelectionManager, UIResource { |
|
1958 |
private String prefix = ""; |
|
1959 |
private String typedString = ""; |
|
1960 |
||
1961 |
public int selectionForKey(char aKey,ComboBoxModel aModel) { |
|
1962 |
if (lastTime == 0L) { |
|
1963 |
prefix = ""; |
|
1964 |
typedString = ""; |
|
1965 |
} |
|
1966 |
boolean startingFromSelection = true; |
|
1967 |
||
1968 |
int startIndex = comboBox.getSelectedIndex(); |
|
1969 |
if (time - lastTime < timeFactor) { |
|
1970 |
typedString += aKey; |
|
1971 |
if((prefix.length() == 1) && (aKey == prefix.charAt(0))) { |
|
1972 |
// Subsequent same key presses move the keyboard focus to the next |
|
1973 |
// object that starts with the same letter. |
|
1974 |
startIndex++; |
|
1975 |
} else { |
|
1976 |
prefix = typedString; |
|
1977 |
} |
|
1978 |
} else { |
|
1979 |
startIndex++; |
|
1980 |
typedString = "" + aKey; |
|
1981 |
prefix = typedString; |
|
1982 |
} |
|
1983 |
lastTime = time; |
|
1984 |
||
1985 |
if (startIndex < 0 || startIndex >= aModel.getSize()) { |
|
1986 |
startingFromSelection = false; |
|
1987 |
startIndex = 0; |
|
1988 |
} |
|
1989 |
int index = listBox.getNextMatch(prefix, startIndex, |
|
1990 |
Position.Bias.Forward); |
|
1991 |
if (index < 0 && startingFromSelection) { // wrap |
|
1992 |
index = listBox.getNextMatch(prefix, 0, |
|
1993 |
Position.Bias.Forward); |
|
1994 |
} |
|
1995 |
return index; |
|
1996 |
} |
|
1997 |
} |
|
1998 |
||
1999 |
} |