| 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 | * @exception HeadlessException If GraphicsEnvironment.isHeadless()
|
|---|
| 92 | * returns true
|
|---|
| 93 | */
|
|---|
| 94 | public Choice()
|
|---|
| 95 | {
|
|---|
| 96 | if (GraphicsEnvironment.isHeadless())
|
|---|
| 97 | throw new HeadlessException ();
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /*************************************************************************/
|
|---|
| 101 |
|
|---|
| 102 | /*
|
|---|
| 103 | * Instance Methods
|
|---|
| 104 | */
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Returns the number of items in the list.
|
|---|
| 108 | *
|
|---|
| 109 | * @return The number of items in the list.
|
|---|
| 110 | */
|
|---|
| 111 | public int
|
|---|
| 112 | getItemCount()
|
|---|
| 113 | {
|
|---|
| 114 | return(pItems.size());
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /*************************************************************************/
|
|---|
| 118 |
|
|---|
| 119 | /**
|
|---|
| 120 | * Returns the number of items in the list.
|
|---|
| 121 | *
|
|---|
| 122 | * @return The number of items in the list.
|
|---|
| 123 | *
|
|---|
| 124 | * @deprecated This method is deprecated in favor of <code>getItemCount</code>.
|
|---|
| 125 | */
|
|---|
| 126 | public int
|
|---|
| 127 | countItems()
|
|---|
| 128 | {
|
|---|
| 129 | return(pItems.size());
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /*************************************************************************/
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Returns the item at the specified index in the list.
|
|---|
| 136 | *
|
|---|
| 137 | * @param index The index into the list to return the item from.
|
|---|
| 138 | *
|
|---|
| 139 | * @exception ArrayIndexOutOfBoundsException If the index is invalid.
|
|---|
| 140 | */
|
|---|
| 141 | public String
|
|---|
| 142 | getItem(int index)
|
|---|
| 143 | {
|
|---|
| 144 | return((String)pItems.elementAt(index));
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /*************************************************************************/
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Adds the specified item to this choice box.
|
|---|
| 151 | *
|
|---|
| 152 | * @param item The item to add.
|
|---|
| 153 | *
|
|---|
| 154 | * @exception NullPointerException If the item's value is null
|
|---|
| 155 | *
|
|---|
| 156 | * @since 1.1
|
|---|
| 157 | */
|
|---|
| 158 | public synchronized void
|
|---|
| 159 | add(String item)
|
|---|
| 160 | {
|
|---|
| 161 | if (item == null)
|
|---|
| 162 | throw new NullPointerException ("item must be non-null");
|
|---|
| 163 |
|
|---|
| 164 | pItems.addElement(item);
|
|---|
| 165 |
|
|---|
| 166 | int i = pItems.size () - 1;
|
|---|
| 167 | if (peer != null)
|
|---|
| 168 | {
|
|---|
| 169 | ChoicePeer cp = (ChoicePeer) peer;
|
|---|
| 170 | cp.add (item, i);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | if (i == 0)
|
|---|
| 174 | select (0);
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /*************************************************************************/
|
|---|
| 178 |
|
|---|
| 179 | /**
|
|---|
| 180 | * Adds the specified item to this choice box.
|
|---|
| 181 | *
|
|---|
| 182 | * This method is oboslete since Java 2 platform 1.1. Please use @see add
|
|---|
| 183 | * instead.
|
|---|
| 184 | *
|
|---|
| 185 | * @param item The item to add.
|
|---|
| 186 | *
|
|---|
| 187 | * @exception NullPointerException If the item's value is equal to null
|
|---|
| 188 | */
|
|---|
| 189 | public synchronized void
|
|---|
| 190 | addItem(String item)
|
|---|
| 191 | {
|
|---|
| 192 | add(item);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /*************************************************************************/
|
|---|
| 196 |
|
|---|
| 197 | /** Inserts an item into this Choice. Existing items are shifted
|
|---|
| 198 | * upwards. If the new item is the only item, then it is selected.
|
|---|
| 199 | * If the currently selected item is shifted, then the first item is
|
|---|
| 200 | * selected. If the currently selected item is not shifted, then it
|
|---|
| 201 | * remains selected.
|
|---|
| 202 | *
|
|---|
| 203 | * @param item The item to add.
|
|---|
| 204 | * @param index The index at which the item should be inserted.
|
|---|
| 205 | *
|
|---|
| 206 | * @exception IllegalArgumentException If index is less than 0
|
|---|
| 207 | */
|
|---|
| 208 | public synchronized void
|
|---|
| 209 | insert(String item, int index)
|
|---|
| 210 | {
|
|---|
| 211 | if (index < 0)
|
|---|
| 212 | throw new IllegalArgumentException ("index may not be less then 0");
|
|---|
| 213 |
|
|---|
| 214 | if (index > getItemCount ())
|
|---|
| 215 | index = getItemCount ();
|
|---|
| 216 |
|
|---|
| 217 | pItems.insertElementAt(item, index);
|
|---|
| 218 |
|
|---|
| 219 | if (peer != null)
|
|---|
| 220 | {
|
|---|
| 221 | ChoicePeer cp = (ChoicePeer) peer;
|
|---|
| 222 | cp.add (item, index);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | if (getItemCount () == 1 || selectedIndex >= index)
|
|---|
| 226 | select (0);
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /*************************************************************************/
|
|---|
| 230 |
|
|---|
| 231 | /**
|
|---|
| 232 | * Removes the specified item from the choice box.
|
|---|
| 233 | *
|
|---|
| 234 | * @param item The item to remove.
|
|---|
| 235 | *
|
|---|
| 236 | * @exception IllegalArgumentException If the specified item doesn't exist.
|
|---|
| 237 | */
|
|---|
| 238 | public synchronized void
|
|---|
| 239 | remove(String item)
|
|---|
| 240 | {
|
|---|
| 241 | int index = pItems.indexOf(item);
|
|---|
| 242 | if (index == -1)
|
|---|
| 243 | throw new IllegalArgumentException ("item \""
|
|---|
| 244 | + item + "\" not found in Choice");
|
|---|
| 245 | remove(index);
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /*************************************************************************/
|
|---|
| 249 |
|
|---|
| 250 | /**
|
|---|
| 251 | * Removes the item at the specified index from the choice box.
|
|---|
| 252 | *
|
|---|
| 253 | * @param index The index of the item to remove.
|
|---|
| 254 | *
|
|---|
| 255 | * @exception IndexOutOfBoundsException If the index is not valid.
|
|---|
| 256 | */
|
|---|
| 257 | public synchronized void
|
|---|
| 258 | remove(int index)
|
|---|
| 259 | {
|
|---|
| 260 | pItems.removeElementAt(index);
|
|---|
| 261 |
|
|---|
| 262 | if (peer != null)
|
|---|
| 263 | {
|
|---|
| 264 | ChoicePeer cp = (ChoicePeer) peer;
|
|---|
| 265 | cp.remove (index);
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | if (index == selectedIndex)
|
|---|
| 269 | select (0);
|
|---|
| 270 | else if (selectedIndex > index)
|
|---|
| 271 | --selectedIndex;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | /*************************************************************************/
|
|---|
| 275 |
|
|---|
| 276 | /**
|
|---|
| 277 | * Removes all of the objects from this choice box.
|
|---|
| 278 | */
|
|---|
| 279 | public synchronized void
|
|---|
| 280 | removeAll()
|
|---|
| 281 | {
|
|---|
| 282 | int count = getItemCount();
|
|---|
| 283 |
|
|---|
| 284 | for (int i = 0; i < count; i++)
|
|---|
| 285 | {
|
|---|
| 286 | // Always remove 0.
|
|---|
| 287 | remove(0);
|
|---|
| 288 | }
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | /*************************************************************************/
|
|---|
| 292 |
|
|---|
| 293 | /**
|
|---|
| 294 | * Returns the currently selected item, or null if no item is
|
|---|
| 295 | * selected.
|
|---|
| 296 | *
|
|---|
| 297 | * @return The currently selected item.
|
|---|
| 298 | */
|
|---|
| 299 | public synchronized String
|
|---|
| 300 | getSelectedItem()
|
|---|
| 301 | {
|
|---|
| 302 | return (selectedIndex == -1
|
|---|
| 303 | ? null
|
|---|
| 304 | : ((String)pItems.elementAt(selectedIndex)));
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /*************************************************************************/
|
|---|
| 308 |
|
|---|
| 309 | /**
|
|---|
| 310 | * Returns an array with one row containing the selected item.
|
|---|
| 311 | *
|
|---|
| 312 | * @return An array containing the selected item.
|
|---|
| 313 | */
|
|---|
| 314 | public synchronized Object[]
|
|---|
| 315 | getSelectedObjects()
|
|---|
| 316 | {
|
|---|
| 317 | if (selectedIndex == -1)
|
|---|
| 318 | return null;
|
|---|
| 319 |
|
|---|
| 320 | Object[] objs = new Object[1];
|
|---|
| 321 | objs[0] = pItems.elementAt(selectedIndex);
|
|---|
| 322 |
|
|---|
| 323 | return(objs);
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | /*************************************************************************/
|
|---|
| 327 |
|
|---|
| 328 | /**
|
|---|
| 329 | * Returns the index of the selected item.
|
|---|
| 330 | *
|
|---|
| 331 | * @return The index of the selected item.
|
|---|
| 332 | */
|
|---|
| 333 | public int
|
|---|
| 334 | getSelectedIndex()
|
|---|
| 335 | {
|
|---|
| 336 | return(selectedIndex);
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | /*************************************************************************/
|
|---|
| 340 |
|
|---|
| 341 | /**
|
|---|
| 342 | * Forces the item at the specified index to be selected.
|
|---|
| 343 | *
|
|---|
| 344 | * @param index The index of the row to make selected.
|
|---|
| 345 | *
|
|---|
| 346 | * @exception IllegalArgumentException If the specified index is invalid.
|
|---|
| 347 | */
|
|---|
| 348 | public synchronized void
|
|---|
| 349 | select(int index)
|
|---|
| 350 | {
|
|---|
| 351 | if ((index < 0) || (index > getItemCount()))
|
|---|
| 352 | throw new IllegalArgumentException("Bad index: " + index);
|
|---|
| 353 |
|
|---|
| 354 | this.selectedIndex = index;
|
|---|
| 355 | if (peer != null)
|
|---|
| 356 | {
|
|---|
| 357 | ChoicePeer cp = (ChoicePeer) peer;
|
|---|
| 358 | cp.select (index);
|
|---|
| 359 | }
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | /*************************************************************************/
|
|---|
| 363 |
|
|---|
| 364 | /**
|
|---|
| 365 | * Forces the named item to be selected.
|
|---|
| 366 | *
|
|---|
| 367 | * @param item The item to be selected.
|
|---|
| 368 | *
|
|---|
| 369 | * @exception IllegalArgumentException If the specified item does not exist.
|
|---|
| 370 | */
|
|---|
| 371 | public synchronized void
|
|---|
| 372 | select(String item)
|
|---|
| 373 | {
|
|---|
| 374 | int index = pItems.indexOf(item);
|
|---|
| 375 | if (index >= 0)
|
|---|
| 376 | select(index);
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | /*************************************************************************/
|
|---|
| 380 |
|
|---|
| 381 | /**
|
|---|
| 382 | * Creates the native peer for this object.
|
|---|
| 383 | */
|
|---|
| 384 | public void
|
|---|
| 385 | addNotify()
|
|---|
| 386 | {
|
|---|
| 387 | if (peer == null)
|
|---|
| 388 | peer = getToolkit ().createChoice (this);
|
|---|
| 389 | super.addNotify ();
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | /*************************************************************************/
|
|---|
| 393 |
|
|---|
| 394 | /**
|
|---|
| 395 | * Adds the specified listener to the list of registered listeners for
|
|---|
| 396 | * this object.
|
|---|
| 397 | *
|
|---|
| 398 | * @param listener The listener to add.
|
|---|
| 399 | */
|
|---|
| 400 | public synchronized void
|
|---|
| 401 | addItemListener(ItemListener listener)
|
|---|
| 402 | {
|
|---|
| 403 | item_listeners = AWTEventMulticaster.add(item_listeners, listener);
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | /*************************************************************************/
|
|---|
| 407 |
|
|---|
| 408 | /**
|
|---|
| 409 | * Removes the specified listener from the list of registered listeners for
|
|---|
| 410 | * this object.
|
|---|
| 411 | *
|
|---|
| 412 | * @param listener The listener to remove.
|
|---|
| 413 | */
|
|---|
| 414 | public synchronized void
|
|---|
| 415 | removeItemListener(ItemListener listener)
|
|---|
| 416 | {
|
|---|
| 417 | item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | /*************************************************************************/
|
|---|
| 421 |
|
|---|
| 422 | /**
|
|---|
| 423 | * Processes this event by invoking <code>processItemEvent()</code> if the
|
|---|
| 424 | * event is an instance of <code>ItemEvent</code>, otherwise the event
|
|---|
| 425 | * is passed to the superclass.
|
|---|
| 426 | *
|
|---|
| 427 | * @param event The event to process.
|
|---|
| 428 | */
|
|---|
| 429 | protected void
|
|---|
| 430 | processEvent(AWTEvent event)
|
|---|
| 431 | {
|
|---|
| 432 | if (event instanceof ItemEvent)
|
|---|
| 433 | processItemEvent((ItemEvent)event);
|
|---|
| 434 | else
|
|---|
| 435 | super.processEvent(event);
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | /*************************************************************************/
|
|---|
| 439 |
|
|---|
| 440 | /**
|
|---|
| 441 | * Processes item event by dispatching to any registered listeners.
|
|---|
| 442 | *
|
|---|
| 443 | * @param event The event to process.
|
|---|
| 444 | */
|
|---|
| 445 | protected void
|
|---|
| 446 | processItemEvent(ItemEvent event)
|
|---|
| 447 | {
|
|---|
| 448 | if (item_listeners != null)
|
|---|
| 449 | item_listeners.itemStateChanged(event);
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | void
|
|---|
| 453 | dispatchEventImpl(AWTEvent e)
|
|---|
| 454 | {
|
|---|
| 455 | if (e.id <= ItemEvent.ITEM_LAST
|
|---|
| 456 | && e.id >= ItemEvent.ITEM_FIRST
|
|---|
| 457 | && (item_listeners != null
|
|---|
| 458 | || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0))
|
|---|
| 459 | processEvent(e);
|
|---|
| 460 | else
|
|---|
| 461 | super.dispatchEventImpl(e);
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | /*************************************************************************/
|
|---|
| 465 |
|
|---|
| 466 | /**
|
|---|
| 467 | * Returns a debugging string for this object.
|
|---|
| 468 | *
|
|---|
| 469 | * @return A debugging string for this object.
|
|---|
| 470 | */
|
|---|
| 471 | protected String
|
|---|
| 472 | paramString()
|
|---|
| 473 | {
|
|---|
| 474 | return ("selectedIndex=" + selectedIndex + "," + super.paramString());
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | } // class Choice
|
|---|