/[classpath]/classpath/java/awt/Choice.java
ViewVC logotype

Diff of /classpath/java/awt/Choice.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by mark, Sun Jan 13 15:45:15 2002 UTC revision 1.5 by tromey, Tue Jan 22 22:00:14 2002 UTC
# Line 1  Line 1 
1  /* Choice.java -- Java choice button widget.  /* Choice.java -- Java choice button widget.
2     Copyright (C) 1999 Free Software Foundation, Inc.     Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 63  private Vector pItems = new Vector(); Line 63  private Vector pItems = new Vector();
63  /**  /**
64    * @serial The index of the selected item in the choice box.    * @serial The index of the selected item in the choice box.
65    */    */
66  private int selectedIndex;  private int selectedIndex = -1;
67    
68  // Listener chain  // Listener chain
69  private ItemListener item_listeners;  private ItemListener item_listeners;
# Line 139  getItem(int index) Line 139  getItem(int index)
139  public synchronized void  public synchronized void
140  add(String item)  add(String item)
141  {  {
142      if (item == null)
143        throw new IllegalArgumentException ("item must be non-null");
144    
145    pItems.addElement(item);    pItems.addElement(item);
146    
147    ChoicePeer cp = (ChoicePeer)getPeer();    int i = pItems.size () - 1;
148    if (cp != null)    if (peer != null)
149      cp.add(item, getItemCount());      {
150          ChoicePeer cp = (ChoicePeer) peer;
151          cp.add (item, i);
152        }
153    
154      if (i == 0)
155        select (0);
156  }  }
157    
158  /*************************************************************************/  /*************************************************************************/
# Line 156  add(String item) Line 165  add(String item)
165  public synchronized void  public synchronized void
166  addItem(String item)  addItem(String item)
167  {  {
168    pItems.addElement(item);    add(item);
   
   ChoicePeer cp = (ChoicePeer)getPeer();  
   if (cp != null)  
     cp.addItem(item, getItemCount());  
169  }  }
170    
171  /*************************************************************************/  /*************************************************************************/
172    
173  /**  /** Inserts an item into this Choice.  Existing items are shifted
174    * Inserts the specified item to this choice box at the specified index.   * upwards.  If the new item is the only item, then it is selected.
175    *   * If the currently selected item is shifted, then the first item is
176    * @param item The item to add.   * selected.  If the currently selected item is not shifted, then it
177    * @param index The index at which the item should be inserted.   * remains selected.
178    */   *
179     * @param item The item to add.
180     * @param index The index at which the item should be inserted.
181     */
182  public synchronized void  public synchronized void
183  insert(String item, int index)  insert(String item, int index)
184  {  {
185      if (index > getItemCount ())
186        index = getItemCount ();
187    
188    pItems.insertElementAt(item, index);    pItems.insertElementAt(item, index);
189    
190    ChoicePeer cp = (ChoicePeer)getPeer();    if (peer != null)
191    if (cp != null)      {
192      cp.addItem(item, getItemCount());        ChoicePeer cp = (ChoicePeer) peer;
193          cp.add (item, index);
194        }
195    
196      if (getItemCount () == 1 || selectedIndex >= index)
197        select (0);
198  }  }
199    
200  /*************************************************************************/  /*************************************************************************/
# Line 194  public synchronized void Line 210  public synchronized void
210  remove(String item)  remove(String item)
211  {  {
212    int index = pItems.indexOf(item);    int index = pItems.indexOf(item);
213      if (index == -1)
214        throw new IllegalArgumentException ("item \""
215                                            + item + "\" not found in Choice");
216    remove(index);    remove(index);
217  }  }
218    
# Line 211  remove(int index) Line 230  remove(int index)
230  {  {
231    pItems.removeElementAt(index);    pItems.removeElementAt(index);
232    
233    ChoicePeer cp = (ChoicePeer)getPeer();    if (peer != null)
234    if (cp != null)      {
235      cp.remove(index);        ChoicePeer cp = (ChoicePeer) peer;
236          cp.remove (index);
237        }
238    
239      if (index == selectedIndex)
240        select (0);
241      else if (selectedIndex > index)
242        --selectedIndex;
243  }  }
244    
245  /*************************************************************************/  /*************************************************************************/
# Line 226  removeAll() Line 252  removeAll()
252  {  {
253    int count = getItemCount();    int count = getItemCount();
254    
255    if (count > 0)    for (int i = 0; i < count; i++)
256      for (int i = 0; i < count; i++)      {
257        remove(i);        // Always remove 0.
258          remove(0);
259    pItems = new Vector();      }
260  }  }
261    
262  /*************************************************************************/  /*************************************************************************/
263    
264  /**  /**
265    * Returns the currently selected item.    * Returns the currently selected item, or null if no item is
266      * selected.
267    *    *
268    * @return The currently selected item.    * @return The currently selected item.
269    */    */
270  public synchronized String  public synchronized String
271  getSelectedItem()  getSelectedItem()
272  {  {
273    return((String)pItems.elementAt(selectedIndex));    return (selectedIndex == -1
274              ? null
275              : ((String)pItems.elementAt(selectedIndex)));
276  }  }
277    
278  /*************************************************************************/  /*************************************************************************/
# Line 256  getSelectedItem() Line 285  getSelectedItem()
285  public synchronized Object[]  public synchronized Object[]
286  getSelectedObjects()  getSelectedObjects()
287  {  {
288      if (selectedIndex == -1)
289        return null;
290    
291    Object[] objs = new Object[1];    Object[] objs = new Object[1];
292    objs[0] = pItems.elementAt(selectedIndex);    objs[0] = pItems.elementAt(selectedIndex);
293    
# Line 291  select(int index) Line 323  select(int index)
323      throw new IllegalArgumentException("Bad index: " + index);      throw new IllegalArgumentException("Bad index: " + index);
324    
325    this.selectedIndex = index;    this.selectedIndex = index;
326      if (peer != null)
327    ChoicePeer cp = (ChoicePeer)getPeer();      {
328    if (cp != null)        ChoicePeer cp = (ChoicePeer) peer;
329      cp.select(index);        cp.select (index);
330        }
331  }  }
332    
333  /*************************************************************************/  /*************************************************************************/
# Line 310  public synchronized void Line 343  public synchronized void
343  select(String item)  select(String item)
344  {  {
345    int index = pItems.indexOf(item);    int index = pItems.indexOf(item);
346    select(index);    if (index >= 0)
347        select(index);
348  }  }
349    
350  /*************************************************************************/  /*************************************************************************/
# Line 321  select(String item) Line 355  select(String item)
355  public void  public void
356  addNotify()  addNotify()
357  {  {
358    if (getPeer() == null)    if (peer == null)
359      setPeer((ComponentPeer)getToolkit().createChoice(this));      peer = getToolkit ().createChoice (this);
360      super.addNotify ();
361  }  }
362    
363  /*************************************************************************/  /*************************************************************************/
# Line 395  processItemEvent(ItemEvent event) Line 430  processItemEvent(ItemEvent event)
430  protected String  protected String
431  paramString()  paramString()
432  {  {
433    return(getClass().getName() + "(selectedIndex=" + selectedIndex + ")");    return ("selectedIndex=" + selectedIndex + "," + super.paramString());
434  }  }
435    
436  } // class Choice  } // class Choice
   

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26