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