jdk/src/java.desktop/share/classes/javax/swing/JMenu.java
author neugens
Wed, 11 Feb 2015 07:54:39 +0100
changeset 29248 fcc330d07e3d
parent 25859 3317bb8137f4
child 33253 78e735319356
permissions -rw-r--r--
8071705: Java application menu misbehaves when running multiple screen stacked vertically Summary: JMenu miscalculates the position of the Popup origin when on multiple monitors stacked vertically Reviewed-by: alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
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 javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.AWTEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.ComponentOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.GraphicsConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.GraphicsDevice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.Polygon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.swing.plaf.basic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * An implementation of a menu -- a popup window containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <code>JMenuItem</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * is displayed when the user selects an item on the <code>JMenuBar</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * In addition to <code>JMenuItem</code>s, a <code>JMenu</code> can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * also contain <code>JSeparator</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * In essence, a menu is a button with an associated <code>JPopupMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * When the "button" is pressed, the <code>JPopupMenu</code> appears. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * "button" is on the <code>JMenuBar</code>, the menu is a top-level window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * If the "button" is another menu item, then the <code>JPopupMenu</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * "pull-right" menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Menus can be configured, and to some degree controlled, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <code><a href="Action.html">Action</a></code>s.  Using an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <code>Action</code> with a menu has many benefits beyond directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * configuring a menu.  Refer to <a href="Action.html#buttonActions">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Swing Components Supporting <code>Action</code></a> for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * details, and you can find more information in <a
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 11268
diff changeset
    79
 * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * For information and examples of using menus see
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 11268
diff changeset
    83
 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
    96
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *   attribute: isContainer true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * description: A popup window containing menu items displayed in a menu bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * @author Georges Saab
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * @author David Karlton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * @see JMenuItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * @see JSeparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @see JMenuBar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @see JPopupMenu
25201
4adc75e0c4e5 8046485: Add missing @since tag under javax.swing.*
henryjen
parents: 23010
diff changeset
   111
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   113
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
public class JMenu extends JMenuItem implements Accessible,MenuElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static final String uiClassID = "MenuUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The popup menu portion of the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private JPopupMenu popupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * The button's model listeners.  Default is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private ChangeListener menuChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Only one <code>MenuEvent</code> is needed for each menu since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * event's only state is the source property.  The source of events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * generated is always "this".  Default is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private MenuEvent menuEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Used by the look and feel (L&F) code to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * implementation specific menu behaviors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private int delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
      * Location of the popup component. Location is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
      * if it was not customized by <code>setMenuLocation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     private Point customMenuLocation = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /* Diagnostic aids -- should be false for production builds. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static final boolean TRACE =   false; // trace creates and disposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static final boolean VERBOSE = false; // show reuse hits/misses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static final boolean DEBUG =   false;  // show bad params, misc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Constructs a new <code>JMenu</code> with no text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public JMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Constructs a new <code>JMenu</code> with the supplied string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * as its text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param s  the text for the menu label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public JMenu(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        super(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Constructs a menu whose properties are taken from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <code>Action</code> supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param a an <code>Action</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public JMenu(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        setAction(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Constructs a new <code>JMenu</code> with the supplied string as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * its text and specified as a tear-off menu or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param s the text for the menu label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param b can the menu be torn off (not yet implemented)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public JMenu(String s, boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        this(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Overriden to do nothing. We want JMenu to be focusable, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <code>JMenuItem</code> doesn't want to be, thus we override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * do nothing. We don't invoke <code>setFocusable(true)</code> after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * super's constructor has completed as this has the side effect that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <code>JMenu</code> will be considered traversable via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * keyboard, which we don't want. Making a Component traversable by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * the keyboard after invoking <code>setFocusable(true)</code> is OK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * as <code>setFocusable</code> is new API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * and is speced as such, but internally we don't want to use it like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * this else we change the keyboard traversability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    void initFocusability() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Resets the UI property with a value from the current look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see JComponent#updateUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        setUI((MenuItemUI)UIManager.getUI(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if ( popupMenu != null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
          {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            popupMenu.setUI((PopupMenuUI)UIManager.getUI(popupMenu));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   229
     * Returns the name of the L&amp;F class that renders this component.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @return the string "MenuUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    //    public void repaint(long tm, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    //        Thread.currentThread().dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    //        super.repaint(tm,x,y,width,height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    //    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Sets the data model for the "menu button" -- the label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * that the user clicks to open or close the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param newModel the <code>ButtonModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * description: The menu's model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *      expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *      hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public void setModel(ButtonModel newModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        ButtonModel oldModel = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        super.setModel(newModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (oldModel != null && menuChangeListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            oldModel.removeChangeListener(menuChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            menuChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        model = newModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (newModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            menuChangeListener = createMenuChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            newModel.addChangeListener(menuChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Returns true if the menu is currently selected (highlighted).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @return true if the menu is selected, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public boolean isSelected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return getModel().isSelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Sets the selection status of the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param b  true to select (highlight) the menu; false to de-select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *          the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *      description: When the menu is selected, its popup child is shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *           expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *           hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public void setSelected(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        ButtonModel model = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        boolean oldValue = model.isSelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // TIGER - 4840653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // Removed code which fired an AccessibleState.SELECTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // PropertyChangeEvent since this resulted in two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        // identical events being fired since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // AbstractButton.fireItemStateChanged also fires the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // same event. This caused screen readers to speak the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // name of the item twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (b != model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            getModel().setSelected(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Returns true if the menu's popup window is visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @return true if the menu is visible, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public boolean isPopupMenuVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return popupMenu.isVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Sets the visibility of the menu's popup.  If the menu is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * not enabled, this method will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param b  a boolean value -- true to make the menu visible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *           false to hide it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *      description: The popup menu's visibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *           expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *           hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public void setPopupMenuVisible(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            System.out.println("in JMenu.setPopupMenuVisible " + b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        boolean isVisible = isPopupMenuVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (b != isVisible && (isEnabled() || !b)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if ((b==true) && isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                // Set location of popupMenu (pulldown or pullright)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                Point p = getCustomMenuLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    p = getPopupMenuOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                getPopupMenu().show(this, p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                getPopupMenu().setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Computes the origin for the <code>JMenu</code>'s popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * This method uses Look and Feel properties named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <code>Menu.menuPopupOffsetX</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * <code>Menu.menuPopupOffsetY</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <code>Menu.submenuPopupOffsetX</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <code>Menu.submenuPopupOffsetY</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * to adjust the exact location of popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @return a <code>Point</code> in the coordinate space of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *          menu which should be used as the origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *          of the <code>JMenu</code>'s popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    protected Point getPopupMenuOrigin() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   369
        int x;
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   370
        int y;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        JPopupMenu pm = getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // Figure out the sizes needed to caclulate the menu position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        Dimension s = getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        Dimension pmSize = pm.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // For the first time the menu is popped up,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        // the size has not yet been initiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (pmSize.width==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            pmSize = pm.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        Point position = getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        Toolkit toolkit = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        GraphicsConfiguration gc = getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        GraphicsEnvironment ge =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            GraphicsEnvironment.getLocalGraphicsEnvironment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        GraphicsDevice[] gd = ge.getScreenDevices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        for(int i = 0; i < gd.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                GraphicsConfiguration dgc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    gd[i].getDefaultConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                if(dgc.getBounds().contains(position)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    gc = dgc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (gc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            screenBounds = gc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            // take screen insets (e.g. taskbar) into account
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            Insets screenInsets = toolkit.getScreenInsets(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            screenBounds.width -=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        Math.abs(screenInsets.left + screenInsets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            screenBounds.height -=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        Math.abs(screenInsets.top + screenInsets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            position.x -= Math.abs(screenInsets.left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            position.y -= Math.abs(screenInsets.top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        Container parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (parent instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            // We are a submenu (pull-right)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            int xOffset = UIManager.getInt("Menu.submenuPopupOffsetX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            int yOffset = UIManager.getInt("Menu.submenuPopupOffsetY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if( SwingUtilities.isLeftToRight(this) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                // First determine x:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                x = s.width + xOffset;   // Prefer placement to the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                if (position.x + x + pmSize.width >= screenBounds.width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                                                     + screenBounds.x &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    // popup doesn't fit - place it wherever there's more room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    screenBounds.width - s.width < 2*(position.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                                    - screenBounds.x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    x = 0 - xOffset - pmSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                // First determine x:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                x = 0 - xOffset - pmSize.width; // Prefer placement to the left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                if (position.x + x < screenBounds.x &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    // popup doesn't fit - place it wherever there's more room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    screenBounds.width - s.width > 2*(position.x -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                                    screenBounds.x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    x = s.width + xOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            // Then the y:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            y = yOffset;                     // Prefer dropping down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            if (position.y + y + pmSize.height >= screenBounds.height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                                  + screenBounds.y &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                // popup doesn't fit - place it wherever there's more room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                screenBounds.height - s.height < 2*(position.y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                                                  - screenBounds.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                y = s.height - yOffset - pmSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            // We are a toplevel menu (pull-down)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            int xOffset = UIManager.getInt("Menu.menuPopupOffsetX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            int yOffset = UIManager.getInt("Menu.menuPopupOffsetY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if( SwingUtilities.isLeftToRight(this) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                // First determine the x:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                x = xOffset;                   // Extend to the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                if (position.x + x + pmSize.width >= screenBounds.width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                                     + screenBounds.x &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    // popup doesn't fit - place it wherever there's more room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    screenBounds.width - s.width < 2*(position.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                                    - screenBounds.x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    x = s.width - xOffset - pmSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                // First determine the x:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                x = s.width - xOffset - pmSize.width; // Extend to the left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                if (position.x + x < screenBounds.x &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    // popup doesn't fit - place it wherever there's more room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    screenBounds.width - s.width > 2*(position.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                                    - screenBounds.x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    x = xOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            // Then the y:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            y = s.height + yOffset;    // Prefer dropping down
29248
fcc330d07e3d 8071705: Java application menu misbehaves when running multiple screen stacked vertically
neugens
parents: 25859
diff changeset
   479
            if (position.y + y + pmSize.height >= screenBounds.height
fcc330d07e3d 8071705: Java application menu misbehaves when running multiple screen stacked vertically
neugens
parents: 25859
diff changeset
   480
                                                  + screenBounds.y &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                // popup doesn't fit - place it wherever there's more room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                screenBounds.height - s.height < 2*(position.y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                                  - screenBounds.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                y = 0 - yOffset - pmSize.height;   // Otherwise drop 'up'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        return new Point(x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Returns the suggested delay, in milliseconds, before submenus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * are popped up or down.
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   495
     * Each look and feel (L&amp;F) may determine its own policy for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * observing the <code>delay</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * In most cases, the delay is not observed for top level menus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * or while dragging.  The default for <code>delay</code> is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * This method is a property of the look and feel code and is used
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   500
     * to manage the idiosyncrasies of the various UI implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @return the <code>delay</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public int getDelay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Sets the suggested delay before the menu's <code>PopupMenu</code>
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   511
     * is popped up or down.  Each look and feel (L&amp;F) may determine
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * it's own policy for observing the delay property.  In most cases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * the delay is not observed for top level menus or while dragging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * This method is a property of the look and feel code and is used
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   515
     * to manage the idiosyncrasies of the various UI implementations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @param       d the number of milliseconds to delay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @exception   IllegalArgumentException if <code>d</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *                       is less than 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *      description: The delay between menu selection and making the popup menu visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *           expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    public void setDelay(int d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (d < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            throw new IllegalArgumentException("Delay must be a positive integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        delay = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * The window-closing listener for the popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @see WinListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    protected WinListener popupListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    private void ensurePopupMenuCreated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (popupMenu == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            final JMenu thisMenu = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            this.popupMenu = new JPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            popupMenu.setInvoker(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            popupListener = createWinListener(popupMenu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Return the customized location of the popup component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    private Point getCustomMenuLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return customMenuLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Sets the location of the popup component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @param x the x coordinate of the popup's new position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @param y the y coordinate of the popup's new position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public void setMenuLocation(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        customMenuLocation = new Point(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            popupMenu.setLocation(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Appends a menu item to the end of this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Returns the menu item added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @param menuItem the <code>JMenuitem</code> to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @return the <code>JMenuItem</code> added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    public JMenuItem add(JMenuItem menuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return popupMenu.add(menuItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Appends a component to the end of this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Returns the component added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @param c the <code>Component</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @return the <code>Component</code> added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    public Component add(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        popupMenu.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Adds the specified component to this container at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * position. If <code>index</code> equals -1, the component will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * be appended to the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @param     c   the <code>Component</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @param     index    the position at which to insert the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @return    the <code>Component</code> added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @see       #remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @see java.awt.Container#add(Component, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    public Component add(Component c, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        popupMenu.add(c, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Creates a new menu item with the specified text and appends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * it to the end of this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @param s the string for the menu item to be added
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   612
     * @return the new {@code JMenuItem}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    public JMenuItem add(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        return add(new JMenuItem(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /**
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   619
     * Creates a new menu item attached to the specified {@code Action} object
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   620
     * and appends it to the end of this menu.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   622
     * @param a the {@code Action} for the menu item to be added
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   623
     * @return the new {@code JMenuItem}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @see Action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    public JMenuItem add(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        JMenuItem mi = createActionComponent(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        mi.setAction(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        add(mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * Factory method which creates the <code>JMenuItem</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * <code>Action</code>s added to the <code>JMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @param a the <code>Action</code> for the menu item to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @return the new menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @see Action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    protected JMenuItem createActionComponent(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        JMenuItem mi = new JMenuItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                PropertyChangeListener pcl = createActionChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                if (pcl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    pcl = super.createActionPropertyChangeListener(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                return pcl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        mi.setHorizontalTextPosition(JButton.TRAILING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        mi.setVerticalTextPosition(JButton.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   659
     * Returns a properly configured {@code PropertyChangeListener}
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   660
     * which updates the control as changes to the {@code Action} occur.
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   661
     *
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   662
     * @param b a menu item for which to create a {@code PropertyChangeListener}
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   663
     * @return a {@code PropertyChangeListener} for {@code b}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    protected PropertyChangeListener createActionChangeListener(JMenuItem b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        return b.createActionPropertyChangeListener0(b.getAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * Appends a new separator to the end of the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    public void addSeparator()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        popupMenu.addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * Inserts a new menu item with the specified text at a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * given position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @param s the text for the menu item to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @param pos an integer specifying the position at which to add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *               new menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @exception IllegalArgumentException when the value of
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   686
     *                  <code>pos</code> &lt; 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    public void insert(String s, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        popupMenu.insert(new JMenuItem(s), pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * Inserts the specified <code>JMenuitem</code> at a given position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @param mi the <code>JMenuitem</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @param pos an integer specifying the position at which to add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *               new <code>JMenuitem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @return the new menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @exception IllegalArgumentException if the value of
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   705
     *                  <code>pos</code> &lt; 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    public JMenuItem insert(JMenuItem mi, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        popupMenu.insert(mi, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * Inserts a new menu item attached to the specified <code>Action</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * object at a given position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @param a the <code>Action</code> object for the menu item to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @param pos an integer specifying the position at which to add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *               new menu item
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   723
     * @return the new menu item
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @exception IllegalArgumentException if the value of
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   725
     *                  <code>pos</code> &lt; 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    public JMenuItem insert(Action a, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        JMenuItem mi = new JMenuItem(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        mi.setHorizontalTextPosition(JButton.TRAILING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        mi.setVerticalTextPosition(JButton.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        popupMenu.insert(mi, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * Inserts a separator at the specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @param       index an integer specifying the position at which to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *                    insert the menu separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @exception   IllegalArgumentException if the value of
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   746
     *                       <code>index</code> &lt; 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    public void insertSeparator(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        popupMenu.insert( new JPopupMenu.Separator(), index );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /**
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   758
     * Returns the {@code JMenuItem} at the specified position.
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   759
     * If the component at {@code pos} is not a menu item,
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   760
     * {@code null} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * This method is included for AWT compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   763
     * @param pos  an integer specifying the position
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @return  the menu item at the specified position; or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *          if the item as the specified position is not a menu item
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   766
     * @exception  IllegalArgumentException if the value of
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   767
     *             {@code pos} &lt; 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    public JMenuItem getItem(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        Component c = getMenuComponent(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        if (c instanceof JMenuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            JMenuItem mi = (JMenuItem) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        // 4173633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * Returns the number of items on the menu, including separators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * This method is included for AWT compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @return an integer equal to the number of items on the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @see #getMenuComponentCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    public int getItemCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        return getMenuComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * Returns true if the menu can be torn off.  This method is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * yet implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @return true if the menu can be torn off, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * @exception  Error  if invoked -- this method is not yet implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    public boolean isTearOff() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        throw new Error("boolean isTearOff() {} not yet implemented");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Removes the specified menu item from this menu.  If there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * popup menu, this method will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @param    item the <code>JMenuItem</code> to be removed from the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    public void remove(JMenuItem item) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            popupMenu.remove(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Removes the menu item at the specified index from this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @param       pos the position of the item to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @exception   IllegalArgumentException if the value of
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 21278
diff changeset
   822
     *                       <code>pos</code> &lt; 0, or if <code>pos</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *                       is greater than the number of menu items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    public void remove(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        if (pos > getItemCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            throw new IllegalArgumentException("index greater than the number of items.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            popupMenu.remove(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * Removes the component <code>c</code> from this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @param       c the component to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    public void remove(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            popupMenu.remove(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * Removes all menu items from this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    public void removeAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            popupMenu.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * Returns the number of components on the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @return an integer containing the number of components on the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    public int getMenuComponentCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        int componentCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            componentCount = popupMenu.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        return componentCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * Returns the component at position <code>n</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * @param n the position of the component to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @return the component requested, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *                  if there is no popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    public Component getMenuComponent(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            return popupMenu.getComponent(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * Returns an array of <code>Component</code>s of the menu's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * subcomponents.  Note that this returns all <code>Component</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * in the popup menu, including separators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @return an array of <code>Component</code>s or an empty array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *          if there is no popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    public Component[] getMenuComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            return popupMenu.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        return new Component[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * Returns true if the menu is a 'top-level menu', that is, if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * the direct child of a menubar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * @return true if the menu is activated from the menu bar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     *         false if the menu is activated from a menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *         on another menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    public boolean isTopLevelMenu() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   905
        return getParent() instanceof JMenuBar;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * Returns true if the specified component exists in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * submenu hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @param c the <code>Component</code> to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @return true if the <code>Component</code> exists, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    public boolean isMenuComponent(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        // Are we in the MenuItem part of the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        // Are we in the PopupMenu?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        if (c instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            JPopupMenu comp = (JPopupMenu) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            if (comp == this.getPopupMenu())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        // Are we in a Component on the PopupMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        int ncomponents = this.getMenuComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        Component[] component = this.getMenuComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            // Are we in the current component?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            if (comp == c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            // Hmmm, what about Non-menu containers?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            // Recursive call for the Menu case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            if (comp instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                JMenu subMenu = (JMenu) comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                if (subMenu.isMenuComponent(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * Returns a point in the coordinate space of this menu's popupmenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * which corresponds to the point <code>p</code> in the menu's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * coordinate space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @param p the point to be translated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * @return the point in the coordinate space of this menu's popupmenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    private Point translateToPopupMenu(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        return translateToPopupMenu(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * Returns a point in the coordinate space of this menu's popupmenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * which corresponds to the point (x,y) in the menu's coordinate space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * @param x the x coordinate of the point to be translated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @param y the y coordinate of the point to be translated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @return the point in the coordinate space of this menu's popupmenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    private Point translateToPopupMenu(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            int newX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            int newY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            if (getParent() instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                newX = x - getSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                newY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                newX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                newY = y - getSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            return new Point(newX, newY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * Returns the popupmenu associated with this menu.  If there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * no popupmenu, it will create one.
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   985
     *
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
   986
     * @return the {@code JPopupMenu} associated with this menu
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    public JPopupMenu getPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        return popupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * Adds a listener for menu events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @param l the listener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    public void addMenuListener(MenuListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        listenerList.add(MenuListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * Removes a listener for menu events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * @param l the listener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    public void removeMenuListener(MenuListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        listenerList.remove(MenuListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * Returns an array of all the <code>MenuListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * to this JMenu with addMenuListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * @return all of the <code>MenuListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    public MenuListener[] getMenuListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1020
        return listenerList.getListeners(MenuListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * is created lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * @exception Error  if there is a <code>null</code> listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    protected void fireMenuSelected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            System.out.println("In JMenu.fireMenuSelected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            if (listeners[i]==MenuListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                if (listeners[i+1]== null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    throw new Error(getText() +" has a NULL Listener!! " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    if (menuEvent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                        menuEvent = new MenuEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    ((MenuListener)listeners[i+1]).menuSelected(menuEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * is created lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * @exception Error if there is a <code>null</code> listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    protected void fireMenuDeselected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            System.out.println("In JMenu.fireMenuDeselected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            if (listeners[i]==MenuListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                if (listeners[i+1]== null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                    throw new Error(getText() +" has a NULL Listener!! " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    if (menuEvent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                        menuEvent = new MenuEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    ((MenuListener)listeners[i+1]).menuDeselected(menuEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * is created lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * @exception Error if there is a <code>null</code> listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    protected void fireMenuCanceled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            System.out.println("In JMenu.fireMenuCanceled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            if (listeners[i]==MenuListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                if (listeners[i+1]== null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                    throw new Error(getText() +" has a NULL Listener!! "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                                       + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    if (menuEvent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                        menuEvent = new MenuEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    ((MenuListener)listeners[i+1]).menuCanceled(menuEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    // Overriden to do nothing, JMenu doesn't support an accelerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    void configureAcceleratorFromAction(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
  1118
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    class MenuChangeListener implements ChangeListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        boolean isSelected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            ButtonModel model = (ButtonModel) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            boolean modelSelected = model.isSelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            if (modelSelected != isSelected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                if (modelSelected == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                    fireMenuSelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                    fireMenuDeselected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                isSelected = modelSelected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    private ChangeListener createMenuChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        return new MenuChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * Creates a window-closing listener for the popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * @param p the <code>JPopupMenu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * @return the new window-closing listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * @see WinListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    protected WinListener createWinListener(JPopupMenu p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        return new WinListener(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * A listener class that watches for a popup window closing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * When the popup is closing, the listener deselects the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
  1162
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
  1166
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    protected class WinListener extends WindowAdapter implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        JPopupMenu popupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
         *  Create the window listener for the specified popup.
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
  1171
         *
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 25201
diff changeset
  1172
         * @param p the popup menu for which to create a listener
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        public WinListener(JPopupMenu p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            this.popupMenu = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
         * Deselect the menu when the popup is closed from outside.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        public void windowClosing(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * Messaged when the menubar selection changes to activate or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * deactivate this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * Overrides <code>JMenuItem.menuSelectionChanged</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @param isIncluded  true if this menu is active, false if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     *        it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    public void menuSelectionChanged(boolean isIncluded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            System.out.println("In JMenu.menuSelectionChanged to " + isIncluded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        setSelected(isIncluded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * Returns an array of <code>MenuElement</code>s containing the submenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * for this menu component.  If popup menu is <code>null</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * an empty array.  This method is required to conform to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * <code>MenuElement</code> interface.  Note that since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * <code>JSeparator</code>s do not conform to the <code>MenuElement</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * interface, this array will only contain <code>JMenuItem</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * @return an array of <code>MenuElement</code> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    public MenuElement[] getSubElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        if(popupMenu == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            return new MenuElement[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            MenuElement result[] = new MenuElement[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            result[0] = popupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    // implements javax.swing.MenuElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * Returns the <code>java.awt.Component</code> used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * paint this <code>MenuElement</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * The returned component is used to convert events and detect if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * an event is inside a menu component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    public Component getComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * Sets the <code>ComponentOrientation</code> property of this menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * and all components contained within it. This includes all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * components returned by {@link #getMenuComponents getMenuComponents}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @param o the new component orientation of this menu and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *        the components contained within it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * @exception NullPointerException if <code>orientation</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @see java.awt.Component#setComponentOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * @see java.awt.Component#getComponentOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    public void applyComponentOrientation(ComponentOrientation o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        super.applyComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        if ( popupMenu != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            int ncomponents = getMenuComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            for (int i = 0 ; i < ncomponents ; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                getMenuComponent(i).applyComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            popupMenu.setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    public void setComponentOrientation(ComponentOrientation o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        super.setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        if ( popupMenu != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            popupMenu.setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * <code>setAccelerator</code> is not defined for <code>JMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * Use <code>setMnemonic</code> instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * @param keyStroke  the keystroke combination which will invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *                  the <code>JMenuItem</code>'s actionlisteners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     *                  without navigating the menu hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * @exception Error  if invoked -- this method is not defined for JMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     *                  Use <code>setMnemonic</code> instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     *     description: The keystroke combination which will invoke the JMenuItem's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     *                  actionlisteners without navigating the menu hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     *          hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    public void setAccelerator(KeyStroke keyStroke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        throw new Error("setAccelerator() is not defined for JMenu.  Use setMnemonic() instead.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * Processes key stroke events such as mnemonics and accelerators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * @param evt  the key event to be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    protected void processKeyEvent(KeyEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        MenuSelectionManager.defaultManager().processKeyEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        if (evt.isConsumed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        super.processKeyEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * Programmatically performs a "click".  This overrides the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * <code>AbstractButton.doClick</code> in order to make the menu pop up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * @param pressTime  indicates the number of milliseconds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     *          button was pressed for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    public void doClick(int pressTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        MenuElement me[] = buildMenuElementArray(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        MenuSelectionManager.defaultManager().setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * Build an array of menu elements - from <code>PopupMenu</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * the root <code>JMenuBar</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * @param  leaf  the leaf node from which to start building up the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * @return the array of menu items
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    private MenuElement[] buildMenuElementArray(JMenu leaf) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1314
        Vector<MenuElement> elements = new Vector<MenuElement>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        Component current = leaf.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        JPopupMenu pop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        JMenu menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        JMenuBar bar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            if (current instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                pop = (JPopupMenu) current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                elements.insertElementAt(pop, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                current = pop.getInvoker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            } else if (current instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                menu = (JMenu) current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                elements.insertElementAt(menu, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                current = menu.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            } else if (current instanceof JMenuBar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                bar = (JMenuBar) current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                elements.insertElementAt(bar, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                MenuElement me[] = new MenuElement[elements.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                elements.copyInto(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                return me;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * See <code>readObject</code> and <code>writeObject</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * <code>JComponent</code> for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * Returns a string representation of this <code>JMenu</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * @return  a string representation of this JMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        return super.paramString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * Gets the AccessibleContext associated with this JMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * For JMenus, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * AccessibleJMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * A new AccessibleJMenu instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * @return an AccessibleJMenu that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     *         AccessibleContext of this JMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            accessibleContext = new AccessibleJMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * <code>JMenu</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * Java Accessibility API appropriate to menu user-interface elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
  1401
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
  1405
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    protected class AccessibleJMenu extends AccessibleJMenuItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        implements AccessibleSelection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
         * Returns the number of accessible children in the object.  If all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
         * of the children of this object implement Accessible, than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
         * method should return the number of children of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
         * @return the number of accessible children in the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        public int getAccessibleChildrenCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            Component[] children = getMenuComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            int count = 0;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1419
            for (Component child : children) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1420
                if (child instanceof Accessible) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
         * Returns the nth Accessible child of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
         * @param i zero-based index of child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
         * @return the nth Accessible child of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        public Accessible getAccessibleChild(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            Component[] children = getMenuComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            int count = 0;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1436
            for (Component child : children) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1437
                if (child instanceof Accessible) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                    if (count == i) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1439
                        if (child instanceof JComponent) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                            // FIXME:  [[[WDW - probably should set this when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                            // the component is added to the menu.  I tried
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                            // to do this in most cases, but the separators
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                            // added by addSeparator are hard to get to.]]]
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1444
                            AccessibleContext ac = child.getAccessibleContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                            ac.setAccessibleParent(JMenu.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                        }
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1447
                        return (Accessible) child;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            return AccessibleRole.MENU;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
         * Get the AccessibleSelection associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
         * AccessibleSelection interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        public AccessibleSelection getAccessibleSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
         * Returns 1 if a sub-menu is currently selected in this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
         * @return 1 if a menu is currently selected, else 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        public int getAccessibleSelectionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            MenuElement me[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                MenuSelectionManager.defaultManager().getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            if (me != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                for (int i = 0; i < me.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                    if (me[i] == JMenu.this) {   // this menu is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                        if (i+1 < me.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
         * Returns the currently selected sub-menu if one is selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
         * otherwise null (there can only be one selection, and it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
         * only be a sub-menu, as otherwise menu items don't remain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
         * selected).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        public Accessible getAccessibleSelection(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            // if i is a sub-menu & popped, return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            if (i < 0 || i >= getItemCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            MenuElement me[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                MenuSelectionManager.defaultManager().getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            if (me != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                for (int j = 0; j < me.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                    if (me[j] == JMenu.this) {   // this menu is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                        // so find the next JMenuItem in the MenuElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                        // array, and return it!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                        while (++j < me.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                            if (me[j] instanceof JMenuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                                return (Accessible) me[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
         * Returns true if the current child of this object is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
         * (that is, if this child is a popped-up submenu).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
         * @param i the zero-based index of the child in this Accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
         * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
         * @see AccessibleContext#getAccessibleChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        public boolean isAccessibleChildSelected(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            // if i is a sub-menu and is pop-ed up, return true, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            MenuElement me[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                MenuSelectionManager.defaultManager().getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            if (me != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                JMenuItem mi = JMenu.this.getItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                for (int j = 0; j < me.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    if (me[j] == mi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
         * Selects the <code>i</code>th menu in the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
         * If that item is a submenu,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
         * it will pop up in response.  If a different item is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
         * popped up, this will force it to close.  If this is a sub-menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
         * that is already popped up (selected), this method has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
         * effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
         * @param i the index of the item to be selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
         * @see #getAccessibleStateSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        public void addAccessibleSelection(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            if (i < 0 || i >= getItemCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            JMenuItem mi = getItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            if (mi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                if (mi instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                    MenuElement me[] = buildMenuElementArray((JMenu) mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                    MenuSelectionManager.defaultManager().setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                    MenuSelectionManager.defaultManager().setSelectedPath(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
         * Removes the nth item from the selection.  In general, menus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
         * can only have one item within them selected at a time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
         * (e.g. one sub-menu popped open).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
         * @param i the zero-based index of the selected item
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        public void removeAccessibleSelection(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            if (i < 0 || i >= getItemCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            JMenuItem mi = getItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            if (mi != null && mi instanceof JMenu) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1591
                if (mi.isSelected()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                    MenuElement old[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                        MenuSelectionManager.defaultManager().getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                    MenuElement me[] = new MenuElement[old.length-2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                    for (int j = 0; j < old.length -2; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                        me[j] = old[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                    MenuSelectionManager.defaultManager().setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
         * Clears the selection in the object, so that nothing in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
         * object is selected.  This will close any open sub-menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        public void clearAccessibleSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            // if this menu is selected, reset selection to only go
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            // to this menu; else do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            MenuElement old[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                MenuSelectionManager.defaultManager().getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            if (old != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                for (int j = 0; j < old.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                    if (old[j] == JMenu.this) {  // menu is in the selection!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                        MenuElement me[] = new MenuElement[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                        System.arraycopy(old, 0, me, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                        me[j] = JMenu.this.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                        MenuSelectionManager.defaultManager().setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
         * Normally causes every selected item in the object to be selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
         * if the object supports multiple selections.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
         * makes no sense in a menu bar, and so does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        public void selectAllAccessibleSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
    } // inner class AccessibleJMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
}