source: trunk/src/gcc/libjava/java/awt/Choice.java@ 1213

Last change on this file since 1213 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 10.5 KB
Line 
1/* Choice.java -- Java choice button widget.
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.peer.ChoicePeer;
42import java.awt.peer.ComponentPeer;
43import java.awt.event.ItemEvent;
44import java.awt.event.ItemListener;
45import java.io.Serializable;
46import java.util.Vector;
47
48/**
49 * This class implements a drop down choice list.
50 *
51 * @author Aaron M. Renn ([email protected])
52 */
53public class Choice extends Component implements ItemSelectable, Serializable
54{
55
56/*
57 * Static Variables
58 */
59
60// Serialization constant
61private static final long serialVersionUID = -4075310674757313071L;
62
63/*************************************************************************/
64
65/*
66 * Instance Variables
67 */
68
69/**
70 * @serial A list of items for the choice box, which can be <code>null</code>.
71 */
72private Vector pItems = new Vector();
73
74/**
75 * @serial The index of the selected item in the choice box.
76 */
77private int selectedIndex = -1;
78
79// Listener chain
80private ItemListener item_listeners;
81
82/*************************************************************************/
83
84/*
85 * Constructors
86 */
87
88/**
89 * Initializes a new instance of <code>Choice</code>.
90 */
91public
92Choice()
93{
94}
95
96/*************************************************************************/
97
98/*
99 * Instance Methods
100 */
101
102/**
103 * Returns the number of items in the list.
104 *
105 * @return The number of items in the list.
106 */
107public int
108getItemCount()
109{
110 return(pItems.size());
111}
112
113/*************************************************************************/
114
115/**
116 * Returns the number of items in the list.
117 *
118 * @return The number of items in the list.
119 *
120 * @deprecated This method is deprecated in favor of <code>getItemCount</code>.
121 */
122public int
123countItems()
124{
125 return(pItems.size());
126}
127
128/*************************************************************************/
129
130/**
131 * Returns the item at the specified index in the list.
132 *
133 * @param index The index into the list to return the item from.
134 *
135 * @exception ArrayIndexOutOfBoundsException If the index is invalid.
136 */
137public String
138getItem(int index)
139{
140 return((String)pItems.elementAt(index));
141}
142
143/*************************************************************************/
144
145/**
146 * Adds the specified item to this choice box.
147 *
148 * @param item The item to add.
149 */
150public synchronized void
151add(String item)
152{
153 if (item == null)
154 throw new IllegalArgumentException ("item must be non-null");
155
156 pItems.addElement(item);
157
158 int i = pItems.size () - 1;
159 if (peer != null)
160 {
161 ChoicePeer cp = (ChoicePeer) peer;
162 cp.add (item, i);
163 }
164
165 if (i == 0)
166 select (0);
167}
168
169/*************************************************************************/
170
171/**
172 * Adds the specified item to this choice box.
173 *
174 * @param item The item to add.
175 */
176public synchronized void
177addItem(String item)
178{
179 add(item);
180}
181
182/*************************************************************************/
183
184/** Inserts an item into this Choice. Existing items are shifted
185 * upwards. If the new item is the only item, then it is selected.
186 * If the currently selected item is shifted, then the first item is
187 * selected. If the currently selected item is not shifted, then it
188 * remains selected.
189 *
190 * @param item The item to add.
191 * @param index The index at which the item should be inserted.
192 */
193public synchronized void
194insert(String item, int index)
195{
196 if (index > getItemCount ())
197 index = getItemCount ();
198
199 pItems.insertElementAt(item, index);
200
201 if (peer != null)
202 {
203 ChoicePeer cp = (ChoicePeer) peer;
204 cp.add (item, index);
205 }
206
207 if (getItemCount () == 1 || selectedIndex >= index)
208 select (0);
209}
210
211/*************************************************************************/
212
213/**
214 * Removes the specified item from the choice box.
215 *
216 * @param item The item to remove.
217 *
218 * @param IllegalArgumentException If the specified item doesn't exist.
219 */
220public synchronized void
221remove(String item)
222{
223 int index = pItems.indexOf(item);
224 if (index == -1)
225 throw new IllegalArgumentException ("item \""
226 + item + "\" not found in Choice");
227 remove(index);
228}
229
230/*************************************************************************/
231
232/**
233 * Removes the item at the specified index from the choice box.
234 *
235 * @param index The index of the item to remove.
236 *
237 * @exception ArrayIndexOutOfBoundException If the index is not valid.
238 */
239public synchronized void
240remove(int index)
241{
242 pItems.removeElementAt(index);
243
244 if (peer != null)
245 {
246 ChoicePeer cp = (ChoicePeer) peer;
247 cp.remove (index);
248 }
249
250 if (index == selectedIndex)
251 select (0);
252 else if (selectedIndex > index)
253 --selectedIndex;
254}
255
256/*************************************************************************/
257
258/**
259 * Removes all of the objects from this choice box.
260 */
261public synchronized void
262removeAll()
263{
264 int count = getItemCount();
265
266 for (int i = 0; i < count; i++)
267 {
268 // Always remove 0.
269 remove(0);
270 }
271}
272
273/*************************************************************************/
274
275/**
276 * Returns the currently selected item, or null if no item is
277 * selected.
278 *
279 * @return The currently selected item.
280 */
281public synchronized String
282getSelectedItem()
283{
284 return (selectedIndex == -1
285 ? null
286 : ((String)pItems.elementAt(selectedIndex)));
287}
288
289/*************************************************************************/
290
291/**
292 * Returns an array with one row containing the selected item.
293 *
294 * @return An array containing the selected item.
295 */
296public synchronized Object[]
297getSelectedObjects()
298{
299 if (selectedIndex == -1)
300 return null;
301
302 Object[] objs = new Object[1];
303 objs[0] = pItems.elementAt(selectedIndex);
304
305 return(objs);
306}
307
308/*************************************************************************/
309
310/**
311 * Returns the index of the selected item.
312 *
313 * @return The index of the selected item.
314 */
315public int
316getSelectedIndex()
317{
318 return(selectedIndex);
319}
320
321/*************************************************************************/
322
323/**
324 * Forces the item at the specified index to be selected.
325 *
326 * @param index The index of the row to make selected.
327 *
328 * @param IllegalArgumentException If the specified index is invalid.
329 */
330public synchronized void
331select(int index)
332{
333 if ((index < 0) || (index > getItemCount()))
334 throw new IllegalArgumentException("Bad index: " + index);
335
336 this.selectedIndex = index;
337 if (peer != null)
338 {
339 ChoicePeer cp = (ChoicePeer) peer;
340 cp.select (index);
341 }
342}
343
344/*************************************************************************/
345
346/**
347 * Forces the named item to be selected.
348 *
349 * @param item The item to be selected.
350 *
351 * @exception IllegalArgumentException If the specified item does not exist.
352 */
353public synchronized void
354select(String item)
355{
356 int index = pItems.indexOf(item);
357 if (index >= 0)
358 select(index);
359}
360
361/*************************************************************************/
362
363/**
364 * Creates the native peer for this object.
365 */
366public void
367addNotify()
368{
369 if (peer == null)
370 peer = getToolkit ().createChoice (this);
371 super.addNotify ();
372}
373
374/*************************************************************************/
375
376/**
377 * Adds the specified listener to the list of registered listeners for
378 * this object.
379 *
380 * @param listener The listener to add.
381 */
382public synchronized void
383addItemListener(ItemListener listener)
384{
385 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
386}
387
388/*************************************************************************/
389
390/**
391 * Removes the specified listener from the list of registered listeners for
392 * this object.
393 *
394 * @param listener The listener to remove.
395 */
396public synchronized void
397removeItemListener(ItemListener listener)
398{
399 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
400}
401
402/*************************************************************************/
403
404/**
405 * Processes this event by invoking <code>processItemEvent()</code> if the
406 * event is an instance of <code>ItemEvent</code>, otherwise the event
407 * is passed to the superclass.
408 *
409 * @param event The event to process.
410 */
411protected void
412processEvent(AWTEvent event)
413{
414 if (event instanceof ItemEvent)
415 processItemEvent((ItemEvent)event);
416 else
417 super.processEvent(event);
418}
419
420/*************************************************************************/
421
422/**
423 * Processes item event by dispatching to any registered listeners.
424 *
425 * @param event The event to process.
426 */
427protected void
428processItemEvent(ItemEvent event)
429{
430 if (item_listeners != null)
431 item_listeners.itemStateChanged(event);
432}
433
434/*************************************************************************/
435
436/**
437 * Returns a debugging string for this object.
438 *
439 * @return A debugging string for this object.
440 */
441protected String
442paramString()
443{
444 return ("selectedIndex=" + selectedIndex + "," + super.paramString());
445}
446
447} // class Choice
Note: See TracBrowser for help on using the repository browser.