2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / awt / Choice.java
blob863888c5b37c6f5fbffe38956344335192539f65
1 /* Choice.java -- Java choice button widget.
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt;
41 import java.awt.event.ItemEvent;
42 import java.awt.event.ItemListener;
43 import java.awt.peer.ChoicePeer;
44 import java.io.Serializable;
45 import java.util.EventListener;
46 import java.util.Vector;
48 /**
49 * This class implements a drop down choice list.
51 * @author Aaron M. Renn (arenn@urbanophile.com)
53 public class Choice extends Component implements ItemSelectable, Serializable
57 * Static Variables
60 // Serialization constant
61 private static final long serialVersionUID = -4075310674757313071L;
63 /*************************************************************************/
66 * Instance Variables
69 /**
70 * @serial A list of items for the choice box, which can be <code>null</code>.
72 private Vector pItems = new Vector();
74 /**
75 * @serial The index of the selected item in the choice box.
77 private int selectedIndex = -1;
79 // Listener chain
80 private ItemListener item_listeners;
82 /*************************************************************************/
85 * Constructors
88 /**
89 * Initializes a new instance of <code>Choice</code>.
91 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
92 * returns true
94 public Choice()
96 if (GraphicsEnvironment.isHeadless())
97 throw new HeadlessException ();
100 /*************************************************************************/
103 * Instance Methods
107 * Returns the number of items in the list.
109 * @return The number of items in the list.
111 public int
112 getItemCount()
114 return(pItems.size());
117 /*************************************************************************/
120 * Returns the number of items in the list.
122 * @return The number of items in the list.
124 * @deprecated This method is deprecated in favor of <code>getItemCount</code>.
126 public int
127 countItems()
129 return(pItems.size());
132 /*************************************************************************/
135 * Returns the item at the specified index in the list.
137 * @param index The index into the list to return the item from.
139 * @exception ArrayIndexOutOfBoundsException If the index is invalid.
141 public String
142 getItem(int index)
144 return((String)pItems.elementAt(index));
147 /*************************************************************************/
150 * Adds the specified item to this choice box.
152 * @param item The item to add.
154 * @exception NullPointerException If the item's value is null
156 * @since 1.1
158 public synchronized void
159 add(String item)
161 if (item == null)
162 throw new NullPointerException ("item must be non-null");
164 pItems.addElement(item);
166 int i = pItems.size () - 1;
167 if (peer != null)
169 ChoicePeer cp = (ChoicePeer) peer;
170 cp.add (item, i);
173 if (i == 0)
175 selectedIndex = 0;
176 // We must generate an ItemEvent here
177 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent (
178 new ItemEvent ((ItemSelectable)this,
179 ItemEvent.ITEM_STATE_CHANGED,
180 getItem(0),
181 ItemEvent.SELECTED));
185 /*************************************************************************/
188 * Adds the specified item to this choice box.
190 * This method is oboslete since Java 2 platform 1.1. Please use @see add
191 * instead.
193 * @param item The item to add.
195 * @exception NullPointerException If the item's value is equal to null
197 public synchronized void
198 addItem(String item)
200 add(item);
203 /*************************************************************************/
205 /** Inserts an item into this Choice. Existing items are shifted
206 * upwards. If the new item is the only item, then it is selected.
207 * If the currently selected item is shifted, then the first item is
208 * selected. If the currently selected item is not shifted, then it
209 * remains selected.
211 * @param item The item to add.
212 * @param index The index at which the item should be inserted.
214 * @exception IllegalArgumentException If index is less than 0
216 public synchronized void
217 insert(String item, int index)
219 if (index < 0)
220 throw new IllegalArgumentException ("index may not be less then 0");
222 if (index > getItemCount ())
223 index = getItemCount ();
225 pItems.insertElementAt(item, index);
227 if (peer != null)
229 ChoicePeer cp = (ChoicePeer) peer;
230 cp.add (item, index);
233 if (getItemCount () == 1 || selectedIndex >= index)
235 select (0);
236 // We must generate an ItemEvent here
237 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent (
238 new ItemEvent ((ItemSelectable)this,
239 ItemEvent.ITEM_STATE_CHANGED,
240 getItem(0),
241 ItemEvent.SELECTED));
245 /*************************************************************************/
248 * Removes the specified item from the choice box.
250 * @param item The item to remove.
252 * @exception IllegalArgumentException If the specified item doesn't exist.
254 public synchronized void
255 remove(String item)
257 int index = pItems.indexOf(item);
258 if (index == -1)
259 throw new IllegalArgumentException ("item \""
260 + item + "\" not found in Choice");
261 remove(index);
264 /*************************************************************************/
267 * Removes the item at the specified index from the choice box.
269 * @param index The index of the item to remove.
271 * @exception IndexOutOfBoundsException If the index is not valid.
273 public synchronized void
274 remove(int index)
276 pItems.removeElementAt(index);
278 if (peer != null)
280 ChoicePeer cp = (ChoicePeer) peer;
281 cp.remove (index);
284 if ((index == selectedIndex) && (getItemCount() > 0))
286 select (0);
287 // We must generate an ItemEvent here
288 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent (
289 new ItemEvent ((ItemSelectable)this,
290 ItemEvent.ITEM_STATE_CHANGED,
291 getItem(0),
292 ItemEvent.SELECTED));
294 else if (selectedIndex > index)
295 --selectedIndex;
298 /*************************************************************************/
301 * Removes all of the objects from this choice box.
303 public synchronized void
304 removeAll()
306 int count = getItemCount();
308 if (count <= 0)
309 return;
311 ChoicePeer cp = (ChoicePeer) peer;
313 // Select the first item to prevent an spurious ItemEvent to be generated
314 if (cp != null)
316 cp.select (0);
317 selectedIndex = 0; // Just to keep consistent
320 for (int i = (count - 1); i >= 0; i--)
322 // Always remove the last to avoid generation of ItemEvents.
323 pItems.removeElementAt(i);
324 if (cp != null)
325 cp.remove (i);
328 selectedIndex = -1;
331 /*************************************************************************/
334 * Returns the currently selected item, or null if no item is
335 * selected.
337 * @return The currently selected item.
339 public synchronized String
340 getSelectedItem()
342 return (selectedIndex == -1
343 ? null
344 : ((String)pItems.elementAt(selectedIndex)));
347 /*************************************************************************/
350 * Returns an array with one row containing the selected item.
352 * @return An array containing the selected item.
354 public synchronized Object[]
355 getSelectedObjects()
357 if (selectedIndex == -1)
358 return null;
360 Object[] objs = new Object[1];
361 objs[0] = pItems.elementAt(selectedIndex);
363 return(objs);
366 /*************************************************************************/
369 * Returns the index of the selected item.
371 * @return The index of the selected item.
373 public int
374 getSelectedIndex()
376 return(selectedIndex);
379 /*************************************************************************/
382 * Forces the item at the specified index to be selected.
384 * @param index The index of the row to make selected.
386 * @exception IllegalArgumentException If the specified index is invalid.
388 public synchronized void
389 select(int index)
391 if ((index < 0) || (index > getItemCount()))
392 throw new IllegalArgumentException("Bad index: " + index);
394 this.selectedIndex = index;
395 if (peer != null)
397 ChoicePeer cp = (ChoicePeer) peer;
398 cp.select (index);
402 /*************************************************************************/
405 * Forces the named item to be selected.
407 * @param item The item to be selected.
409 * @exception IllegalArgumentException If the specified item does not exist.
411 public synchronized void
412 select(String item)
414 int index = pItems.indexOf(item);
415 if (index >= 0)
416 select(index);
419 /*************************************************************************/
422 * Creates the native peer for this object.
424 public void
425 addNotify()
427 if (peer == null)
428 peer = getToolkit ().createChoice (this);
429 super.addNotify ();
432 /*************************************************************************/
435 * Adds the specified listener to the list of registered listeners for
436 * this object.
438 * @param listener The listener to add.
440 public synchronized void
441 addItemListener(ItemListener listener)
443 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
446 /*************************************************************************/
449 * Removes the specified listener from the list of registered listeners for
450 * this object.
452 * @param listener The listener to remove.
454 public synchronized void
455 removeItemListener(ItemListener listener)
457 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
460 /*************************************************************************/
463 * Processes this event by invoking <code>processItemEvent()</code> if the
464 * event is an instance of <code>ItemEvent</code>, otherwise the event
465 * is passed to the superclass.
467 * @param event The event to process.
469 protected void
470 processEvent(AWTEvent event)
472 if (event instanceof ItemEvent)
473 processItemEvent((ItemEvent)event);
474 else
475 super.processEvent(event);
478 /*************************************************************************/
481 * Processes item event by dispatching to any registered listeners.
483 * @param event The event to process.
485 protected void
486 processItemEvent(ItemEvent event)
488 if (item_listeners != null)
489 item_listeners.itemStateChanged(event);
492 void
493 dispatchEventImpl(AWTEvent e)
495 if (e.id <= ItemEvent.ITEM_LAST
496 && e.id >= ItemEvent.ITEM_FIRST
497 && (item_listeners != null
498 || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0))
499 processEvent(e);
500 else
501 super.dispatchEventImpl(e);
504 /*************************************************************************/
507 * Returns a debugging string for this object.
509 * @return A debugging string for this object.
511 protected String
512 paramString()
514 return ("selectedIndex=" + selectedIndex + "," + super.paramString());
518 * Returns an array of all the objects currently registered as FooListeners
519 * upon this Choice. FooListeners are registered using the addFooListener
520 * method.
522 * @exception ClassCastException If listenerType doesn't specify a class or
523 * interface that implements java.util.EventListener.
525 * @since 1.3
527 public EventListener[] getListeners (Class listenerType)
529 if (listenerType == ItemListener.class)
530 return AWTEventMulticaster.getListeners (item_listeners, listenerType);
532 return super.getListeners (listenerType);
536 * Returns all registered item listeners.
538 * @since 1.4
540 public ItemListener[] getItemListeners ()
542 return (ItemListener[]) getListeners (ItemListener.class);
544 } // class Choice