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)
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
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
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. */
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
;
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
60 // Serialization constant
61 private static final long serialVersionUID
= -4075310674757313071L;
63 /*************************************************************************/
70 * @serial A list of items for the choice box, which can be <code>null</code>.
72 private Vector pItems
= new Vector();
75 * @serial The index of the selected item in the choice box.
77 private int selectedIndex
= -1;
80 private ItemListener item_listeners
;
82 /*************************************************************************/
89 * Initializes a new instance of <code>Choice</code>.
91 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
96 if (GraphicsEnvironment
.isHeadless())
97 throw new HeadlessException ();
100 /*************************************************************************/
107 * Returns the number of items in the list.
109 * @return The number of items in the list.
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>.
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.
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
158 public synchronized void
162 throw new NullPointerException ("item must be non-null");
164 pItems
.addElement(item
);
166 int i
= pItems
.size () - 1;
169 ChoicePeer cp
= (ChoicePeer
) peer
;
176 // We must generate an ItemEvent here
177 Toolkit
.getDefaultToolkit().getSystemEventQueue().postEvent (
178 new ItemEvent ((ItemSelectable
)this,
179 ItemEvent
.ITEM_STATE_CHANGED
,
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
193 * @param item The item to add.
195 * @exception NullPointerException If the item's value is equal to null
197 public synchronized void
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
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
)
220 throw new IllegalArgumentException ("index may not be less then 0");
222 if (index
> getItemCount ())
223 index
= getItemCount ();
225 pItems
.insertElementAt(item
, index
);
229 ChoicePeer cp
= (ChoicePeer
) peer
;
230 cp
.add (item
, index
);
233 if (getItemCount () == 1 || selectedIndex
>= index
)
236 // We must generate an ItemEvent here
237 Toolkit
.getDefaultToolkit().getSystemEventQueue().postEvent (
238 new ItemEvent ((ItemSelectable
)this,
239 ItemEvent
.ITEM_STATE_CHANGED
,
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
257 int index
= pItems
.indexOf(item
);
259 throw new IllegalArgumentException ("item \""
260 + item
+ "\" not found in Choice");
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
276 pItems
.removeElementAt(index
);
280 ChoicePeer cp
= (ChoicePeer
) peer
;
284 if ((index
== selectedIndex
) && (getItemCount() > 0))
287 // We must generate an ItemEvent here
288 Toolkit
.getDefaultToolkit().getSystemEventQueue().postEvent (
289 new ItemEvent ((ItemSelectable
)this,
290 ItemEvent
.ITEM_STATE_CHANGED
,
292 ItemEvent
.SELECTED
));
294 else if (selectedIndex
> index
)
298 /*************************************************************************/
301 * Removes all of the objects from this choice box.
303 public synchronized void
306 int count
= getItemCount();
311 ChoicePeer cp
= (ChoicePeer
) peer
;
313 // Select the first item to prevent an spurious ItemEvent to be generated
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
);
331 /*************************************************************************/
334 * Returns the currently selected item, or null if no item is
337 * @return The currently selected item.
339 public synchronized String
342 return (selectedIndex
== -1
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
[]
357 if (selectedIndex
== -1)
360 Object
[] objs
= new Object
[1];
361 objs
[0] = pItems
.elementAt(selectedIndex
);
366 /*************************************************************************/
369 * Returns the index of the selected item.
371 * @return The index of the selected item.
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
391 if ((index
< 0) || (index
> getItemCount()))
392 throw new IllegalArgumentException("Bad index: " + index
);
394 this.selectedIndex
= index
;
397 ChoicePeer cp
= (ChoicePeer
) peer
;
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
414 int index
= pItems
.indexOf(item
);
419 /*************************************************************************/
422 * Creates the native peer for this object.
428 peer
= getToolkit ().createChoice (this);
432 /*************************************************************************/
435 * Adds the specified listener to the list of registered listeners for
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
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.
470 processEvent(AWTEvent event
)
472 if (event
instanceof ItemEvent
)
473 processItemEvent((ItemEvent
)event
);
475 super.processEvent(event
);
478 /*************************************************************************/
481 * Processes item event by dispatching to any registered listeners.
483 * @param event The event to process.
486 processItemEvent(ItemEvent event
)
488 if (item_listeners
!= null)
489 item_listeners
.itemStateChanged(event
);
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))
501 super.dispatchEventImpl(e
);
504 /*************************************************************************/
507 * Returns a debugging string for this object.
509 * @return A debugging string for this object.
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
522 * @exception ClassCastException If listenerType doesn't specify a class or
523 * interface that implements java.util.EventListener.
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.
540 public ItemListener
[] getItemListeners ()
542 return (ItemListener
[]) getListeners (ItemListener
.class);