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