0N/A/*
2362N/A * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt;
0N/A
0N/Aimport java.awt.peer.PopupMenuPeer;
0N/Aimport javax.accessibility.*;
0N/A
1338N/Aimport sun.awt.AWTAccessor;
0N/A
0N/A/**
0N/A * A class that implements a menu which can be dynamically popped up
0N/A * at a specified position within a component.
0N/A * <p>
0N/A * As the inheritance hierarchy implies, a <code>PopupMenu</code>
0N/A * can be used anywhere a <code>Menu</code> can be used.
0N/A * However, if you use a <code>PopupMenu</code> like a <code>Menu</code>
0N/A * (e.g., you add it to a <code>MenuBar</code>), then you <b>cannot</b>
0N/A * call <code>show</code> on that <code>PopupMenu</code>.
0N/A *
0N/A * @author Amy Fowler
0N/A */
0N/Apublic class PopupMenu extends Menu {
0N/A
0N/A private static final String base = "popup";
0N/A static int nameCounter = 0;
0N/A
0N/A transient boolean isTrayIconPopup = false;
0N/A
1338N/A static {
1338N/A AWTAccessor.setPopupMenuAccessor(
1338N/A new AWTAccessor.PopupMenuAccessor() {
1338N/A public boolean isTrayIconPopup(PopupMenu popupMenu) {
1338N/A return popupMenu.isTrayIconPopup;
1338N/A }
1338N/A });
1338N/A }
1338N/A
0N/A /*
0N/A * JDK 1.1 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = -4620452533522760060L;
0N/A
0N/A /**
0N/A * Creates a new popup menu with an empty name.
0N/A * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A */
0N/A public PopupMenu() throws HeadlessException {
0N/A this("");
0N/A }
0N/A
0N/A /**
0N/A * Creates a new popup menu with the specified name.
0N/A *
0N/A * @param label a non-<code>null</code> string specifying
0N/A * the popup menu's label
0N/A * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A */
0N/A public PopupMenu(String label) throws HeadlessException {
0N/A super(label);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public MenuContainer getParent() {
0N/A if (isTrayIconPopup) {
0N/A return null;
0N/A }
0N/A return super.getParent();
0N/A }
0N/A
0N/A /**
0N/A * Constructs a name for this <code>MenuComponent</code>.
0N/A * Called by <code>getName</code> when the name is <code>null</code>.
0N/A */
0N/A String constructComponentName() {
0N/A synchronized (PopupMenu.class) {
0N/A return base + nameCounter++;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates the popup menu's peer.
0N/A * The peer allows us to change the appearance of the popup menu without
0N/A * changing any of the popup menu's functionality.
0N/A */
0N/A public void addNotify() {
0N/A synchronized (getTreeLock()) {
0N/A // If our parent is not a Component, then this PopupMenu is
0N/A // really just a plain, old Menu.
0N/A if (parent != null && !(parent instanceof Component)) {
0N/A super.addNotify();
0N/A }
0N/A else {
0N/A if (peer == null)
0N/A peer = Toolkit.getDefaultToolkit().createPopupMenu(this);
0N/A int nitems = getItemCount();
0N/A for (int i = 0 ; i < nitems ; i++) {
0N/A MenuItem mi = getItem(i);
0N/A mi.parent = this;
0N/A mi.addNotify();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Shows the popup menu at the x, y position relative to an origin
0N/A * component.
0N/A * The origin component must be contained within the component
0N/A * hierarchy of the popup menu's parent. Both the origin and the parent
0N/A * must be showing on the screen for this method to be valid.
0N/A * <p>
0N/A * If this <code>PopupMenu</code> is being used as a <code>Menu</code>
0N/A * (i.e., it has a non-<code>Component</code> parent),
0N/A * then you cannot call this method on the <code>PopupMenu</code>.
0N/A *
0N/A * @param origin the component which defines the coordinate space
0N/A * @param x the x coordinate position to popup the menu
0N/A * @param y the y coordinate position to popup the menu
0N/A * @exception NullPointerException if the parent is <code>null</code>
0N/A * @exception IllegalArgumentException if this <code>PopupMenu</code>
0N/A * has a non-<code>Component</code> parent
0N/A * @exception IllegalArgumentException if the origin is not in the
0N/A * parent's heirarchy
0N/A * @exception RuntimeException if the parent is not showing on screen
0N/A */
0N/A public void show(Component origin, int x, int y) {
0N/A // Use localParent for thread safety.
0N/A MenuContainer localParent = parent;
0N/A if (localParent == null) {
0N/A throw new NullPointerException("parent is null");
0N/A }
0N/A if (!(localParent instanceof Component)) {
0N/A throw new IllegalArgumentException(
0N/A "PopupMenus with non-Component parents cannot be shown");
0N/A }
0N/A Component compParent = (Component)localParent;
0N/A //Fixed 6278745: Incorrect exception throwing in PopupMenu.show() method
0N/A //Exception was not thrown if compParent was not equal to origin and
0N/A //was not Container
0N/A if (compParent != origin) {
0N/A if (compParent instanceof Container) {
0N/A if (!((Container)compParent).isAncestorOf(origin)) {
0N/A throw new IllegalArgumentException("origin not in parent's hierarchy");
0N/A }
0N/A } else {
0N/A throw new IllegalArgumentException("origin not in parent's hierarchy");
0N/A }
0N/A }
0N/A if (compParent.getPeer() == null || !compParent.isShowing()) {
0N/A throw new RuntimeException("parent not showing on screen");
0N/A }
0N/A if (peer == null) {
0N/A addNotify();
0N/A }
0N/A synchronized (getTreeLock()) {
0N/A if (peer != null) {
0N/A ((PopupMenuPeer)peer).show(
0N/A new Event(origin, 0, Event.MOUSE_DOWN, x, y, 0, 0));
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A /**
0N/A * Gets the <code>AccessibleContext</code> associated with this
0N/A * <code>PopupMenu</code>.
0N/A *
0N/A * @return the <code>AccessibleContext</code> of this
0N/A * <code>PopupMenu</code>
0N/A * @since 1.3
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleAWTPopupMenu();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * Inner class of PopupMenu used to provide default support for
0N/A * accessibility. This class is not meant to be used directly by
0N/A * application developers, but is instead meant only to be
0N/A * subclassed by menu component developers.
0N/A * <p>
0N/A * The class used to obtain the accessible role for this object.
0N/A * @since 1.3
0N/A */
0N/A protected class AccessibleAWTPopupMenu extends AccessibleAWTMenu
0N/A {
0N/A /*
0N/A * JDK 1.3 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = -4282044795947239955L;
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the
0N/A * object
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.POPUP_MENU;
0N/A }
0N/A
0N/A } // class AccessibleAWTPopupMenu
0N/A
0N/A}