1 /* 2 * Copyright (c) 1995, 2006, 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.awt.peer.MenuItemPeer; 28 import java.awt.event.*; 29 import java.util.EventListener; 30 import java.io.ObjectOutputStream; 31 import java.io.ObjectInputStream; 32 import java.io.IOException; 33 import javax.accessibility.*; 34 import sun.awt.AWTAccessor; 35 36 /** 37 * All items in a menu must belong to the class 38 * <code>MenuItem</code>, or one of its subclasses. 39 * <p> 40 * The default <code>MenuItem</code> object embodies 41 * a simple labeled menu item. 42 * <p> 43 * This picture of a menu bar shows five menu items: 44 * <IMG SRC="doc-files/MenuBar-1.gif" alt="The following text describes this graphic." 45 * ALIGN=CENTER HSPACE=10 VSPACE=7> 46 * <br CLEAR=LEFT> 47 * The first two items are simple menu items, labeled 48 * <code>"Basic"</code> and <code>"Simple"</code>. 49 * Following these two items is a separator, which is itself 50 * a menu item, created with the label <code>"-"</code>. 51 * Next is an instance of <code>CheckboxMenuItem</code> 52 * labeled <code>"Check"</code>. The final menu item is a 53 * submenu labeled <code>"More Examples"</code>, 54 * and this submenu is an instance of <code>Menu</code>. 55 * <p> 56 * When a menu item is selected, AWT sends an action event to 57 * the menu item. Since the event is an 58 * instance of <code>ActionEvent</code>, the <code>processEvent</code> 59 * method examines the event and passes it along to 60 * <code>processActionEvent</code>. The latter method redirects the 61 * event to any <code>ActionListener</code> objects that have 62 * registered an interest in action events generated by this 63 * menu item. 64 * <P> 65 * Note that the subclass <code>Menu</code> overrides this behavior and 66 * does not send any event to the frame until one of its subitems is 67 * selected. 68 * 69 * @author Sami Shaio 70 */ 71 public class MenuItem extends MenuComponent implements Accessible { 72 73 static { 74 /* ensure that the necessary native libraries are loaded */ 75 Toolkit.loadLibraries(); 76 if (!GraphicsEnvironment.isHeadless()) { 77 initIDs(); 78 } 79 80 AWTAccessor.setMenuItemAccessor( 81 new AWTAccessor.MenuItemAccessor() { 82 public boolean isEnabled(MenuItem item) { 83 return item.enabled; 84 } 85 86 public String getLabel(MenuItem item) { 87 return item.label; 88 } 89 90 public MenuShortcut getShortcut(MenuItem item) { 91 return item.shortcut; 92 } 93 94 public String getActionCommandImpl(MenuItem item) { 95 return item.getActionCommandImpl(); 96 } 97 98 public boolean isItemEnabled(MenuItem item) { 99 return item.isItemEnabled(); 100 } 101 }); 102 } 103 104 /** 105 * A value to indicate whether a menu item is enabled 106 * or not. If it is enabled, <code>enabled</code> will 107 * be set to true. Else <code>enabled</code> will 108 * be set to false. 109 * 110 * @serial 111 * @see #isEnabled() 112 * @see #setEnabled(boolean) 113 */ 114 boolean enabled = true; 115 116 /** 117 * <code>label</code> is the label of a menu item. 118 * It can be any string. 119 * 120 * @serial 121 * @see #getLabel() 122 * @see #setLabel(String) 123 */ 124 String label; 125 126 /** 127 * This field indicates the command tha has been issued 128 * by a particular menu item. 129 * By default the <code>actionCommand</code> 130 * is the label of the menu item, unless it has been 131 * set using setActionCommand. 132 * 133 * @serial 134 * @see #setActionCommand(String) 135 * @see #getActionCommand() 136 */ 137 String actionCommand; 138 139 /** 140 * The eventMask is ONLY set by subclasses via enableEvents. 141 * The mask should NOT be set when listeners are registered 142 * so that we can distinguish the difference between when 143 * listeners request events and subclasses request them. 144 * 145 * @serial 146 */ 147 long eventMask; 148 149 transient ActionListener actionListener; 150 151 /** 152 * A sequence of key stokes that ia associated with 153 * a menu item. 154 * Note :in 1.1.2 you must use setActionCommand() 155 * on a menu item in order for its shortcut to 156 * work. 157 * 158 * @serial 159 * @see #getShortcut() 160 * @see #setShortcut(MenuShortcut) 161 * @see #deleteShortcut() 162 */ 163 private MenuShortcut shortcut = null; 164 165 private static final String base = "menuitem"; 166 private static int nameCounter = 0; 167 168 /* 169 * JDK 1.1 serialVersionUID 170 */ 171 private static final long serialVersionUID = -21757335363267194L; 172 173 /** 174 * Constructs a new MenuItem with an empty label and no keyboard 175 * shortcut. 176 * @exception HeadlessException if GraphicsEnvironment.isHeadless() 177 * returns true. 178 * @see java.awt.GraphicsEnvironment#isHeadless 179 * @since JDK1.1 180 */ 181 public MenuItem() throws HeadlessException { 182 this("", null); 183 } 184 185 /** 186 * Constructs a new MenuItem with the specified label 187 * and no keyboard shortcut. Note that use of "-" in 188 * a label is reserved to indicate a separator between 189 * menu items. By default, all menu items except for 190 * separators are enabled. 191 * @param label the label for this menu item. 192 * @exception HeadlessException if GraphicsEnvironment.isHeadless() 193 * returns true. 194 * @see java.awt.GraphicsEnvironment#isHeadless 195 * @since JDK1.0 196 */ 197 public MenuItem(String label) throws HeadlessException { 198 this(label, null); 199 } 200 201 /** 202 * Create a menu item with an associated keyboard shortcut. 203 * Note that use of "-" in a label is reserved to indicate 204 * a separator between menu items. By default, all menu 205 * items except for separators are enabled. 206 * @param label the label for this menu item. 207 * @param s the instance of <code>MenuShortcut</code> 208 * associated with this menu item. 209 * @exception HeadlessException if GraphicsEnvironment.isHeadless() 210 * returns true. 211 * @see java.awt.GraphicsEnvironment#isHeadless 212 * @since JDK1.1 213 */ 214 public MenuItem(String label, MenuShortcut s) throws HeadlessException { 215 this.label = label; 216 this.shortcut = s; 217 } 218 219 /** 220 * Construct a name for this MenuComponent. Called by getName() when 221 * the name is null. 222 */ 223 String constructComponentName() { 224 synchronized (MenuItem.class) { 225 return base + nameCounter++; 226 } 227 } 228 229 /** 230 * Creates the menu item's peer. The peer allows us to modify the 231 * appearance of the menu item without changing its functionality. 232 */ 233 public void addNotify() { 234 synchronized (getTreeLock()) { 235 if (peer == null) 236 peer = Toolkit.getDefaultToolkit().createMenuItem(this); 237 } 238 } 239 240 /** 241 * Gets the label for this menu item. 242 * @return the label of this menu item, or <code>null</code> 243 if this menu item has no label. 244 * @see java.awt.MenuItem#setLabel 245 * @since JDK1.0 246 */ 247 public String getLabel() { 248 return label; 249 } 250 251 /** 252 * Sets the label for this menu item to the specified label. 253 * @param label the new label, or <code>null</code> for no label. 254 * @see java.awt.MenuItem#getLabel 255 * @since JDK1.0 256 */ 257 public synchronized void setLabel(String label) { 258 this.label = label; 259 MenuItemPeer peer = (MenuItemPeer)this.peer; 260 if (peer != null) { 261 peer.setLabel(label); 262 } 263 } 264 265 /** 266 * Checks whether this menu item is enabled. 267 * @see java.awt.MenuItem#setEnabled 268 * @since JDK1.0 269 */ 270 public boolean isEnabled() { 271 return enabled; 272 } 273 274 /** 275 * Sets whether or not this menu item can be chosen. 276 * @param b if <code>true</code>, enables this menu item; 277 * if <code>false</code>, disables it. 278 * @see java.awt.MenuItem#isEnabled 279 * @since JDK1.1 280 */ 281 public synchronized void setEnabled(boolean b) { 282 enable(b); 283 } 284 285 /** 286 * @deprecated As of JDK version 1.1, 287 * replaced by <code>setEnabled(boolean)</code>. 288 */ 289 @Deprecated 290 public synchronized void enable() { 291 enabled = true; 292 MenuItemPeer peer = (MenuItemPeer)this.peer; 293 if (peer != null) { 294 peer.enable(); 295 } 296 } 297 298 /** 299 * @deprecated As of JDK version 1.1, 300 * replaced by <code>setEnabled(boolean)</code>. 301 */ 302 @Deprecated 303 public void enable(boolean b) { 304 if (b) { 305 enable(); 306 } else { 307 disable(); 308 } 309 } 310 311 /** 312 * @deprecated As of JDK version 1.1, 313 * replaced by <code>setEnabled(boolean)</code>. 314 */ 315 @Deprecated 316 public synchronized void disable() { 317 enabled = false; 318 MenuItemPeer peer = (MenuItemPeer)this.peer; 319 if (peer != null) { 320 peer.disable(); 321 } 322 } 323 324 /** 325 * Get the <code>MenuShortcut</code> object associated with this 326 * menu item, 327 * @return the menu shortcut associated with this menu item, 328 * or <code>null</code> if none has been specified. 329 * @see java.awt.MenuItem#setShortcut 330 * @since JDK1.1 331 */ 332 public MenuShortcut getShortcut() { 333 return shortcut; 334 } 335 336 /** 337 * Set the <code>MenuShortcut</code> object associated with this 338 * menu item. If a menu shortcut is already associated with 339 * this menu item, it is replaced. 340 * @param s the menu shortcut to associate 341 * with this menu item. 342 * @see java.awt.MenuItem#getShortcut 343 * @since JDK1.1 344 */ 345 public void setShortcut(MenuShortcut s) { 346 shortcut = s; 347 MenuItemPeer peer = (MenuItemPeer)this.peer; 348 if (peer != null) { 349 peer.setLabel(label); 350 } 351 } 352 353 /** 354 * Delete any <code>MenuShortcut</code> object associated 355 * with this menu item. 356 * @since JDK1.1 357 */ 358 public void deleteShortcut() { 359 shortcut = null; 360 MenuItemPeer peer = (MenuItemPeer)this.peer; 361 if (peer != null) { 362 peer.setLabel(label); 363 } 364 } 365 366 /* 367 * Delete a matching MenuShortcut associated with this MenuItem. 368 * Used when iterating Menus. 369 */ 370 void deleteShortcut(MenuShortcut s) { 371 if (s.equals(shortcut)) { 372 shortcut = null; 373 MenuItemPeer peer = (MenuItemPeer)this.peer; 374 if (peer != null) { 375 peer.setLabel(label); 376 } 377 } 378 } 379 380 /* 381 * The main goal of this method is to post an appropriate event 382 * to the event queue when menu shortcut is pressed. However, 383 * in subclasses this method may do more than just posting 384 * an event. 385 */ 386 void doMenuEvent(long when, int modifiers) { 387 Toolkit.getEventQueue().postEvent( 388 new ActionEvent(this, ActionEvent.ACTION_PERFORMED, 389 getActionCommand(), when, modifiers)); 390 } 391 392 /* 393 * Returns true if the item and all its ancestors are 394 * enabled, false otherwise 395 */ 396 private final boolean isItemEnabled() { 397 // Fix For 6185151: Menu shortcuts of all menuitems within a menu 398 // should be disabled when the menu itself is disabled 399 if (!isEnabled()) { 400 return false; 401 } 402 MenuContainer container = getParent_NoClientCode(); 403 do { 404 if (!(container instanceof Menu)) { 405 return true; 406 } 407 Menu menu = (Menu)container; 408 if (!menu.isEnabled()) { 409 return false; 410 } 411 container = menu.getParent_NoClientCode(); 412 } while (container != null); 413 return true; 414 } 415 416 /* 417 * Post an ActionEvent to the target (on 418 * keydown) and the item is enabled. 419 * Returns true if there is an associated shortcut. 420 */ 421 boolean handleShortcut(KeyEvent e) { 422 MenuShortcut s = new MenuShortcut(e.getKeyCode(), 423 (e.getModifiers() & InputEvent.SHIFT_MASK) > 0); 424 // Fix For 6185151: Menu shortcuts of all menuitems within a menu 425 // should be disabled when the menu itself is disabled 426 if (s.equals(shortcut) && isItemEnabled()) { 427 // MenuShortcut match -- issue an event on keydown. 428 if (e.getID() == KeyEvent.KEY_PRESSED) { 429 doMenuEvent(e.getWhen(), e.getModifiers()); 430 } else { 431 // silently eat key release. 432 } 433 return true; 434 } 435 return false; 436 } 437 438 MenuItem getShortcutMenuItem(MenuShortcut s) { 439 return (s.equals(shortcut)) ? this : null; 440 } 441 442 /** 443 * Enables event delivery to this menu item for events 444 * to be defined by the specified event mask parameter 445 * <p> 446 * Since event types are automatically enabled when a listener for 447 * that type is added to the menu item, this method only needs 448 * to be invoked by subclasses of <code>MenuItem</code> which desire to 449 * have the specified event types delivered to <code>processEvent</code> 450 * regardless of whether a listener is registered. 451 * 452 * @param eventsToEnable the event mask defining the event types 453 * @see java.awt.MenuItem#processEvent 454 * @see java.awt.MenuItem#disableEvents 455 * @see java.awt.Component#enableEvents 456 * @since JDK1.1 457 */ 458 protected final void enableEvents(long eventsToEnable) { 459 eventMask |= eventsToEnable; 460 newEventsOnly = true; 461 } 462 463 /** 464 * Disables event delivery to this menu item for events 465 * defined by the specified event mask parameter. 466 * 467 * @param eventsToDisable the event mask defining the event types 468 * @see java.awt.MenuItem#processEvent 469 * @see java.awt.MenuItem#enableEvents 470 * @see java.awt.Component#disableEvents 471 * @since JDK1.1 472 */ 473 protected final void disableEvents(long eventsToDisable) { 474 eventMask &= ~eventsToDisable; 475 } 476 477 /** 478 * Sets the command name of the action event that is fired 479 * by this menu item. 480 * <p> 481 * By default, the action command is set to the label of 482 * the menu item. 483 * @param command the action command to be set 484 * for this menu item. 485 * @see java.awt.MenuItem#getActionCommand 486 * @since JDK1.1 487 */ 488 public void setActionCommand(String command) { 489 actionCommand = command; 490 } 491 492 /** 493 * Gets the command name of the action event that is fired 494 * by this menu item. 495 * @see java.awt.MenuItem#setActionCommand 496 * @since JDK1.1 497 */ 498 public String getActionCommand() { 499 return getActionCommandImpl(); 500 } 501 502 // This is final so it can be called on the Toolkit thread. 503 final String getActionCommandImpl() { 504 return (actionCommand == null? label : actionCommand); 505 } 506 507 /** 508 * Adds the specified action listener to receive action events 509 * from this menu item. 510 * If l is null, no exception is thrown and no action is performed. 511 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" 512 * >AWT Threading Issues</a> for details on AWT's threading model. 513 * 514 * @param l the action listener. 515 * @see #removeActionListener 516 * @see #getActionListeners 517 * @see java.awt.event.ActionEvent 518 * @see java.awt.event.ActionListener 519 * @since JDK1.1 520 */ 521 public synchronized void addActionListener(ActionListener l) { 522 if (l == null) { 523 return; 524 } 525 actionListener = AWTEventMulticaster.add(actionListener, l); 526 newEventsOnly = true; 527 } 528 529 /** 530 * Removes the specified action listener so it no longer receives 531 * action events from this menu item. 532 * If l is null, no exception is thrown and no action is performed. 533 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" 534 * >AWT Threading Issues</a> for details on AWT's threading model. 535 * 536 * @param l the action listener. 537 * @see #addActionListener 538 * @see #getActionListeners 539 * @see java.awt.event.ActionEvent 540 * @see java.awt.event.ActionListener 541 * @since JDK1.1 542 */ 543 public synchronized void removeActionListener(ActionListener l) { 544 if (l == null) { 545 return; 546 } 547 actionListener = AWTEventMulticaster.remove(actionListener, l); 548 } 549 550 /** 551 * Returns an array of all the action listeners 552 * registered on this menu item. 553 * 554 * @return all of this menu item's <code>ActionListener</code>s 555 * or an empty array if no action 556 * listeners are currently registered 557 * 558 * @see #addActionListener 559 * @see #removeActionListener 560 * @see java.awt.event.ActionEvent 561 * @see java.awt.event.ActionListener 562 * @since 1.4 563 */ 564 public synchronized ActionListener[] getActionListeners() { 565 return (ActionListener[])(getListeners(ActionListener.class)); 566 } 567 568 /** 569 * Returns an array of all the objects currently registered 570 * as <code><em>Foo</em>Listener</code>s 571 * upon this <code>MenuItem</code>. 572 * <code><em>Foo</em>Listener</code>s are registered using the 573 * <code>add<em>Foo</em>Listener</code> method. 574 * 575 * <p> 576 * You can specify the <code>listenerType</code> argument 577 * with a class literal, such as 578 * <code><em>Foo</em>Listener.class</code>. 579 * For example, you can query a 580 * <code>MenuItem</code> <code>m</code> 581 * for its action listeners with the following code: 582 * 583 * <pre>ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));</pre> 584 * 585 * If no such listeners exist, this method returns an empty array. 586 * 587 * @param listenerType the type of listeners requested; this parameter 588 * should specify an interface that descends from 589 * <code>java.util.EventListener</code> 590 * @return an array of all objects registered as 591 * <code><em>Foo</em>Listener</code>s on this menu item, 592 * or an empty array if no such 593 * listeners have been added 594 * @exception ClassCastException if <code>listenerType</code> 595 * doesn't specify a class or interface that implements 596 * <code>java.util.EventListener</code> 597 * 598 * @see #getActionListeners 599 * @since 1.3 600 */ 601 public <T extends EventListener> T[] getListeners(Class<T> listenerType) { 602 EventListener l = null; 603 if (listenerType == ActionListener.class) { 604 l = actionListener; 605 } 606 return AWTEventMulticaster.getListeners(l, listenerType); 607 } 608 609 /** 610 * Processes events on this menu item. If the event is an 611 * instance of <code>ActionEvent</code>, it invokes 612 * <code>processActionEvent</code>, another method 613 * defined by <code>MenuItem</code>. 614 * <p> 615 * Currently, menu items only support action events. 616 * <p>Note that if the event parameter is <code>null</code> 617 * the behavior is unspecified and may result in an 618 * exception. 619 * 620 * @param e the event 621 * @see java.awt.MenuItem#processActionEvent 622 * @since JDK1.1 623 */ 624 protected void processEvent(AWTEvent e) { 625 if (e instanceof ActionEvent) { 626 processActionEvent((ActionEvent)e); 627 } 628 } 629 630 // REMIND: remove when filtering is done at lower level 631 boolean eventEnabled(AWTEvent e) { 632 if (e.id == ActionEvent.ACTION_PERFORMED) { 633 if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 || 634 actionListener != null) { 635 return true; 636 } 637 return false; 638 } 639 return super.eventEnabled(e); 640 } 641 642 /** 643 * Processes action events occurring on this menu item, 644 * by dispatching them to any registered 645 * <code>ActionListener</code> objects. 646 * This method is not called unless action events are 647 * enabled for this component. Action events are enabled 648 * when one of the following occurs: 649 * <p><ul> 650 * <li>An <code>ActionListener</code> object is registered 651 * via <code>addActionListener</code>. 652 * <li>Action events are enabled via <code>enableEvents</code>. 653 * </ul> 654 * <p>Note that if the event parameter is <code>null</code> 655 * the behavior is unspecified and may result in an 656 * exception. 657 * 658 * @param e the action event 659 * @see java.awt.event.ActionEvent 660 * @see java.awt.event.ActionListener 661 * @see java.awt.MenuItem#enableEvents 662 * @since JDK1.1 663 */ 664 protected void processActionEvent(ActionEvent e) { 665 ActionListener listener = actionListener; 666 if (listener != null) { 667 listener.actionPerformed(e); 668 } 669 } 670 671 /** 672 * Returns a string representing the state of this <code>MenuItem</code>. 673 * This method is intended to be used only for debugging purposes, and the 674 * content and format of the returned string may vary between 675 * implementations. The returned string may be empty but may not be 676 * <code>null</code>. 677 * 678 * @return the parameter string of this menu item 679 */ 680 public String paramString() { 681 String str = ",label=" + label; 682 if (shortcut != null) { 683 str += ",shortcut=" + shortcut; 684 } 685 return super.paramString() + str; 686 } 687 688 689 /* Serialization support. 690 */ 691 692 /** 693 * Menu item serialized data version. 694 * 695 * @serial 696 */ 697 private int menuItemSerializedDataVersion = 1; 698 699 /** 700 * Writes default serializable fields to stream. Writes 701 * a list of serializable <code>ActionListeners</code> 702 * as optional data. The non-serializable listeners are 703 * detected and no attempt is made to serialize them. 704 * 705 * @param s the <code>ObjectOutputStream</code> to write 706 * @serialData <code>null</code> terminated sequence of 0 707 * or more pairs; the pair consists of a <code>String</code> 708 * and an <code>Object</code>; the <code>String</code> 709 * indicates the type of object and is one of the following: 710 * <code>actionListenerK</code> indicating an 711 * <code>ActionListener</code> object 712 * 713 * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener) 714 * @see #readObject(ObjectInputStream) 715 */ 716 private void writeObject(ObjectOutputStream s) 717 throws IOException 718 { 719 s.defaultWriteObject(); 720 721 AWTEventMulticaster.save(s, actionListenerK, actionListener); 722 s.writeObject(null); 723 } 724 725 /** 726 * Reads the <code>ObjectInputStream</code> and if it 727 * isn't <code>null</code> adds a listener to receive 728 * action events fired by the <code>Menu</code> Item. 729 * Unrecognized keys or values will be ignored. 730 * 731 * @param s the <code>ObjectInputStream</code> to read 732 * @exception HeadlessException if 733 * <code>GraphicsEnvironment.isHeadless</code> returns 734 * <code>true</code> 735 * @see #removeActionListener(ActionListener) 736 * @see #addActionListener(ActionListener) 737 * @see #writeObject(ObjectOutputStream) 738 */ 739 private void readObject(ObjectInputStream s) 740 throws ClassNotFoundException, IOException, HeadlessException 741 { 742 // HeadlessException will be thrown from MenuComponent's readObject 743 s.defaultReadObject(); 744 745 Object keyOrNull; 746 while(null != (keyOrNull = s.readObject())) { 747 String key = ((String)keyOrNull).intern(); 748 749 if (actionListenerK == key) 750 addActionListener((ActionListener)(s.readObject())); 751 752 else // skip value for unrecognized key 753 s.readObject(); 754 } 755 } 756 757 /** 758 * Initialize JNI field and method IDs 759 */ 760 private static native void initIDs(); 761 762 763 ///////////////// 764 // Accessibility support 765 //////////////// 766 767 /** 768 * Gets the AccessibleContext associated with this MenuItem. 769 * For menu items, the AccessibleContext takes the form of an 770 * AccessibleAWTMenuItem. 771 * A new AccessibleAWTMenuItem instance is created if necessary. 772 * 773 * @return an AccessibleAWTMenuItem that serves as the 774 * AccessibleContext of this MenuItem 775 * @since 1.3 776 */ 777 public AccessibleContext getAccessibleContext() { 778 if (accessibleContext == null) { 779 accessibleContext = new AccessibleAWTMenuItem(); 780 } 781 return accessibleContext; 782 } 783 784 /** 785 * Inner class of MenuItem used to provide default support for 786 * accessibility. This class is not meant to be used directly by 787 * application developers, but is instead meant only to be 788 * subclassed by menu component developers. 789 * <p> 790 * This class implements accessibility support for the 791 * <code>MenuItem</code> class. It provides an implementation of the 792 * Java Accessibility API appropriate to menu item user-interface elements. 793 * @since 1.3 794 */ 795 protected class AccessibleAWTMenuItem extends AccessibleAWTMenuComponent 796 implements AccessibleAction, AccessibleValue 797 { 798 /* 799 * JDK 1.3 serialVersionUID 800 */ 801 private static final long serialVersionUID = -217847831945965825L; 802 803 /** 804 * Get the accessible name of this object. 805 * 806 * @return the localized name of the object -- can be null if this 807 * object does not have a name 808 */ 809 public String getAccessibleName() { 810 if (accessibleName != null) { 811 return accessibleName; 812 } else { 813 if (getLabel() == null) { 814 return super.getAccessibleName(); 815 } else { 816 return getLabel(); 817 } 818 } 819 } 820 821 /** 822 * Get the role of this object. 823 * 824 * @return an instance of AccessibleRole describing the role of the 825 * object 826 */ 827 public AccessibleRole getAccessibleRole() { 828 return AccessibleRole.MENU_ITEM; 829 } 830 831 /** 832 * Get the AccessibleAction associated with this object. In the 833 * implementation of the Java Accessibility API for this class, 834 * return this object, which is responsible for implementing the 835 * AccessibleAction interface on behalf of itself. 836 * 837 * @return this object 838 */ 839 public AccessibleAction getAccessibleAction() { 840 return this; 841 } 842 843 /** 844 * Get the AccessibleValue associated with this object. In the 845 * implementation of the Java Accessibility API for this class, 846 * return this object, which is responsible for implementing the 847 * AccessibleValue interface on behalf of itself. 848 * 849 * @return this object 850 */ 851 public AccessibleValue getAccessibleValue() { 852 return this; 853 } 854 855 /** 856 * Returns the number of Actions available in this object. The 857 * default behavior of a menu item is to have one action. 858 * 859 * @return 1, the number of Actions in this object 860 */ 861 public int getAccessibleActionCount() { 862 return 1; 863 } 864 865 /** 866 * Return a description of the specified action of the object. 867 * 868 * @param i zero-based index of the actions 869 */ 870 public String getAccessibleActionDescription(int i) { 871 if (i == 0) { 872 // [[[PENDING: WDW -- need to provide a localized string]]] 873 return new String("click"); 874 } else { 875 return null; 876 } 877 } 878 879 /** 880 * Perform the specified Action on the object 881 * 882 * @param i zero-based index of actions 883 * @return true if the action was performed; otherwise false. 884 */ 885 public boolean doAccessibleAction(int i) { 886 if (i == 0) { 887 // Simulate a button click 888 Toolkit.getEventQueue().postEvent( 889 new ActionEvent(MenuItem.this, 890 ActionEvent.ACTION_PERFORMED, 891 MenuItem.this.getActionCommand(), 892 EventQueue.getMostRecentEventTime(), 893 0)); 894 return true; 895 } else { 896 return false; 897 } 898 } 899 900 /** 901 * Get the value of this object as a Number. 902 * 903 * @return An Integer of 0 if this isn't selected or an Integer of 1 if 904 * this is selected. 905 * @see javax.swing.AbstractButton#isSelected() 906 */ 907 public Number getCurrentAccessibleValue() { 908 return Integer.valueOf(0); 909 } 910 911 /** 912 * Set the value of this object as a Number. 913 * 914 * @return True if the value was set. 915 */ 916 public boolean setCurrentAccessibleValue(Number n) { 917 return false; 918 } 919 920 /** 921 * Get the minimum value of this object as a Number. 922 * 923 * @return An Integer of 0. 924 */ 925 public Number getMinimumAccessibleValue() { 926 return Integer.valueOf(0); 927 } 928 929 /** 930 * Get the maximum value of this object as a Number. 931 * 932 * @return An Integer of 0. 933 */ 934 public Number getMaximumAccessibleValue() { 935 return Integer.valueOf(0); 936 } 937 938 } // class AccessibleAWTMenuItem 939 940 }