JComboBox.java 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. /* JComboBox.java --
  2. Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.swing;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.awt.ItemSelectable;
  34. import java.awt.event.ActionEvent;
  35. import java.awt.event.ActionListener;
  36. import java.awt.event.ItemEvent;
  37. import java.awt.event.ItemListener;
  38. import java.awt.event.KeyEvent;
  39. import java.beans.PropertyChangeEvent;
  40. import java.beans.PropertyChangeListener;
  41. import java.util.Vector;
  42. import javax.accessibility.Accessible;
  43. import javax.accessibility.AccessibleAction;
  44. import javax.accessibility.AccessibleContext;
  45. import javax.accessibility.AccessibleRole;
  46. import javax.accessibility.AccessibleSelection;
  47. import javax.swing.event.ListDataEvent;
  48. import javax.swing.event.ListDataListener;
  49. import javax.swing.event.PopupMenuEvent;
  50. import javax.swing.event.PopupMenuListener;
  51. import javax.swing.plaf.ComboBoxUI;
  52. import javax.swing.plaf.ComponentUI;
  53. import javax.swing.plaf.basic.ComboPopup;
  54. /**
  55. * A component that allows a user to select any item in its list and
  56. * displays the selected item to the user. JComboBox also can show/hide a
  57. * popup menu containing its list of item whenever the mouse is pressed
  58. * over it.
  59. *
  60. * @author Andrew Selkirk
  61. * @author Olga Rodimina
  62. * @author Robert Schuster
  63. */
  64. public class JComboBox extends JComponent implements ItemSelectable,
  65. ListDataListener,
  66. ActionListener,
  67. Accessible
  68. {
  69. private static final long serialVersionUID = 5654585963292734470L;
  70. /**
  71. * Classes implementing this interface are
  72. * responsible for matching key characters typed by the user with combo
  73. * box's items.
  74. */
  75. public static interface KeySelectionManager
  76. {
  77. int selectionForKey(char aKey, ComboBoxModel aModel);
  78. }
  79. /**
  80. * Maximum number of rows that should be visible by default in the
  81. * JComboBox's popup
  82. */
  83. private static final int DEFAULT_MAXIMUM_ROW_COUNT = 8;
  84. /**
  85. * Data model used by JComboBox to keep track of its list data and currently
  86. * selected element in the list.
  87. */
  88. protected ComboBoxModel dataModel;
  89. /**
  90. * Renderer renders(paints) every object in the combo box list in its
  91. * associated list cell. This ListCellRenderer is used only when this
  92. * JComboBox is uneditable.
  93. */
  94. protected ListCellRenderer renderer;
  95. /**
  96. * Editor that is responsible for editing an object in a combo box list.
  97. */
  98. protected ComboBoxEditor editor;
  99. /**
  100. * Number of rows that will be visible in the JComboBox's popup.
  101. */
  102. protected int maximumRowCount;
  103. /**
  104. * This field indicates if textfield of this JComboBox is editable or not.
  105. */
  106. protected boolean isEditable;
  107. /**
  108. * This field is reference to the current selection of the combo box.
  109. */
  110. protected Object selectedItemReminder;
  111. /**
  112. * keySelectionManager
  113. */
  114. protected KeySelectionManager keySelectionManager;
  115. /**
  116. * This actionCommand is used in ActionEvent that is fired to JComboBox's
  117. * ActionListeneres.
  118. */
  119. protected String actionCommand;
  120. /**
  121. * This property indicates if heavyweight popup or lightweight popup will be
  122. * used to diplay JComboBox's elements.
  123. */
  124. protected boolean lightWeightPopupEnabled;
  125. /**
  126. * The action taken when new item is selected in the JComboBox
  127. */
  128. private Action action;
  129. /**
  130. * since 1.4 If this field is set then comboBox's display area for the
  131. * selected item will be set by default to this value.
  132. */
  133. private Object prototypeDisplayValue;
  134. /**
  135. * Constructs JComboBox object with specified data model for it.
  136. * <p>Note that the JComboBox will not change the value that
  137. * is preselected by your ComboBoxModel implementation.</p>
  138. *
  139. * @param model Data model that will be used by this JComboBox to keep track
  140. * of its list of items.
  141. */
  142. public JComboBox(ComboBoxModel model)
  143. {
  144. setEditable(false);
  145. setEnabled(true);
  146. setMaximumRowCount(DEFAULT_MAXIMUM_ROW_COUNT);
  147. setModel(model);
  148. setActionCommand("comboBoxChanged");
  149. lightWeightPopupEnabled = true;
  150. isEditable = false;
  151. updateUI();
  152. }
  153. /**
  154. * Constructs JComboBox with specified list of items.
  155. *
  156. * @param itemArray array containing list of items for this JComboBox
  157. */
  158. public JComboBox(Object[] itemArray)
  159. {
  160. this(new DefaultComboBoxModel(itemArray));
  161. if (itemArray.length > 0)
  162. setSelectedIndex(0);
  163. }
  164. /**
  165. * Constructs JComboBox object with specified list of items.
  166. *
  167. * @param itemVector vector containing list of items for this JComboBox.
  168. */
  169. public JComboBox(Vector<?> itemVector)
  170. {
  171. this(new DefaultComboBoxModel(itemVector));
  172. if (itemVector.size() > 0)
  173. setSelectedIndex(0);
  174. }
  175. /**
  176. * Constructor. Creates new empty JComboBox. ComboBox's data model is set to
  177. * DefaultComboBoxModel.
  178. */
  179. public JComboBox()
  180. {
  181. this(new DefaultComboBoxModel());
  182. }
  183. /**
  184. * This method returns true JComboBox is editable and false otherwise
  185. *
  186. * @return boolean true if JComboBox is editable and false otherwise
  187. */
  188. public boolean isEditable()
  189. {
  190. return isEditable;
  191. }
  192. /*
  193. * This method adds ancestor listener to this JComboBox.
  194. */
  195. protected void installAncestorListener()
  196. {
  197. /* FIXME: Need to implement.
  198. *
  199. * Need to add ancestor listener to this JComboBox. This listener
  200. * should close combo box's popup list of items whenever it
  201. * receives an AncestorEvent.
  202. */
  203. }
  204. /**
  205. * Set the "UI" property of the combo box, which is a look and feel class
  206. * responsible for handling comboBox's input events and painting it.
  207. *
  208. * @param ui The new "UI" property
  209. */
  210. public void setUI(ComboBoxUI ui)
  211. {
  212. super.setUI(ui);
  213. }
  214. /**
  215. * This method sets this comboBox's UI to the UIManager's default for the
  216. * current look and feel.
  217. */
  218. public void updateUI()
  219. {
  220. setUI((ComboBoxUI) UIManager.getUI(this));
  221. }
  222. /**
  223. * This method returns the String identifier for the UI class to the used
  224. * with the JComboBox.
  225. *
  226. * @return The String identifier for the UI class.
  227. */
  228. public String getUIClassID()
  229. {
  230. return "ComboBoxUI";
  231. }
  232. /**
  233. * This method returns the UI used to display the JComboBox.
  234. *
  235. * @return The UI used to display the JComboBox.
  236. */
  237. public ComboBoxUI getUI()
  238. {
  239. return (ComboBoxUI) ui;
  240. }
  241. /**
  242. * Set the data model for this JComboBox. This un-registers all listeners
  243. * associated with the current model, and re-registers them with the new
  244. * model.
  245. *
  246. * @param newDataModel The new data model for this JComboBox
  247. */
  248. public void setModel(ComboBoxModel newDataModel)
  249. {
  250. // dataModel is null if it this method is called from inside the constructors.
  251. if (dataModel != null)
  252. {
  253. // Prevents unneccessary updates.
  254. if (dataModel == newDataModel)
  255. return;
  256. // Removes itself (as DataListener) from the to-be-replaced model.
  257. dataModel.removeListDataListener(this);
  258. }
  259. /* Adds itself as a DataListener to the new model.
  260. * It is intentioned that this operation will fail with a NullPointerException if the
  261. * caller delivered a null argument.
  262. */
  263. newDataModel.addListDataListener(this);
  264. // Stores old data model for event notification.
  265. ComboBoxModel oldDataModel = dataModel;
  266. dataModel = newDataModel;
  267. selectedItemReminder = newDataModel.getSelectedItem();
  268. // Notifies the listeners of the model change.
  269. firePropertyChange("model", oldDataModel, dataModel);
  270. }
  271. /**
  272. * This method returns data model for this comboBox.
  273. *
  274. * @return ComboBoxModel containing items for this combo box.
  275. */
  276. public ComboBoxModel getModel()
  277. {
  278. return dataModel;
  279. }
  280. /**
  281. * This method sets JComboBox's popup to be either lightweight or
  282. * heavyweight. If 'enabled' is true then lightweight popup is used and
  283. * heavyweight otherwise. By default lightweight popup is used to display
  284. * this JComboBox's elements.
  285. *
  286. * @param enabled indicates if lightweight popup or heavyweight popup should
  287. * be used to display JComboBox's elements.
  288. */
  289. public void setLightWeightPopupEnabled(boolean enabled)
  290. {
  291. lightWeightPopupEnabled = enabled;
  292. }
  293. /**
  294. * This method returns whether popup menu that is used to display list of
  295. * combo box's item is lightWeight or not.
  296. *
  297. * @return boolean true if popup menu is lightweight and false otherwise.
  298. */
  299. public boolean isLightWeightPopupEnabled()
  300. {
  301. return lightWeightPopupEnabled;
  302. }
  303. /**
  304. * This method sets editability of the combo box. If combo box is editable
  305. * the user can choose component from the combo box list by typing
  306. * component's name in the editor(JTextfield by default). Otherwise if not
  307. * editable, the user should use the list to choose the component. This
  308. * method fires PropertyChangeEvents to JComboBox's registered
  309. * PropertyChangeListeners to indicate that 'editable' property of the
  310. * JComboBox has changed.
  311. *
  312. * @param editable indicates if the JComboBox's textfield should be editable
  313. * or not.
  314. */
  315. public void setEditable(boolean editable)
  316. {
  317. if (isEditable != editable)
  318. {
  319. isEditable = editable;
  320. firePropertyChange("editable", !isEditable, isEditable);
  321. }
  322. }
  323. /**
  324. * Sets number of rows that should be visible in this JComboBox's popup. If
  325. * this JComboBox's popup has more elements that maximum number or rows
  326. * then popup will have a scroll pane to allow users to view other
  327. * elements.
  328. *
  329. * @param rowCount number of rows that will be visible in JComboBox's popup.
  330. */
  331. public void setMaximumRowCount(int rowCount)
  332. {
  333. if (maximumRowCount != rowCount)
  334. {
  335. int oldMaximumRowCount = maximumRowCount;
  336. maximumRowCount = rowCount;
  337. firePropertyChange("maximumRowCount", oldMaximumRowCount,
  338. maximumRowCount);
  339. }
  340. }
  341. /**
  342. * This method returns number of rows visible in the JComboBox's list of
  343. * items.
  344. *
  345. * @return int maximun number of visible rows in the JComboBox's list.
  346. */
  347. public int getMaximumRowCount()
  348. {
  349. return maximumRowCount;
  350. }
  351. /**
  352. * This method sets cell renderer for this JComboBox that will be used to
  353. * paint combo box's items. The Renderer should only be used only when
  354. * JComboBox is not editable. In the case when JComboBox is editable the
  355. * editor must be used. This method also fires PropertyChangeEvent when
  356. * cellRendered for this JComboBox has changed.
  357. *
  358. * @param aRenderer cell renderer that will be used by this JComboBox to
  359. * paint its elements.
  360. */
  361. public void setRenderer(ListCellRenderer aRenderer)
  362. {
  363. if (renderer != aRenderer)
  364. {
  365. ListCellRenderer oldRenderer = renderer;
  366. renderer = aRenderer;
  367. firePropertyChange("renderer", oldRenderer, renderer);
  368. }
  369. }
  370. /**
  371. * This method returns renderer responsible for rendering selected item in
  372. * the combo box
  373. *
  374. * @return ListCellRenderer
  375. */
  376. public ListCellRenderer getRenderer()
  377. {
  378. return renderer;
  379. }
  380. /**
  381. * Sets editor for this JComboBox
  382. *
  383. * @param newEditor ComboBoxEditor for this JComboBox. This method fires
  384. * PropertyChangeEvent when 'editor' property is changed.
  385. */
  386. public void setEditor(ComboBoxEditor newEditor)
  387. {
  388. if (editor == newEditor)
  389. return;
  390. if (editor != null)
  391. editor.removeActionListener(this);
  392. ComboBoxEditor oldEditor = editor;
  393. editor = newEditor;
  394. if (editor != null)
  395. editor.addActionListener(this);
  396. firePropertyChange("editor", oldEditor, editor);
  397. }
  398. /**
  399. * Returns editor component that is responsible for displaying/editing
  400. * selected item in the combo box.
  401. *
  402. * @return ComboBoxEditor
  403. */
  404. public ComboBoxEditor getEditor()
  405. {
  406. return editor;
  407. }
  408. /**
  409. * Forces combo box to select given item
  410. *
  411. * @param item element in the combo box to select.
  412. */
  413. public void setSelectedItem(Object item)
  414. {
  415. dataModel.setSelectedItem(item);
  416. fireActionEvent();
  417. }
  418. /**
  419. * Returns currently selected item in the combo box.
  420. * The result may be <code>null</code> to indicate that nothing is
  421. * currently selected.
  422. *
  423. * @return element that is currently selected in this combo box.
  424. */
  425. public Object getSelectedItem()
  426. {
  427. return dataModel.getSelectedItem();
  428. }
  429. /**
  430. * Forces JComboBox to select component located in the given index in the
  431. * combo box.
  432. * <p>If the index is below -1 or exceeds the upper bound an
  433. * <code>IllegalArgumentException</code> is thrown.<p/>
  434. * <p>If the index is -1 then no item gets selected.</p>
  435. *
  436. * @param index index specifying location of the component that should be
  437. * selected.
  438. */
  439. public void setSelectedIndex(int index)
  440. {
  441. if (index < -1 || index >= dataModel.getSize())
  442. // Fails because index is out of bounds.
  443. throw new IllegalArgumentException("illegal index: " + index);
  444. else
  445. // Selects the item at the given index or clears the selection if the
  446. // index value is -1.
  447. setSelectedItem((index == -1) ? null : dataModel.getElementAt(index));
  448. }
  449. /**
  450. * Returns index of the item that is currently selected in the combo box. If
  451. * no item is currently selected, then -1 is returned.
  452. * <p>
  453. * Note: For performance reasons you should minimize invocation of this
  454. * method. If the data model is not an instance of
  455. * <code>DefaultComboBoxModel</code> the complexity is O(n) where n is the
  456. * number of elements in the combo box.
  457. * </p>
  458. *
  459. * @return int Index specifying location of the currently selected item in the
  460. * combo box or -1 if nothing is selected in the combo box.
  461. */
  462. public int getSelectedIndex()
  463. {
  464. Object selectedItem = getSelectedItem();
  465. if (selectedItem != null)
  466. {
  467. if (dataModel instanceof DefaultComboBoxModel)
  468. // Uses special method of DefaultComboBoxModel to retrieve the index.
  469. return ((DefaultComboBoxModel) dataModel).getIndexOf(selectedItem);
  470. else
  471. {
  472. // Iterates over all items to retrieve the index.
  473. int size = dataModel.getSize();
  474. for (int i = 0; i < size; i++)
  475. {
  476. Object o = dataModel.getElementAt(i);
  477. // XXX: Is special handling of ComparableS neccessary?
  478. if ((selectedItem != null) ? selectedItem.equals(o) : o == null)
  479. return i;
  480. }
  481. }
  482. }
  483. // returns that no item is currently selected
  484. return -1;
  485. }
  486. /**
  487. * Returns an object that is used as the display value when calculating the
  488. * preferred size for the combo box. This value is, of course, never
  489. * displayed anywhere.
  490. *
  491. * @return The prototype display value (possibly <code>null</code>).
  492. *
  493. * @since 1.4
  494. * @see #setPrototypeDisplayValue(Object)
  495. */
  496. public Object getPrototypeDisplayValue()
  497. {
  498. return prototypeDisplayValue;
  499. }
  500. /**
  501. * Sets the object that is assumed to be the displayed item when calculating
  502. * the preferred size for the combo box. A {@link PropertyChangeEvent} (with
  503. * the name <code>prototypeDisplayValue</code>) is sent to all registered
  504. * listeners.
  505. *
  506. * @param value the new value (<code>null</code> permitted).
  507. *
  508. * @since 1.4
  509. * @see #getPrototypeDisplayValue()
  510. */
  511. public void setPrototypeDisplayValue(Object value)
  512. {
  513. Object oldValue = prototypeDisplayValue;
  514. prototypeDisplayValue = value;
  515. firePropertyChange("prototypeDisplayValue", oldValue, value);
  516. }
  517. /**
  518. * This method adds given element to this JComboBox.
  519. * <p>A <code>RuntimeException</code> is thrown if the data model is not
  520. * an instance of {@link MutableComboBoxModel}.</p>
  521. *
  522. * @param element element to add
  523. */
  524. public void addItem(Object element)
  525. {
  526. if (dataModel instanceof MutableComboBoxModel)
  527. ((MutableComboBoxModel) dataModel).addElement(element);
  528. else
  529. throw new RuntimeException("Unable to add the item because the data "
  530. + "model it is not an instance of "
  531. + "MutableComboBoxModel.");
  532. }
  533. /**
  534. * Inserts given element at the specified index to this JComboBox.
  535. * <p>A <code>RuntimeException</code> is thrown if the data model is not
  536. * an instance of {@link MutableComboBoxModel}.</p>
  537. *
  538. * @param element element to insert
  539. * @param index position where to insert the element
  540. */
  541. public void insertItemAt(Object element, int index)
  542. {
  543. if (dataModel instanceof MutableComboBoxModel)
  544. ((MutableComboBoxModel) dataModel).insertElementAt(element, index);
  545. else
  546. throw new RuntimeException("Unable to insert the item because the data "
  547. + "model it is not an instance of "
  548. + "MutableComboBoxModel.");
  549. }
  550. /**
  551. * This method removes given element from this JComboBox.
  552. * <p>A <code>RuntimeException</code> is thrown if the data model is not
  553. * an instance of {@link MutableComboBoxModel}.</p>
  554. *
  555. * @param element element to remove
  556. */
  557. public void removeItem(Object element)
  558. {
  559. if (dataModel instanceof MutableComboBoxModel)
  560. ((MutableComboBoxModel) dataModel).removeElement(element);
  561. else
  562. throw new RuntimeException("Unable to remove the item because the data "
  563. + "model it is not an instance of "
  564. + "MutableComboBoxModel.");
  565. }
  566. /**
  567. * This method remove element location in the specified index in the
  568. * JComboBox.
  569. * <p>A <code>RuntimeException</code> is thrown if the data model is not
  570. * an instance of {@link MutableComboBoxModel}.</p>
  571. *
  572. * @param index index specifying position of the element to remove
  573. */
  574. public void removeItemAt(int index)
  575. {
  576. if (dataModel instanceof MutableComboBoxModel)
  577. ((MutableComboBoxModel) dataModel).removeElementAt(index);
  578. else
  579. throw new RuntimeException("Unable to remove the item because the data "
  580. + "model it is not an instance of "
  581. + "MutableComboBoxModel.");
  582. }
  583. /**
  584. * This method removes all elements from this JComboBox.
  585. * <p>
  586. * A <code>RuntimeException</code> is thrown if the data model is not an
  587. * instance of {@link MutableComboBoxModel}.
  588. * </p>
  589. */
  590. public void removeAllItems()
  591. {
  592. if (dataModel instanceof DefaultComboBoxModel)
  593. // Uses special method if we have a DefaultComboBoxModel.
  594. ((DefaultComboBoxModel) dataModel).removeAllElements();
  595. else if (dataModel instanceof MutableComboBoxModel)
  596. {
  597. // Iterates over all items and removes each.
  598. MutableComboBoxModel mcbm = (MutableComboBoxModel) dataModel;
  599. // We intentionally remove the items backwards to support models which
  600. // shift their content to the beginning (e.g. linked lists)
  601. for (int i = mcbm.getSize() - 1; i >= 0; i--)
  602. mcbm.removeElementAt(i);
  603. }
  604. else
  605. throw new RuntimeException("Unable to remove the items because the data "
  606. + "model it is not an instance of "
  607. + "MutableComboBoxModel.");
  608. }
  609. /**
  610. * This method displays popup with list of combo box's items on the screen
  611. */
  612. public void showPopup()
  613. {
  614. setPopupVisible(true);
  615. }
  616. /**
  617. * This method hides popup containing list of combo box's items
  618. */
  619. public void hidePopup()
  620. {
  621. setPopupVisible(false);
  622. }
  623. /**
  624. * This method either displayes or hides the popup containing list of combo
  625. * box's items.
  626. *
  627. * @param visible show popup if 'visible' is true and hide it otherwise
  628. */
  629. public void setPopupVisible(boolean visible)
  630. {
  631. getUI().setPopupVisible(this, visible);
  632. }
  633. /**
  634. * Checks if popup is currently visible on the screen.
  635. *
  636. * @return boolean true if popup is visible and false otherwise
  637. */
  638. public boolean isPopupVisible()
  639. {
  640. return getUI().isPopupVisible(this);
  641. }
  642. /**
  643. * This method sets actionCommand to the specified string. ActionEvent fired
  644. * to this JComboBox registered ActionListeners will contain this
  645. * actionCommand.
  646. *
  647. * @param aCommand new action command for the JComboBox's ActionEvent
  648. */
  649. public void setActionCommand(String aCommand)
  650. {
  651. actionCommand = aCommand;
  652. }
  653. /**
  654. * Returns actionCommand associated with the ActionEvent fired by the
  655. * JComboBox to its registered ActionListeners.
  656. *
  657. * @return String actionCommand for the ActionEvent
  658. */
  659. public String getActionCommand()
  660. {
  661. return actionCommand;
  662. }
  663. /**
  664. * setAction
  665. *
  666. * @param a action to set
  667. */
  668. public void setAction(Action a)
  669. {
  670. Action old = action;
  671. action = a;
  672. configurePropertiesFromAction(action);
  673. if (action != null)
  674. // FIXME: remove from old action and add to new action
  675. // PropertyChangeListener to listen to changes in the action
  676. addActionListener(action);
  677. }
  678. /**
  679. * This method returns Action that is invoked when selected item is changed
  680. * in the JComboBox.
  681. *
  682. * @return Action
  683. */
  684. public Action getAction()
  685. {
  686. return action;
  687. }
  688. /**
  689. * Configure properties of the JComboBox by reading properties of specified
  690. * action. This method always sets the comboBox's "enabled" property to the
  691. * value of the Action's "enabled" property.
  692. *
  693. * @param a An Action to configure the combo box from
  694. */
  695. protected void configurePropertiesFromAction(Action a)
  696. {
  697. if (a == null)
  698. {
  699. setEnabled(true);
  700. setToolTipText(null);
  701. }
  702. else
  703. {
  704. setEnabled(a.isEnabled());
  705. setToolTipText((String) (a.getValue(Action.SHORT_DESCRIPTION)));
  706. }
  707. }
  708. /**
  709. * Creates PropertyChangeListener to listen for the changes in comboBox's
  710. * action properties.
  711. *
  712. * @param action action to listen to for property changes
  713. *
  714. * @return a PropertyChangeListener that listens to changes in
  715. * action properties.
  716. */
  717. protected PropertyChangeListener createActionPropertyChangeListener(Action action)
  718. {
  719. return new PropertyChangeListener()
  720. {
  721. public void propertyChange(PropertyChangeEvent e)
  722. {
  723. Action act = (Action) (e.getSource());
  724. configurePropertiesFromAction(act);
  725. }
  726. };
  727. }
  728. /**
  729. * This method fires ItemEvent to this JComboBox's registered ItemListeners.
  730. * This method is invoked when currently selected item in this combo box
  731. * has changed.
  732. *
  733. * @param e the ItemEvent describing the change in the combo box's
  734. * selection.
  735. */
  736. protected void fireItemStateChanged(ItemEvent e)
  737. {
  738. ItemListener[] ll = getItemListeners();
  739. for (int i = 0; i < ll.length; i++)
  740. ll[i].itemStateChanged(e);
  741. }
  742. /**
  743. * This method fires ActionEvent to this JComboBox's registered
  744. * ActionListeners. This method is invoked when user explicitly changes
  745. * currently selected item.
  746. */
  747. protected void fireActionEvent()
  748. {
  749. ActionListener[] ll = getActionListeners();
  750. for (int i = 0; i < ll.length; i++)
  751. ll[i].actionPerformed(new ActionEvent(this,
  752. ActionEvent.ACTION_PERFORMED,
  753. actionCommand));
  754. }
  755. /**
  756. * Fires a popupMenuCanceled() event to all <code>PopupMenuListeners</code>.
  757. *
  758. * Note: This method is intended for use by plaf classes only.
  759. */
  760. public void firePopupMenuCanceled()
  761. {
  762. PopupMenuListener[] listeners = getPopupMenuListeners();
  763. PopupMenuEvent e = new PopupMenuEvent(this);
  764. for (int i = 0; i < listeners.length; i++)
  765. listeners[i].popupMenuCanceled(e);
  766. }
  767. /**
  768. * Fires a popupMenuWillBecomeInvisible() event to all
  769. * <code>PopupMenuListeners</code>.
  770. *
  771. * Note: This method is intended for use by plaf classes only.
  772. */
  773. public void firePopupMenuWillBecomeInvisible()
  774. {
  775. PopupMenuListener[] listeners = getPopupMenuListeners();
  776. PopupMenuEvent e = new PopupMenuEvent(this);
  777. for (int i = 0; i < listeners.length; i++)
  778. listeners[i].popupMenuWillBecomeInvisible(e);
  779. }
  780. /**
  781. * Fires a popupMenuWillBecomeVisible() event to all
  782. * <code>PopupMenuListeners</code>.
  783. *
  784. * Note: This method is intended for use by plaf classes only.
  785. */
  786. public void firePopupMenuWillBecomeVisible()
  787. {
  788. PopupMenuListener[] listeners = getPopupMenuListeners();
  789. PopupMenuEvent e = new PopupMenuEvent(this);
  790. for (int i = 0; i < listeners.length; i++)
  791. listeners[i].popupMenuWillBecomeVisible(e);
  792. }
  793. /**
  794. * This method is invoked whenever selected item changes in the combo box's
  795. * data model. It fires ItemEvent and ActionEvent to all registered
  796. * ComboBox's ItemListeners and ActionListeners respectively, indicating
  797. * the change.
  798. */
  799. protected void selectedItemChanged()
  800. {
  801. // Fire ItemEvent to indicated that previously selected item is now
  802. // deselected
  803. if (selectedItemReminder != null)
  804. fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
  805. selectedItemReminder,
  806. ItemEvent.DESELECTED));
  807. // Fire ItemEvent to indicate that new item is selected
  808. Object newSelection = getSelectedItem();
  809. if (newSelection != null)
  810. fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
  811. newSelection, ItemEvent.SELECTED));
  812. // Fire Action Event to JComboBox's registered listeners
  813. fireActionEvent();
  814. selectedItemReminder = newSelection;
  815. }
  816. /**
  817. * Returns Object array of size 1 containing currently selected element in
  818. * the JComboBox.
  819. *
  820. * @return Object[] Object array of size 1 containing currently selected
  821. * element in the JComboBox.
  822. */
  823. public Object[] getSelectedObjects()
  824. {
  825. return new Object[] { getSelectedItem() };
  826. }
  827. /**
  828. * This method handles actionEvents fired by the ComboBoxEditor. It changes
  829. * this JComboBox's selection to the new value currently in the editor and
  830. * hides list of combo box items.
  831. *
  832. * @param e the ActionEvent
  833. */
  834. public void actionPerformed(ActionEvent e)
  835. {
  836. setSelectedItem(getEditor().getItem());
  837. setPopupVisible(false);
  838. }
  839. /**
  840. * This method selects item in this combo box that matches specified
  841. * specified keyChar and returns true if such item is found. Otherwise
  842. * false is returned.
  843. *
  844. * @param keyChar character indicating which item in the combo box should be
  845. * selected.
  846. *
  847. * @return boolean true if item corresponding to the specified keyChar
  848. * exists in the combo box. Otherwise false is returned.
  849. */
  850. public boolean selectWithKeyChar(char keyChar)
  851. {
  852. if (keySelectionManager == null)
  853. {
  854. keySelectionManager = createDefaultKeySelectionManager();
  855. }
  856. int index = keySelectionManager.selectionForKey(keyChar, getModel());
  857. boolean retVal = false;
  858. if (index >= 0)
  859. {
  860. setSelectedIndex(index);
  861. retVal = true;
  862. }
  863. return retVal;
  864. }
  865. /**
  866. * The part of implementation of ListDataListener interface. This method is
  867. * invoked when some items where added to the JComboBox's data model.
  868. *
  869. * @param event ListDataEvent describing the change
  870. */
  871. public void intervalAdded(ListDataEvent event)
  872. {
  873. // FIXME: Need to implement
  874. repaint();
  875. }
  876. /**
  877. * The part of implementation of ListDataListener interface. This method is
  878. * invoked when some items where removed from the JComboBox's data model.
  879. *
  880. * @param event ListDataEvent describing the change.
  881. */
  882. public void intervalRemoved(ListDataEvent event)
  883. {
  884. // FIXME: Need to implement
  885. repaint();
  886. }
  887. /**
  888. * The part of implementation of ListDataListener interface. This method is
  889. * invoked when contents of the JComboBox's data model changed.
  890. *
  891. * @param event ListDataEvent describing the change
  892. */
  893. public void contentsChanged(ListDataEvent event)
  894. {
  895. // if first and last index of the given ListDataEvent are both -1,
  896. // then it indicates that selected item in the combo box data model
  897. // have changed.
  898. if (event.getIndex0() == -1 && event.getIndex1() == -1)
  899. selectedItemChanged();
  900. }
  901. /**
  902. * This method disables or enables JComboBox. If the JComboBox is enabled,
  903. * then user is able to make item choice, otherwise if JComboBox is
  904. * disabled then user is not able to make a selection.
  905. *
  906. * @param enabled if 'enabled' is true then enable JComboBox and disable it
  907. */
  908. public void setEnabled(boolean enabled)
  909. {
  910. boolean oldEnabled = super.isEnabled();
  911. if (enabled != oldEnabled)
  912. {
  913. super.setEnabled(enabled);
  914. firePropertyChange("enabled", oldEnabled, enabled);
  915. }
  916. }
  917. /**
  918. * This method initializes specified ComboBoxEditor to display given item.
  919. *
  920. * @param anEditor ComboBoxEditor to initialize
  921. * @param anItem Item that should displayed in the specified editor
  922. */
  923. public void configureEditor(ComboBoxEditor anEditor, Object anItem)
  924. {
  925. anEditor.setItem(anItem);
  926. }
  927. /**
  928. * This method is fired whenever a key is pressed with the combo box
  929. * in focus
  930. *
  931. * @param e The KeyEvent indicating which key was pressed.
  932. */
  933. public void processKeyEvent(KeyEvent e)
  934. {
  935. if (e.getKeyCode() == KeyEvent.VK_TAB)
  936. setPopupVisible(false);
  937. else
  938. super.processKeyEvent(e);
  939. }
  940. /**
  941. * setKeySelectionManager
  942. *
  943. * @param aManager
  944. */
  945. public void setKeySelectionManager(KeySelectionManager aManager)
  946. {
  947. keySelectionManager = aManager;
  948. }
  949. /**
  950. * getKeySelectionManager
  951. *
  952. * @return JComboBox.KeySelectionManager
  953. */
  954. public KeySelectionManager getKeySelectionManager()
  955. {
  956. return keySelectionManager;
  957. }
  958. /**
  959. * This method returns number of elements in this JComboBox
  960. *
  961. * @return int number of elements in this JComboBox
  962. */
  963. public int getItemCount()
  964. {
  965. return dataModel.getSize();
  966. }
  967. /**
  968. * Returns elements located in the combo box at the given index.
  969. *
  970. * @param index index specifying location of the component to return.
  971. *
  972. * @return component in the combo box that is located in the given index.
  973. */
  974. public Object getItemAt(int index)
  975. {
  976. return dataModel.getElementAt(index);
  977. }
  978. /**
  979. * createDefaultKeySelectionManager
  980. *
  981. * @return KeySelectionManager
  982. */
  983. protected KeySelectionManager createDefaultKeySelectionManager()
  984. {
  985. return new DefaultKeySelectionManager();
  986. }
  987. /**
  988. * Returns an implementation-dependent string describing the attributes of
  989. * this <code>JComboBox</code>.
  990. *
  991. * @return A string describing the attributes of this <code>JComboBox</code>
  992. * (never <code>null</code>).
  993. */
  994. protected String paramString()
  995. {
  996. String superParamStr = super.paramString();
  997. CPStringBuilder sb = new CPStringBuilder();
  998. sb.append(",isEditable=").append(isEditable());
  999. sb.append(",lightWeightPopupEnabled=").append(isLightWeightPopupEnabled());
  1000. sb.append(",maximumRowCount=").append(getMaximumRowCount());
  1001. sb.append(",selectedItemReminder=");
  1002. if (selectedItemReminder != null)
  1003. sb.append(selectedItemReminder);
  1004. return superParamStr + sb.toString();
  1005. }
  1006. /**
  1007. * Returns the object that provides accessibility features for this
  1008. * <code>JComboBox</code> component.
  1009. *
  1010. * @return The accessible context (an instance of
  1011. * {@link AccessibleJComboBox}).
  1012. */
  1013. public AccessibleContext getAccessibleContext()
  1014. {
  1015. if (accessibleContext == null)
  1016. accessibleContext = new AccessibleJComboBox();
  1017. return accessibleContext;
  1018. }
  1019. /**
  1020. * This methods adds specified ActionListener to this JComboBox.
  1021. *
  1022. * @param listener to add
  1023. */
  1024. public void addActionListener(ActionListener listener)
  1025. {
  1026. listenerList.add(ActionListener.class, listener);
  1027. }
  1028. /**
  1029. * This method removes specified ActionListener from this JComboBox.
  1030. *
  1031. * @param listener ActionListener
  1032. */
  1033. public void removeActionListener(ActionListener listener)
  1034. {
  1035. listenerList.remove(ActionListener.class, listener);
  1036. }
  1037. /**
  1038. * This method returns array of ActionListeners that are registered with
  1039. * this JComboBox.
  1040. *
  1041. * @since 1.4
  1042. */
  1043. public ActionListener[] getActionListeners()
  1044. {
  1045. return (ActionListener[]) getListeners(ActionListener.class);
  1046. }
  1047. /**
  1048. * This method registers given ItemListener with this JComboBox
  1049. *
  1050. * @param listener to remove
  1051. */
  1052. public void addItemListener(ItemListener listener)
  1053. {
  1054. listenerList.add(ItemListener.class, listener);
  1055. }
  1056. /**
  1057. * This method unregisters given ItemListener from this JComboBox
  1058. *
  1059. * @param listener to remove
  1060. */
  1061. public void removeItemListener(ItemListener listener)
  1062. {
  1063. listenerList.remove(ItemListener.class, listener);
  1064. }
  1065. /**
  1066. * This method returns array of ItemListeners that are registered with this
  1067. * JComboBox.
  1068. *
  1069. * @since 1.4
  1070. */
  1071. public ItemListener[] getItemListeners()
  1072. {
  1073. return (ItemListener[]) getListeners(ItemListener.class);
  1074. }
  1075. /**
  1076. * Adds PopupMenuListener to combo box to listen to the events fired by the
  1077. * combo box's popup menu containing its list of items
  1078. *
  1079. * @param listener to add
  1080. */
  1081. public void addPopupMenuListener(PopupMenuListener listener)
  1082. {
  1083. listenerList.add(PopupMenuListener.class, listener);
  1084. }
  1085. /**
  1086. * Removes PopupMenuListener to combo box to listen to the events fired by
  1087. * the combo box's popup menu containing its list of items
  1088. *
  1089. * @param listener to add
  1090. */
  1091. public void removePopupMenuListener(PopupMenuListener listener)
  1092. {
  1093. listenerList.remove(PopupMenuListener.class, listener);
  1094. }
  1095. /**
  1096. * Returns array of PopupMenuListeners that are registered with combo box.
  1097. */
  1098. public PopupMenuListener[] getPopupMenuListeners()
  1099. {
  1100. return (PopupMenuListener[]) getListeners(PopupMenuListener.class);
  1101. }
  1102. /**
  1103. * Accessibility support for <code>JComboBox</code>.
  1104. */
  1105. protected class AccessibleJComboBox extends AccessibleJComponent
  1106. implements AccessibleAction, AccessibleSelection
  1107. {
  1108. private static final long serialVersionUID = 8217828307256675666L;
  1109. /**
  1110. * @specnote This constructor was protected in 1.4, but made public
  1111. * in 1.5.
  1112. */
  1113. public AccessibleJComboBox()
  1114. {
  1115. // Nothing to do here.
  1116. }
  1117. /**
  1118. * Returns the number of accessible children of this object. The
  1119. * implementation of AccessibleJComboBox delegates this call to the UI
  1120. * of the associated JComboBox.
  1121. *
  1122. * @return the number of accessible children of this object
  1123. *
  1124. * @see ComponentUI#getAccessibleChildrenCount(JComponent)
  1125. */
  1126. public int getAccessibleChildrenCount()
  1127. {
  1128. ComponentUI ui = getUI();
  1129. int count;
  1130. if (ui != null)
  1131. count = ui.getAccessibleChildrenCount(JComboBox.this);
  1132. else
  1133. count = super.getAccessibleChildrenCount();
  1134. return count;
  1135. }
  1136. /**
  1137. * Returns the number of accessible children of this object. The
  1138. * implementation of AccessibleJComboBox delegates this call to the UI
  1139. * of the associated JComboBox.
  1140. *
  1141. * @param index the index of the accessible child to fetch
  1142. *
  1143. * @return the number of accessible children of this object
  1144. *
  1145. * @see ComponentUI#getAccessibleChild(JComponent, int)
  1146. */
  1147. public Accessible getAccessibleChild(int index)
  1148. {
  1149. ComponentUI ui = getUI();
  1150. Accessible child = null;
  1151. if (ui != null)
  1152. child = ui.getAccessibleChild(JComboBox.this, index);
  1153. else
  1154. child = super.getAccessibleChild(index);
  1155. return child;
  1156. }
  1157. /**
  1158. * Returns the AccessibleSelection object associated with this object.
  1159. * AccessibleJComboBoxes handle their selection themselves, so this
  1160. * always returns <code>this</code>.
  1161. *
  1162. * @return the AccessibleSelection object associated with this object
  1163. */
  1164. public AccessibleSelection getAccessibleSelection()
  1165. {
  1166. return this;
  1167. }
  1168. /**
  1169. * Returns the accessible selection from this AccssibleJComboBox.
  1170. *
  1171. * @param index the index of the selected child to fetch
  1172. *
  1173. * @return the accessible selection from this AccssibleJComboBox
  1174. */
  1175. public Accessible getAccessibleSelection(int index)
  1176. {
  1177. // Get hold of the actual popup.
  1178. Accessible popup = getUI().getAccessibleChild(JComboBox.this, 0);
  1179. Accessible selected = null;
  1180. if (popup != null && popup instanceof ComboPopup)
  1181. {
  1182. ComboPopup cPopup = (ComboPopup) popup;
  1183. // Query the list for the currently selected child.
  1184. JList l = cPopup.getList();
  1185. AccessibleContext listCtx = l.getAccessibleContext();
  1186. if (listCtx != null)
  1187. {
  1188. AccessibleSelection s = listCtx.getAccessibleSelection();
  1189. if (s != null)
  1190. {
  1191. selected = s.getAccessibleSelection(index);
  1192. }
  1193. }
  1194. }
  1195. return selected;
  1196. }
  1197. /**
  1198. * Returns <code>true</code> if the accessible child with the specified
  1199. * <code>index</code> is selected, <code>false</code> otherwise.
  1200. *
  1201. * @param index the index of the accessible child
  1202. *
  1203. * @return <code>true</code> if the accessible child with the specified
  1204. * <code>index</code> is selected, <code>false</code> otherwise
  1205. */
  1206. public boolean isAccessibleChildSelected(int index)
  1207. {
  1208. return getSelectedIndex() == index;
  1209. }
  1210. /**
  1211. * Returns the accessible role for the <code>JComboBox</code> component.
  1212. *
  1213. * @return {@link AccessibleRole#COMBO_BOX}.
  1214. */
  1215. public AccessibleRole getAccessibleRole()
  1216. {
  1217. return AccessibleRole.COMBO_BOX;
  1218. }
  1219. /**
  1220. * Returns the accessible action associated to this accessible object.
  1221. * AccessibleJComboBox implements its own AccessibleAction, so this
  1222. * method returns <code>this</code>.
  1223. *
  1224. * @return the accessible action associated to this accessible object
  1225. */
  1226. public AccessibleAction getAccessibleAction()
  1227. {
  1228. return this;
  1229. }
  1230. /**
  1231. * Returns the description of the specified action. AccessibleJComboBox
  1232. * implements 1 action (toggle the popup menu) and thus returns
  1233. * <code>UIManager.getString("ComboBox.togglePopupText")</code>
  1234. *
  1235. * @param actionIndex the index of the action for which to return the
  1236. * description
  1237. *
  1238. * @return the description of the specified action
  1239. */
  1240. public String getAccessibleActionDescription(int actionIndex)
  1241. {
  1242. return UIManager.getString("ComboBox.togglePopupText");
  1243. }
  1244. /**
  1245. * Returns the number of accessible actions that can be performed by
  1246. * this object. AccessibleJComboBox implement s one accessible action
  1247. * (toggle the popup menu), so this method always returns <code>1</code>.
  1248. *
  1249. * @return the number of accessible actions that can be performed by
  1250. * this object
  1251. */
  1252. public int getAccessibleActionCount()
  1253. {
  1254. return 1;
  1255. }
  1256. /**
  1257. * Performs the accessible action with the specified index.
  1258. * AccessibleJComboBox has 1 accessible action
  1259. * (<code>actionIndex == 0</code>), which is to toggle the
  1260. * popup menu. All other action indices have no effect and return
  1261. * <code<>false</code>.
  1262. *
  1263. * @param actionIndex the index of the action to perform
  1264. *
  1265. * @return <code>true</code> if the action has been performed,
  1266. * <code>false</code> otherwise
  1267. */
  1268. public boolean doAccessibleAction(int actionIndex)
  1269. {
  1270. boolean actionPerformed = false;
  1271. if (actionIndex == 0)
  1272. {
  1273. setPopupVisible(! isPopupVisible());
  1274. actionPerformed = true;
  1275. }
  1276. return actionPerformed;
  1277. }
  1278. /**
  1279. * Returns the number of selected accessible children of this object. This
  1280. * returns <code>1</code> if the combobox has a selected entry,
  1281. * <code>0</code> otherwise.
  1282. *
  1283. * @return the number of selected accessible children of this object
  1284. */
  1285. public int getAccessibleSelectionCount()
  1286. {
  1287. Object sel = getSelectedItem();
  1288. int count = 0;
  1289. if (sel != null)
  1290. count = 1;
  1291. return count;
  1292. }
  1293. /**
  1294. * Sets the current selection to the specified <code>index</code>.
  1295. *
  1296. * @param index the index to set as selection
  1297. */
  1298. public void addAccessibleSelection(int index)
  1299. {
  1300. setSelectedIndex(index);
  1301. }
  1302. /**
  1303. * Removes the specified index from the current selection.
  1304. *
  1305. * @param index the index to remove from the selection
  1306. */
  1307. public void removeAccessibleSelection(int index)
  1308. {
  1309. if (getSelectedIndex() == index)
  1310. clearAccessibleSelection();
  1311. }
  1312. /**
  1313. * Clears the current selection.
  1314. */
  1315. public void clearAccessibleSelection()
  1316. {
  1317. setSelectedIndex(-1);
  1318. }
  1319. /**
  1320. * Multiple selection is not supported by AccessibleJComboBox, so this
  1321. * does nothing.
  1322. */
  1323. public void selectAllAccessibleSelection()
  1324. {
  1325. // Nothing to do here.
  1326. }
  1327. }
  1328. private class DefaultKeySelectionManager
  1329. implements KeySelectionManager
  1330. {
  1331. public int selectionForKey(char aKey, ComboBoxModel aModel)
  1332. {
  1333. int selectedIndex = getSelectedIndex();
  1334. // Start at currently selected item and iterate to end of list
  1335. for (int i = selectedIndex + 1; i < aModel.getSize(); i++)
  1336. {
  1337. String nextItem = aModel.getElementAt(i).toString();
  1338. if (nextItem.charAt(0) == aKey)
  1339. return i;
  1340. }
  1341. // Wrap to start of list if no match yet
  1342. for (int i = 0; i <= selectedIndex; i++)
  1343. {
  1344. String nextItem = aModel.getElementAt(i).toString();
  1345. if (nextItem.charAt(0) == aKey)
  1346. return i;
  1347. }
  1348. return - 1;
  1349. }
  1350. }
  1351. }