1 /* Copyright (C) 2000 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 import java.awt.event.*;
11 import java.awt.peer.ChoicePeer;
12 import java.util.ArrayList;
14 /** This component lets the user choose an item from a list of
16 * @author Tom Tromey <tromey@redhat.com>
17 * @date December 25, 2000
19 public class Choice extends Component implements ItemSelectable
21 /** Create a new Choice object. */
24 items = new ArrayList ();
28 /** Add a new item to this Choice object. If the item is the first
29 * item on the list, then it is selected.
30 * @param item The new item; must be non-null.
32 public synchronized void add (String item)
35 throw new IllegalArgumentException ("item must be non-null");
38 int i = items.size () - 1;
41 ChoicePeer cp = (ChoicePeer) peer;
49 /** Add a new item to this Choice object. This is the same as the
51 public void addItem (String item)
56 /** Add a listener for item events.
57 * @param listener The listener to add.
59 public synchronized void addItemListener (ItemListener listener)
61 listeners = AWTEventMulticaster.add (listeners, listener);
64 /** This creates the component's peer. */
65 public void addNotify ()
68 peer = getToolkit ().createChoice (this);
71 /** Returns number of items.
74 public int countItems ()
76 return getItemCount ();
79 /** Returns an item from this choice.
80 * @param index Index of the item. Indices start at zero.
82 public String getItem (int index)
84 return (String) items.get (index);
87 /** Returns number of items in Choice. */
88 public int getItemCount ()
93 /** Returns index of selected item; -1 if no item is selected. */
94 public int getSelectedIndex ()
99 /** Returns currently selected item; null if no item is selected. */
100 public synchronized String getSelectedItem ()
102 return selected == -1 ? null : (String) items.get (selected);
105 /** Returns the currently selected item. */
106 public synchronized Object[] getSelectedObjects ()
108 // The JCL says this can return null but that breaks the contract
109 // for ItemSelectable.
114 r[0] = items.get (selected);
121 /** Inserts an item into this Choice. Existing items are shifted
122 * upwards. If the new item is the only item, then it is selected.
123 * If the currently selected item is shifted, then the first item is
124 * selected. If the currently selected item is not shifted, then it
126 * @param item The new item
127 * @param index The position at which to insert it.
129 public synchronized void insert (String item, int index)
131 if (index > items.size ())
132 index = items.size ();
133 items.add (index, item);
137 ChoicePeer cp = (ChoicePeer) peer;
138 cp.add (item, index);
141 if (items.size () == 1 || selected >= index)
145 /** Generates a String representation of this Choice's state. */
146 protected String paramString ()
149 + "selected=" + selected
153 /** Process an event for this Choice
154 * @param event The event the process.
156 protected void processEvent (AWTEvent event)
158 if (event instanceof ItemEvent)
159 processItemEvent ((ItemEvent) event);
161 super.processEvent (event);
164 /** Process an item event for this Choice.
165 * @param event The ItemEvent to process
167 protected void processItemEvent (ItemEvent event)
169 if (listeners != null)
170 listeners.itemStateChanged (event);
173 /** Remove an item from this Choice. If several matches exist, the
174 * first one is removed. If the removed item is selected, the the
175 * first item is selected.
176 * @param item The item string.
178 public synchronized void remove (String item)
180 int size = items.size ();
181 for (int i = 0; i < size; ++i)
183 if (item.equals (items.get (i)))
189 throw new IllegalArgumentException ("item \"" + item + "\" not in Choice");
192 /** Remove an item from this Choice. If the removed item is
193 * selected, the the first item is selected.
194 * @param index Index of the item to remove
196 public synchronized void remove (int index)
198 items.remove (index);
202 ChoicePeer cp = (ChoicePeer) peer;
206 if (index == selected)
208 else if (selected > index)
212 /** Remove all items from this choice. */
213 public synchronized void removeAll ()
215 int oldsize = items.size ();
221 ChoicePeer cp = (ChoicePeer) peer;
222 for (int i = 0; i < oldsize; ++i)
224 // Always remove item 0.
230 /** Remove an item listener.
231 * @param listener Item listener to remove.
233 public synchronized void removeItemListener (ItemListener listener)
235 listeners = AWTEventMulticaster.remove (listeners, listener);
238 /** Select an item in this Choice.
239 * @param item Name of the item to select.
241 public synchronized void select (String item)
243 int size = items.size ();
244 for (int i = 0; i < size; ++i)
246 if (item.equals (items.get (i)))
254 /** Select an item in this choice.
255 * @param index Index of item to select.
257 public synchronized void select (int index)
259 if (index < 0 || index > items.size ())
260 throw new IllegalArgumentException ("index out of range");
264 ChoicePeer cp = (ChoicePeer) peer;
269 private ItemListener listeners;
273 // Index of selected item.