1 /*
   2  * Copyright (c) 1995, 2016, 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 
  26 package java.awt;
  27 
  28 import java.awt.event.ActionEvent;
  29 import java.awt.peer.MenuComponentPeer;
  30 import java.io.IOException;
  31 import java.io.ObjectInputStream;
  32 import java.security.AccessControlContext;
  33 import java.security.AccessController;
  34 
  35 import javax.accessibility.Accessible;
  36 import javax.accessibility.AccessibleComponent;
  37 import javax.accessibility.AccessibleContext;
  38 import javax.accessibility.AccessibleRole;
  39 import javax.accessibility.AccessibleSelection;
  40 import javax.accessibility.AccessibleState;
  41 import javax.accessibility.AccessibleStateSet;
  42 
  43 import sun.awt.AWTAccessor;
  44 import sun.awt.AppContext;
  45 import sun.awt.ComponentFactory;
  46 
  47 /**
  48  * The abstract class {@code MenuComponent} is the superclass
  49  * of all menu-related components. In this respect, the class
  50  * {@code MenuComponent} is analogous to the abstract superclass
  51  * {@code Component} for AWT components.
  52  * <p>
  53  * Menu components receive and process AWT events, just as components do,
  54  * through the method {@code processEvent}.
  55  *
  56  * @author      Arthur van Hoff
  57  * @since       1.0
  58  */
  59 public abstract class MenuComponent implements java.io.Serializable {
  60 
  61     static {
  62         /* ensure that the necessary native libraries are loaded */
  63         Toolkit.loadLibraries();
  64         if (!GraphicsEnvironment.isHeadless()) {
  65             initIDs();
  66         }
  67     }
  68 
  69     transient volatile MenuComponentPeer peer;
  70     transient volatile MenuContainer parent;
  71 
  72     /**
  73      * The {@code AppContext} of the {@code MenuComponent}.
  74      * This is set in the constructor and never changes.
  75      */
  76     private transient volatile AppContext appContext;
  77 
  78     /**
  79      * The menu component's font. This value can be
  80      * {@code null} at which point a default will be used.
  81      * This defaults to {@code null}.
  82      *
  83      * @serial
  84      * @see #setFont(Font)
  85      * @see #getFont()
  86      */
  87     private volatile Font font;
  88 
  89     /**
  90      * The menu component's name, which defaults to {@code null}.
  91      * @serial
  92      * @see #getName()
  93      * @see #setName(String)
  94      */
  95     private volatile String name;
  96 
  97     /**
  98      * A variable to indicate whether a name is explicitly set.
  99      * If {@code true} the name will be set explicitly.
 100      * This defaults to {@code false}.
 101      * @serial
 102      * @see #setName(String)
 103      */
 104     private volatile boolean nameExplicitlySet;
 105 
 106     /**
 107      * Defaults to {@code false}.
 108      * @serial
 109      * @see #dispatchEvent(AWTEvent)
 110      */
 111     volatile boolean newEventsOnly;
 112 
 113     /*
 114      * The menu's AccessControlContext.
 115      */
 116     private transient volatile AccessControlContext acc =
 117             AccessController.getContext();
 118 
 119     /*
 120      * Returns the acc this menu component was constructed with.
 121      */
 122     final AccessControlContext getAccessControlContext() {
 123         if (acc == null) {
 124             throw new SecurityException(
 125                     "MenuComponent is missing AccessControlContext");
 126         }
 127         return acc;
 128     }
 129 
 130     /*
 131      * Internal constants for serialization.
 132      */
 133     static final String actionListenerK = Component.actionListenerK;
 134     static final String itemListenerK = Component.itemListenerK;
 135 
 136     /*
 137      * JDK 1.1 serialVersionUID
 138      */
 139     private static final long serialVersionUID = -4536902356223894379L;
 140 
 141     static {
 142         AWTAccessor.setMenuComponentAccessor(
 143             new AWTAccessor.MenuComponentAccessor() {
 144                 @Override
 145                 public AppContext getAppContext(MenuComponent menuComp) {
 146                     return menuComp.appContext;
 147                 }
 148                 @Override
 149                 public void setAppContext(MenuComponent menuComp,
 150                                           AppContext appContext) {
 151                     menuComp.appContext = appContext;
 152                 }
 153                 @Override
 154                 @SuppressWarnings("unchecked")
 155                 public <T extends MenuComponentPeer> T getPeer(MenuComponent menuComp) {
 156                     return (T) menuComp.peer;
 157                 }
 158                 @Override
 159                 public MenuContainer getParent(MenuComponent menuComp) {
 160                     return menuComp.parent;
 161                 }
 162                 @Override
 163                 public void setParent(MenuComponent menuComp, MenuContainer menuContainer) {
 164                     menuComp.parent = menuContainer;
 165                 }
 166                 @Override
 167                 public Font getFont_NoClientCode(MenuComponent menuComp) {
 168                     return menuComp.getFont_NoClientCode();
 169                 }
 170             });
 171     }
 172 
 173     /**
 174      * Creates a {@code MenuComponent}.
 175      * @exception HeadlessException if
 176      *    {@code GraphicsEnvironment.isHeadless}
 177      *    returns {@code true}
 178      * @see java.awt.GraphicsEnvironment#isHeadless
 179      */
 180     public MenuComponent() throws HeadlessException {
 181         GraphicsEnvironment.checkHeadless();
 182         appContext = AppContext.getAppContext();
 183     }
 184 
 185     /**
 186      * Constructs a name for this {@code MenuComponent}.
 187      * Called by {@code getName} when the name is {@code null}.
 188      * @return a name for this {@code MenuComponent}
 189      */
 190     String constructComponentName() {
 191         return null; // For strict compliance with prior platform versions, a MenuComponent
 192                      // that doesn't set its name should return null from
 193                      // getName()
 194     }
 195 
 196     final ComponentFactory getComponentFactory() {
 197         final Toolkit toolkit = Toolkit.getDefaultToolkit();
 198         if (toolkit instanceof ComponentFactory) {
 199             return (ComponentFactory) toolkit;
 200         }
 201         throw new AWTError("UI components are unsupported by: " + toolkit);
 202     }
 203 
 204     /**
 205      * Gets the name of the menu component.
 206      * @return        the name of the menu component
 207      * @see           java.awt.MenuComponent#setName(java.lang.String)
 208      * @since         1.1
 209      */
 210     public String getName() {
 211         if (name == null && !nameExplicitlySet) {
 212             synchronized(this) {
 213                 if (name == null && !nameExplicitlySet)
 214                     name = constructComponentName();
 215             }
 216         }
 217         return name;
 218     }
 219 
 220     /**
 221      * Sets the name of the component to the specified string.
 222      * @param         name    the name of the menu component
 223      * @see           java.awt.MenuComponent#getName
 224      * @since         1.1
 225      */
 226     public void setName(String name) {
 227         synchronized(this) {
 228             this.name = name;
 229             nameExplicitlySet = true;
 230         }
 231     }
 232 
 233     /**
 234      * Returns the parent container for this menu component.
 235      * @return    the menu component containing this menu component,
 236      *                 or {@code null} if this menu component
 237      *                 is the outermost component, the menu bar itself
 238      */
 239     public MenuContainer getParent() {
 240         return getParent_NoClientCode();
 241     }
 242     // NOTE: This method may be called by privileged threads.
 243     //       This functionality is implemented in a package-private method
 244     //       to insure that it cannot be overridden by client subclasses.
 245     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
 246     final MenuContainer getParent_NoClientCode() {
 247         return parent;
 248     }
 249 
 250     /**
 251      * Gets the font used for this menu component.
 252      * @return   the font used in this menu component, if there is one;
 253      *                  {@code null} otherwise
 254      * @see     java.awt.MenuComponent#setFont
 255      */
 256     public Font getFont() {
 257         Font font = this.font;
 258         if (font != null) {
 259             return font;
 260         }
 261         MenuContainer parent = this.parent;
 262         if (parent != null) {
 263             return parent.getFont();
 264         }
 265         return null;
 266     }
 267 
 268     // NOTE: This method may be called by privileged threads.
 269     //       This functionality is implemented in a package-private method
 270     //       to insure that it cannot be overridden by client subclasses.
 271     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
 272     final Font getFont_NoClientCode() {
 273         Font font = this.font;
 274         if (font != null) {
 275             return font;
 276         }
 277 
 278         // The MenuContainer interface does not have getFont_NoClientCode()
 279         // and it cannot, because it must be package-private. Because of
 280         // this, we must manually cast classes that implement
 281         // MenuContainer.
 282         Object parent = this.parent;
 283         if (parent != null) {
 284             if (parent instanceof Component) {
 285                 font = ((Component)parent).getFont_NoClientCode();
 286             } else if (parent instanceof MenuComponent) {
 287                 font = ((MenuComponent)parent).getFont_NoClientCode();
 288             }
 289         }
 290         return font;
 291     } // getFont_NoClientCode()
 292 
 293 
 294     /**
 295      * Sets the font to be used for this menu component to the specified
 296      * font. This font is also used by all subcomponents of this menu
 297      * component, unless those subcomponents specify a different font.
 298      * <p>
 299      * Some platforms may not support setting of all font attributes
 300      * of a menu component; in such cases, calling {@code setFont}
 301      * will have no effect on the unsupported font attributes of this
 302      * menu component.  Unless subcomponents of this menu component
 303      * specify a different font, this font will be used by those
 304      * subcomponents if supported by the underlying platform.
 305      *
 306      * @param     f   the font to be set
 307      * @see       #getFont
 308      * @see       Font#getAttributes
 309      * @see       java.awt.font.TextAttribute
 310      */
 311     public void setFont(Font f) {
 312         synchronized (getTreeLock()) {
 313             font = f;
 314             //Fixed 6312943: NullPointerException in method MenuComponent.setFont(Font)
 315             MenuComponentPeer peer = this.peer;
 316             if (peer != null) {
 317                 peer.setFont(f);
 318             }
 319         }
 320     }
 321 
 322     /**
 323      * Removes the menu component's peer.  The peer allows us to modify the
 324      * appearance of the menu component without changing the functionality of
 325      * the menu component.
 326      */
 327     public void removeNotify() {
 328         synchronized (getTreeLock()) {
 329             MenuComponentPeer p = this.peer;
 330             if (p != null) {
 331                 Toolkit.getEventQueue().removeSourceEvents(this, true);
 332                 this.peer = null;
 333                 p.dispose();
 334             }
 335         }
 336     }
 337 
 338     /**
 339      * Posts the specified event to the menu.
 340      * This method is part of the Java&nbsp;1.0 event system
 341      * and it is maintained only for backwards compatibility.
 342      * Its use is discouraged, and it may not be supported
 343      * in the future.
 344      * @param evt the event which is to take place
 345      * @return unconditionally returns false
 346      * @deprecated As of JDK version 1.1, replaced by {@link
 347      * #dispatchEvent(AWTEvent) dispatchEvent}.
 348      */
 349     @Deprecated
 350     public boolean postEvent(Event evt) {
 351         MenuContainer parent = this.parent;
 352         if (parent != null) {
 353             parent.postEvent(evt);
 354         }
 355         return false;
 356     }
 357 
 358     /**
 359      * Delivers an event to this component or one of its sub components.
 360      * @param e the event
 361      */
 362     public final void dispatchEvent(AWTEvent e) {
 363         dispatchEventImpl(e);
 364     }
 365 
 366     void dispatchEventImpl(AWTEvent e) {
 367         EventQueue.setCurrentEventAndMostRecentTime(e);
 368 
 369         Toolkit.getDefaultToolkit().notifyAWTEventListeners(e);
 370 
 371         if (newEventsOnly ||
 372             (parent != null && parent instanceof MenuComponent &&
 373              ((MenuComponent)parent).newEventsOnly)) {
 374             if (eventEnabled(e)) {
 375                 processEvent(e);
 376             } else if (e instanceof ActionEvent && parent != null) {
 377                 e.setSource(parent);
 378                 ((MenuComponent)parent).dispatchEvent(e);
 379             }
 380 
 381         } else { // backward compatibility
 382             Event olde = e.convertToOld();
 383             if (olde != null) {
 384                 postEvent(olde);
 385             }
 386         }
 387     }
 388 
 389     // REMIND: remove when filtering is done at lower level
 390     boolean eventEnabled(AWTEvent e) {
 391         return false;
 392     }
 393     /**
 394      * Processes events occurring on this menu component.
 395      * <p>Note that if the event parameter is {@code null}
 396      * the behavior is unspecified and may result in an
 397      * exception.
 398      *
 399      * @param e the event
 400      * @since 1.1
 401      */
 402     protected void processEvent(AWTEvent e) {
 403     }
 404 
 405     /**
 406      * Returns a string representing the state of this
 407      * {@code MenuComponent}. This method is intended to be used
 408      * only for debugging purposes, and the content and format of the
 409      * returned string may vary between implementations. The returned
 410      * string may be empty but may not be {@code null}.
 411      *
 412      * @return     the parameter string of this menu component
 413      */
 414     protected String paramString() {
 415         String thisName = getName();
 416         return (thisName != null? thisName : "");
 417     }
 418 
 419     /**
 420      * Returns a representation of this menu component as a string.
 421      * @return  a string representation of this menu component
 422      */
 423     public String toString() {
 424         return getClass().getName() + "[" + paramString() + "]";
 425     }
 426 
 427     /**
 428      * Gets this component's locking object (the object that owns the thread
 429      * synchronization monitor) for AWT component-tree and layout
 430      * operations.
 431      * @return this component's locking object
 432      */
 433     protected final Object getTreeLock() {
 434         return Component.LOCK;
 435     }
 436 
 437     /**
 438      * Reads the menu component from an object input stream.
 439      *
 440      * @param s the {@code ObjectInputStream} to read
 441      * @exception HeadlessException if
 442      *   {@code GraphicsEnvironment.isHeadless} returns
 443      *   {@code true}
 444      * @serial
 445      * @see java.awt.GraphicsEnvironment#isHeadless
 446      */
 447     private void readObject(ObjectInputStream s)
 448         throws ClassNotFoundException, IOException, HeadlessException
 449     {
 450         GraphicsEnvironment.checkHeadless();
 451 
 452         acc = AccessController.getContext();
 453 
 454         s.defaultReadObject();
 455 
 456         appContext = AppContext.getAppContext();
 457     }
 458 
 459     /**
 460      * Initialize JNI field and method IDs.
 461      */
 462     private static native void initIDs();
 463 
 464 
 465     /*
 466      * --- Accessibility Support ---
 467      *
 468      *  MenuComponent will contain all of the methods in interface Accessible,
 469      *  though it won't actually implement the interface - that will be up
 470      *  to the individual objects which extend MenuComponent.
 471      */
 472 
 473     AccessibleContext accessibleContext = null;
 474 
 475     /**
 476      * Gets the {@code AccessibleContext} associated with
 477      * this {@code MenuComponent}.
 478      *
 479      * The method implemented by this base class returns {@code null}.
 480      * Classes that extend {@code MenuComponent}
 481      * should implement this method to return the
 482      * {@code AccessibleContext} associated with the subclass.
 483      *
 484      * @return the {@code AccessibleContext} of this
 485      *     {@code MenuComponent}
 486      * @since 1.3
 487      */
 488     public AccessibleContext getAccessibleContext() {
 489         return accessibleContext;
 490     }
 491 
 492     /**
 493      * Inner class of {@code MenuComponent} used to provide
 494      * default support for accessibility.  This class is not meant
 495      * to be used directly by application developers, but is instead
 496      * meant only to be subclassed by menu component developers.
 497      * <p>
 498      * The class used to obtain the accessible role for this object.
 499      * @since 1.3
 500      */
 501     protected abstract class AccessibleAWTMenuComponent
 502         extends AccessibleContext
 503         implements java.io.Serializable, AccessibleComponent,
 504                    AccessibleSelection
 505     {
 506         /*
 507          * JDK 1.3 serialVersionUID
 508          */
 509         private static final long serialVersionUID = -4269533416223798698L;
 510 
 511         /**
 512          * Although the class is abstract, this should be called by
 513          * all sub-classes.
 514          */
 515         protected AccessibleAWTMenuComponent() {
 516         }
 517 
 518         // AccessibleContext methods
 519         //
 520 
 521         /**
 522          * Gets the {@code AccessibleSelection} associated with this
 523          * object which allows its {@code Accessible} children to be selected.
 524          *
 525          * @return {@code AccessibleSelection} if supported by object;
 526          *      else return {@code null}
 527          * @see AccessibleSelection
 528          */
 529         public AccessibleSelection getAccessibleSelection() {
 530             return this;
 531         }
 532 
 533         /**
 534          * Gets the accessible name of this object.  This should almost never
 535          * return {@code java.awt.MenuComponent.getName}, as that
 536          * generally isn't a localized name, and doesn't have meaning for the
 537          * user.  If the object is fundamentally a text object (e.g. a menu item), the
 538          * accessible name should be the text of the object (e.g. "save").
 539          * If the object has a tooltip, the tooltip text may also be an
 540          * appropriate String to return.
 541          *
 542          * @return the localized name of the object -- can be {@code null}
 543          *         if this object does not have a name
 544          * @see AccessibleContext#setAccessibleName
 545          */
 546         public String getAccessibleName() {
 547             return accessibleName;
 548         }
 549 
 550         /**
 551          * Gets the accessible description of this object.  This should be
 552          * a concise, localized description of what this object is - what
 553          * is its meaning to the user.  If the object has a tooltip, the
 554          * tooltip text may be an appropriate string to return, assuming
 555          * it contains a concise description of the object (instead of just
 556          * the name of the object - e.g. a "Save" icon on a toolbar that
 557          * had "save" as the tooltip text shouldn't return the tooltip
 558          * text as the description, but something like "Saves the current
 559          * text document" instead).
 560          *
 561          * @return the localized description of the object -- can be
 562          *     {@code null} if this object does not have a description
 563          * @see AccessibleContext#setAccessibleDescription
 564          */
 565         public String getAccessibleDescription() {
 566             return accessibleDescription;
 567         }
 568 
 569         /**
 570          * Gets the role of this object.
 571          *
 572          * @return an instance of {@code AccessibleRole}
 573          *     describing the role of the object
 574          * @see AccessibleRole
 575          */
 576         public AccessibleRole getAccessibleRole() {
 577             return AccessibleRole.AWT_COMPONENT; // Non-specific -- overridden in subclasses
 578         }
 579 
 580         /**
 581          * Gets the state of this object.
 582          *
 583          * @return an instance of {@code AccessibleStateSet}
 584          *     containing the current state set of the object
 585          * @see AccessibleState
 586          */
 587         public AccessibleStateSet getAccessibleStateSet() {
 588             return MenuComponent.this.getAccessibleStateSet();
 589         }
 590 
 591         /**
 592          * Gets the {@code Accessible} parent of this object.
 593          * If the parent of this object implements {@code Accessible},
 594          * this method should simply return {@code getParent}.
 595          *
 596          * @return the {@code Accessible} parent of this object -- can
 597          *    be {@code null} if this object does not have an
 598          *    {@code Accessible} parent
 599          */
 600         public Accessible getAccessibleParent() {
 601             if (accessibleParent != null) {
 602                 return accessibleParent;
 603             } else {
 604                 MenuContainer parent = MenuComponent.this.getParent();
 605                 if (parent instanceof Accessible) {
 606                     return (Accessible) parent;
 607                 }
 608             }
 609             return null;
 610         }
 611 
 612         /**
 613          * Gets the index of this object in its accessible parent.
 614          *
 615          * @return the index of this object in its parent; -1 if this
 616          *     object does not have an accessible parent
 617          * @see #getAccessibleParent
 618          */
 619         public int getAccessibleIndexInParent() {
 620             return MenuComponent.this.getAccessibleIndexInParent();
 621         }
 622 
 623         /**
 624          * Returns the number of accessible children in the object.  If all
 625          * of the children of this object implement {@code Accessible},
 626          * then this method should return the number of children of this object.
 627          *
 628          * @return the number of accessible children in the object
 629          */
 630         public int getAccessibleChildrenCount() {
 631             return 0; // MenuComponents don't have children
 632         }
 633 
 634         /**
 635          * Returns the nth {@code Accessible} child of the object.
 636          *
 637          * @param i zero-based index of child
 638          * @return the nth Accessible child of the object
 639          */
 640         public Accessible getAccessibleChild(int i) {
 641             return null; // MenuComponents don't have children
 642         }
 643 
 644         /**
 645          * Returns the locale of this object.
 646          *
 647          * @return the locale of this object
 648          */
 649         public java.util.Locale getLocale() {
 650             MenuContainer parent = MenuComponent.this.getParent();
 651             if (parent instanceof Component)
 652                 return ((Component)parent).getLocale();
 653             else
 654                 return java.util.Locale.getDefault();
 655         }
 656 
 657         /**
 658          * Gets the {@code AccessibleComponent} associated with
 659          * this object if one exists.  Otherwise return {@code null}.
 660          *
 661          * @return the component
 662          */
 663         public AccessibleComponent getAccessibleComponent() {
 664             return this;
 665         }
 666 
 667 
 668         // AccessibleComponent methods
 669         //
 670         /**
 671          * Gets the background color of this object.
 672          *
 673          * @return the background color, if supported, of the object;
 674          *     otherwise, {@code null}
 675          */
 676         public Color getBackground() {
 677             return null; // Not supported for MenuComponents
 678         }
 679 
 680         /**
 681          * Sets the background color of this object.
 682          * (For transparency, see {@code isOpaque}.)
 683          *
 684          * @param c the new {@code Color} for the background
 685          * @see Component#isOpaque
 686          */
 687         public void setBackground(Color c) {
 688             // Not supported for MenuComponents
 689         }
 690 
 691         /**
 692          * Gets the foreground color of this object.
 693          *
 694          * @return the foreground color, if supported, of the object;
 695          *     otherwise, {@code null}
 696          */
 697         public Color getForeground() {
 698             return null; // Not supported for MenuComponents
 699         }
 700 
 701         /**
 702          * Sets the foreground color of this object.
 703          *
 704          * @param c the new {@code Color} for the foreground
 705          */
 706         public void setForeground(Color c) {
 707             // Not supported for MenuComponents
 708         }
 709 
 710         /**
 711          * Gets the {@code Cursor} of this object.
 712          *
 713          * @return the {@code Cursor}, if supported, of the object;
 714          *     otherwise, {@code null}
 715          */
 716         public Cursor getCursor() {
 717             return null; // Not supported for MenuComponents
 718         }
 719 
 720         /**
 721          * Sets the {@code Cursor} of this object.
 722          * <p>
 723          * The method may have no visual effect if the Java platform
 724          * implementation and/or the native system do not support
 725          * changing the mouse cursor shape.
 726          * @param cursor the new {@code Cursor} for the object
 727          */
 728         public void setCursor(Cursor cursor) {
 729             // Not supported for MenuComponents
 730         }
 731 
 732         /**
 733          * Gets the {@code Font} of this object.
 734          *
 735          * @return the {@code Font},if supported, for the object;
 736          *     otherwise, {@code null}
 737          */
 738         public Font getFont() {
 739             return MenuComponent.this.getFont();
 740         }
 741 
 742         /**
 743          * Sets the {@code Font} of this object.
 744          *
 745          * @param f the new {@code Font} for the object
 746          */
 747         public void setFont(Font f) {
 748             MenuComponent.this.setFont(f);
 749         }
 750 
 751         /**
 752          * Gets the {@code FontMetrics} of this object.
 753          *
 754          * @param f the {@code Font}
 755          * @return the FontMetrics, if supported, the object;
 756          *              otherwise, {@code null}
 757          * @see #getFont
 758          */
 759         public FontMetrics getFontMetrics(Font f) {
 760             return null; // Not supported for MenuComponents
 761         }
 762 
 763         /**
 764          * Determines if the object is enabled.
 765          *
 766          * @return true if object is enabled; otherwise, false
 767          */
 768         public boolean isEnabled() {
 769             return true; // Not supported for MenuComponents
 770         }
 771 
 772         /**
 773          * Sets the enabled state of the object.
 774          *
 775          * @param b if true, enables this object; otherwise, disables it
 776          */
 777         public void setEnabled(boolean b) {
 778             // Not supported for MenuComponents
 779         }
 780 
 781         /**
 782          * Determines if the object is visible.  Note: this means that the
 783          * object intends to be visible; however, it may not in fact be
 784          * showing on the screen because one of the objects that this object
 785          * is contained by is not visible.  To determine if an object is
 786          * showing on the screen, use {@code isShowing}.
 787          *
 788          * @return true if object is visible; otherwise, false
 789          */
 790         public boolean isVisible() {
 791             return true; // Not supported for MenuComponents
 792         }
 793 
 794         /**
 795          * Sets the visible state of the object.
 796          *
 797          * @param b if true, shows this object; otherwise, hides it
 798          */
 799         public void setVisible(boolean b) {
 800             // Not supported for MenuComponents
 801         }
 802 
 803         /**
 804          * Determines if the object is showing.  This is determined by checking
 805          * the visibility of the object and ancestors of the object.  Note:
 806          * this will return true even if the object is obscured by another
 807          * (for example, it happens to be underneath a menu that was pulled
 808          * down).
 809          *
 810          * @return true if object is showing; otherwise, false
 811          */
 812         public boolean isShowing() {
 813             return true; // Not supported for MenuComponents
 814         }
 815 
 816         /**
 817          * Checks whether the specified point is within this object's bounds,
 818          * where the point's x and y coordinates are defined to be relative to
 819          * the coordinate system of the object.
 820          *
 821          * @param p the {@code Point} relative to the coordinate
 822          *     system of the object
 823          * @return true if object contains {@code Point}; otherwise false
 824          */
 825         public boolean contains(Point p) {
 826             return false; // Not supported for MenuComponents
 827         }
 828 
 829         /**
 830          * Returns the location of the object on the screen.
 831          *
 832          * @return location of object on screen -- can be {@code null}
 833          *     if this object is not on the screen
 834          */
 835         public Point getLocationOnScreen() {
 836             return null; // Not supported for MenuComponents
 837         }
 838 
 839         /**
 840          * Gets the location of the object relative to the parent in the form
 841          * of a point specifying the object's top-left corner in the screen's
 842          * coordinate space.
 843          *
 844          * @return an instance of {@code Point} representing the
 845          *    top-left corner of the object's bounds in the coordinate
 846          *    space of the screen; {@code null} if
 847          *    this object or its parent are not on the screen
 848          */
 849         public Point getLocation() {
 850             return null; // Not supported for MenuComponents
 851         }
 852 
 853         /**
 854          * Sets the location of the object relative to the parent.
 855          */
 856         public void setLocation(Point p) {
 857             // Not supported for MenuComponents
 858         }
 859 
 860         /**
 861          * Gets the bounds of this object in the form of a
 862          * {@code Rectangle} object.
 863          * The bounds specify this object's width, height, and location
 864          * relative to its parent.
 865          *
 866          * @return a rectangle indicating this component's bounds;
 867          *     {@code null} if this object is not on the screen
 868          */
 869         public Rectangle getBounds() {
 870             return null; // Not supported for MenuComponents
 871         }
 872 
 873         /**
 874          * Sets the bounds of this object in the form of a
 875          * {@code Rectangle} object.
 876          * The bounds specify this object's width, height, and location
 877          * relative to its parent.
 878          *
 879          * @param r a rectangle indicating this component's bounds
 880          */
 881         public void setBounds(Rectangle r) {
 882             // Not supported for MenuComponents
 883         }
 884 
 885         /**
 886          * Returns the size of this object in the form of a
 887          * {@code Dimension} object. The height field of
 888          * the {@code Dimension} object contains this object's
 889          * height, and the width field of the {@code Dimension}
 890          * object contains this object's width.
 891          *
 892          * @return a {@code Dimension} object that indicates the
 893          *         size of this component; {@code null}
 894          *         if this object is not on the screen
 895          */
 896         public Dimension getSize() {
 897             return null; // Not supported for MenuComponents
 898         }
 899 
 900         /**
 901          * Resizes this object.
 902          *
 903          * @param d the {@code Dimension} specifying the
 904          *    new size of the object
 905          */
 906         public void setSize(Dimension d) {
 907             // Not supported for MenuComponents
 908         }
 909 
 910         /**
 911          * Returns the {@code Accessible} child, if one exists,
 912          * contained at the local coordinate {@code Point}.
 913          * If there is no {@code Accessible} child, {@code null}
 914          * is returned.
 915          *
 916          * @param p the point defining the top-left corner of the
 917          *    {@code Accessible}, given in the coordinate space
 918          *    of the object's parent
 919          * @return the {@code Accessible}, if it exists,
 920          *    at the specified location; else {@code null}
 921          */
 922         public Accessible getAccessibleAt(Point p) {
 923             return null; // MenuComponents don't have children
 924         }
 925 
 926         /**
 927          * Returns whether this object can accept focus or not.
 928          *
 929          * @return true if object can accept focus; otherwise false
 930          */
 931         public boolean isFocusTraversable() {
 932             return true; // Not supported for MenuComponents
 933         }
 934 
 935         /**
 936          * Requests focus for this object.
 937          */
 938         public void requestFocus() {
 939             // Not supported for MenuComponents
 940         }
 941 
 942         /**
 943          * Adds the specified focus listener to receive focus events from this
 944          * component.
 945          *
 946          * @param l the focus listener
 947          */
 948         public void addFocusListener(java.awt.event.FocusListener l) {
 949             // Not supported for MenuComponents
 950         }
 951 
 952         /**
 953          * Removes the specified focus listener so it no longer receives focus
 954          * events from this component.
 955          *
 956          * @param l the focus listener
 957          */
 958         public void removeFocusListener(java.awt.event.FocusListener l) {
 959             // Not supported for MenuComponents
 960         }
 961 
 962         // AccessibleSelection methods
 963         //
 964 
 965         /**
 966          * Returns the number of {@code Accessible} children currently selected.
 967          * If no children are selected, the return value will be 0.
 968          *
 969          * @return the number of items currently selected
 970          */
 971          public int getAccessibleSelectionCount() {
 972              return 0;  //  To be fully implemented in a future release
 973          }
 974 
 975         /**
 976          * Returns an {@code Accessible} representing the specified
 977          * selected child in the object.  If there isn't a selection, or there are
 978          * fewer children selected than the integer passed in, the return
 979          * value will be {@code null}.
 980          * <p>Note that the index represents the i-th selected child, which
 981          * is different from the i-th child.
 982          *
 983          * @param i the zero-based index of selected children
 984          * @return the i-th selected child
 985          * @see #getAccessibleSelectionCount
 986          */
 987          public Accessible getAccessibleSelection(int i) {
 988              return null;  //  To be fully implemented in a future release
 989          }
 990 
 991         /**
 992          * Determines if the current child of this object is selected.
 993          *
 994          * @return true if the current child of this object is selected;
 995          *    else false
 996          * @param i the zero-based index of the child in this
 997          *      {@code Accessible} object
 998          * @see AccessibleContext#getAccessibleChild
 999          */
1000          public boolean isAccessibleChildSelected(int i) {
1001              return false;  //  To be fully implemented in a future release
1002          }
1003 
1004         /**
1005          * Adds the specified {@code Accessible} child of the object
1006          * to the object's selection.  If the object supports multiple selections,
1007          * the specified child is added to any existing selection, otherwise
1008          * it replaces any existing selection in the object.  If the
1009          * specified child is already selected, this method has no effect.
1010          *
1011          * @param i the zero-based index of the child
1012          * @see AccessibleContext#getAccessibleChild
1013          */
1014          public void addAccessibleSelection(int i) {
1015                //  To be fully implemented in a future release
1016          }
1017 
1018         /**
1019          * Removes the specified child of the object from the object's
1020          * selection.  If the specified item isn't currently selected, this
1021          * method has no effect.
1022          *
1023          * @param i the zero-based index of the child
1024          * @see AccessibleContext#getAccessibleChild
1025          */
1026          public void removeAccessibleSelection(int i) {
1027                //  To be fully implemented in a future release
1028          }
1029 
1030         /**
1031          * Clears the selection in the object, so that no children in the
1032          * object are selected.
1033          */
1034          public void clearAccessibleSelection() {
1035                //  To be fully implemented in a future release
1036          }
1037 
1038         /**
1039          * Causes every child of the object to be selected
1040          * if the object supports multiple selections.
1041          */
1042          public void selectAllAccessibleSelection() {
1043                //  To be fully implemented in a future release
1044          }
1045 
1046     } // inner class AccessibleAWTComponent
1047 
1048     /**
1049      * Gets the index of this object in its accessible parent.
1050      *
1051      * @return -1 if this object does not have an accessible parent;
1052      *      otherwise, the index of the child in its accessible parent.
1053      */
1054     int getAccessibleIndexInParent() {
1055         MenuContainer localParent = parent;
1056         if (!(localParent instanceof MenuComponent)) {
1057             // MenuComponents only have accessible index when inside MenuComponents
1058             return -1;
1059         }
1060         MenuComponent localParentMenu = (MenuComponent)localParent;
1061         return localParentMenu.getAccessibleChildIndex(this);
1062     }
1063 
1064     /**
1065      * Gets the index of the child within this MenuComponent.
1066      *
1067      * @param child MenuComponent whose index we are interested in.
1068      * @return -1 if this object doesn't contain the child,
1069      *      otherwise, index of the child.
1070      */
1071     int getAccessibleChildIndex(MenuComponent child) {
1072         return -1; // Overridden in subclasses.
1073     }
1074 
1075     /**
1076      * Gets the state of this object.
1077      *
1078      * @return an instance of {@code AccessibleStateSet}
1079      *     containing the current state set of the object
1080      * @see AccessibleState
1081      */
1082     AccessibleStateSet getAccessibleStateSet() {
1083         AccessibleStateSet states = new AccessibleStateSet();
1084         return states;
1085     }
1086 
1087 }