| 1 | /* Choice.java -- Java choice button widget.
|
|---|
| 2 | Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 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)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 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.
|
|---|
| 15 |
|
|---|
| 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., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 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
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 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. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.awt;
|
|---|
| 40 |
|
|---|
| 41 | import java.awt.peer.ChoicePeer;
|
|---|
| 42 | import java.awt.peer.ComponentPeer;
|
|---|
| 43 | import java.awt.event.ItemEvent;
|
|---|
| 44 | import java.awt.event.ItemListener;
|
|---|
| 45 | import java.io.Serializable;
|
|---|
| 46 | import java.util.Vector;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * This class implements a drop down choice list.
|
|---|
| 50 | *
|
|---|
| 51 | * @author Aaron M. Renn ([email protected])
|
|---|
| 52 | */
|
|---|
| 53 | public class Choice extends Component implements ItemSelectable, Serializable
|
|---|
| 54 | {
|
|---|
| 55 |
|
|---|
| 56 | /*
|
|---|
| 57 | * Static Variables
|
|---|
| 58 | */
|
|---|
| 59 |
|
|---|
| 60 | // Serialization constant
|
|---|
| 61 | private 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 | */
|
|---|
| 72 | private Vector pItems = new Vector();
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * @serial The index of the selected item in the choice box.
|
|---|
| 76 | */
|
|---|
| 77 | private int selectedIndex = -1;
|
|---|
| 78 |
|
|---|
| 79 | // Listener chain
|
|---|
| 80 | private ItemListener item_listeners;
|
|---|
| 81 |
|
|---|
| 82 | /*************************************************************************/
|
|---|
| 83 |
|
|---|
| 84 | /*
|
|---|
| 85 | * Constructors
|
|---|
| 86 | */
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Initializes a new instance of <code>Choice</code>.
|
|---|
| 90 | */
|
|---|
| 91 | public
|
|---|
| 92 | Choice()
|
|---|
| 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 | */
|
|---|
| 107 | public int
|
|---|
| 108 | getItemCount()
|
|---|
| 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 | */
|
|---|
| 122 | public int
|
|---|
| 123 | countItems()
|
|---|
| 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 | */
|
|---|
| 137 | public String
|
|---|
| 138 | getItem(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 | */
|
|---|
| 150 | public synchronized void
|
|---|
| 151 | add(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 | */
|
|---|
| 176 | public synchronized void
|
|---|
| 177 | addItem(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 | */
|
|---|
| 193 | public synchronized void
|
|---|
| 194 | insert(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 | */
|
|---|
| 220 | public synchronized void
|
|---|
| 221 | remove(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 | */
|
|---|
| 239 | public synchronized void
|
|---|
| 240 | remove(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 | */
|
|---|
| 261 | public synchronized void
|
|---|
| 262 | removeAll()
|
|---|
| 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 | */
|
|---|
| 281 | public synchronized String
|
|---|
| 282 | getSelectedItem()
|
|---|
| 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 | */
|
|---|
| 296 | public synchronized Object[]
|
|---|
| 297 | getSelectedObjects()
|
|---|
| 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 | */
|
|---|
| 315 | public int
|
|---|
| 316 | getSelectedIndex()
|
|---|
| 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 | */
|
|---|
| 330 | public synchronized void
|
|---|
| 331 | select(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 | */
|
|---|
| 353 | public synchronized void
|
|---|
| 354 | select(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 | */
|
|---|
| 366 | public void
|
|---|
| 367 | addNotify()
|
|---|
| 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 | */
|
|---|
| 382 | public synchronized void
|
|---|
| 383 | addItemListener(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 | */
|
|---|
| 396 | public synchronized void
|
|---|
| 397 | removeItemListener(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 | */
|
|---|
| 411 | protected void
|
|---|
| 412 | processEvent(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 | */
|
|---|
| 427 | protected void
|
|---|
| 428 | processItemEvent(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 | */
|
|---|
| 441 | protected String
|
|---|
| 442 | paramString()
|
|---|
| 443 | {
|
|---|
| 444 | return ("selectedIndex=" + selectedIndex + "," + super.paramString());
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | } // class Choice
|
|---|