jdk/src/share/classes/java/awt/PopupMenu.java
author martin
Mon, 10 Mar 2008 14:32:51 -0700
changeset 48 dc5744ca15ea
parent 2 90ce3da70b43
child 3084 67ca55732362
permissions -rw-r--r--
4960438: (process) Need IO redirection API for subprocesses Reviewed-by: alanb, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.peer.PopupMenuPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A class that implements a menu which can be dynamically popped up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * at a specified position within a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * As the inheritance hierarchy implies, a <code>PopupMenu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *  can be used anywhere a <code>Menu</code> can be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * However, if you use a <code>PopupMenu</code> like a <code>Menu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * (e.g., you add it to a <code>MenuBar</code>), then you <b>cannot</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * call <code>show</code> on that <code>PopupMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author      Amy Fowler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class PopupMenu extends Menu {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final String base = "popup";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static int nameCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    transient boolean isTrayIconPopup = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final long serialVersionUID = -4620452533522760060L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Creates a new popup menu with an empty name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public PopupMenu() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Creates a new popup menu with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param label a non-<code>null</code> string specifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *                the popup menu's label
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public PopupMenu(String label) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        super(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public MenuContainer getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (isTrayIconPopup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return super.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Constructs a name for this <code>MenuComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Called by <code>getName</code> when the name is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    String constructComponentName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        synchronized (PopupMenu.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return base + nameCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Creates the popup menu's peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * The peer allows us to change the appearance of the popup menu without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * changing any of the popup menu's functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public void addNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            // If our parent is not a Component, then this PopupMenu is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            // really just a plain, old Menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (parent != null && !(parent instanceof Component)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                super.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (peer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    peer = Toolkit.getDefaultToolkit().createPopupMenu(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                int nitems = getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                for (int i = 0 ; i < nitems ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    MenuItem mi = getItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    mi.parent = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    mi.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Shows the popup menu at the x, y position relative to an origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * The origin component must be contained within the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * hierarchy of the popup menu's parent.  Both the origin and the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * must be showing on the screen for this method to be valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * If this <code>PopupMenu</code> is being used as a <code>Menu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * (i.e., it has a non-<code>Component</code> parent),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * then you cannot call this method on the <code>PopupMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param origin the component which defines the coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param x the x coordinate position to popup the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param y the y coordinate position to popup the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception NullPointerException  if the parent is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @exception IllegalArgumentException  if this <code>PopupMenu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *                has a non-<code>Component</code> parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @exception IllegalArgumentException if the origin is not in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *                parent's heirarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception RuntimeException if the parent is not showing on screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public void show(Component origin, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // Use localParent for thread safety.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        MenuContainer localParent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (localParent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new NullPointerException("parent is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (!(localParent instanceof Component)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                "PopupMenus with non-Component parents cannot be shown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Component compParent = (Component)localParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        //Fixed 6278745: Incorrect exception throwing in PopupMenu.show() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        //Exception was not thrown if compParent was not equal to origin and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        //was not Container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (compParent != origin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (compParent instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                if (!((Container)compParent).isAncestorOf(origin)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    throw new IllegalArgumentException("origin not in parent's hierarchy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                throw new IllegalArgumentException("origin not in parent's hierarchy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (compParent.getPeer() == null || !compParent.isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new RuntimeException("parent not showing on screen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (peer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                ((PopupMenuPeer)peer).show(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    new Event(origin, 0, Event.MOUSE_DOWN, x, y, 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Gets the <code>AccessibleContext</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <code>PopupMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return the <code>AccessibleContext</code> of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *                <code>PopupMenu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            accessibleContext = new AccessibleAWTPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Inner class of PopupMenu used to provide default support for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * accessibility.  This class is not meant to be used directly by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * application developers, but is instead meant only to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * subclassed by menu component developers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * The class used to obtain the accessible role for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    protected class AccessibleAWTPopupMenu extends AccessibleAWTMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * JDK 1.3 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        private static final long serialVersionUID = -4282044795947239955L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return AccessibleRole.POPUP_MENU;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    } // class AccessibleAWTPopupMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
}