1 /* Choice.java -- Java choice button widget.
2 Copyright (C) 1999, 2000, 2001, 2002, 2004 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., 51 Franklin Street, Fifth Floor, 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
;
48 import javax
.accessibility
.Accessible
;
49 import javax
.accessibility
.AccessibleAction
;
50 import javax
.accessibility
.AccessibleContext
;
51 import javax
.accessibility
.AccessibleRole
;
54 * This class implements a drop down choice list.
56 * @author Aaron M. Renn (arenn@urbanophile.com)
58 public class Choice
extends Component
59 implements ItemSelectable
, Serializable
, Accessible
66 // Serialization constant
67 private static final long serialVersionUID
= -4075310674757313071L;
69 /*************************************************************************/
76 * @serial A list of items for the choice box, which can be <code>null</code>.
77 * This is package-private to avoid an accessor method.
79 Vector pItems
= new Vector();
82 * @serial The index of the selected item in the choice box.
84 private int selectedIndex
= -1;
87 private ItemListener item_listeners
;
90 * This class provides accessibility support for the
93 * @author Jerry Quinn (jlquinn@optonline.net)
94 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
96 protected class AccessibleAWTChoice
97 extends AccessibleAWTComponent
98 implements AccessibleAction
102 * Serialization constant to match JDK 1.5
104 private static final long serialVersionUID
= 7175603582428509322L;
107 * Default constructor which simply calls the
108 * super class for generic component accessibility
111 public AccessibleAWTChoice()
117 * Returns an implementation of the <code>AccessibleAction</code>
118 * interface for this accessible object. In this case, the
119 * current instance is simply returned (with a more appropriate
120 * type), as it also implements the accessible action as well as
123 * @return the accessible action associated with this context.
124 * @see javax.accessibility.AccessibleAction
126 public AccessibleAction
getAccessibleAction()
132 * Returns the role of this accessible object.
134 * @return the instance of <code>AccessibleRole</code>,
135 * which describes this object.
136 * @see javax.accessibility.AccessibleRole
138 public AccessibleRole
getAccessibleRole()
140 return AccessibleRole
.COMBO_BOX
;
144 * Returns the number of actions associated with this accessible
145 * object. In this case, it is the number of choices available.
147 * @return the number of choices available.
148 * @see javax.accessibility.AccessibleAction#getAccessibleActionCount()
150 public int getAccessibleActionCount()
152 return pItems
.size();
156 * Returns a description of the action with the supplied id.
157 * In this case, it is the text used in displaying the particular
160 * @param i the id of the choice whose description should be
162 * @return the <code>String</code> used to describe the choice.
163 * @see javax.accessibility.AccessibleAction#getAccessibleActionDescription(int)
165 public String
getAccessibleActionDescription(int i
)
167 return (String
) pItems
.get(i
);
171 * Executes the action with the specified id. In this case,
172 * calling this method provides the same behaviour as would
173 * choosing a choice from the list in a visual manner.
175 * @param i the id of the choice to select.
176 * @return true if a valid choice was specified.
177 * @see javax.accessibility.AccessibleAction#doAccessibleAction(int)
179 public boolean doAccessibleAction(int i
)
181 if (i
< 0 || i
>= pItems
.size())
184 Choice
.this.processItemEvent(new ItemEvent(Choice
.this,
185 ItemEvent
.ITEM_STATE_CHANGED
,
186 this, ItemEvent
.SELECTED
));
191 /*************************************************************************/
198 * Initializes a new instance of <code>Choice</code>.
200 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
205 if (GraphicsEnvironment
.isHeadless())
206 throw new HeadlessException ();
209 /*************************************************************************/
216 * Returns the number of items in the list.
218 * @return The number of items in the list.
223 return countItems ();
226 /*************************************************************************/
229 * Returns the number of items in the list.
231 * @return The number of items in the list.
233 * @deprecated This method is deprecated in favor of <code>getItemCount</code>.
238 return(pItems
.size());
241 /*************************************************************************/
244 * Returns the item at the specified index in the list.
246 * @param index The index into the list to return the item from.
248 * @exception ArrayIndexOutOfBoundsException If the index is invalid.
253 return((String
)pItems
.elementAt(index
));
256 /*************************************************************************/
259 * Adds the specified item to this choice box.
261 * @param item The item to add.
263 * @exception NullPointerException If the item's value is null
267 public synchronized void
271 throw new NullPointerException ("item must be non-null");
273 pItems
.addElement(item
);
275 int i
= pItems
.size () - 1;
278 ChoicePeer cp
= (ChoicePeer
) peer
;
281 else if (selectedIndex
== -1)
285 /*************************************************************************/
288 * Adds the specified item to this choice box.
290 * This method is oboslete since Java 2 platform 1.1. Please use @see add
293 * @param item The item to add.
295 * @exception NullPointerException If the item's value is equal to null
297 public synchronized void
303 /*************************************************************************/
305 /** Inserts an item into this Choice. Existing items are shifted
306 * upwards. If the new item is the only item, then it is selected.
307 * If the currently selected item is shifted, then the first item is
308 * selected. If the currently selected item is not shifted, then it
311 * @param item The item to add.
312 * @param index The index at which the item should be inserted.
314 * @exception IllegalArgumentException If index is less than 0
316 public synchronized void
317 insert(String item
, int index
)
320 throw new IllegalArgumentException ("index may not be less then 0");
322 if (index
> getItemCount ())
323 index
= getItemCount ();
325 pItems
.insertElementAt(item
, index
);
329 ChoicePeer cp
= (ChoicePeer
) peer
;
330 cp
.add (item
, index
);
332 else if (selectedIndex
== -1 || selectedIndex
>= index
)
336 /*************************************************************************/
339 * Removes the specified item from the choice box.
341 * @param item The item to remove.
343 * @exception IllegalArgumentException If the specified item doesn't exist.
345 public synchronized void
348 int index
= pItems
.indexOf(item
);
350 throw new IllegalArgumentException ("item \""
351 + item
+ "\" not found in Choice");
355 /*************************************************************************/
358 * Removes the item at the specified index from the choice box.
360 * @param index The index of the item to remove.
362 * @exception IndexOutOfBoundsException If the index is not valid.
364 public synchronized void
367 if ((index
< 0) || (index
> getItemCount()))
368 throw new IllegalArgumentException("Bad index: " + index
);
370 pItems
.removeElementAt(index
);
374 ChoicePeer cp
= (ChoicePeer
) peer
;
379 if (getItemCount() == 0)
381 else if (index
== selectedIndex
)
385 if (selectedIndex
> index
)
389 /*************************************************************************/
392 * Removes all of the objects from this choice box.
394 public synchronized void
397 if (getItemCount() <= 0)
400 pItems
.removeAllElements ();
404 ChoicePeer cp
= (ChoicePeer
) peer
;
411 /*************************************************************************/
414 * Returns the currently selected item, or null if no item is
417 * @return The currently selected item.
419 public synchronized String
422 return (selectedIndex
== -1
424 : ((String
)pItems
.elementAt(selectedIndex
)));
427 /*************************************************************************/
430 * Returns an array with one row containing the selected item.
432 * @return An array containing the selected item.
434 public synchronized Object
[]
437 if (selectedIndex
== -1)
440 Object
[] objs
= new Object
[1];
441 objs
[0] = pItems
.elementAt(selectedIndex
);
446 /*************************************************************************/
449 * Returns the index of the selected item.
451 * @return The index of the selected item.
456 return(selectedIndex
);
459 /*************************************************************************/
462 * Forces the item at the specified index to be selected.
464 * @param index The index of the row to make selected.
466 * @exception IllegalArgumentException If the specified index is invalid.
468 public synchronized void
471 if ((index
< 0) || (index
>= getItemCount()))
472 throw new IllegalArgumentException("Bad index: " + index
);
474 if (pItems
.size() > 0) {
475 selectedIndex
= index
;
476 ChoicePeer cp
= (ChoicePeer
) peer
;
483 /*************************************************************************/
486 * Forces the named item to be selected.
488 * @param item The item to be selected.
490 * @exception IllegalArgumentException If the specified item does not exist.
492 public synchronized void
495 int index
= pItems
.indexOf(item
);
500 /*************************************************************************/
503 * Creates the native peer for this object.
509 peer
= getToolkit ().createChoice (this);
513 /*************************************************************************/
516 * Adds the specified listener to the list of registered listeners for
519 * @param listener The listener to add.
521 public synchronized void
522 addItemListener(ItemListener listener
)
524 item_listeners
= AWTEventMulticaster
.add(item_listeners
, listener
);
527 /*************************************************************************/
530 * Removes the specified listener from the list of registered listeners for
533 * @param listener The listener to remove.
535 public synchronized void
536 removeItemListener(ItemListener listener
)
538 item_listeners
= AWTEventMulticaster
.remove(item_listeners
, listener
);
541 /*************************************************************************/
544 * Processes this event by invoking <code>processItemEvent()</code> if the
545 * event is an instance of <code>ItemEvent</code>, otherwise the event
546 * is passed to the superclass.
548 * @param event The event to process.
551 processEvent(AWTEvent event
)
553 if (event
instanceof ItemEvent
)
554 processItemEvent((ItemEvent
)event
);
556 super.processEvent(event
);
559 /*************************************************************************/
562 * Processes item event by dispatching to any registered listeners.
564 * @param event The event to process.
567 processItemEvent(ItemEvent event
)
569 int index
= pItems
.indexOf((String
) event
.getItem());
570 // Don't call back into the peers when selecting index here
571 if (event
.getStateChange() == ItemEvent
.SELECTED
)
572 this.selectedIndex
= index
;
573 if (item_listeners
!= null)
574 item_listeners
.itemStateChanged(event
);
577 /*************************************************************************/
580 * Returns a debugging string for this object.
582 * @return A debugging string for this object.
587 return ("selectedIndex=" + selectedIndex
+ "," + super.paramString());
591 * Returns an array of all the objects currently registered as FooListeners
592 * upon this Choice. FooListeners are registered using the addFooListener
595 * @exception ClassCastException If listenerType doesn't specify a class or
596 * interface that implements java.util.EventListener.
600 public EventListener
[] getListeners (Class listenerType
)
602 if (listenerType
== ItemListener
.class)
603 return AWTEventMulticaster
.getListeners (item_listeners
, listenerType
);
605 return super.getListeners (listenerType
);
609 * Returns all registered item listeners.
613 public ItemListener
[] getItemListeners ()
615 return (ItemListener
[]) getListeners (ItemListener
.class);
619 * Gets the AccessibleContext associated with this <code>Choice</code>.
620 * The context is created, if necessary.
622 * @return the associated context
624 public AccessibleContext
getAccessibleContext()
626 /* Create the context if this is the first request */
627 if (accessibleContext
== null)
628 accessibleContext
= new AccessibleAWTChoice();
629 return accessibleContext
;