1 /*
2 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
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
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
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 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25 package javax.swing;
26
27 import java.beans.PropertyChangeEvent;
28 import java.beans.PropertyChangeListener;
29 import java.beans.Transient;
30 import java.util;
31
32 import java.awt;
33 import java.awt.event;
34
35 import java.io.Serializable;
36 import java.io.ObjectOutputStream;
37 import java.io.ObjectInputStream;
38 import java.io.IOException;
39
40 import javax.swing.event;
41 import javax.swing.plaf;
42 import javax.swing.border;
43
44 import javax.accessibility;
45
46 /**
47 * A component that combines a button or editable field and a drop-down list.
48 * The user can select a value from the drop-down list, which appears at the
49 * user's request. If you make the combo box editable, then the combo box
50 * includes an editable field into which the user can type a value.
51 * <p>
52 * <strong>Warning:</strong> Swing is not thread safe. For more
53 * information see <a
54 * href="package-summary.html#threading">Swing's Threading
55 * Policy</a>.
56 * <p>
57 * <strong>Warning:</strong>
58 * Serialized objects of this class will not be compatible with
59 * future Swing releases. The current serialization support is
60 * appropriate for short term storage or RMI between applications running
61 * the same version of Swing. As of 1.4, support for long term storage
62 * of all JavaBeans<sup><font size="-2">TM</font></sup>
63 * has been added to the <code>java.beans</code> package.
64 * Please see {@link java.beans.XMLEncoder}.
65 *
66 * <p>
67 * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html">How to Use Combo Boxes</a>
68 * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
69 * for further information.
70 * <p>
71 * @see ComboBoxModel
72 * @see DefaultComboBoxModel
73 *
74 * @beaninfo
75 * attribute: isContainer false
76 * description: A combination of a text field and a drop-down list.
77 *
78 * @author Arnaud Weber
79 * @author Mark Davidson
80 */
81 public class JComboBox extends JComponent
82 implements ItemSelectable,ListDataListener,ActionListener, Accessible {
83 /**
84 * @see #getUIClassID
85 * @see #readObject
86 */
87 private static final String uiClassID = "ComboBoxUI";
88
89 /**
90 * This protected field is implementation specific. Do not access directly
91 * or override. Use the accessor methods instead.
92 *
93 * @see #getModel
94 * @see #setModel
95 */
96 protected ComboBoxModel dataModel;
97 /**
98 * This protected field is implementation specific. Do not access directly
99 * or override. Use the accessor methods instead.
100 *
101 * @see #getRenderer
102 * @see #setRenderer
103 */
104 protected ListCellRenderer renderer;
105 /**
106 * This protected field is implementation specific. Do not access directly
107 * or override. Use the accessor methods instead.
108 *
109 * @see #getEditor
110 * @see #setEditor
111 */
112 protected ComboBoxEditor editor;
113 /**
114 * This protected field is implementation specific. Do not access directly
115 * or override. Use the accessor methods instead.
116 *
117 * @see #getMaximumRowCount
118 * @see #setMaximumRowCount
119 */
120 protected int maximumRowCount = 8;
121
122 /**
123 * This protected field is implementation specific. Do not access directly
124 * or override. Use the accessor methods instead.
125 *
126 * @see #isEditable
127 * @see #setEditable
128 */
129 protected boolean isEditable = false;
130 /**
131 * This protected field is implementation specific. Do not access directly
132 * or override. Use the accessor methods instead.
133 *
134 * @see #setKeySelectionManager
135 * @see #getKeySelectionManager
136 */
137 protected KeySelectionManager keySelectionManager = null;
138 /**
139 * This protected field is implementation specific. Do not access directly
140 * or override. Use the accessor methods instead.
141 *
142 * @see #setActionCommand
143 * @see #getActionCommand
144 */
145 protected String actionCommand = "comboBoxChanged";
146 /**
147 * This protected field is implementation specific. Do not access directly
148 * or override. Use the accessor methods instead.
149 *
150 * @see #setLightWeightPopupEnabled
151 * @see #isLightWeightPopupEnabled
152 */
153 protected boolean lightWeightPopupEnabled = JPopupMenu.getDefaultLightWeightPopupEnabled();
154
155 /**
156 * This protected field is implementation specific. Do not access directly
157 * or override.
158 */
159 protected Object selectedItemReminder = null;
160
161 private Object prototypeDisplayValue;
162
163 // Flag to ensure that infinite loops do not occur with ActionEvents.
164 private boolean firingActionEvent = false;
165
166 // Flag to ensure the we don't get multiple ActionEvents on item selection.
167 private boolean selectingItem = false;
168
169 /**
170 * Creates a <code>JComboBox</code> that takes its items from an
171 * existing <code>ComboBoxModel</code>. Since the
172 * <code>ComboBoxModel</code> is provided, a combo box created using
173 * this constructor does not create a default combo box model and
174 * may impact how the insert, remove and add methods behave.
175 *
176 * @param aModel the <code>ComboBoxModel</code> that provides the
177 * displayed list of items
178 * @see DefaultComboBoxModel
179 */
180 public JComboBox(ComboBoxModel aModel) {
181 super();
182 setModel(aModel);
183 init();
184 }
185
186 /**
187 * Creates a <code>JComboBox</code> that contains the elements
188 * in the specified array. By default the first item in the array
189 * (and therefore the data model) becomes selected.
190 *
191 * @param items an array of objects to insert into the combo box
192 * @see DefaultComboBoxModel
193 */
194 public JComboBox(final Object items[]) {
195 super();
196 setModel(new DefaultComboBoxModel(items));
197 init();
198 }
199
200 /**
201 * Creates a <code>JComboBox</code> that contains the elements
202 * in the specified Vector. By default the first item in the vector
203 * (and therefore the data model) becomes selected.
204 *
205 * @param items an array of vectors to insert into the combo box
206 * @see DefaultComboBoxModel
207 */
208 public JComboBox(Vector<?> items) {
209 super();
210 setModel(new DefaultComboBoxModel(items));
211 init();
212 }
213
214 /**
215 * Creates a <code>JComboBox</code> with a default data model.
216 * The default data model is an empty list of objects.
217 * Use <code>addItem</code> to add items. By default the first item
218 * in the data model becomes selected.
219 *
220 * @see DefaultComboBoxModel
221 */
222 public JComboBox() {
223 super();
224 setModel(new DefaultComboBoxModel());
225 init();
226 }
227
228 private void init() {
229 installAncestorListener();
230 setOpaque(true);
231 updateUI();
232 }
233
234 protected void installAncestorListener() {
235 addAncestorListener(new AncestorListener(){
236 public void ancestorAdded(AncestorEvent event){ hidePopup();}
237 public void ancestorRemoved(AncestorEvent event){ hidePopup();}
238 public void ancestorMoved(AncestorEvent event){
239 if (event.getSource() != JComboBox.this)
240 hidePopup();
241 }});
242 }
243
244 /**
245 * Sets the L&F object that renders this component.
246 *
247 * @param ui the <code>ComboBoxUI</code> L&F object
248 * @see UIDefaults#getUI
249 *
250 * @beaninfo
251 * bound: true
252 * hidden: true
253 * attribute: visualUpdate true
254 * description: The UI object that implements the Component's LookAndFeel.
255 */
256 public void setUI(ComboBoxUI ui) {
257 super.setUI(ui);
258 }
259
260 /**
261 * Resets the UI property to a value from the current look and feel.
262 *
263 * @see JComponent#updateUI
264 */
265 public void updateUI() {
266 setUI((ComboBoxUI)UIManager.getUI(this));
267
268 ListCellRenderer renderer = getRenderer();
269 if (renderer instanceof Component) {
270 SwingUtilities.updateComponentTreeUI((Component)renderer);
271 }
272 }
273
274
275 /**
276 * Returns the name of the L&F class that renders this component.
277 *
278 * @return the string "ComboBoxUI"
279 * @see JComponent#getUIClassID
280 * @see UIDefaults#getUI
281 */
282 public String getUIClassID() {
283 return uiClassID;
284 }
285
286
287 /**
288 * Returns the L&F object that renders this component.
289 *
290 * @return the ComboBoxUI object that renders this component
291 */
292 public ComboBoxUI getUI() {
293 return(ComboBoxUI)ui;
294 }
295
296 /**
297 * Sets the data model that the <code>JComboBox</code> uses to obtain
298 * the list of items.
299 *
300 * @param aModel the <code>ComboBoxModel</code> that provides the
301 * displayed list of items
302 *
303 * @beaninfo
304 * bound: true
305 * description: Model that the combo box uses to get data to display.
306 */
307 public void setModel(ComboBoxModel aModel) {
308 ComboBoxModel oldModel = dataModel;
309 if (oldModel != null) {
310 oldModel.removeListDataListener(this);
311 }
312 dataModel = aModel;
313 dataModel.addListDataListener(this);
314
315 // set the current selected item.
316 selectedItemReminder = dataModel.getSelectedItem();
317
318 firePropertyChange( "model", oldModel, dataModel);
319 }
320
321 /**
322 * Returns the data model currently used by the <code>JComboBox</code>.
323 *
324 * @return the <code>ComboBoxModel</code> that provides the displayed
325 * list of items
326 */
327 public ComboBoxModel getModel() {
328 return dataModel;
329 }
330
331 /*
332 * Properties
333 */
334
335 /**
336 * Sets the <code>lightWeightPopupEnabled</code> property, which
337 * provides a hint as to whether or not a lightweight
338 * <code>Component</code> should be used to contain the
339 * <code>JComboBox</code>, versus a heavyweight
340 * <code>Component</code> such as a <code>Panel</code>
341 * or a <code>Window</code>. The decision of lightweight
342 * versus heavyweight is ultimately up to the
343 * <code>JComboBox</code>. Lightweight windows are more
344 * efficient than heavyweight windows, but lightweight
345 * and heavyweight components do not mix well in a GUI.
346 * If your application mixes lightweight and heavyweight
347 * components, you should disable lightweight popups.
348 * The default value for the <code>lightWeightPopupEnabled</code>
349 * property is <code>true</code>, unless otherwise specified
350 * by the look and feel. Some look and feels always use
351 * heavyweight popups, no matter what the value of this property.
352 * <p>
353 * See the article <a href="http://java.sun.com/products/jfc/tsc/articles/mixing/index.html">Mixing Heavy and Light Components</a>
354 * on <a href="http://java.sun.com/products/jfc/tsc">
355 * <em>The Swing Connection</em></a>
356 * This method fires a property changed event.
357 *
358 * @param aFlag if <code>true</code>, lightweight popups are desired
359 *
360 * @beaninfo
361 * bound: true
362 * expert: true
363 * description: Set to <code>false</code> to require heavyweight popups.
364 */
365 public void setLightWeightPopupEnabled(boolean aFlag) {
366 boolean oldFlag = lightWeightPopupEnabled;
367 lightWeightPopupEnabled = aFlag;
368 firePropertyChange("lightWeightPopupEnabled", oldFlag, lightWeightPopupEnabled);
369 }
370
371 /**
372 * Gets the value of the <code>lightWeightPopupEnabled</code>
373 * property.
374 *
375 * @return the value of the <code>lightWeightPopupEnabled</code>
376 * property
377 * @see #setLightWeightPopupEnabled
378 */
379 public boolean isLightWeightPopupEnabled() {
380 return lightWeightPopupEnabled;
381 }
382
383 /**
384 * Determines whether the <code>JComboBox</code> field is editable.
385 * An editable <code>JComboBox</code> allows the user to type into the
386 * field or selected an item from the list to initialize the field,
387 * after which it can be edited. (The editing affects only the field,
388 * the list item remains intact.) A non editable <code>JComboBox</code>
389 * displays the selected item in the field,
390 * but the selection cannot be modified.
391 *
392 * @param aFlag a boolean value, where true indicates that the
393 * field is editable
394 *
395 * @beaninfo
396 * bound: true
397 * preferred: true
398 * description: If true, the user can type a new value in the combo box.
399 */
400 public void setEditable(boolean aFlag) {
401 boolean oldFlag = isEditable;
402 isEditable = aFlag;
403 firePropertyChange( "editable", oldFlag, isEditable );
404 }
405
406 /**
407 * Returns true if the <code>JComboBox</code> is editable.
408 * By default, a combo box is not editable.
409 *
410 * @return true if the <code>JComboBox</code> is editable, else false
411 */
412 public boolean isEditable() {
413 return isEditable;
414 }
415
416 /**
417 * Sets the maximum number of rows the <code>JComboBox</code> displays.
418 * If the number of objects in the model is greater than count,
419 * the combo box uses a scrollbar.
420 *
421 * @param count an integer specifying the maximum number of items to
422 * display in the list before using a scrollbar
423 * @beaninfo
424 * bound: true
425 * preferred: true
426 * description: The maximum number of rows the popup should have
427 */
428 public void setMaximumRowCount(int count) {
429 int oldCount = maximumRowCount;
430 maximumRowCount = count;
431 firePropertyChange( "maximumRowCount", oldCount, maximumRowCount );
432 }
433
434 /**
435 * Returns the maximum number of items the combo box can display
436 * without a scrollbar
437 *
438 * @return an integer specifying the maximum number of items that are
439 * displayed in the list before using a scrollbar
440 */
441 public int getMaximumRowCount() {
442 return maximumRowCount;
443 }
444
445 /**
446 * Sets the renderer that paints the list items and the item selected from the list in
447 * the JComboBox field. The renderer is used if the JComboBox is not
448 * editable. If it is editable, the editor is used to render and edit
449 * the selected item.
450 * <p>
451 * The default renderer displays a string or an icon.
452 * Other renderers can handle graphic images and composite items.
453 * <p>
454 * To display the selected item,
455 * <code>aRenderer.getListCellRendererComponent</code>
456 * is called, passing the list object and an index of -1.
457 *
458 * @param aRenderer the <code>ListCellRenderer</code> that
459 * displays the selected item
460 * @see #setEditor
461 * @beaninfo
462 * bound: true
463 * expert: true
464 * description: The renderer that paints the item selected in the list.
465 */
466 public void setRenderer(ListCellRenderer aRenderer) {
467 ListCellRenderer oldRenderer = renderer;
468 renderer = aRenderer;
469 firePropertyChange( "renderer", oldRenderer, renderer );
470 invalidate();
471 }
472
473 /**
474 * Returns the renderer used to display the selected item in the
475 * <code>JComboBox</code> field.
476 *
477 * @return the <code>ListCellRenderer</code> that displays
478 * the selected item.
479 */
480 public ListCellRenderer getRenderer() {
481 return renderer;
482 }
483
484 /**
485 * Sets the editor used to paint and edit the selected item in the
486 * <code>JComboBox</code> field. The editor is used only if the
487 * receiving <code>JComboBox</code> is editable. If not editable,
488 * the combo box uses the renderer to paint the selected item.
489 *
490 * @param anEditor the <code>ComboBoxEditor</code> that
491 * displays the selected item
492 * @see #setRenderer
493 * @beaninfo
494 * bound: true
495 * expert: true
496 * description: The editor that combo box uses to edit the current value
497 */
498 public void setEditor(ComboBoxEditor anEditor) {
499 ComboBoxEditor oldEditor = editor;
500
501 if ( editor != null ) {
502 editor.removeActionListener(this);
503 }
504 editor = anEditor;
505 if ( editor != null ) {
506 editor.addActionListener(this);
507 }
508 firePropertyChange( "editor", oldEditor, editor );
509 }
510
511 /**
512 * Returns the editor used to paint and edit the selected item in the
513 * <code>JComboBox</code> field.
514 *
515 * @return the <code>ComboBoxEditor</code> that displays the selected item
516 */
517 public ComboBoxEditor getEditor() {
518 return editor;
519 }
520
521 //
522 // Selection
523 //
524
525 /**
526 * Sets the selected item in the combo box display area to the object in
527 * the argument.
528 * If <code>anObject</code> is in the list, the display area shows
529 * <code>anObject</code> selected.
530 * <p>
531 * If <code>anObject</code> is <i>not</i> in the list and the combo box is
532 * uneditable, it will not change the current selection. For editable
533 * combo boxes, the selection will change to <code>anObject</code>.
534 * <p>
535 * If this constitutes a change in the selected item,
536 * <code>ItemListener</code>s added to the combo box will be notified with
537 * one or two <code>ItemEvent</code>s.
538 * If there is a current selected item, an <code>ItemEvent</code> will be
539 * fired and the state change will be <code>ItemEvent.DESELECTED</code>.
540 * If <code>anObject</code> is in the list and is not currently selected
541 * then an <code>ItemEvent</code> will be fired and the state change will
542 * be <code>ItemEvent.SELECTED</code>.
543 * <p>
544 * <code>ActionListener</code>s added to the combo box will be notified
545 * with an <code>ActionEvent</code> when this method is called.
546 *
547 * @param anObject the list object to select; use <code>null</code> to
548 clear the selection
549 * @beaninfo
550 * preferred: true
551 * description: Sets the selected item in the JComboBox.
552 */
553 public void setSelectedItem(Object anObject) {
554 Object oldSelection = selectedItemReminder;
555 Object objectToSelect = anObject;
556 if (oldSelection == null || !oldSelection.equals(anObject)) {
557
558 if (anObject != null && !isEditable()) {
559 // For non editable combo boxes, an invalid selection
560 // will be rejected.
561 boolean found = false;
562 for (int i = 0; i < dataModel.getSize(); i++) {
563 Object element = dataModel.getElementAt(i);
564 if (anObject.equals(element)) {
565 found = true;
566 objectToSelect = element;
567 break;
568 }
569 }
570 if (!found) {
571 return;
572 }
573 }
574
575 // Must toggle the state of this flag since this method
576 // call may result in ListDataEvents being fired.
577 selectingItem = true;
578 dataModel.setSelectedItem(objectToSelect);
579 selectingItem = false;
580
581 if (selectedItemReminder != dataModel.getSelectedItem()) {
582 // in case a users implementation of ComboBoxModel
583 // doesn't fire a ListDataEvent when the selection
584 // changes.
585 selectedItemChanged();
586 }
587 }
588 fireActionEvent();
589 }
590
591 /**
592 * Returns the current selected item.
593 * <p>
594 * If the combo box is editable, then this value may not have been added
595 * to the combo box with <code>addItem</code>, <code>insertItemAt</code>
596 * or the data constructors.
597 *
598 * @return the current selected Object
599 * @see #setSelectedItem
600 */
601 public Object getSelectedItem() {
602 return dataModel.getSelectedItem();
603 }
604
605 /**
606 * Selects the item at index <code>anIndex</code>.
607 *
608 * @param anIndex an integer specifying the list item to select,
609 * where 0 specifies the first item in the list and -1 indicates no selection
610 * @exception IllegalArgumentException if <code>anIndex</code> < -1 or
611 * <code>anIndex</code> is greater than or equal to size
612 * @beaninfo
613 * preferred: true
614 * description: The item at index is selected.
615 */
616 public void setSelectedIndex(int anIndex) {
617 int size = dataModel.getSize();
618
619 if ( anIndex == -1 ) {
620 setSelectedItem( null );
621 } else if ( anIndex < -1 || anIndex >= size ) {
622 throw new IllegalArgumentException("setSelectedIndex: " + anIndex + " out of bounds");
623 } else {
624 setSelectedItem(dataModel.getElementAt(anIndex));
625 }
626 }
627
628 /**
629 * Returns the first item in the list that matches the given item.
630 * The result is not always defined if the <code>JComboBox</code>
631 * allows selected items that are not in the list.
632 * Returns -1 if there is no selected item or if the user specified
633 * an item which is not in the list.
634
635 * @return an integer specifying the currently selected list item,
636 * where 0 specifies
637 * the first item in the list;
638 * or -1 if no item is selected or if
639 * the currently selected item is not in the list
640 */
641 @Transient
642 public int getSelectedIndex() {
643 Object sObject = dataModel.getSelectedItem();
644 int i,c;
645 Object obj;
646
647 for ( i=0,c=dataModel.getSize();i<c;i++ ) {
648 obj = dataModel.getElementAt(i);
649 if ( obj != null && obj.equals(sObject) )
650 return i;
651 }
652 return -1;
653 }
654
655 /**
656 * Returns the "prototypical display" value - an Object used
657 * for the calculation of the display height and width.
658 *
659 * @return the value of the <code>prototypeDisplayValue</code> property
660 * @see #setPrototypeDisplayValue
661 * @since 1.4
662 */
663 public Object getPrototypeDisplayValue() {
664 return prototypeDisplayValue;
665 }
666
667 /**
668 * Sets the prototype display value used to calculate the size of the display
669 * for the UI portion.
670 * <p>
671 * If a prototype display value is specified, the preferred size of
672 * the combo box is calculated by configuring the renderer with the
673 * prototype display value and obtaining its preferred size. Specifying
674 * the preferred display value is often useful when the combo box will be
675 * displaying large amounts of data. If no prototype display value has
676 * been specified, the renderer must be configured for each value from
677 * the model and its preferred size obtained, which can be
678 * relatively expensive.
679 *
680 * @param prototypeDisplayValue
681 * @see #getPrototypeDisplayValue
682 * @since 1.4
683 * @beaninfo
684 * bound: true
685 * attribute: visualUpdate true
686 * description: The display prototype value, used to compute display width and height.
687 */
688 public void setPrototypeDisplayValue(Object prototypeDisplayValue) {
689 Object oldValue = this.prototypeDisplayValue;
690 this.prototypeDisplayValue = prototypeDisplayValue;
691 firePropertyChange("prototypeDisplayValue", oldValue, prototypeDisplayValue);
692 }
693
694 /**
695 * Adds an item to the item list.
696 * This method works only if the <code>JComboBox</code> uses a
697 * mutable data model.
698 * <p>
699 * <strong>Warning:</strong>
700 * Focus and keyboard navigation problems may arise if you add duplicate
701 * String objects. A workaround is to add new objects instead of String
702 * objects and make sure that the toString() method is defined.
703 * For example:
704 * <pre>
705 * comboBox.addItem(makeObj("Item 1"));
706 * comboBox.addItem(makeObj("Item 1"));
707 * ...
708 * private Object makeObj(final String item) {
709 * return new Object() { public String toString() { return item; } };
710 * }
711 * </pre>
712 *
713 * @param anObject the Object to add to the list
714 * @see MutableComboBoxModel
715 */
716 public void addItem(Object anObject) {
717 checkMutableComboBoxModel();
718 ((MutableComboBoxModel)dataModel).addElement(anObject);
719 }
720
721 /**
722 * Inserts an item into the item list at a given index.
723 * This method works only if the <code>JComboBox</code> uses a
724 * mutable data model.
725 *
726 * @param anObject the <code>Object</code> to add to the list
727 * @param index an integer specifying the position at which
728 * to add the item
729 * @see MutableComboBoxModel
730 */
731 public void insertItemAt(Object anObject, int index) {
732 checkMutableComboBoxModel();
733 ((MutableComboBoxModel)dataModel).insertElementAt(anObject,index);
734 }
735
736 /**
737 * Removes an item from the item list.
738 * This method works only if the <code>JComboBox</code> uses a
739 * mutable data model.
740 *
741 * @param anObject the object to remove from the item list
742 * @see MutableComboBoxModel
743 */
744 public void removeItem(Object anObject) {
745 checkMutableComboBoxModel();
746 ((MutableComboBoxModel)dataModel).removeElement(anObject);
747 }
748
749 /**
750 * Removes the item at <code>anIndex</code>
751 * This method works only if the <code>JComboBox</code> uses a
752 * mutable data model.
753 *
754 * @param anIndex an int specifying the index of the item to remove,
755 * where 0
756 * indicates the first item in the list
757 * @see MutableComboBoxModel
758 */
759 public void removeItemAt(int anIndex) {
760 checkMutableComboBoxModel();
761 ((MutableComboBoxModel)dataModel).removeElementAt( anIndex );
762 }
763
764 /**
765 * Removes all items from the item list.
766 */
767 public void removeAllItems() {
768 checkMutableComboBoxModel();
769 MutableComboBoxModel model = (MutableComboBoxModel)dataModel;
770 int size = model.getSize();
771
772 if ( model instanceof DefaultComboBoxModel ) {
773 ((DefaultComboBoxModel)model).removeAllElements();
774 }
775 else {
776 for ( int i = 0; i < size; ++i ) {
777 Object element = model.getElementAt( 0 );
778 model.removeElement( element );
779 }
780 }
781 selectedItemReminder = null;
782 if (isEditable()) {
783 editor.setItem(null);
784 }
785 }
786
787 /**
788 * Checks that the <code>dataModel</code> is an instance of
789 * <code>MutableComboBoxModel</code>. If not, it throws an exception.
790 * @exception RuntimeException if <code>dataModel</code> is not an
791 * instance of <code>MutableComboBoxModel</code>.
792 */
793 void checkMutableComboBoxModel() {
794 if ( !(dataModel instanceof MutableComboBoxModel) )
795 throw new RuntimeException("Cannot use this method with a non-Mutable data model.");
796 }
797
798 /**
799 * Causes the combo box to display its popup window.
800 * @see #setPopupVisible
801 */
802 public void showPopup() {
803 setPopupVisible(true);
804 }
805
806 /**
807 * Causes the combo box to close its popup window.
808 * @see #setPopupVisible
809 */
810 public void hidePopup() {
811 setPopupVisible(false);
812 }
813
814 /**
815 * Sets the visibility of the popup.
816 */
817 public void setPopupVisible(boolean v) {
818 getUI().setPopupVisible(this, v);
819 }
820
821 /**
822 * Determines the visibility of the popup.
823 *
824 * @return true if the popup is visible, otherwise returns false
825 */
826 public boolean isPopupVisible() {
827 return getUI().isPopupVisible(this);
828 }
829
830 /** Selection **/
831
832 /**
833 * Adds an <code>ItemListener</code>.
834 * <p>
835 * <code>aListener</code> will receive one or two <code>ItemEvent</code>s when
836 * the selected item changes.
837 *
838 * @param aListener the <code>ItemListener</code> that is to be notified
839 * @see #setSelectedItem
840 */
841 public void addItemListener(ItemListener aListener) {
842 listenerList.add(ItemListener.class,aListener);
843 }
844
845 /** Removes an <code>ItemListener</code>.
846 *
847 * @param aListener the <code>ItemListener</code> to remove
848 */
849 public void removeItemListener(ItemListener aListener) {
850 listenerList.remove(ItemListener.class,aListener);
851 }
852
853 /**
854 * Returns an array of all the <code>ItemListener</code>s added
855 * to this JComboBox with addItemListener().
856 *
857 * @return all of the <code>ItemListener</code>s added or an empty
858 * array if no listeners have been added
859 * @since 1.4
860 */
861 public ItemListener[] getItemListeners() {
862 return (ItemListener[])listenerList.getListeners(ItemListener.class);
863 }
864
865 /**
866 * Adds an <code>ActionListener</code>.
867 * <p>
868 * The <code>ActionListener</code> will receive an <code>ActionEvent</code>
869 * when a selection has been made. If the combo box is editable, then
870 * an <code>ActionEvent</code> will be fired when editing has stopped.
871 *
872 * @param l the <code>ActionListener</code> that is to be notified
873 * @see #setSelectedItem
874 */
875 public void addActionListener(ActionListener l) {
876 listenerList.add(ActionListener.class,l);
877 }
878
879 /** Removes an <code>ActionListener</code>.
880 *
881 * @param l the <code>ActionListener</code> to remove
882 */
883 public void removeActionListener(ActionListener l) {
884 if ((l != null) && (getAction() == l)) {
885 setAction(null);
886 } else {
887 listenerList.remove(ActionListener.class, l);
888 }
889 }
890
891 /**
892 * Returns an array of all the <code>ActionListener</code>s added
893 * to this JComboBox with addActionListener().
894 *
895 * @return all of the <code>ActionListener</code>s added or an empty
896 * array if no listeners have been added
897 * @since 1.4
898 */
899 public ActionListener[] getActionListeners() {
900 return (ActionListener[])listenerList.getListeners(
901 ActionListener.class);
902 }
903
904 /**
905 * Adds a <code>PopupMenu</code> listener which will listen to notification
906 * messages from the popup portion of the combo box.
907 * <p>
908 * For all standard look and feels shipped with Java, the popup list
909 * portion of combo box is implemented as a <code>JPopupMenu</code>.
910 * A custom look and feel may not implement it this way and will
911 * therefore not receive the notification.
912 *
913 * @param l the <code>PopupMenuListener</code> to add
914 * @since 1.4
915 */
916 public void addPopupMenuListener(PopupMenuListener l) {
917 listenerList.add(PopupMenuListener.class,l);
918 }
919
920 /**
921 * Removes a <code>PopupMenuListener</code>.
922 *
923 * @param l the <code>PopupMenuListener</code> to remove
924 * @see #addPopupMenuListener
925 * @since 1.4
926 */
927 public void removePopupMenuListener(PopupMenuListener l) {
928 listenerList.remove(PopupMenuListener.class,l);
929 }
930
931 /**
932 * Returns an array of all the <code>PopupMenuListener</code>s added
933 * to this JComboBox with addPopupMenuListener().
934 *
935 * @return all of the <code>PopupMenuListener</code>s added or an empty
936 * array if no listeners have been added
937 * @since 1.4
938 */
939 public PopupMenuListener[] getPopupMenuListeners() {
940 return (PopupMenuListener[])listenerList.getListeners(
941 PopupMenuListener.class);
942 }
943
944 /**
945 * Notifies <code>PopupMenuListener</code>s that the popup portion of the
946 * combo box will become visible.
947 * <p>
948 * This method is public but should not be called by anything other than
949 * the UI delegate.
950 * @see #addPopupMenuListener
951 * @since 1.4
952 */
953 public void firePopupMenuWillBecomeVisible() {
954 Object[] listeners = listenerList.getListenerList();
955 PopupMenuEvent e=null;
956 for (int i = listeners.length-2; i>=0; i-=2) {
957 if (listeners[i]==PopupMenuListener.class) {
958 if (e == null)
959 e = new PopupMenuEvent(this);
960 ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeVisible(e);
961 }
962 }
963 }
964
965 /**
966 * Notifies <code>PopupMenuListener</code>s that the popup portion of the
967 * combo box has become invisible.
968 * <p>
969 * This method is public but should not be called by anything other than
970 * the UI delegate.
971 * @see #addPopupMenuListener
972 * @since 1.4
973 */
974 public void firePopupMenuWillBecomeInvisible() {
975 Object[] listeners = listenerList.getListenerList();
976 PopupMenuEvent e=null;
977 for (int i = listeners.length-2; i>=0; i-=2) {
978 if (listeners[i]==PopupMenuListener.class) {
979 if (e == null)
980 e = new PopupMenuEvent(this);
981 ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeInvisible(e);
982 }
983 }
984 }
985
986 /**
987 * Notifies <code>PopupMenuListener</code>s that the popup portion of the
988 * combo box has been canceled.
989 * <p>
990 * This method is public but should not be called by anything other than
991 * the UI delegate.
992 * @see #addPopupMenuListener
993 * @since 1.4
994 */
995 public void firePopupMenuCanceled() {
996 Object[] listeners = listenerList.getListenerList();
997 PopupMenuEvent e=null;
998 for (int i = listeners.length-2; i>=0; i-=2) {
999 if (listeners[i]==PopupMenuListener.class) {
1000 if (e == null)
1001 e = new PopupMenuEvent(this);
1002 ((PopupMenuListener)listeners[i+1]).popupMenuCanceled(e);
1003 }
1004 }
1005 }
1006
1007 /**
1008 * Sets the action command that should be included in the event
1009 * sent to action listeners.
1010 *
1011 * @param aCommand a string containing the "command" that is sent
1012 * to action listeners; the same listener can then
1013 * do different things depending on the command it
1014 * receives
1015 */
1016 public void setActionCommand(String aCommand) {
1017 actionCommand = aCommand;
1018 }
1019
1020 /**
1021 * Returns the action command that is included in the event sent to
1022 * action listeners.
1023 *
1024 * @return the string containing the "command" that is sent
1025 * to action listeners.
1026 */
1027 public String getActionCommand() {
1028 return actionCommand;
1029 }
1030
1031 private Action action;
1032 private PropertyChangeListener actionPropertyChangeListener;
1033
1034 /**
1035 * Sets the <code>Action</code> for the <code>ActionEvent</code> source.
1036 * The new <code>Action</code> replaces any previously set
1037 * <code>Action</code> but does not affect <code>ActionListeners</code>
1038 * independently added with <code>addActionListener</code>.
1039 * If the <code>Action</code> is already a registered
1040 * <code>ActionListener</code> for the <code>ActionEvent</code> source,
1041 * it is not re-registered.
1042 * <p>
1043 * Setting the <code>Action</code> results in immediately changing
1044 * all the properties described in <a href="Action.html#buttonActions">
1045 * Swing Components Supporting <code>Action</code></a>.
1046 * Subsequently, the combobox's properties are automatically updated
1047 * as the <code>Action</code>'s properties change.
1048 * <p>
1049 * This method uses three other methods to set
1050 * and help track the <code>Action</code>'s property values.
1051 * It uses the <code>configurePropertiesFromAction</code> method
1052 * to immediately change the combobox's properties.
1053 * To track changes in the <code>Action</code>'s property values,
1054 * this method registers the <code>PropertyChangeListener</code>
1055 * returned by <code>createActionPropertyChangeListener</code>. The
1056 * default {@code PropertyChangeListener} invokes the
1057 * {@code actionPropertyChanged} method when a property in the
1058 * {@code Action} changes.
1059 *
1060 * @param a the <code>Action</code> for the <code>JComboBox</code>,
1061 * or <code>null</code>.
1062 * @since 1.3
1063 * @see Action
1064 * @see #getAction
1065 * @see #configurePropertiesFromAction
1066 * @see #createActionPropertyChangeListener
1067 * @see #actionPropertyChanged
1068 * @beaninfo
1069 * bound: true
1070 * attribute: visualUpdate true
1071 * description: the Action instance connected with this ActionEvent source
1072 */
1073 public void setAction(Action a) {
1074 Action oldValue = getAction();
1075 if (action==null || !action.equals(a)) {
1076 action = a;
1077 if (oldValue!=null) {
1078 removeActionListener(oldValue);
1079 oldValue.removePropertyChangeListener(actionPropertyChangeListener);
1080 actionPropertyChangeListener = null;
1081 }
1082 configurePropertiesFromAction(action);
1083 if (action!=null) {
1084 // Don't add if it is already a listener
1085 if (!isListener(ActionListener.class, action)) {
1086 addActionListener(action);
1087 }
1088 // Reverse linkage:
1089 actionPropertyChangeListener = createActionPropertyChangeListener(action);
1090 action.addPropertyChangeListener(actionPropertyChangeListener);
1091 }
1092 firePropertyChange("action", oldValue, action);
1093 }
1094 }
1095
1096 private boolean isListener(Class c, ActionListener a) {
1097 boolean isListener = false;
1098 Object[] listeners = listenerList.getListenerList();
1099 for (int i = listeners.length-2; i>=0; i-=2) {
1100 if (listeners[i]==c && listeners[i+1]==a) {
1101 isListener=true;
1102 }
1103 }
1104 return isListener;
1105 }
1106
1107 /**
1108 * Returns the currently set <code>Action</code> for this
1109 * <code>ActionEvent</code> source, or <code>null</code> if no
1110 * <code>Action</code> is set.
1111 *
1112 * @return the <code>Action</code> for this <code>ActionEvent</code>
1113 * source; or <code>null</code>
1114 * @since 1.3
1115 * @see Action
1116 * @see #setAction
1117 */
1118 public Action getAction() {
1119 return action;
1120 }
1121
1122 /**
1123 * Sets the properties on this combobox to match those in the specified
1124 * <code>Action</code>. Refer to <a href="Action.html#buttonActions">
1125 * Swing Components Supporting <code>Action</code></a> for more
1126 * details as to which properties this sets.
1127 *
1128 * @param a the <code>Action</code> from which to get the properties,
1129 * or <code>null</code>
1130 * @since 1.3
1131 * @see Action
1132 * @see #setAction
1133 */
1134 protected void configurePropertiesFromAction(Action a) {
1135 AbstractAction.setEnabledFromAction(this, a);
1136 AbstractAction.setToolTipTextFromAction(this, a);
1137 setActionCommandFromAction(a);
1138 }
1139
1140 /**
1141 * Creates and returns a <code>PropertyChangeListener</code> that is
1142 * responsible for listening for changes from the specified
1143 * <code>Action</code> and updating the appropriate properties.
1144 * <p>
1145 * <b>Warning:</b> If you subclass this do not create an anonymous
1146 * inner class. If you do the lifetime of the combobox will be tied to
1147 * that of the <code>Action</code>.
1148 *
1149 * @param a the combobox's action
1150 * @since 1.3
1151 * @see Action
1152 * @see #setAction
1153 */
1154 protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
1155 return new ComboBoxActionPropertyChangeListener(this, a);
1156 }
1157
1158 /**
1159 * Updates the combobox's state in response to property changes in
1160 * associated action. This method is invoked from the
1161 * {@code PropertyChangeListener} returned from
1162 * {@code createActionPropertyChangeListener}. Subclasses do not normally
1163 * need to invoke this. Subclasses that support additional {@code Action}
1164 * properties should override this and
1165 * {@code configurePropertiesFromAction}.
1166 * <p>
1167 * Refer to the table at <a href="Action.html#buttonActions">
1168 * Swing Components Supporting <code>Action</code></a> for a list of
1169 * the properties this method sets.
1170 *
1171 * @param action the <code>Action</code> associated with this combobox
1172 * @param propertyName the name of the property that changed
1173 * @since 1.6
1174 * @see Action
1175 * @see #configurePropertiesFromAction
1176 */
1177 protected void actionPropertyChanged(Action action, String propertyName) {
1178 if (propertyName == Action.ACTION_COMMAND_KEY) {
1179 setActionCommandFromAction(action);
1180 } else if (propertyName == "enabled") {
1181 AbstractAction.setEnabledFromAction(this, action);
1182 } else if (Action.SHORT_DESCRIPTION == propertyName) {
1183 AbstractAction.setToolTipTextFromAction(this, action);
1184 }
1185 }
1186
1187 private void setActionCommandFromAction(Action a) {
1188 setActionCommand((a != null) ?
1189 (String)a.getValue(Action.ACTION_COMMAND_KEY) :
1190 null);
1191 }
1192
1193
1194 private static class ComboBoxActionPropertyChangeListener
1195 extends ActionPropertyChangeListener<JComboBox> {
1196 ComboBoxActionPropertyChangeListener(JComboBox b, Action a) {
1197 super(b, a);
1198 }
1199 protected void actionPropertyChanged(JComboBox cb,
1200 Action action,
1201 PropertyChangeEvent e) {
1202 if (AbstractAction.shouldReconfigure(e)) {
1203 cb.configurePropertiesFromAction(action);
1204 } else {
1205 cb.actionPropertyChanged(action, e.getPropertyName());
1206 }
1207 }
1208 }
1209
1210 /**
1211 * Notifies all listeners that have registered interest for
1212 * notification on this event type.
1213 * @param e the event of interest
1214 *
1215 * @see EventListenerList
1216 */
1217 protected void fireItemStateChanged(ItemEvent e) {
1218 // Guaranteed to return a non-null array
1219 Object[] listeners = listenerList.getListenerList();
1220 // Process the listeners last to first, notifying
1221 // those that are interested in this event
1222 for ( int i = listeners.length-2; i>=0; i-=2 ) {
1223 if ( listeners[i]==ItemListener.class ) {
1224 // Lazily create the event:
1225 // if (changeEvent == null)
1226 // changeEvent = new ChangeEvent(this);
1227 ((ItemListener)listeners[i+1]).itemStateChanged(e);
1228 }
1229 }
1230 }
1231
1232 /**
1233 * Notifies all listeners that have registered interest for
1234 * notification on this event type.
1235 *
1236 * @see EventListenerList
1237 */
1238 protected void fireActionEvent() {
1239 if (!firingActionEvent) {
1240 // Set flag to ensure that an infinite loop is not created
1241 firingActionEvent = true;
1242 ActionEvent e = null;
1243 // Guaranteed to return a non-null array
1244 Object[] listeners = listenerList.getListenerList();
1245 long mostRecentEventTime = EventQueue.getMostRecentEventTime();
1246 int modifiers = 0;
1247 AWTEvent currentEvent = EventQueue.getCurrentEvent();
1248 if (currentEvent instanceof InputEvent) {
1249 modifiers = ((InputEvent)currentEvent).getModifiers();
1250 } else if (currentEvent instanceof ActionEvent) {
1251 modifiers = ((ActionEvent)currentEvent).getModifiers();
1252 }
1253 // Process the listeners last to first, notifying
1254 // those that are interested in this event
1255 for ( int i = listeners.length-2; i>=0; i-=2 ) {
1256 if ( listeners[i]==ActionListener.class ) {
1257 // Lazily create the event:
1258 if ( e == null )
1259 e = new ActionEvent(this,ActionEvent.ACTION_PERFORMED,
1260 getActionCommand(),
1261 mostRecentEventTime, modifiers);
1262 ((ActionListener)listeners[i+1]).actionPerformed(e);
1263 }
1264 }
1265 firingActionEvent = false;
1266 }
1267 }
1268
1269 /**
1270 * This protected method is implementation specific. Do not access directly
1271 * or override.
1272 */
1273 protected void selectedItemChanged() {
1274 if (selectedItemReminder != null ) {
1275 fireItemStateChanged(new ItemEvent(this,ItemEvent.ITEM_STATE_CHANGED,
1276 selectedItemReminder,
1277 ItemEvent.DESELECTED));
1278 }
1279
1280 // set the new selected item.
1281 selectedItemReminder = dataModel.getSelectedItem();
1282
1283 if (selectedItemReminder != null ) {
1284 fireItemStateChanged(new ItemEvent(this,ItemEvent.ITEM_STATE_CHANGED,
1285 selectedItemReminder,
1286 ItemEvent.SELECTED));
1287 }
1288 }
1289
1290 /**
1291 * Returns an array containing the selected item.
1292 * This method is implemented for compatibility with
1293 * <code>ItemSelectable</code>.
1294 *
1295 * @return an array of <code>Objects</code> containing one
1296 * element -- the selected item
1297 */
1298 public Object[] getSelectedObjects() {
1299 Object selectedObject = getSelectedItem();
1300 if ( selectedObject == null )
1301 return new Object[0];
1302 else {
1303 Object result[] = new Object[1];
1304 result[0] = selectedObject;
1305 return result;
1306 }
1307 }
1308
1309 /**
1310 * This method is public as an implementation side effect.
1311 * do not call or override.
1312 */
1313 public void actionPerformed(ActionEvent e) {
1314 Object newItem = getEditor().getItem();
1315 setPopupVisible(false);
1316 getModel().setSelectedItem(newItem);
1317 String oldCommand = getActionCommand();
1318 setActionCommand("comboBoxEdited");
1319 fireActionEvent();
1320 setActionCommand(oldCommand);
1321 }
1322
1323 /**
1324 * This method is public as an implementation side effect.
1325 * do not call or override.
1326 */
1327 public void contentsChanged(ListDataEvent e) {
1328 Object oldSelection = selectedItemReminder;
1329 Object newSelection = dataModel.getSelectedItem();
1330 if (oldSelection == null || !oldSelection.equals(newSelection)) {
1331 selectedItemChanged();
1332 if (!selectingItem) {
1333 fireActionEvent();
1334 }
1335 }
1336 }
1337
1338 /**
1339 * This method is public as an implementation side effect.
1340 * do not call or override.
1341 */
1342 public void intervalAdded(ListDataEvent e) {
1343 if (selectedItemReminder != dataModel.getSelectedItem()) {
1344 selectedItemChanged();
1345 }
1346 }
1347
1348 /**
1349 * This method is public as an implementation side effect.
1350 * do not call or override.
1351 */
1352 public void intervalRemoved(ListDataEvent e) {
1353 contentsChanged(e);
1354 }
1355
1356 /**
1357 * Selects the list item that corresponds to the specified keyboard
1358 * character and returns true, if there is an item corresponding
1359 * to that character. Otherwise, returns false.
1360 *
1361 * @param keyChar a char, typically this is a keyboard key
1362 * typed by the user
1363 */
1364 public boolean selectWithKeyChar(char keyChar) {
1365 int index;
1366
1367 if ( keySelectionManager == null )
1368 keySelectionManager = createDefaultKeySelectionManager();
1369
1370 index = keySelectionManager.selectionForKey(keyChar,getModel());
1371 if ( index != -1 ) {
1372 setSelectedIndex(index);
1373 return true;
1374 }
1375 else
1376 return false;
1377 }
1378
1379 /**
1380 * Enables the combo box so that items can be selected. When the
1381 * combo box is disabled, items cannot be selected and values
1382 * cannot be typed into its field (if it is editable).
1383 *
1384 * @param b a boolean value, where true enables the component and
1385 * false disables it
1386 * @beaninfo
1387 * bound: true
1388 * preferred: true
1389 * description: Whether the combo box is enabled.
1390 */
1391 public void setEnabled(boolean b) {
1392 super.setEnabled(b);
1393 firePropertyChange( "enabled", !isEnabled(), isEnabled() );
1394 }
1395
1396 /**
1397 * Initializes the editor with the specified item.
1398 *
1399 * @param anEditor the <code>ComboBoxEditor</code> that displays
1400 * the list item in the
1401 * combo box field and allows it to be edited
1402 * @param anItem the object to display and edit in the field
1403 */
1404 public void configureEditor(ComboBoxEditor anEditor, Object anItem) {
1405 anEditor.setItem(anItem);
1406 }
1407
1408 /**
1409 * Handles <code>KeyEvent</code>s, looking for the Tab key.
1410 * If the Tab key is found, the popup window is closed.
1411 *
1412 * @param e the <code>KeyEvent</code> containing the keyboard
1413 * key that was pressed
1414 */
1415 public void processKeyEvent(KeyEvent e) {
1416 if ( e.getKeyCode() == KeyEvent.VK_TAB ) {
1417 hidePopup();
1418 }
1419 super.processKeyEvent(e);
1420 }
1421
1422 /**
1423 * Sets the object that translates a keyboard character into a list
1424 * selection. Typically, the first selection with a matching first
1425 * character becomes the selected item.
1426 *
1427 * @beaninfo
1428 * expert: true
1429 * description: The objects that changes the selection when a key is pressed.
1430 */
1431 public void setKeySelectionManager(KeySelectionManager aManager) {
1432 keySelectionManager = aManager;
1433 }
1434
1435 /**
1436 * Returns the list's key-selection manager.
1437 *
1438 * @return the <code>KeySelectionManager</code> currently in use
1439 */
1440 public KeySelectionManager getKeySelectionManager() {
1441 return keySelectionManager;
1442 }
1443
1444 /* Accessing the model */
1445 /**
1446 * Returns the number of items in the list.
1447 *
1448 * @return an integer equal to the number of items in the list
1449 */
1450 public int getItemCount() {
1451 return dataModel.getSize();
1452 }
1453
1454 /**
1455 * Returns the list item at the specified index. If <code>index</code>
1456 * is out of range (less than zero or greater than or equal to size)
1457 * it will return <code>null</code>.
1458 *
1459 * @param index an integer indicating the list position, where the first
1460 * item starts at zero
1461 * @return the <code>Object</code> at that list position; or
1462 * <code>null</code> if out of range
1463 */
1464 public Object getItemAt(int index) {
1465 return dataModel.getElementAt(index);
1466 }
1467
1468 /**
1469 * Returns an instance of the default key-selection manager.
1470 *
1471 * @return the <code>KeySelectionManager</code> currently used by the list
1472 * @see #setKeySelectionManager
1473 */
1474 protected KeySelectionManager createDefaultKeySelectionManager() {
1475 return new DefaultKeySelectionManager();
1476 }
1477
1478
1479 /**
1480 * The interface that defines a <code>KeySelectionManager</code>.
1481 * To qualify as a <code>KeySelectionManager</code>,
1482 * the class needs to implement the method
1483 * that identifies the list index given a character and the
1484 * combo box data model.
1485 */
1486 public interface KeySelectionManager {
1487 /** Given <code>aKey</code> and the model, returns the row
1488 * that should become selected. Return -1 if no match was
1489 * found.
1490 *
1491 * @param aKey a char value, usually indicating a keyboard key that
1492 * was pressed
1493 * @param aModel a ComboBoxModel -- the component's data model, containing
1494 * the list of selectable items
1495 * @return an int equal to the selected row, where 0 is the
1496 * first item and -1 is none.
1497 */
1498 int selectionForKey(char aKey,ComboBoxModel aModel);
1499 }
1500
1501 class DefaultKeySelectionManager implements KeySelectionManager, Serializable {
1502 public int selectionForKey(char aKey,ComboBoxModel aModel) {
1503 int i,c;
1504 int currentSelection = -1;
1505 Object selectedItem = aModel.getSelectedItem();
1506 String v;
1507 String pattern;
1508
1509 if ( selectedItem != null ) {
1510 for ( i=0,c=aModel.getSize();i<c;i++ ) {
1511 if ( selectedItem == aModel.getElementAt(i) ) {
1512 currentSelection = i;
1513 break;
1514 }
1515 }
1516 }
1517
1518 pattern = ("" + aKey).toLowerCase();
1519 aKey = pattern.charAt(0);
1520
1521 for ( i = ++currentSelection, c = aModel.getSize() ; i < c ; i++ ) {
1522 Object elem = aModel.getElementAt(i);
1523 if (elem != null && elem.toString() != null) {
1524 v = elem.toString().toLowerCase();
1525 if ( v.length() > 0 && v.charAt(0) == aKey )
1526 return i;
1527 }
1528 }
1529
1530 for ( i = 0 ; i < currentSelection ; i ++ ) {
1531 Object elem = aModel.getElementAt(i);
1532 if (elem != null && elem.toString() != null) {
1533 v = elem.toString().toLowerCase();
1534 if ( v.length() > 0 && v.charAt(0) == aKey )
1535 return i;
1536 }
1537 }
1538 return -1;
1539 }
1540 }
1541
1542
1543 /**
1544 * See <code>readObject</code> and <code>writeObject</code> in
1545 * <code>JComponent</code> for more
1546 * information about serialization in Swing.
1547 */
1548 private void writeObject(ObjectOutputStream s) throws IOException {
1549 s.defaultWriteObject();
1550 if (getUIClassID().equals(uiClassID)) {
1551 byte count = JComponent.getWriteObjCounter(this);
1552 JComponent.setWriteObjCounter(this, --count);
1553 if (count == 0 && ui != null) {
1554 ui.installUI(this);
1555 }
1556 }
1557 }
1558
1559
1560 /**
1561 * Returns a string representation of this <code>JComboBox</code>.
1562 * This method is intended to be used only for debugging purposes,
1563 * and the content and format of the returned string may vary between
1564 * implementations. The returned string may be empty but may not
1565 * be <code>null</code>.
1566 *
1567 * @return a string representation of this <code>JComboBox</code>
1568 */
1569 protected String paramString() {
1570 String selectedItemReminderString = (selectedItemReminder != null ?
1571 selectedItemReminder.toString() :
1572 "");
1573 String isEditableString = (isEditable ? "true" : "false");
1574 String lightWeightPopupEnabledString = (lightWeightPopupEnabled ?
1575 "true" : "false");
1576
1577 return super.paramString() +
1578 ",isEditable=" + isEditableString +
1579 ",lightWeightPopupEnabled=" + lightWeightPopupEnabledString +
1580 ",maximumRowCount=" + maximumRowCount +
1581 ",selectedItemReminder=" + selectedItemReminderString;
1582 }
1583
1584
1585 ///////////////////
1586 // Accessibility support
1587 ///////////////////
1588
1589 /**
1590 * Gets the AccessibleContext associated with this JComboBox.
1591 * For combo boxes, the AccessibleContext takes the form of an
1592 * AccessibleJComboBox.
1593 * A new AccessibleJComboBox instance is created if necessary.
1594 *
1595 * @return an AccessibleJComboBox that serves as the
1596 * AccessibleContext of this JComboBox
1597 */
1598 public AccessibleContext getAccessibleContext() {
1599 if ( accessibleContext == null ) {
1600 accessibleContext = new AccessibleJComboBox();
1601 }
1602 return accessibleContext;
1603 }
1604
1605 /**
1606 * This class implements accessibility support for the
1607 * <code>JComboBox</code> class. It provides an implementation of the
1608 * Java Accessibility API appropriate to Combo Box user-interface elements.
1609 * <p>
1610 * <strong>Warning:</strong>
1611 * Serialized objects of this class will not be compatible with
1612 * future Swing releases. The current serialization support is
1613 * appropriate for short term storage or RMI between applications running
1614 * the same version of Swing. As of 1.4, support for long term storage
1615 * of all JavaBeans<sup><font size="-2">TM</font></sup>
1616 * has been added to the <code>java.beans</code> package.
1617 * Please see {@link java.beans.XMLEncoder}.
1618 */
1619 protected class AccessibleJComboBox extends AccessibleJComponent
1620 implements AccessibleAction, AccessibleSelection {
1621
1622
1623 private JList popupList; // combo box popup list
1624 private Accessible previousSelectedAccessible = null;
1625
1626 /**
1627 * Returns an AccessibleJComboBox instance
1628 * @since 1.4
1629 */
1630 public AccessibleJComboBox() {
1631 // set the combo box editor's accessible name and description
1632 JComboBox.this.addPropertyChangeListener(new AccessibleJComboBoxPropertyChangeListener());
1633 setEditorNameAndDescription();
1634
1635 // Get the popup list
1636 Accessible a = getUI().getAccessibleChild(JComboBox.this, 0);
1637 if (a instanceof javax.swing.plaf.basic.ComboPopup) {
1638 // Listen for changes to the popup menu selection.
1639 popupList = ((javax.swing.plaf.basic.ComboPopup)a).getList();
1640 popupList.addListSelectionListener(
1641 new AccessibleJComboBoxListSelectionListener());
1642 }
1643 // Listen for popup menu show/hide events
1644 JComboBox.this.addPopupMenuListener(
1645 new AccessibleJComboBoxPopupMenuListener());
1646 }
1647
1648 /*
1649 * JComboBox PropertyChangeListener
1650 */
1651 private class AccessibleJComboBoxPropertyChangeListener
1652 implements PropertyChangeListener {
1653
1654 public void propertyChange(PropertyChangeEvent e) {
1655 if (e.getPropertyName() == "editor") {
1656 // set the combo box editor's accessible name
1657 // and description
1658 setEditorNameAndDescription();
1659 }
1660 }
1661 }
1662
1663 /*
1664 * Sets the combo box editor's accessible name and descripton
1665 */
1666 private void setEditorNameAndDescription() {
1667 ComboBoxEditor editor = JComboBox.this.getEditor();
1668 if (editor != null) {
1669 Component comp = editor.getEditorComponent();
1670 if (comp instanceof Accessible) {
1671 AccessibleContext ac = ((Accessible)comp).getAccessibleContext();
1672 if (ac != null) { // may be null
1673 ac.setAccessibleName(getAccessibleName());
1674 ac.setAccessibleDescription(getAccessibleDescription());
1675 }
1676 }
1677 }
1678 }
1679
1680 /*
1681 * Listener for combo box popup menu
1682 * TIGER - 4669379 4894434
1683 */
1684 private class AccessibleJComboBoxPopupMenuListener
1685 implements PopupMenuListener {
1686
1687 /**
1688 * This method is called before the popup menu becomes visible
1689 */
1690 public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
1691 // save the initial selection
1692 if (popupList == null) {
1693 return;
1694 }
1695 int selectedIndex = popupList.getSelectedIndex();
1696 if (selectedIndex < 0) {
1697 return;
1698 }
1699 previousSelectedAccessible =
1700 popupList.getAccessibleContext().getAccessibleChild(selectedIndex);
1701 }
1702
1703 /**
1704 * This method is called before the popup menu becomes invisible
1705 * Note that a JPopupMenu can become invisible any time
1706 */
1707 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
1708 // ignore
1709 }
1710
1711 /**
1712 * This method is called when the popup menu is canceled
1713 */
1714 public void popupMenuCanceled(PopupMenuEvent e) {
1715 // ignore
1716 }
1717 }
1718
1719 /*
1720 * Handles changes to the popup list selection.
1721 * TIGER - 4669379 4894434 4933143
1722 */
1723 private class AccessibleJComboBoxListSelectionListener
1724 implements ListSelectionListener {
1725
1726 public void valueChanged(ListSelectionEvent e) {
1727 if (popupList == null) {
1728 return;
1729 }
1730
1731 // Get the selected popup list item.
1732 int selectedIndex = popupList.getSelectedIndex();
1733 if (selectedIndex < 0) {
1734 return;
1735 }
1736 Accessible selectedAccessible =
1737 popupList.getAccessibleContext().getAccessibleChild(selectedIndex);
1738 if (selectedAccessible == null) {
1739 return;
1740 }
1741
1742 // Fire a FOCUSED lost PropertyChangeEvent for the
1743 // previously selected list item.
1744 PropertyChangeEvent pce = null;
1745
1746 if (previousSelectedAccessible != null) {
1747 pce = new PropertyChangeEvent(previousSelectedAccessible,
1748 AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
1749 AccessibleState.FOCUSED, null);
1750 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
1751 null, pce);
1752 }
1753 // Fire a FOCUSED gained PropertyChangeEvent for the
1754 // currently selected list item.
1755 pce = new PropertyChangeEvent(selectedAccessible,
1756 AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
1757 null, AccessibleState.FOCUSED);
1758 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
1759 null, pce);
1760
1761 // Fire the ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY event
1762 // for the combo box.
1763 firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
1764 previousSelectedAccessible, selectedAccessible);
1765
1766 // Save the previous selection.
1767 previousSelectedAccessible = selectedAccessible;
1768 }
1769 }
1770
1771
1772 /**
1773 * Returns the number of accessible children in the object. If all
1774 * of the children of this object implement Accessible, than this
1775 * method should return the number of children of this object.
1776 *
1777 * @return the number of accessible children in the object.
1778 */
1779 public int getAccessibleChildrenCount() {
1780 // Always delegate to the UI if it exists
1781 if (ui != null) {
1782 return ui.getAccessibleChildrenCount(JComboBox.this);
1783 } else {
1784 return super.getAccessibleChildrenCount();
1785 }
1786 }
1787
1788 /**
1789 * Returns the nth Accessible child of the object.
1790 * The child at index zero represents the popup.
1791 * If the combo box is editable, the child at index one
1792 * represents the editor.
1793 *
1794 * @param i zero-based index of child
1795 * @return the nth Accessible child of the object
1796 */
1797 public Accessible getAccessibleChild(int i) {
1798 // Always delegate to the UI if it exists
1799 if (ui != null) {
1800 return ui.getAccessibleChild(JComboBox.this, i);
1801 } else {
1802 return super.getAccessibleChild(i);
1803 }
1804 }
1805
1806 /**
1807 * Get the role of this object.
1808 *
1809 * @return an instance of AccessibleRole describing the role of the
1810 * object
1811 * @see AccessibleRole
1812 */
1813 public AccessibleRole getAccessibleRole() {
1814 return AccessibleRole.COMBO_BOX;
1815 }
1816
1817 /**
1818 * Gets the state set of this object. The AccessibleStateSet of
1819 * an object is composed of a set of unique AccessibleStates.
1820 * A change in the AccessibleStateSet of an object will cause a
1821 * PropertyChangeEvent to be fired for the ACCESSIBLE_STATE_PROPERTY
1822 * property.
1823 *
1824 * @return an instance of AccessibleStateSet containing the
1825 * current state set of the object
1826 * @see AccessibleStateSet
1827 * @see AccessibleState
1828 * @see #addPropertyChangeListener
1829 *
1830 */
1831 public AccessibleStateSet getAccessibleStateSet() {
1832 // TIGER - 4489748
1833 AccessibleStateSet ass = super.getAccessibleStateSet();
1834 if (ass == null) {
1835 ass = new AccessibleStateSet();
1836 }
1837 if (JComboBox.this.isPopupVisible()) {
1838 ass.add(AccessibleState.EXPANDED);
1839 } else {
1840 ass.add(AccessibleState.COLLAPSED);
1841 }
1842 return ass;
1843 }
1844
1845 /**
1846 * Get the AccessibleAction associated with this object. In the
1847 * implementation of the Java Accessibility API for this class,
1848 * return this object, which is responsible for implementing the
1849 * AccessibleAction interface on behalf of itself.
1850 *
1851 * @return this object
1852 */
1853 public AccessibleAction getAccessibleAction() {
1854 return this;
1855 }
1856
1857 /**
1858 * Return a description of the specified action of the object.
1859 *
1860 * @param i zero-based index of the actions
1861 */
1862 public String getAccessibleActionDescription(int i) {
1863 if (i == 0) {
1864 return UIManager.getString("ComboBox.togglePopupText");
1865 }
1866 else {
1867 return null;
1868 }
1869 }
1870
1871 /**
1872 * Returns the number of Actions available in this object. The
1873 * default behavior of a combo box is to have one action.
1874 *
1875 * @return 1, the number of Actions in this object
1876 */
1877 public int getAccessibleActionCount() {
1878 return 1;
1879 }
1880
1881 /**
1882 * Perform the specified Action on the object
1883 *
1884 * @param i zero-based index of actions
1885 * @return true if the the action was performed; else false.
1886 */
1887 public boolean doAccessibleAction(int i) {
1888 if (i == 0) {
1889 setPopupVisible(!isPopupVisible());
1890 return true;
1891 }
1892 else {
1893 return false;
1894 }
1895 }
1896
1897
1898 /**
1899 * Get the AccessibleSelection associated with this object. In the
1900 * implementation of the Java Accessibility API for this class,
1901 * return this object, which is responsible for implementing the
1902 * AccessibleSelection interface on behalf of itself.
1903 *
1904 * @return this object
1905 */
1906 public AccessibleSelection getAccessibleSelection() {
1907 return this;
1908 }
1909
1910 /**
1911 * Returns the number of Accessible children currently selected.
1912 * If no children are selected, the return value will be 0.
1913 *
1914 * @return the number of items currently selected.
1915 * @since 1.3
1916 */
1917 public int getAccessibleSelectionCount() {
1918 Object o = JComboBox.this.getSelectedItem();
1919 if (o != null) {
1920 return 1;
1921 } else {
1922 return 0;
1923 }
1924 }
1925
1926 /**
1927 * Returns an Accessible representing the specified selected child
1928 * in the popup. If there isn't a selection, or there are
1929 * fewer children selected than the integer passed in, the return
1930 * value will be null.
1931 * <p>Note that the index represents the i-th selected child, which
1932 * is different from the i-th child.
1933 *
1934 * @param i the zero-based index of selected children
1935 * @return the i-th selected child
1936 * @see #getAccessibleSelectionCount
1937 * @since 1.3
1938 */
1939 public Accessible getAccessibleSelection(int i) {
1940 // Get the popup
1941 Accessible a =
1942 JComboBox.this.getUI().getAccessibleChild(JComboBox.this, 0);
1943 if (a != null &&
1944 a instanceof javax.swing.plaf.basic.ComboPopup) {
1945
1946 // get the popup list
1947 JList list = ((javax.swing.plaf.basic.ComboPopup)a).getList();
1948
1949 // return the i-th selection in the popup list
1950 AccessibleContext ac = list.getAccessibleContext();
1951 if (ac != null) {
1952 AccessibleSelection as = ac.getAccessibleSelection();
1953 if (as != null) {
1954 return as.getAccessibleSelection(i);
1955 }
1956 }
1957 }
1958 return null;
1959 }
1960
1961 /**
1962 * Determines if the current child of this object is selected.
1963 *
1964 * @return true if the current child of this object is selected;
1965 * else false
1966 * @param i the zero-based index of the child in this Accessible
1967 * object.
1968 * @see AccessibleContext#getAccessibleChild
1969 * @since 1.3
1970 */
1971 public boolean isAccessibleChildSelected(int i) {
1972 return JComboBox.this.getSelectedIndex() == i;
1973 }
1974
1975 /**
1976 * Adds the specified Accessible child of the object to the object's
1977 * selection. If the object supports multiple selections,
1978 * the specified child is added to any existing selection, otherwise
1979 * it replaces any existing selection in the object. If the
1980 * specified child is already selected, this method has no effect.
1981 *
1982 * @param i the zero-based index of the child
1983 * @see AccessibleContext#getAccessibleChild
1984 * @since 1.3
1985 */
1986 public void addAccessibleSelection(int i) {
1987 // TIGER - 4856195
1988 clearAccessibleSelection();
1989 JComboBox.this.setSelectedIndex(i);
1990 }
1991
1992 /**
1993 * Removes the specified child of the object from the object's
1994 * selection. If the specified item isn't currently selected, this
1995 * method has no effect.
1996 *
1997 * @param i the zero-based index of the child
1998 * @see AccessibleContext#getAccessibleChild
1999 * @since 1.3
2000 */
2001 public void removeAccessibleSelection(int i) {
2002 if (JComboBox.this.getSelectedIndex() == i) {
2003 clearAccessibleSelection();
2004 }
2005 }
2006
2007 /**
2008 * Clears the selection in the object, so that no children in the
2009 * object are selected.
2010 * @since 1.3
2011 */
2012 public void clearAccessibleSelection() {
2013 JComboBox.this.setSelectedIndex(-1);
2014 }
2015
2016 /**
2017 * Causes every child of the object to be selected
2018 * if the object supports multiple selections.
2019 * @since 1.3
2020 */
2021 public void selectAllAccessibleSelection() {
2022 // do nothing since multiple selection is not supported
2023 }
2024
2025 // public Accessible getAccessibleAt(Point p) {
2026 // Accessible a = getAccessibleChild(1);
2027 // if ( a != null ) {
2028 // return a; // the editor
2029 // }
2030 // else {
2031 // return getAccessibleChild(0); // the list
2032 // }
2033 // }
2034 private EditorAccessibleContext editorAccessibleContext = null;
2035
2036 private class AccessibleEditor implements Accessible {
2037 public AccessibleContext getAccessibleContext() {
2038 if (editorAccessibleContext == null) {
2039 Component c = JComboBox.this.getEditor().getEditorComponent();
2040 if (c instanceof Accessible) {
2041 editorAccessibleContext =
2042 new EditorAccessibleContext((Accessible)c);
2043 }
2044 }
2045 return editorAccessibleContext;
2046 }
2047 }
2048
2049 /*
2050 * Wrapper class for the AccessibleContext implemented by the
2051 * combo box editor. Delegates all method calls except
2052 * getAccessibleIndexInParent to the editor. The
2053 * getAccessibleIndexInParent method returns the selected
2054 * index in the combo box.
2055 */
2056 private class EditorAccessibleContext extends AccessibleContext {
2057
2058 private AccessibleContext ac;
2059
2060 private EditorAccessibleContext() {
2061 }
2062
2063 /*
2064 * @param a the AccessibleContext implemented by the
2065 * combo box editor
2066 */
2067 EditorAccessibleContext(Accessible a) {
2068 this.ac = a.getAccessibleContext();
2069 }
2070
2071 /**
2072 * Gets the accessibleName property of this object. The accessibleName
2073 * property of an object is a localized String that designates the purpose
2074 * of the object. For example, the accessibleName property of a label
2075 * or button might be the text of the label or button itself. In the
2076 * case of an object that doesn't display its name, the accessibleName
2077 * should still be set. For example, in the case of a text field used
2078 * to enter the name of a city, the accessibleName for the en_US locale
2079 * could be 'city.'
2080 *
2081 * @return the localized name of the object; null if this
2082 * object does not have a name
2083 *
2084 * @see #setAccessibleName
2085 */
2086 public String getAccessibleName() {
2087 return ac.getAccessibleName();
2088 }
2089
2090 /**
2091 * Sets the localized accessible name of this object. Changing the
2092 * name will cause a PropertyChangeEvent to be fired for the
2093 * ACCESSIBLE_NAME_PROPERTY property.
2094 *
2095 * @param s the new localized name of the object.
2096 *
2097 * @see #getAccessibleName
2098 * @see #addPropertyChangeListener
2099 *
2100 * @beaninfo
2101 * preferred: true
2102 * description: Sets the accessible name for the component.
2103 */
2104 public void setAccessibleName(String s) {
2105 ac.setAccessibleName(s);
2106 }
2107
2108 /**
2109 * Gets the accessibleDescription property of this object. The
2110 * accessibleDescription property of this object is a short localized
2111 * phrase describing the purpose of the object. For example, in the
2112 * case of a 'Cancel' button, the accessibleDescription could be
2113 * 'Ignore changes and close dialog box.'
2114 *
2115 * @return the localized description of the object; null if
2116 * this object does not have a description
2117 *
2118 * @see #setAccessibleDescription
2119 */
2120 public String getAccessibleDescription() {
2121 return ac.getAccessibleDescription();
2122 }
2123
2124 /**
2125 * Sets the accessible description of this object. Changing the
2126 * name will cause a PropertyChangeEvent to be fired for the
2127 * ACCESSIBLE_DESCRIPTION_PROPERTY property.
2128 *
2129 * @param s the new localized description of the object
2130 *
2131 * @see #setAccessibleName
2132 * @see #addPropertyChangeListener
2133 *
2134 * @beaninfo
2135 * preferred: true
2136 * description: Sets the accessible description for the component.
2137 */
2138 public void setAccessibleDescription(String s) {
2139 ac.setAccessibleDescription(s);
2140 }
2141
2142 /**
2143 * Gets the role of this object. The role of the object is the generic
2144 * purpose or use of the class of this object. For example, the role
2145 * of a push button is AccessibleRole.PUSH_BUTTON. The roles in
2146 * AccessibleRole are provided so component developers can pick from
2147 * a set of predefined roles. This enables assistive technologies to
2148 * provide a consistent interface to various tweaked subclasses of
2149 * components (e.g., use AccessibleRole.PUSH_BUTTON for all components
2150 * that act like a push button) as well as distinguish between sublasses
2151 * that behave differently (e.g., AccessibleRole.CHECK_BOX for check boxes
2152 * and AccessibleRole.RADIO_BUTTON for radio buttons).
2153 * <p>Note that the AccessibleRole class is also extensible, so
2154 * custom component developers can define their own AccessibleRole's
2155 * if the set of predefined roles is inadequate.
2156 *
2157 * @return an instance of AccessibleRole describing the role of the object
2158 * @see AccessibleRole
2159 */
2160 public AccessibleRole getAccessibleRole() {
2161 return ac.getAccessibleRole();
2162 }
2163
2164 /**
2165 * Gets the state set of this object. The AccessibleStateSet of an object
2166 * is composed of a set of unique AccessibleStates. A change in the
2167 * AccessibleStateSet of an object will cause a PropertyChangeEvent to
2168 * be fired for the ACCESSIBLE_STATE_PROPERTY property.
2169 *
2170 * @return an instance of AccessibleStateSet containing the
2171 * current state set of the object
2172 * @see AccessibleStateSet
2173 * @see AccessibleState
2174 * @see #addPropertyChangeListener
2175 */
2176 public AccessibleStateSet getAccessibleStateSet() {
2177 return ac.getAccessibleStateSet();
2178 }
2179
2180 /**
2181 * Gets the Accessible parent of this object.
2182 *
2183 * @return the Accessible parent of this object; null if this
2184 * object does not have an Accessible parent
2185 */
2186 public Accessible getAccessibleParent() {
2187 return ac.getAccessibleParent();
2188 }
2189
2190 /**
2191 * Sets the Accessible parent of this object. This is meant to be used
2192 * only in the situations where the actual component's parent should
2193 * not be treated as the component's accessible parent and is a method
2194 * that should only be called by the parent of the accessible child.
2195 *
2196 * @param a - Accessible to be set as the parent
2197 */
2198 public void setAccessibleParent(Accessible a) {
2199 ac.setAccessibleParent(a);
2200 }
2201
2202 /**
2203 * Gets the 0-based index of this object in its accessible parent.
2204 *
2205 * @return the 0-based index of this object in its parent; -1 if this
2206 * object does not have an accessible parent.
2207 *
2208 * @see #getAccessibleParent
2209 * @see #getAccessibleChildrenCount
2210 * @see #getAccessibleChild
2211 */
2212 public int getAccessibleIndexInParent() {
2213 return JComboBox.this.getSelectedIndex();
2214 }
2215
2216 /**
2217 * Returns the number of accessible children of the object.
2218 *
2219 * @return the number of accessible children of the object.
2220 */
2221 public int getAccessibleChildrenCount() {
2222 return ac.getAccessibleChildrenCount();
2223 }
2224
2225 /**
2226 * Returns the specified Accessible child of the object. The Accessible
2227 * children of an Accessible object are zero-based, so the first child
2228 * of an Accessible child is at index 0, the second child is at index 1,
2229 * and so on.
2230 *
2231 * @param i zero-based index of child
2232 * @return the Accessible child of the object
2233 * @see #getAccessibleChildrenCount
2234 */
2235 public Accessible getAccessibleChild(int i) {
2236 return ac.getAccessibleChild(i);
2237 }
2238
2239 /**
2240 * Gets the locale of the component. If the component does not have a
2241 * locale, then the locale of its parent is returned.
2242 *
2243 * @return this component's locale. If this component does not have
2244 * a locale, the locale of its parent is returned.
2245 *
2246 * @exception IllegalComponentStateException
2247 * If the Component does not have its own locale and has not yet been
2248 * added to a containment hierarchy such that the locale can be
2249 * determined from the containing parent.
2250 */
2251 public Locale getLocale() throws IllegalComponentStateException {
2252 return ac.getLocale();
2253 }
2254
2255 /**
2256 * Adds a PropertyChangeListener to the listener list.
2257 * The listener is registered for all Accessible properties and will
2258 * be called when those properties change.
2259 *
2260 * @see #ACCESSIBLE_NAME_PROPERTY
2261 * @see #ACCESSIBLE_DESCRIPTION_PROPERTY
2262 * @see #ACCESSIBLE_STATE_PROPERTY
2263 * @see #ACCESSIBLE_VALUE_PROPERTY
2264 * @see #ACCESSIBLE_SELECTION_PROPERTY
2265 * @see #ACCESSIBLE_TEXT_PROPERTY
2266 * @see #ACCESSIBLE_VISIBLE_DATA_PROPERTY
2267 *
2268 * @param listener The PropertyChangeListener to be added
2269 */
2270 public void addPropertyChangeListener(PropertyChangeListener listener) {
2271 ac.addPropertyChangeListener(listener);
2272 }
2273
2274 /**
2275 * Removes a PropertyChangeListener from the listener list.
2276 * This removes a PropertyChangeListener that was registered
2277 * for all properties.
2278 *
2279 * @param listener The PropertyChangeListener to be removed
2280 */
2281 public void removePropertyChangeListener(PropertyChangeListener listener) {
2282 ac.removePropertyChangeListener(listener);
2283 }
2284
2285 /**
2286 * Gets the AccessibleAction associated with this object that supports
2287 * one or more actions.
2288 *
2289 * @return AccessibleAction if supported by object; else return null
2290 * @see AccessibleAction
2291 */
2292 public AccessibleAction getAccessibleAction() {
2293 return ac.getAccessibleAction();
2294 }
2295
2296 /**
2297 * Gets the AccessibleComponent associated with this object that has a
2298 * graphical representation.
2299 *
2300 * @return AccessibleComponent if supported by object; else return null
2301 * @see AccessibleComponent
2302 */
2303 public AccessibleComponent getAccessibleComponent() {
2304 return ac.getAccessibleComponent();
2305 }
2306
2307 /**
2308 * Gets the AccessibleSelection associated with this object which allows its
2309 * Accessible children to be selected.
2310 *
2311 * @return AccessibleSelection if supported by object; else return null
2312 * @see AccessibleSelection
2313 */
2314 public AccessibleSelection getAccessibleSelection() {
2315 return ac.getAccessibleSelection();
2316 }
2317
2318 /**
2319 * Gets the AccessibleText associated with this object presenting
2320 * text on the display.
2321 *
2322 * @return AccessibleText if supported by object; else return null
2323 * @see AccessibleText
2324 */
2325 public AccessibleText getAccessibleText() {
2326 return ac.getAccessibleText();
2327 }
2328
2329 /**
2330 * Gets the AccessibleEditableText associated with this object
2331 * presenting editable text on the display.
2332 *
2333 * @return AccessibleEditableText if supported by object; else return null
2334 * @see AccessibleEditableText
2335 */
2336 public AccessibleEditableText getAccessibleEditableText() {
2337 return ac.getAccessibleEditableText();
2338 }
2339
2340 /**
2341 * Gets the AccessibleValue associated with this object that supports a
2342 * Numerical value.
2343 *
2344 * @return AccessibleValue if supported by object; else return null
2345 * @see AccessibleValue
2346 */
2347 public AccessibleValue getAccessibleValue() {
2348 return ac.getAccessibleValue();
2349 }
2350
2351 /**
2352 * Gets the AccessibleIcons associated with an object that has
2353 * one or more associated icons
2354 *
2355 * @return an array of AccessibleIcon if supported by object;
2356 * otherwise return null
2357 * @see AccessibleIcon
2358 */
2359 public AccessibleIcon [] getAccessibleIcon() {
2360 return ac.getAccessibleIcon();
2361 }
2362
2363 /**
2364 * Gets the AccessibleRelationSet associated with an object
2365 *
2366 * @return an AccessibleRelationSet if supported by object;
2367 * otherwise return null
2368 * @see AccessibleRelationSet
2369 */
2370 public AccessibleRelationSet getAccessibleRelationSet() {
2371 return ac.getAccessibleRelationSet();
2372 }
2373
2374 /**
2375 * Gets the AccessibleTable associated with an object
2376 *
2377 * @return an AccessibleTable if supported by object;
2378 * otherwise return null
2379 * @see AccessibleTable
2380 */
2381 public AccessibleTable getAccessibleTable() {
2382 return ac.getAccessibleTable();
2383 }
2384
2385 /**
2386 * Support for reporting bound property changes. If oldValue and
2387 * newValue are not equal and the PropertyChangeEvent listener list
2388 * is not empty, then fire a PropertyChange event to each listener.
2389 * In general, this is for use by the Accessible objects themselves
2390 * and should not be called by an application program.
2391 * @param propertyName The programmatic name of the property that
2392 * was changed.
2393 * @param oldValue The old value of the property.
2394 * @param newValue The new value of the property.
2395 * @see java.beans.PropertyChangeSupport
2396 * @see #addPropertyChangeListener
2397 * @see #removePropertyChangeListener
2398 * @see #ACCESSIBLE_NAME_PROPERTY
2399 * @see #ACCESSIBLE_DESCRIPTION_PROPERTY
2400 * @see #ACCESSIBLE_STATE_PROPERTY
2401 * @see #ACCESSIBLE_VALUE_PROPERTY
2402 * @see #ACCESSIBLE_SELECTION_PROPERTY
2403 * @see #ACCESSIBLE_TEXT_PROPERTY
2404 * @see #ACCESSIBLE_VISIBLE_DATA_PROPERTY
2405 */
2406 public void firePropertyChange(String propertyName,
2407