OSDN Git Service

* java/awt/MenuContainer.java: Fixed typo.
[pf3gnuchains/gcc-fork.git] / libjava / java / awt / Choice.java
1 /* Copyright (C) 2000  Free Software Foundation
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package java.awt;
10 import java.awt.event.*;
11 import java.awt.peer.ChoicePeer;
12 import java.util.ArrayList;
13
14 /** This component lets the user choose an item from a list of
15  * Strings.
16  * @author Tom Tromey <tromey@redhat.com>
17  * @date December 25, 2000
18  */
19 public class Choice extends Component implements ItemSelectable
20 {
21   /** Create a new Choice object.  */
22   public Choice ()
23   {
24     items = new ArrayList ();
25     selected = -1;
26   }
27
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.
31    */
32   public synchronized void add (String item)
33   {
34     if (item == null)
35       throw new IllegalArgumentException ("item must be non-null");
36     items.add (item);
37
38     int i = items.size () - 1;
39     if (peer != null)
40       {
41         ChoicePeer cp = (ChoicePeer) peer;
42         cp.add (item, i);
43       }
44
45     if (i == 0)
46       select (0);
47   }
48
49   /** Add a new item to this Choice object.  This is the same as the
50    * add method.  */
51   public void addItem (String item)
52   {
53     add (item);
54   }
55
56   /** Add a listener for item events.
57    * @param listener The listener to add.
58    */
59   public synchronized void addItemListener (ItemListener listener)
60   {
61     listeners = AWTEventMulticaster.add (listeners, listener);
62   }
63
64   /** This creates the component's peer.  */
65   public void addNotify ()
66   {
67     if (peer == null)
68       peer = getToolkit ().createChoice (this);
69   }
70
71   /** Returns number of items.
72    * @deprecated
73    */
74   public int countItems ()
75   {
76     return getItemCount ();
77   }
78
79   /** Returns an item from this choice.
80    * @param index Index of the item.  Indices start at zero.
81    */
82   public String getItem (int index)
83   {
84     return (String) items.get (index);
85   }
86
87   /** Returns number of items in Choice.  */
88   public int getItemCount ()
89   {
90     return items.size ();
91   }
92
93   /** Returns index of selected item; -1 if no item is selected.  */
94   public int getSelectedIndex ()
95   {
96     return selected;
97   }
98
99   /** Returns currently selected item; null if no item is selected.  */
100   public synchronized String getSelectedItem ()
101   {
102     return selected == -1 ? null : (String) items.get (selected);
103   }
104
105   /** Returns the currently selected item.  */
106   public synchronized Object[] getSelectedObjects ()
107   {
108     // The JCL says this can return null but that breaks the contract
109     // for ItemSelectable.
110     Object[] r;
111     if (selected != -1)
112       {
113         r = new Object[1];
114         r[0] = items.get (selected);
115       }
116     else
117       r = new Object[0];
118     return r;
119   }
120
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
125    * remains selected.
126    * @param item The new item
127    * @param index The position at which to insert it.
128    */
129   public synchronized void insert (String item, int index)
130   {
131     if (index > items.size ())
132       index = items.size ();
133     items.add (index, item);
134
135     if (peer != null)
136       {
137         ChoicePeer cp = (ChoicePeer) peer;
138         cp.add (item, index);
139       }
140
141     if (items.size () == 1 || selected >= index)
142       select (0);
143   }
144
145   /** Generates a String representation of this Choice's state.  */
146   protected String paramString ()
147   {
148     return ("Choice["
149             + "selected=" + selected
150             + "]");
151   }
152
153   /** Process an event for this Choice
154    * @param event The event the process.
155    */
156   protected void processEvent (AWTEvent event)
157   {
158     if (event instanceof ItemEvent)
159       processItemEvent ((ItemEvent) event);
160     else
161       super.processEvent (event);
162   }
163
164   /** Process an item event for this Choice.
165    * @param event The ItemEvent to process
166    */
167   protected void processItemEvent (ItemEvent event)
168   {
169     if (listeners != null)
170       listeners.itemStateChanged (event);
171   }
172
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.
177    */
178   public synchronized void remove (String item)
179   {
180     int size = items.size ();
181     for (int i = 0; i < size; ++i)
182       {
183         if (item.equals (items.get (i)))
184           {
185             remove (i);
186             break;
187           }
188       }
189     throw new IllegalArgumentException ("item \"" + item + "\" not in Choice");
190   }
191
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
195    */
196   public synchronized void remove (int index)
197   {
198     items.remove (index);
199
200     if (peer != null)
201       {
202         ChoicePeer cp = (ChoicePeer) peer;
203         cp.remove (index);
204       }
205
206     if (index == selected)
207       select (0);
208     else if (selected > index)
209       --selected;
210   }
211
212   /** Remove all items from this choice.  */
213   public synchronized void removeAll ()
214   {
215     int oldsize = items.size ();
216     items.clear ();
217     selected = -1;
218
219     if (peer != null)
220       {
221         ChoicePeer cp = (ChoicePeer) peer;
222         for (int i = 0; i < oldsize; ++i)
223           {
224             // Always remove item 0.
225             cp.remove (0);
226           }
227       }
228   }
229
230   /** Remove an item listener.
231    * @param listener Item listener to remove.
232    */
233   public synchronized void removeItemListener (ItemListener listener)
234   {
235     listeners = AWTEventMulticaster.remove (listeners, listener);
236   }
237
238   /** Select an item in this Choice.
239    * @param item Name of the item to select.
240    */
241   public synchronized void select (String item)
242   {
243     int size = items.size ();
244     for (int i = 0; i < size; ++i)
245       {
246         if (item.equals (items.get (i)))
247           {
248             select (i);
249             break;
250           }
251       }
252   }
253
254   /** Select an item in this choice.
255    * @param index Index of item to select.
256    */
257   public synchronized void select (int index)
258   {
259     if (index < 0 || index > items.size ())
260       throw new IllegalArgumentException ("index out of range");
261     selected = index;
262     if (peer != null)
263       {
264         ChoicePeer cp = (ChoicePeer) peer;
265         cp.select (index);
266       }
267   }
268
269   private ItemListener listeners;
270
271   // List of items.
272   ArrayList items;
273   // Index of selected item.
274   int selected;
275 }