Showing the changes to java/pkg/java/awt/Choice.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,811 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | +package java.awt; | |
| 26 | + | |
| 27 | +import java.util.*; | |
| 28 | +import java.awt.peer.ChoicePeer; | |
| 29 | +import java.awt.event.*; | |
| 30 | +import java.util.EventListener; | |
| 31 | +import java.io.ObjectOutputStream; | |
| 32 | +import java.io.ObjectInputStream; | |
| 33 | +import java.io.IOException; | |
| 34 | + | |
| 35 | +import javax.accessibility.*; | |
| 36 | + | |
| 37 | +/** | |
| 38 | + * The <code>Choice</code> class presents a pop-up menu of choices. | |
| 39 | + * The current choice is displayed as the title of the menu. | |
| 40 | + * <p> | |
| 41 | + * The following code example produces a pop-up menu: | |
| 42 | + * <p> | |
| 43 | + * <hr><blockquote><pre> | |
| 44 | + * Choice ColorChooser = new Choice(); | |
| 45 | + * ColorChooser.add("Green"); | |
| 46 | + * ColorChooser.add("Red"); | |
| 47 | + * ColorChooser.add("Blue"); | |
| 48 | + * </pre></blockquote><hr> | |
| 49 | + * <p> | |
| 50 | + * After this choice menu has been added to a panel, | |
| 51 | + * it appears as follows in its normal state: | |
| 52 | + * <p> | |
| 53 | + * <img src="doc-files/Choice-1.gif" alt="The following text describes the graphic" | |
| 54 | + * ALIGN=center HSPACE=10 VSPACE=7> | |
| 55 | + * <p> | |
| 56 | + * In the picture, <code>"Green"</code> is the current choice. | |
| 57 | + * Pushing the mouse button down on the object causes a menu to | |
| 58 | + * appear with the current choice highlighted. | |
| 59 | + * <p> | |
| 60 | + * Some native platforms do not support arbitrary resizing of | |
| 61 | + * <code>Choice</code> components and the behavior of | |
| 62 | + * <code>setSize()/getSize()</code> is bound by | |
| 63 | + * such limitations. | |
| 64 | + * Native GUI <code>Choice</code> components' size are often bound by such | |
| 65 | + * attributes as font size and length of items contained within | |
| 66 | + * the <code>Choice</code>. | |
| 67 | + * <p> | |
| 68 | + * @author Sami Shaio | |
| 69 | + * @author Arthur van Hoff | |
| 70 | + * @since JDK1.0 | |
| 71 | + */ | |
| 72 | +public class Choice extends Component implements ItemSelectable, Accessible { | |
| 73 | + /** | |
| 74 | + * The items for the <code>Choice</code>. | |
| 75 | + * This can be a <code>null</code> value. | |
| 76 | + * @serial | |
| 77 | + * @see #add(String) | |
| 78 | + * @see #addItem(String) | |
| 79 | + * @see #getItem(int) | |
| 80 | + * @see #getItemCount() | |
| 81 | + * @see #insert(String, int) | |
| 82 | + * @see #remove(String) | |
| 83 | + */ | |
| 84 | + Vector pItems; | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * The index of the current choice for this <code>Choice</code> | |
| 88 | + * or -1 if nothing is selected. | |
| 89 | + * @serial | |
| 90 | + * @see #getSelectedItem() | |
| 91 | + * @see #select(int) | |
| 92 | + */ | |
| 93 | + int selectedIndex = -1; | |
| 94 | + | |
| 95 | + transient ItemListener itemListener; | |
| 96 | + | |
| 97 | + private static final String base = "choice"; | |
| 98 | + private static int nameCounter = 0; | |
| 99 | + | |
| 100 | + /* | |
| 101 | + * JDK 1.1 serialVersionUID | |
| 102 | + */ | |
| 103 | + private static final long serialVersionUID = -4075310674757313071L; | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Creates a new choice menu. The menu initially has no items in it. | |
| 107 | + * <p> | |
| 108 | + * By default, the first item added to the choice menu becomes the | |
| 109 | + * selected item, until a different selection is made by the user | |
| 110 | + * by calling one of the <code>select</code> methods. | |
| 111 | + * @exception HeadlessException if GraphicsEnvironment.isHeadless() | |
| 112 | + * returns true | |
| 113 | + * @see java.awt.GraphicsEnvironment#isHeadless | |
| 114 | + * @see #select(int) | |
| 115 | + * @see #select(java.lang.String) | |
| 116 | + */ | |
| 117 | + public Choice() throws HeadlessException { | |
| 118 | + GraphicsEnvironment.checkHeadless(); | |
| 119 | + pItems = new Vector(); | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * Constructs a name for this component. Called by | |
| 124 | + * <code>getName</code> when the name is <code>null</code>. | |
| 125 | + */ | |
| 126 | + String constructComponentName() { | |
| 127 | + synchronized (Choice.class) { | |
| 128 | + return base + nameCounter++; | |
| 129 | + } | |
| 130 | + } | |
| 131 | + | |
| 132 | + /** | |
| 133 | + * Creates the <code>Choice</code>'s peer. This peer allows us | |
| 134 | + * to change the look | |
| 135 | + * of the <code>Choice</code> without changing its functionality. | |
| 136 | + * @see java.awt.Toolkit#createChoice(java.awt.Choice) | |
| 137 | + * @see java.awt.Component#getToolkit() | |
| 138 | + */ | |
| 139 | + public void addNotify() { | |
| 140 | + synchronized (getTreeLock()) { | |
| 141 | + if (peer == null) | |
| 142 | + peer = getToolkit().createChoice(this); | |
| 143 | + super.addNotify(); | |
| 144 | + } | |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * Returns the number of items in this <code>Choice</code> menu. | |
| 149 | + * @return the number of items in this <code>Choice</code> menu | |
| 150 | + * @see #getItem | |
| 151 | + * @since JDK1.1 | |
| 152 | + */ | |
| 153 | + public int getItemCount() { | |
| 154 | + return countItems(); | |
| 155 | + } | |
| 156 | + | |
| 157 | + /** | |
| 158 | + * @deprecated As of JDK version 1.1, | |
| 159 | + * replaced by <code>getItemCount()</code>. | |
| 160 | + */ | |
| 161 | + @Deprecated | |
| 162 | + public int countItems() { | |
| 163 | + return pItems.size(); | |
| 164 | + } | |
| 165 | + | |
| 166 | + /** | |
| 167 | + * Gets the string at the specified index in this | |
| 168 | + * <code>Choice</code> menu. | |
| 169 | + * @param index the index at which to begin | |
| 170 | + * @see #getItemCount | |
| 171 | + */ | |
| 172 | + public String getItem(int index) { | |
| 173 | + return getItemImpl(index); | |
| 174 | + } | |
| 175 | + | |
| 176 | + /* | |
| 177 | + * This is called by the native code, so client code can't | |
| 178 | + * be called on the toolkit thread. | |
| 179 | + */ | |
| 180 | + final String getItemImpl(int index) { | |
| 181 | + return (String)pItems.elementAt(index); | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 185 | + * Adds an item to this <code>Choice</code> menu. | |
| 186 | + * @param item the item to be added | |
| 187 | + * @exception NullPointerException if the item's value is | |
| 188 | + * <code>null</code> | |
| 189 | + * @since JDK1.1 | |
| 190 | + */ | |
| 191 | + public void add(String item) { | |
| 192 | + addItem(item); | |
| 193 | + } | |
| 194 | + | |
| 195 | + /** | |
| 196 | + * Obsolete as of Java 2 platform v1.1. Please use the | |
| 197 | + * <code>add</code> method instead. | |
| 198 | + * <p> | |
| 199 | + * Adds an item to this <code>Choice</code> menu. | |
| 200 | + * @param item the item to be added | |
| 201 | + * @exception NullPointerException if the item's value is equal to | |
| 202 | + * <code>null</code> | |
| 203 | + */ | |
| 204 | + public void addItem(String item) { | |
| 205 | + synchronized (this) { | |
| 206 | + insertNoInvalidate(item, pItems.size()); | |
| 207 | + } | |
| 208 | + | |
| 209 | + // This could change the preferred size of the Component. | |
| 210 | + invalidateIfValid(); | |
| 211 | + } | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * Inserts an item to this <code>Choice</code>, | |
| 215 | + * but does not invalidate the <code>Choice</code>. | |
| 216 | + * Client methods must provide their own synchronization before | |
| 217 | + * invoking this method. | |
| 218 | + * @param item the item to be added | |
| 219 | + * @param index the new item position | |
| 220 | + * @exception NullPointerException if the item's value is equal to | |
| 221 | + * <code>null</code> | |
| 222 | + */ | |
| 223 | + private void insertNoInvalidate(String item, int index) { | |
| 224 | + if (item == null) { | |
| 225 | + throw new | |
| 226 | + NullPointerException("cannot add null item to Choice"); | |
| 227 | + } | |
| 228 | + pItems.insertElementAt(item, index); | |
| 229 | + ChoicePeer peer = (ChoicePeer)this.peer; | |
| 230 | + if (peer != null) { | |
| 231 | + peer.add(item, index); | |
| 232 | + } | |
| 233 | + // no selection or selection shifted up | |
| 234 | + if (selectedIndex < 0 || selectedIndex >= index) { | |
| 235 | + select(0); | |
| 236 | + } | |
| 237 | + } | |
| 238 | + | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * Inserts the item into this choice at the specified position. | |
| 242 | + * Existing items at an index greater than or equal to | |
| 243 | + * <code>index</code> are shifted up by one to accommodate | |
| 244 | + * the new item. If <code>index</code> is greater than or | |
| 245 | + * equal to the number of items in this choice, | |
| 246 | + * <code>item</code> is added to the end of this choice. | |
| 247 | + * <p> | |
| 248 | + * If the item is the first one being added to the choice, | |
| 249 | + * then the item becomes selected. Otherwise, if the | |
| 250 | + * selected item was one of the items shifted, the first | |
| 251 | + * item in the choice becomes the selected item. If the | |
| 252 | + * selected item was no among those shifted, it remains | |
| 253 | + * the selected item. | |
| 254 | + * @param item the non-<code>null</code> item to be inserted | |
| 255 | + * @param index the position at which the item should be inserted | |
| 256 | + * @exception IllegalArgumentException if index is less than 0 | |
| 257 | + */ | |
| 258 | + public void insert(String item, int index) { | |
| 259 | + synchronized (this) { | |
| 260 | + if (index < 0) { | |
| 261 | + throw new IllegalArgumentException("index less than zero."); | |
| 262 | + } | |
| 263 | + /* if the index greater than item count, add item to the end */ | |
| 264 | + index = Math.min(index, pItems.size()); | |
| 265 | + | |
| 266 | + insertNoInvalidate(item, index); | |
| 267 | + } | |
| 268 | + | |
| 269 | + // This could change the preferred size of the Component. | |
| 270 | + invalidateIfValid(); | |
| 271 | + } | |
| 272 | + | |
| 273 | + /** | |
| 274 | + * Removes the first occurrence of <code>item</code> | |
| 275 | + * from the <code>Choice</code> menu. If the item | |
| 276 | + * being removed is the currently selected item, | |
| 277 | + * then the first item in the choice becomes the | |
| 278 | + * selected item. Otherwise, the currently selected | |
| 279 | + * item remains selected (and the selected index is | |
| 280 | + * updated accordingly). | |
| 281 | + * @param item the item to remove from this <code>Choice</code> menu | |
| 282 | + * @exception IllegalArgumentException if the item doesn't | |
| 283 | + * exist in the choice menu | |
| 284 | + * @since JDK1.1 | |
| 285 | + */ | |
| 286 | + public void remove(String item) { | |
| 287 | + synchronized (this) { | |
| 288 | + int index = pItems.indexOf(item); | |
| 289 | + if (index < 0) { | |
| 290 | + throw new IllegalArgumentException("item " + item + | |
| 291 | + " not found in choice"); | |
| 292 | + } else { | |
| 293 | + removeNoInvalidate(index); | |
| 294 | + } | |
| 295 | + } | |
| 296 | + | |
| 297 | + // This could change the preferred size of the Component. | |
| 298 | + invalidateIfValid(); | |
| 299 | + } | |
| 300 | + | |
| 301 | + /** | |
| 302 | + * Removes an item from the choice menu | |
| 303 | + * at the specified position. If the item | |
| 304 | + * being removed is the currently selected item, | |
| 305 | + * then the first item in the choice becomes the | |
| 306 | + * selected item. Otherwise, the currently selected | |
| 307 | + * item remains selected (and the selected index is | |
| 308 | + * updated accordingly). | |
| 309 | + * @param position the position of the item | |
| 310 | + * @throws IndexOutOfBoundsException if the specified | |
| 311 | + * position is out of bounds | |
| 312 | + * @since JDK1.1 | |
| 313 | + */ | |
| 314 | + public void remove(int position) { | |
| 315 | + synchronized (this) { | |
| 316 | + removeNoInvalidate(position); | |
| 317 | + } | |
| 318 | + | |
| 319 | + // This could change the preferred size of the Component. | |
| 320 | + invalidateIfValid(); | |
| 321 | + } | |
| 322 | + | |
| 323 | + /** | |
| 324 | + * Removes an item from the <code>Choice</code> at the | |
| 325 | + * specified position, but does not invalidate the <code>Choice</code>. | |
| 326 | + * Client methods must provide their | |
| 327 | + * own synchronization before invoking this method. | |
| 328 | + * @param position the position of the item | |
| 329 | + */ | |
| 330 | + private void removeNoInvalidate(int position) { | |
| 331 | + pItems.removeElementAt(position); | |
| 332 | + ChoicePeer peer = (ChoicePeer)this.peer; | |
| 333 | + if (peer != null) { | |
| 334 | + peer.remove(position); | |
| 335 | + } | |
| 336 | + /* Adjust selectedIndex if selected item was removed. */ | |
| 337 | + if (pItems.size() == 0) { | |
| 338 | + selectedIndex = -1; | |
| 339 | + } else if (selectedIndex == position) { | |
| 340 | + select(0); | |
| 341 | + } else if (selectedIndex > position) { | |
| 342 | + select(selectedIndex-1); | |
| 343 | + } | |
| 344 | + } | |
| 345 | + | |
| 346 | + | |
| 347 | + /** | |
| 348 | + * Removes all items from the choice menu. | |
| 349 | + * @see #remove | |
| 350 | + * @since JDK1.1 | |
| 351 | + */ | |
| 352 | + public void removeAll() { | |
| 353 | + synchronized (this) { | |
| 354 | + if (peer != null) { | |
| 355 | + ((ChoicePeer)peer).removeAll(); | |
| 356 | + } | |
| 357 | + pItems.removeAllElements(); | |
| 358 | + selectedIndex = -1; | |
| 359 | + } | |
| 360 | + | |
| 361 | + // This could change the preferred size of the Component. | |
| 362 | + invalidateIfValid(); | |
| 363 | + } | |
| 364 | + | |
| 365 | + /** | |
| 366 | + * Gets a representation of the current choice as a string. | |
| 367 | + * @return a string representation of the currently | |
| 368 | + * selected item in this choice menu | |
| 369 | + * @see #getSelectedIndex | |
| 370 | + */ | |
| 371 | + public synchronized String getSelectedItem() { | |
| 372 | + return (selectedIndex >= 0) ? getItem(selectedIndex) : null; | |
| 373 | + } | |
| 374 | + | |
| 375 | + /** | |
| 376 | + * Returns an array (length 1) containing the currently selected | |
| 377 | + * item. If this choice has no items, returns <code>null</code>. | |
| 378 | + * @see ItemSelectable | |
| 379 | + */ | |
| 380 | + public synchronized Object[] getSelectedObjects() { | |
| 381 | + if (selectedIndex >= 0) { | |
| 382 | + Object[] items = new Object[1]; | |
| 383 | + items[0] = getItem(selectedIndex); | |
| 384 | + return items; | |
| 385 | + } | |
| 386 | + return null; | |
| 387 | + } | |
| 388 | + | |
| 389 | + /** | |
| 390 | + * Returns the index of the currently selected item. | |
| 391 | + * If nothing is selected, returns -1. | |
| 392 | + * | |
| 393 | + * @return the index of the currently selected item, or -1 if nothing | |
| 394 | + * is currently selected | |
| 395 | + * @see #getSelectedItem | |
| 396 | + */ | |
| 397 | + public int getSelectedIndex() { | |
| 398 | + return selectedIndex; | |
| 399 | + } | |
| 400 | + | |
| 401 | + /** | |
| 402 | + * Sets the selected item in this <code>Choice</code> menu to be the | |
| 403 | + * item at the specified position. | |
| 404 | + * | |
| 405 | + * <p>Note that this method should be primarily used to | |
| 406 | + * initially select an item in this component. | |
| 407 | + * Programmatically calling this method will <i>not</i> trigger | |
| 408 | + * an <code>ItemEvent</code>. The only way to trigger an | |
| 409 | + * <code>ItemEvent</code> is by user interaction. | |
| 410 | + * | |
| 411 | + * @param pos the positon of the selected item | |
| 412 | + * @exception IllegalArgumentException if the specified | |
| 413 | + * position is greater than the | |
| 414 | + * number of items or less than zero | |
| 415 | + * @see #getSelectedItem | |
| 416 | + * @see #getSelectedIndex | |
| 417 | + */ | |
| 418 | + public synchronized void select(int pos) { | |
| 419 | + if ((pos >= pItems.size()) || (pos < 0)) { | |
| 420 | + throw new IllegalArgumentException("illegal Choice item position: " + pos); | |
| 421 | + } | |
| 422 | + if (pItems.size() > 0) { | |
| 423 | + selectedIndex = pos; | |
| 424 | + ChoicePeer peer = (ChoicePeer)this.peer; | |
| 425 | + if (peer != null) { | |
| 426 | + peer.select(pos); | |
| 427 | + } | |
| 428 | + } | |
| 429 | + } | |
| 430 | + | |
| 431 | + /** | |
| 432 | + * Sets the selected item in this <code>Choice</code> menu | |
| 433 | + * to be the item whose name is equal to the specified string. | |
| 434 | + * If more than one item matches (is equal to) the specified string, | |
| 435 | + * the one with the smallest index is selected. | |
| 436 | + * | |
| 437 | + * <p>Note that this method should be primarily used to | |
| 438 | + * initially select an item in this component. | |
| 439 | + * Programmatically calling this method will <i>not</i> trigger | |
| 440 | + * an <code>ItemEvent</code>. The only way to trigger an | |
| 441 | + * <code>ItemEvent</code> is by user interaction. | |
| 442 | + * | |
| 443 | + * @param str the specified string | |
| 444 | + * @see #getSelectedItem | |
| 445 | + * @see #getSelectedIndex | |
| 446 | + */ | |
| 447 | + public synchronized void select(String str) { | |
| 448 | + int index = pItems.indexOf(str); | |
| 449 | + if (index >= 0) { | |
| 450 | + select(index); | |
| 451 | + } | |
| 452 | + } | |
| 453 | + | |
| 454 | + /** | |
| 455 | + * Adds the specified item listener to receive item events from | |
| 456 | + * this <code>Choice</code> menu. Item events are sent in response | |
| 457 | + * to user input, but not in response to calls to <code>select</code>. | |
| 458 | + * If l is <code>null</code>, no exception is thrown and no action | |
| 459 | + * is performed. | |
| 460 | + * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" | |
| 461 | + * >AWT Threading Issues</a> for details on AWT's threading model. | |
| 462 | + * @param l the item listener | |
| 463 | + * @see #removeItemListener | |
| 464 | + * @see #getItemListeners | |
| 465 | + * @see #select | |
| 466 | + * @see java.awt.event.ItemEvent | |
| 467 | + * @see java.awt.event.ItemListener | |
| 468 | + * @since JDK1.1 | |
| 469 | + */ | |
| 470 | + public synchronized void addItemListener(ItemListener l) { | |
| 471 | + if (l == null) { | |
| 472 | + return; | |
| 473 | + } | |
| 474 | + itemListener = AWTEventMulticaster.add(itemListener, l); | |
| 475 | + newEventsOnly = true; | |
| 476 | + } | |
| 477 | + | |
| 478 | + /** | |
| 479 | + * Removes the specified item listener so that it no longer receives | |
| 480 | + * item events from this <code>Choice</code> menu. | |
| 481 | + * If l is <code>null</code>, no exception is thrown and no | |
| 482 | + * action is performed. | |
| 483 | + * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" | |
| 484 | + * >AWT Threading Issues</a> for details on AWT's threading model. | |
| 485 | + * @param l the item listener | |
| 486 | + * @see #addItemListener | |
| 487 | + * @see #getItemListeners | |
| 488 | + * @see java.awt.event.ItemEvent | |
| 489 | + * @see java.awt.event.ItemListener | |
| 490 | + * @since JDK1.1 | |
| 491 | + */ | |
| 492 | + public synchronized void removeItemListener(ItemListener l) { | |
| 493 | + if (l == null) { | |
| 494 | + return; | |
| 495 | + } | |
| 496 | + itemListener = AWTEventMulticaster.remove(itemListener, l); | |
| 497 | + } | |
| 498 | + | |
| 499 | + /** | |
| 500 | + * Returns an array of all the item listeners | |
| 501 | + * registered on this choice. | |
| 502 | + * | |
| 503 | + * @return all of this choice's <code>ItemListener</code>s | |
| 504 | + * or an empty array if no item | |
| 505 | + * listeners are currently registered | |
| 506 | + * | |
| 507 | + * @see #addItemListener | |
| 508 | + * @see #removeItemListener | |
| 509 | + * @see java.awt.event.ItemEvent | |
| 510 | + * @see java.awt.event.ItemListener | |
| 511 | + * @since 1.4 | |
| 512 | + */ | |
| 513 | + public synchronized ItemListener[] getItemListeners() { | |
| 514 | + return (ItemListener[])(getListeners(ItemListener.class)); | |
| 515 | + } | |
| 516 | + | |
| 517 | + /** | |
| 518 | + * Returns an array of all the objects currently registered | |
| 519 | + * as <code><em>Foo</em>Listener</code>s | |
| 520 | + * upon this <code>Choice</code>. | |
| 521 | + * <code><em>Foo</em>Listener</code>s are registered using the | |
| 522 | + * <code>add<em>Foo</em>Listener</code> method. | |
| 523 | + * | |
| 524 | + * <p> | |
| 525 | + * You can specify the <code>listenerType</code> argument | |
| 526 | + * with a class literal, such as | |
| 527 | + * <code><em>Foo</em>Listener.class</code>. | |
| 528 | + * For example, you can query a | |
| 529 | + * <code>Choice</code> <code>c</code> | |
| 530 | + * for its item listeners with the following code: | |
| 531 | + * | |
| 532 | + * <pre>ItemListener[] ils = (ItemListener[])(c.getListeners(ItemListener.class));</pre> | |
| 533 | + * | |
| 534 | + * If no such listeners exist, this method returns an empty array. | |
| 535 | + * | |
| 536 | + * @param listenerType the type of listeners requested; this parameter | |
| 537 | + * should specify an interface that descends from | |
| 538 | + * <code>java.util.EventListener</code> | |
| 539 | + * @return an array of all objects registered as | |
| 540 | + * <code><em>Foo</em>Listener</code>s on this choice, | |
| 541 | + * or an empty array if no such | |
| 542 | + * listeners have been added | |
| 543 | + * @exception ClassCastException if <code>listenerType</code> | |
| 544 | + * doesn't specify a class or interface that implements | |
| 545 | + * <code>java.util.EventListener</code> | |
| 546 | + * | |
| 547 | + * @see #getItemListeners | |
| 548 | + * @since 1.3 | |
| 549 | + */ | |
| 550 | + public <T extends EventListener> T[] getListeners(Class<T> listenerType) { | |
| 551 | + EventListener l = null; | |
| 552 | + if (listenerType == ItemListener.class) { | |
| 553 | + l = itemListener; | |
| 554 | + } else { | |
| 555 | + return super.getListeners(listenerType); | |
| 556 | + } | |
| 557 | + return AWTEventMulticaster.getListeners(l, listenerType); | |
| 558 | + } | |
| 559 | + | |
| 560 | + // REMIND: remove when filtering is done at lower level | |
| 561 | + boolean eventEnabled(AWTEvent e) { | |
| 562 | + if (e.id == ItemEvent.ITEM_STATE_CHANGED) { | |
| 563 | + if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 || | |
| 564 | + itemListener != null) { | |
| 565 | + return true; | |
| 566 | + } | |
| 567 | + return false; | |
| 568 | + } | |
| 569 | + return super.eventEnabled(e); | |
| 570 | + } | |
| 571 | + | |
| 572 | + /** | |
| 573 | + * Processes events on this choice. If the event is an | |
| 574 | + * instance of <code>ItemEvent</code>, it invokes the | |
| 575 | + * <code>processItemEvent</code> method. Otherwise, it calls its | |
| 576 | + * superclass's <code>processEvent</code> method. | |
| 577 | + * <p>Note that if the event parameter is <code>null</code> | |
| 578 | + * the behavior is unspecified and may result in an | |
| 579 | + * exception. | |
| 580 | + * | |
| 581 | + * @param e the event | |
| 582 | + * @see java.awt.event.ItemEvent | |
| 583 | + * @see #processItemEvent | |
| 584 | + * @since JDK1.1 | |
| 585 | + */ | |
| 586 | + protected void processEvent(AWTEvent e) { | |
| 587 | + if (e instanceof ItemEvent) { | |
| 588 | + processItemEvent((ItemEvent)e); | |
| 589 | + return; | |
| 590 | + } | |
| 591 | + super.processEvent(e); | |
| 592 | + } | |
| 593 | + | |
| 594 | + /** | |
| 595 | + * Processes item events occurring on this <code>Choice</code> | |
| 596 | + * menu by dispatching them to any registered | |
| 597 | + * <code>ItemListener</code> objects. | |
| 598 | + * <p> | |
| 599 | + * This method is not called unless item events are | |
| 600 | + * enabled for this component. Item events are enabled | |
| 601 | + * when one of the following occurs: | |
| 602 | + * <p><ul> | |
| 603 | + * <li>An <code>ItemListener</code> object is registered | |
| 604 | + * via <code>addItemListener</code>. | |
| 605 | + * <li>Item events are enabled via <code>enableEvents</code>. | |
| 606 | + * </ul> | |
| 607 | + * <p>Note that if the event parameter is <code>null</code> | |
| 608 | + * the behavior is unspecified and may result in an | |
| 609 | + * exception. | |
| 610 | + * | |
| 611 | + * @param e the item event | |
| 612 | + * @see java.awt.event.ItemEvent | |
| 613 | + * @see java.awt.event.ItemListener | |
| 614 | + * @see #addItemListener(ItemListener) | |
| 615 | + * @see java.awt.Component#enableEvents | |
| 616 | + * @since JDK1.1 | |
| 617 | + */ | |
| 618 | + protected void processItemEvent(ItemEvent e) { | |
| 619 | + ItemListener listener = itemListener; | |
| 620 | + if (listener != null) { | |
| 621 | + listener.itemStateChanged(e); | |
| 622 | + } | |
| 623 | + } | |
| 624 | + | |
| 625 | + /** | |
| 626 | + * Returns a string representing the state of this <code>Choice</code> | |
| 627 | + * menu. This method is intended to be used only for debugging purposes, | |
| 628 | + * and the content and format of the returned string may vary between | |
| 629 | + * implementations. The returned string may be empty but may not be | |
| 630 | + * <code>null</code>. | |
| 631 | + * | |
| 632 | + * @return the parameter string of this <code>Choice</code> menu | |
| 633 | + */ | |
| 634 | + protected String paramString() { | |
| 635 | + return super.paramString() + ",current=" + getSelectedItem(); | |
| 636 | + } | |
| 637 | + | |
| 638 | + | |
| 639 | + /* Serialization support. | |
| 640 | + */ | |
| 641 | + | |
| 642 | + /* | |
| 643 | + * Choice Serial Data Version. | |
| 644 | + * @serial | |
| 645 | + */ | |
| 646 | + private int choiceSerializedDataVersion = 1; | |
| 647 | + | |
| 648 | + /** | |
| 649 | + * Writes default serializable fields to stream. Writes | |
| 650 | + * a list of serializable <code>ItemListeners</code> | |
| 651 | + * as optional data. The non-serializable | |
| 652 | + * <code>ItemListeners</code> are detected and | |
| 653 | + * no attempt is made to serialize them. | |
| 654 | + * | |
| 655 | + * @param s the <code>ObjectOutputStream</code> to write | |
| 656 | + * @serialData <code>null</code> terminated sequence of 0 | |
| 657 | + * or more pairs; the pair consists of a <code>String</code> | |
| 658 | + * and an <code>Object</code>; the <code>String</code> indicates | |
| 659 | + * the type of object and is one of the following: | |
| 660 | + * <code>itemListenerK</code> indicating an | |
| 661 | + * <code>ItemListener</code> object | |
| 662 | + * | |
| 663 | + * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener) | |
| 664 | + * @see java.awt.Component#itemListenerK | |
| 665 | + * @see #readObject(ObjectInputStream) | |
| 666 | + */ | |
| 667 | + private void writeObject(ObjectOutputStream s) | |
| 668 | + throws java.io.IOException | |
| 669 | + { | |
| 670 | + s.defaultWriteObject(); | |
| 671 | + | |
| 672 | + AWTEventMulticaster.save(s, itemListenerK, itemListener); | |
| 673 | + s.writeObject(null); | |
| 674 | + } | |
| 675 | + | |
| 676 | + /** | |
| 677 | + * Reads the <code>ObjectInputStream</code> and if it | |
| 678 | + * isn't <code>null</code> adds a listener to receive | |
| 679 | + * item events fired by the <code>Choice</code> item. | |
| 680 | + * Unrecognized keys or values will be ignored. | |
| 681 | + * | |
| 682 | + * @param s the <code>ObjectInputStream</code> to read | |
| 683 | + * @exception HeadlessException if | |
| 684 | + * <code>GraphicsEnvironment.isHeadless</code> returns | |
| 685 | + * <code>true</code> | |
| 686 | + * @serial | |
| 687 | + * @see #removeItemListener(ItemListener) | |
| 688 | + * @see #addItemListener(ItemListener) | |
| 689 | + * @see java.awt.GraphicsEnvironment#isHeadless | |
| 690 | + * @see #writeObject(ObjectOutputStream) | |
| 691 | + */ | |
| 692 | + private void readObject(ObjectInputStream s) | |
| 693 | + throws ClassNotFoundException, IOException, HeadlessException | |
| 694 | + { | |
| 695 | + GraphicsEnvironment.checkHeadless(); | |
| 696 | + s.defaultReadObject(); | |
| 697 | + | |
| 698 | + Object keyOrNull; | |
| 699 | + while(null != (keyOrNull = s.readObject())) { | |
| 700 | + String key = ((String)keyOrNull).intern(); | |
| 701 | + | |
| 702 | + if (itemListenerK == key) | |
| 703 | + addItemListener((ItemListener)(s.readObject())); | |
| 704 | + | |
| 705 | + else // skip value for unrecognized key | |
| 706 | + s.readObject(); | |
| 707 | + } | |
| 708 | + } | |
| 709 | + | |
| 710 | + | |
| 711 | +///////////////// | |
| 712 | +// Accessibility support | |
| 713 | +//////////////// | |
| 714 | + | |
| 715 | + | |
| 716 | + /** | |
| 717 | + * Gets the <code>AccessibleContext</code> associated with this | |
| 718 | + * <code>Choice</code>. For <code>Choice</code> components, | |
| 719 | + * the <code>AccessibleContext</code> takes the form of an | |
| 720 | + * <code>AccessibleAWTChoice</code>. A new <code>AccessibleAWTChoice</code> | |
| 721 | + * instance is created if necessary. | |
| 722 | + * | |
| 723 | + * @return an <code>AccessibleAWTChoice</code> that serves as the | |
| 724 | + * <code>AccessibleContext</code> of this <code>Choice</code> | |
| 725 | + * @since 1.3 | |
| 726 | + */ | |
| 727 | + public AccessibleContext getAccessibleContext() { | |
| 728 | + if (accessibleContext == null) { | |
| 729 | + accessibleContext = new AccessibleAWTChoice(); | |
| 730 | + } | |
| 731 | + return accessibleContext; | |
| 732 | + } | |
| 733 | + | |
| 734 | + /** | |
| 735 | + * This class implements accessibility support for the | |
| 736 | + * <code>Choice</code> class. It provides an implementation of the | |
| 737 | + * Java Accessibility API appropriate to choice user-interface elements. | |
| 738 | + * @since 1.3 | |
| 739 | + */ | |
| 740 | + protected class AccessibleAWTChoice extends AccessibleAWTComponent | |
| 741 | + implements AccessibleAction | |
| 742 | + { | |
| 743 | + /* | |
| 744 | + * JDK 1.3 serialVersionUID | |
| 745 | + */ | |
| 746 | + private static final long serialVersionUID = 7175603582428509322L; | |
| 747 | + | |
| 748 | + public AccessibleAWTChoice() { | |
| 749 | + super(); | |
| 750 | + } | |
| 751 | + | |
| 752 | + /** | |
| 753 | + * Get the AccessibleAction associated with this object. In the | |
| 754 | + * implementation of the Java Accessibility API for this class, | |
| 755 | + * return this object, which is responsible for implementing the | |
| 756 | + * AccessibleAction interface on behalf of itself. | |
| 757 | + * | |
| 758 | + * @return this object | |
| 759 | + * @see AccessibleAction | |
| 760 | + */ | |
| 761 | + public AccessibleAction getAccessibleAction() { | |
| 762 | + return this; | |
| 763 | + } | |
| 764 | + | |
| 765 | + /** | |
| 766 | + * Get the role of this object. | |
| 767 | + * | |
| 768 | + * @return an instance of AccessibleRole describing the role of the | |
| 769 | + * object | |
| 770 | + * @see AccessibleRole | |
| 771 | + */ | |
| 772 | + public AccessibleRole getAccessibleRole() { | |
| 773 | + return AccessibleRole.COMBO_BOX; | |
| 774 | + } | |
| 775 | + | |
| 776 | + /** | |
| 777 | + * Returns the number of accessible actions available in this object | |
| 778 | + * If there are more than one, the first one is considered the "default" | |
| 779 | + * action of the object. | |
| 780 | + * | |
| 781 | + * @return the zero-based number of Actions in this object | |
| 782 | + */ | |
| 783 | + public int getAccessibleActionCount() { | |
| 784 | + return 0; // To be fully implemented in a future release | |
| 785 | + } | |
| 786 | + | |
| 787 | + /** | |
| 788 | + * Returns a description of the specified action of the object. | |
| 789 | + * | |
| 790 | + * @param i zero-based index of the actions | |
| 791 | + * @return a String description of the action | |
| 792 | + * @see #getAccessibleActionCount | |
| 793 | + */ | |
| 794 | + public String getAccessibleActionDescription(int i) { | |
| 795 | + return null; // To be fully implemented in a future release | |
| 796 | + } | |
| 797 | + | |
| 798 | + /** | |
| 799 | + * Perform the specified Action on the object | |
| 800 | + * | |
| 801 | + * @param i zero-based index of actions | |
| 802 | + * @return true if the action was performed; otherwise false. | |
| 803 | + * @see #getAccessibleActionCount | |
| 804 | + */ | |
| 805 | + public boolean doAccessibleAction(int i) { | |
| 806 | + return false; // To be fully implemented in a future release | |
| 807 | + } | |
| 808 | + | |
| 809 | + } // inner class AccessibleAWTChoice | |
| 810 | + | |
| 811 | +} | |