Choice.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /* Choice.java -- Java choice button widget.
  2. Copyright (C) 1999, 2000, 2001, 2002 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., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 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 java.awt;
  32. import java.awt.peer.ChoicePeer;
  33. import java.awt.peer.ComponentPeer;
  34. import java.awt.event.ItemEvent;
  35. import java.awt.event.ItemListener;
  36. import java.io.Serializable;
  37. import java.util.Vector;
  38. /**
  39. * This class implements a drop down choice list.
  40. *
  41. * @author Aaron M. Renn (arenn@urbanophile.com)
  42. */
  43. public class Choice extends Component implements ItemSelectable, Serializable
  44. {
  45. /*
  46. * Static Variables
  47. */
  48. // Serialization constant
  49. private static final long serialVersionUID = -4075310674757313071L;
  50. /*************************************************************************/
  51. /*
  52. * Instance Variables
  53. */
  54. /**
  55. * @serial A list of items for the choice box, which can be <code>null</code>.
  56. */
  57. private Vector pItems = new Vector();
  58. /**
  59. * @serial The index of the selected item in the choice box.
  60. */
  61. private int selectedIndex = -1;
  62. // Listener chain
  63. private ItemListener item_listeners;
  64. /*************************************************************************/
  65. /*
  66. * Constructors
  67. */
  68. /**
  69. * Initializes a new instance of <code>Choice</code>.
  70. *
  71. * @exception HeadlessException If GraphicsEnvironment.isHeadless()
  72. * returns true
  73. */
  74. public Choice()
  75. {
  76. if (GraphicsEnvironment.isHeadless())
  77. throw new HeadlessException ();
  78. }
  79. /*************************************************************************/
  80. /*
  81. * Instance Methods
  82. */
  83. /**
  84. * Returns the number of items in the list.
  85. *
  86. * @return The number of items in the list.
  87. */
  88. public int
  89. getItemCount()
  90. {
  91. return(pItems.size());
  92. }
  93. /*************************************************************************/
  94. /**
  95. * Returns the number of items in the list.
  96. *
  97. * @return The number of items in the list.
  98. *
  99. * @deprecated This method is deprecated in favor of <code>getItemCount</code>.
  100. */
  101. public int
  102. countItems()
  103. {
  104. return(pItems.size());
  105. }
  106. /*************************************************************************/
  107. /**
  108. * Returns the item at the specified index in the list.
  109. *
  110. * @param index The index into the list to return the item from.
  111. *
  112. * @exception ArrayIndexOutOfBoundsException If the index is invalid.
  113. */
  114. public String
  115. getItem(int index)
  116. {
  117. return((String)pItems.elementAt(index));
  118. }
  119. /*************************************************************************/
  120. /**
  121. * Adds the specified item to this choice box.
  122. *
  123. * @param item The item to add.
  124. *
  125. * @exception NullPointerException If the item's value is null
  126. *
  127. * @since 1.1
  128. */
  129. public synchronized void
  130. add(String item)
  131. {
  132. if (item == null)
  133. throw new NullPointerException ("item must be non-null");
  134. pItems.addElement(item);
  135. int i = pItems.size () - 1;
  136. if (peer != null)
  137. {
  138. ChoicePeer cp = (ChoicePeer) peer;
  139. cp.add (item, i);
  140. }
  141. if (i == 0)
  142. select (0);
  143. }
  144. /*************************************************************************/
  145. /**
  146. * Adds the specified item to this choice box.
  147. *
  148. * This method is oboslete since Java 2 platform 1.1. Please use @see add
  149. * instead.
  150. *
  151. * @param item The item to add.
  152. *
  153. * @exception NullPointerException If the item's value is equal to null
  154. */
  155. public synchronized void
  156. addItem(String item)
  157. {
  158. add(item);
  159. }
  160. /*************************************************************************/
  161. /** Inserts an item into this Choice. Existing items are shifted
  162. * upwards. If the new item is the only item, then it is selected.
  163. * If the currently selected item is shifted, then the first item is
  164. * selected. If the currently selected item is not shifted, then it
  165. * remains selected.
  166. *
  167. * @param item The item to add.
  168. * @param index The index at which the item should be inserted.
  169. *
  170. * @exception IllegalArgumentException If index is less than 0
  171. */
  172. public synchronized void
  173. insert(String item, int index)
  174. {
  175. if (index < 0)
  176. throw new IllegalArgumentException ("index may not be less then 0");
  177. if (index > getItemCount ())
  178. index = getItemCount ();
  179. pItems.insertElementAt(item, index);
  180. if (peer != null)
  181. {
  182. ChoicePeer cp = (ChoicePeer) peer;
  183. cp.add (item, index);
  184. }
  185. if (getItemCount () == 1 || selectedIndex >= index)
  186. select (0);
  187. }
  188. /*************************************************************************/
  189. /**
  190. * Removes the specified item from the choice box.
  191. *
  192. * @param item The item to remove.
  193. *
  194. * @exception IllegalArgumentException If the specified item doesn't exist.
  195. */
  196. public synchronized void
  197. remove(String item)
  198. {
  199. int index = pItems.indexOf(item);
  200. if (index == -1)
  201. throw new IllegalArgumentException ("item \""
  202. + item + "\" not found in Choice");
  203. remove(index);
  204. }
  205. /*************************************************************************/
  206. /**
  207. * Removes the item at the specified index from the choice box.
  208. *
  209. * @param index The index of the item to remove.
  210. *
  211. * @exception IndexOutOfBoundsException If the index is not valid.
  212. */
  213. public synchronized void
  214. remove(int index)
  215. {
  216. pItems.removeElementAt(index);
  217. if (peer != null)
  218. {
  219. ChoicePeer cp = (ChoicePeer) peer;
  220. cp.remove (index);
  221. }
  222. if (index == selectedIndex)
  223. select (0);
  224. else if (selectedIndex > index)
  225. --selectedIndex;
  226. }
  227. /*************************************************************************/
  228. /**
  229. * Removes all of the objects from this choice box.
  230. */
  231. public synchronized void
  232. removeAll()
  233. {
  234. int count = getItemCount();
  235. for (int i = 0; i < count; i++)
  236. {
  237. // Always remove 0.
  238. remove(0);
  239. }
  240. }
  241. /*************************************************************************/
  242. /**
  243. * Returns the currently selected item, or null if no item is
  244. * selected.
  245. *
  246. * @return The currently selected item.
  247. */
  248. public synchronized String
  249. getSelectedItem()
  250. {
  251. return (selectedIndex == -1
  252. ? null
  253. : ((String)pItems.elementAt(selectedIndex)));
  254. }
  255. /*************************************************************************/
  256. /**
  257. * Returns an array with one row containing the selected item.
  258. *
  259. * @return An array containing the selected item.
  260. */
  261. public synchronized Object[]
  262. getSelectedObjects()
  263. {
  264. if (selectedIndex == -1)
  265. return null;
  266. Object[] objs = new Object[1];
  267. objs[0] = pItems.elementAt(selectedIndex);
  268. return(objs);
  269. }
  270. /*************************************************************************/
  271. /**
  272. * Returns the index of the selected item.
  273. *
  274. * @return The index of the selected item.
  275. */
  276. public int
  277. getSelectedIndex()
  278. {
  279. return(selectedIndex);
  280. }
  281. /*************************************************************************/
  282. /**
  283. * Forces the item at the specified index to be selected.
  284. *
  285. * @param index The index of the row to make selected.
  286. *
  287. * @exception IllegalArgumentException If the specified index is invalid.
  288. */
  289. public synchronized void
  290. select(int index)
  291. {
  292. if ((index < 0) || (index > getItemCount()))
  293. throw new IllegalArgumentException("Bad index: " + index);
  294. this.selectedIndex = index;
  295. if (peer != null)
  296. {
  297. ChoicePeer cp = (ChoicePeer) peer;
  298. cp.select (index);
  299. }
  300. }
  301. /*************************************************************************/
  302. /**
  303. * Forces the named item to be selected.
  304. *
  305. * @param item The item to be selected.
  306. *
  307. * @exception IllegalArgumentException If the specified item does not exist.
  308. */
  309. public synchronized void
  310. select(String item)
  311. {
  312. int index = pItems.indexOf(item);
  313. if (index >= 0)
  314. select(index);
  315. }
  316. /*************************************************************************/
  317. /**
  318. * Creates the native peer for this object.
  319. */
  320. public void
  321. addNotify()
  322. {
  323. if (peer == null)
  324. peer = getToolkit ().createChoice (this);
  325. super.addNotify ();
  326. }
  327. /*************************************************************************/
  328. /**
  329. * Adds the specified listener to the list of registered listeners for
  330. * this object.
  331. *
  332. * @param listener The listener to add.
  333. */
  334. public synchronized void
  335. addItemListener(ItemListener listener)
  336. {
  337. item_listeners = AWTEventMulticaster.add(item_listeners, listener);
  338. }
  339. /*************************************************************************/
  340. /**
  341. * Removes the specified listener from the list of registered listeners for
  342. * this object.
  343. *
  344. * @param listener The listener to remove.
  345. */
  346. public synchronized void
  347. removeItemListener(ItemListener listener)
  348. {
  349. item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
  350. }
  351. /*************************************************************************/
  352. /**
  353. * Processes this event by invoking <code>processItemEvent()</code> if the
  354. * event is an instance of <code>ItemEvent</code>, otherwise the event
  355. * is passed to the superclass.
  356. *
  357. * @param event The event to process.
  358. */
  359. protected void
  360. processEvent(AWTEvent event)
  361. {
  362. if (event instanceof ItemEvent)
  363. processItemEvent((ItemEvent)event);
  364. else
  365. super.processEvent(event);
  366. }
  367. /*************************************************************************/
  368. /**
  369. * Processes item event by dispatching to any registered listeners.
  370. *
  371. * @param event The event to process.
  372. */
  373. protected void
  374. processItemEvent(ItemEvent event)
  375. {
  376. if (item_listeners != null)
  377. item_listeners.itemStateChanged(event);
  378. }
  379. void
  380. dispatchEventImpl(AWTEvent e)
  381. {
  382. if (e.id <= ItemEvent.ITEM_LAST
  383. && e.id >= ItemEvent.ITEM_FIRST
  384. && (item_listeners != null
  385. || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0))
  386. processEvent(e);
  387. else
  388. super.dispatchEventImpl(e);
  389. }
  390. /*************************************************************************/
  391. /**
  392. * Returns a debugging string for this object.
  393. *
  394. * @return A debugging string for this object.
  395. */
  396. protected String
  397. paramString()
  398. {
  399. return ("selectedIndex=" + selectedIndex + "," + super.paramString());
  400. }
  401. } // class Choice