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