0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing;
0N/A
0N/Aimport java.awt.AWTEvent;
0N/Aimport java.awt.Component;
0N/Aimport java.awt.ComponentOrientation;
0N/Aimport java.awt.Container;
0N/Aimport java.awt.Dimension;
0N/Aimport java.awt.Frame;
0N/Aimport java.awt.Graphics;
0N/Aimport java.awt.GraphicsConfiguration;
0N/Aimport java.awt.GraphicsDevice;
0N/Aimport java.awt.GraphicsEnvironment;
0N/Aimport java.awt.Insets;
0N/Aimport java.awt.Point;
0N/Aimport java.awt.Polygon;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.Toolkit;
0N/Aimport java.awt.event.*;
0N/Aimport java.beans.*;
0N/A
0N/Aimport java.util.*;
0N/A
0N/Aimport java.io.Serializable;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.IOException;
0N/A
0N/Aimport javax.swing.event.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.swing.plaf.basic.*;
0N/Aimport javax.accessibility.*;
0N/A
0N/Aimport java.lang.ref.WeakReference;
0N/A
0N/A/**
0N/A * An implementation of a menu -- a popup window containing
0N/A * <code>JMenuItem</code>s that
0N/A * is displayed when the user selects an item on the <code>JMenuBar</code>.
0N/A * In addition to <code>JMenuItem</code>s, a <code>JMenu</code> can
0N/A * also contain <code>JSeparator</code>s.
0N/A * <p>
0N/A * In essence, a menu is a button with an associated <code>JPopupMenu</code>.
0N/A * When the "button" is pressed, the <code>JPopupMenu</code> appears. If the
0N/A * "button" is on the <code>JMenuBar</code>, the menu is a top-level window.
0N/A * If the "button" is another menu item, then the <code>JPopupMenu</code> is
0N/A * "pull-right" menu.
0N/A * <p>
0N/A * Menus can be configured, and to some degree controlled, by
0N/A * <code><a href="Action.html">Action</a></code>s. Using an
0N/A * <code>Action</code> with a menu has many benefits beyond directly
0N/A * configuring a menu. Refer to <a href="Action.html#buttonActions">
0N/A * Swing Components Supporting <code>Action</code></a> for more
0N/A * details, and you can find more information in <a
0N/A * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
0N/A * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
0N/A * <p>
0N/A * For information and examples of using menus see
0N/A * <a href="http://java.sun.com/doc/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
0N/A * a section in <em>The Java Tutorial.</em>
0N/A * <p>
0N/A * <strong>Warning:</strong> Swing is not thread safe. For more
0N/A * information see <a
0N/A * href="package-summary.html#threading">Swing's Threading
0N/A * Policy</a>.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @beaninfo
0N/A * attribute: isContainer true
0N/A * description: A popup window containing menu items displayed in a menu bar.
0N/A *
0N/A * @author Georges Saab
0N/A * @author David Karlton
0N/A * @author Arnaud Weber
0N/A * @see JMenuItem
0N/A * @see JSeparator
0N/A * @see JMenuBar
0N/A * @see JPopupMenu
0N/A */
0N/Apublic class JMenu extends JMenuItem implements Accessible,MenuElement
0N/A{
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "MenuUI";
0N/A
0N/A /*
0N/A * The popup menu portion of the menu.
0N/A */
0N/A private JPopupMenu popupMenu;
0N/A
0N/A /*
0N/A * The button's model listeners. Default is <code>null</code>.
0N/A */
0N/A private ChangeListener menuChangeListener = null;
0N/A
0N/A /*
0N/A * Only one <code>MenuEvent</code> is needed for each menu since the
0N/A * event's only state is the source property. The source of events
0N/A * generated is always "this". Default is <code>null</code>.
0N/A */
0N/A private MenuEvent menuEvent = null;
0N/A
0N/A /* Registry of listeners created for <code>Action-JMenuItem</code>
0N/A * linkage. This is needed so that references can
0N/A * be cleaned up at remove time to allow garbage collection
0N/A * Default is <code>null</code>.
0N/A */
0N/A private static Hashtable listenerRegistry = null;
0N/A
0N/A /*
0N/A * Used by the look and feel (L&F) code to handle
0N/A * implementation specific menu behaviors.
0N/A */
0N/A private int delay;
0N/A
0N/A /*
0N/A * Location of the popup component. Location is <code>null</code>
0N/A * if it was not customized by <code>setMenuLocation</code>
0N/A */
0N/A private Point customMenuLocation = null;
0N/A
0N/A /* Diagnostic aids -- should be false for production builds. */
0N/A private static final boolean TRACE = false; // trace creates and disposes
0N/A private static final boolean VERBOSE = false; // show reuse hits/misses
0N/A private static final boolean DEBUG = false; // show bad params, misc.
0N/A
0N/A /**
0N/A * Constructs a new <code>JMenu</code> with no text.
0N/A */
0N/A public JMenu() {
0N/A this("");
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new <code>JMenu</code> with the supplied string
0N/A * as its text.
0N/A *
0N/A * @param s the text for the menu label
0N/A */
0N/A public JMenu(String s) {
0N/A super(s);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a menu whose properties are taken from the
0N/A * <code>Action</code> supplied.
0N/A * @param a an <code>Action</code>
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public JMenu(Action a) {
0N/A this();
0N/A setAction(a);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new <code>JMenu</code> with the supplied string as
0N/A * its text and specified as a tear-off menu or not.
0N/A *
0N/A * @param s the text for the menu label
0N/A * @param b can the menu be torn off (not yet implemented)
0N/A */
0N/A public JMenu(String s, boolean b) {
0N/A this(s);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Overriden to do nothing. We want JMenu to be focusable, but
0N/A * <code>JMenuItem</code> doesn't want to be, thus we override this
0N/A * do nothing. We don't invoke <code>setFocusable(true)</code> after
0N/A * super's constructor has completed as this has the side effect that
0N/A * <code>JMenu</code> will be considered traversable via the
0N/A * keyboard, which we don't want. Making a Component traversable by
0N/A * the keyboard after invoking <code>setFocusable(true)</code> is OK,
0N/A * as <code>setFocusable</code> is new API
0N/A * and is speced as such, but internally we don't want to use it like
0N/A * this else we change the keyboard traversability.
0N/A */
0N/A void initFocusability() {
0N/A }
0N/A
0N/A /**
0N/A * Resets the UI property with a value from the current look and feel.
0N/A *
0N/A * @see JComponent#updateUI
0N/A */
0N/A public void updateUI() {
0N/A setUI((MenuItemUI)UIManager.getUI(this));
0N/A
0N/A if ( popupMenu != null )
0N/A {
0N/A popupMenu.setUI((PopupMenuUI)UIManager.getUI(popupMenu));
0N/A }
0N/A
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the name of the L&F class that renders this component.
0N/A *
0N/A * @return the string "MenuUI"
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A */
0N/A public String getUIClassID() {
0N/A return uiClassID;
0N/A }
0N/A
0N/A // public void repaint(long tm, int x, int y, int width, int height) {
0N/A // Thread.currentThread().dumpStack();
0N/A // super.repaint(tm,x,y,width,height);
0N/A // }
0N/A
0N/A /**
0N/A * Sets the data model for the "menu button" -- the label
0N/A * that the user clicks to open or close the menu.
0N/A *
0N/A * @param newModel the <code>ButtonModel</code>
0N/A * @see #getModel
0N/A * @beaninfo
0N/A * description: The menu's model
0N/A * bound: true
0N/A * expert: true
0N/A * hidden: true
0N/A */
0N/A public void setModel(ButtonModel newModel) {
0N/A ButtonModel oldModel = getModel();
0N/A
0N/A super.setModel(newModel);
0N/A
0N/A if (oldModel != null && menuChangeListener != null) {
0N/A oldModel.removeChangeListener(menuChangeListener);
0N/A menuChangeListener = null;
0N/A }
0N/A
0N/A model = newModel;
0N/A
0N/A if (newModel != null) {
0N/A menuChangeListener = createMenuChangeListener();
0N/A newModel.addChangeListener(menuChangeListener);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the menu is currently selected (highlighted).
0N/A *
0N/A * @return true if the menu is selected, else false
0N/A */
0N/A public boolean isSelected() {
0N/A return getModel().isSelected();
0N/A }
0N/A
0N/A /**
0N/A * Sets the selection status of the menu.
0N/A *
0N/A * @param b true to select (highlight) the menu; false to de-select
0N/A * the menu
0N/A * @beaninfo
0N/A * description: When the menu is selected, its popup child is shown.
0N/A * expert: true
0N/A * hidden: true
0N/A */
0N/A public void setSelected(boolean b) {
0N/A ButtonModel model = getModel();
0N/A boolean oldValue = model.isSelected();
0N/A
0N/A // TIGER - 4840653
0N/A // Removed code which fired an AccessibleState.SELECTED
0N/A // PropertyChangeEvent since this resulted in two
0N/A // identical events being fired since
0N/A // AbstractButton.fireItemStateChanged also fires the
0N/A // same event. This caused screen readers to speak the
0N/A // name of the item twice.
0N/A
0N/A if (b != model.isSelected()) {
0N/A getModel().setSelected(b);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the menu's popup window is visible.
0N/A *
0N/A * @return true if the menu is visible, else false
0N/A */
0N/A public boolean isPopupMenuVisible() {
0N/A ensurePopupMenuCreated();
0N/A return popupMenu.isVisible();
0N/A }
0N/A
0N/A /**
0N/A * Sets the visibility of the menu's popup. If the menu is
0N/A * not enabled, this method will have no effect.
0N/A *
0N/A * @param b a boolean value -- true to make the menu visible,
0N/A * false to hide it
0N/A * @beaninfo
0N/A * description: The popup menu's visibility
0N/A * expert: true
0N/A * hidden: true
0N/A */
0N/A public void setPopupMenuVisible(boolean b) {
0N/A if (DEBUG) {
0N/A System.out.println("in JMenu.setPopupMenuVisible " + b);
0N/A // Thread.dumpStack();
0N/A }
0N/A
0N/A boolean isVisible = isPopupMenuVisible();
0N/A if (b != isVisible && (isEnabled() || !b)) {
0N/A ensurePopupMenuCreated();
0N/A if ((b==true) && isShowing()) {
0N/A // Set location of popupMenu (pulldown or pullright)
0N/A Point p = getCustomMenuLocation();
0N/A if (p == null) {
0N/A p = getPopupMenuOrigin();
0N/A }
0N/A getPopupMenu().show(this, p.x, p.y);
0N/A } else {
0N/A getPopupMenu().setVisible(false);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Computes the origin for the <code>JMenu</code>'s popup menu.
0N/A * This method uses Look and Feel properties named
0N/A * <code>Menu.menuPopupOffsetX</code>,
0N/A * <code>Menu.menuPopupOffsetY</code>,
0N/A * <code>Menu.submenuPopupOffsetX</code>, and
0N/A * <code>Menu.submenuPopupOffsetY</code>
0N/A * to adjust the exact location of popup.
0N/A *
0N/A * @return a <code>Point</code> in the coordinate space of the
0N/A * menu which should be used as the origin
0N/A * of the <code>JMenu</code>'s popup menu
0N/A *
0N/A * @since 1.3
0N/A */
0N/A protected Point getPopupMenuOrigin() {
625N/A int x;
625N/A int y;
0N/A JPopupMenu pm = getPopupMenu();
0N/A // Figure out the sizes needed to caclulate the menu position
0N/A Dimension s = getSize();
0N/A Dimension pmSize = pm.getSize();
0N/A // For the first time the menu is popped up,
0N/A // the size has not yet been initiated
0N/A if (pmSize.width==0) {
0N/A pmSize = pm.getPreferredSize();
0N/A }
0N/A Point position = getLocationOnScreen();
0N/A Toolkit toolkit = Toolkit.getDefaultToolkit();
0N/A GraphicsConfiguration gc = getGraphicsConfiguration();
0N/A Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
0N/A GraphicsEnvironment ge =
0N/A GraphicsEnvironment.getLocalGraphicsEnvironment();
0N/A GraphicsDevice[] gd = ge.getScreenDevices();
0N/A for(int i = 0; i < gd.length; i++) {
0N/A if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
0N/A GraphicsConfiguration dgc =
0N/A gd[i].getDefaultConfiguration();
0N/A if(dgc.getBounds().contains(position)) {
0N/A gc = dgc;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A if (gc != null) {
0N/A screenBounds = gc.getBounds();
0N/A // take screen insets (e.g. taskbar) into account
0N/A Insets screenInsets = toolkit.getScreenInsets(gc);
0N/A
0N/A screenBounds.width -=
0N/A Math.abs(screenInsets.left + screenInsets.right);
0N/A screenBounds.height -=
0N/A Math.abs(screenInsets.top + screenInsets.bottom);
0N/A position.x -= Math.abs(screenInsets.left);
0N/A position.y -= Math.abs(screenInsets.top);
0N/A }
0N/A
0N/A Container parent = getParent();
0N/A if (parent instanceof JPopupMenu) {
0N/A // We are a submenu (pull-right)
0N/A int xOffset = UIManager.getInt("Menu.submenuPopupOffsetX");
0N/A int yOffset = UIManager.getInt("Menu.submenuPopupOffsetY");
0N/A
0N/A if( SwingUtilities.isLeftToRight(this) ) {
0N/A // First determine x:
0N/A x = s.width + xOffset; // Prefer placement to the right
0N/A if (position.x + x + pmSize.width >= screenBounds.width
0N/A + screenBounds.x &&
0N/A // popup doesn't fit - place it wherever there's more room
0N/A screenBounds.width - s.width < 2*(position.x
0N/A - screenBounds.x)) {
0N/A
0N/A x = 0 - xOffset - pmSize.width;
0N/A }
0N/A } else {
0N/A // First determine x:
0N/A x = 0 - xOffset - pmSize.width; // Prefer placement to the left
0N/A if (position.x + x < screenBounds.x &&
0N/A // popup doesn't fit - place it wherever there's more room
0N/A screenBounds.width - s.width > 2*(position.x -
0N/A screenBounds.x)) {
0N/A
0N/A x = s.width + xOffset;
0N/A }
0N/A }
0N/A // Then the y:
0N/A y = yOffset; // Prefer dropping down
0N/A if (position.y + y + pmSize.height >= screenBounds.height
0N/A + screenBounds.y &&
0N/A // popup doesn't fit - place it wherever there's more room
0N/A screenBounds.height - s.height < 2*(position.y
0N/A - screenBounds.y)) {
0N/A
0N/A y = s.height - yOffset - pmSize.height;
0N/A }
0N/A } else {
0N/A // We are a toplevel menu (pull-down)
0N/A int xOffset = UIManager.getInt("Menu.menuPopupOffsetX");
0N/A int yOffset = UIManager.getInt("Menu.menuPopupOffsetY");
0N/A
0N/A if( SwingUtilities.isLeftToRight(this) ) {
0N/A // First determine the x:
0N/A x = xOffset; // Extend to the right
0N/A if (position.x + x + pmSize.width >= screenBounds.width
0N/A + screenBounds.x &&
0N/A // popup doesn't fit - place it wherever there's more room
0N/A screenBounds.width - s.width < 2*(position.x
0N/A - screenBounds.x)) {
0N/A
0N/A x = s.width - xOffset - pmSize.width;
0N/A }
0N/A } else {
0N/A // First determine the x:
0N/A x = s.width - xOffset - pmSize.width; // Extend to the left
0N/A if (position.x + x < screenBounds.x &&
0N/A // popup doesn't fit - place it wherever there's more room
0N/A screenBounds.width - s.width > 2*(position.x
0N/A - screenBounds.x)) {
0N/A
0N/A x = xOffset;
0N/A }
0N/A }
0N/A // Then the y:
0N/A y = s.height + yOffset; // Prefer dropping down
0N/A if (position.y + y + pmSize.height >= screenBounds.height &&
0N/A // popup doesn't fit - place it wherever there's more room
0N/A screenBounds.height - s.height < 2*(position.y
0N/A - screenBounds.y)) {
0N/A
0N/A y = 0 - yOffset - pmSize.height; // Otherwise drop 'up'
0N/A }
0N/A }
0N/A return new Point(x,y);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the suggested delay, in milliseconds, before submenus
0N/A * are popped up or down.
0N/A * Each look and feel (L&F) may determine its own policy for
0N/A * observing the <code>delay</code> property.
0N/A * In most cases, the delay is not observed for top level menus
0N/A * or while dragging. The default for <code>delay</code> is 0.
0N/A * This method is a property of the look and feel code and is used
0N/A * to manage the idiosyncracies of the various UI implementations.
0N/A *
0N/A *
0N/A * @return the <code>delay</code> property
0N/A */
0N/A public int getDelay() {
0N/A return delay;
0N/A }
0N/A
0N/A /**
0N/A * Sets the suggested delay before the menu's <code>PopupMenu</code>
0N/A * is popped up or down. Each look and feel (L&F) may determine
0N/A * it's own policy for observing the delay property. In most cases,
0N/A * the delay is not observed for top level menus or while dragging.
0N/A * This method is a property of the look and feel code and is used
0N/A * to manage the idiosyncracies of the various UI implementations.
0N/A *
0N/A * @param d the number of milliseconds to delay
0N/A * @exception IllegalArgumentException if <code>d</code>
0N/A * is less than 0
0N/A * @beaninfo
0N/A * description: The delay between menu selection and making the popup menu visible
0N/A * expert: true
0N/A */
0N/A public void setDelay(int d) {
0N/A if (d < 0)
0N/A throw new IllegalArgumentException("Delay must be a positive integer");
0N/A
0N/A delay = d;
0N/A }
0N/A
0N/A /**
0N/A * The window-closing listener for the popup.
0N/A *
0N/A * @see WinListener
0N/A */
0N/A protected WinListener popupListener;
0N/A
0N/A private void ensurePopupMenuCreated() {
0N/A if (popupMenu == null) {
0N/A final JMenu thisMenu = this;
0N/A this.popupMenu = new JPopupMenu();
0N/A popupMenu.setInvoker(this);
0N/A popupListener = createWinListener(popupMenu);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Return the customized location of the popup component.
0N/A */
0N/A private Point getCustomMenuLocation() {
0N/A return customMenuLocation;
0N/A }
0N/A
0N/A /**
0N/A * Sets the location of the popup component.
0N/A *
0N/A * @param x the x coordinate of the popup's new position
0N/A * @param y the y coordinate of the popup's new position
0N/A */
0N/A public void setMenuLocation(int x, int y) {
0N/A customMenuLocation = new Point(x, y);
0N/A if (popupMenu != null)
0N/A popupMenu.setLocation(x, y);
0N/A }
0N/A
0N/A /**
0N/A * Appends a menu item to the end of this menu.
0N/A * Returns the menu item added.
0N/A *
0N/A * @param menuItem the <code>JMenuitem</code> to be added
0N/A * @return the <code>JMenuItem</code> added
0N/A */
0N/A public JMenuItem add(JMenuItem menuItem) {
0N/A ensurePopupMenuCreated();
0N/A return popupMenu.add(menuItem);
0N/A }
0N/A
0N/A /**
0N/A * Appends a component to the end of this menu.
0N/A * Returns the component added.
0N/A *
0N/A * @param c the <code>Component</code> to add
0N/A * @return the <code>Component</code> added
0N/A */
0N/A public Component add(Component c) {
0N/A ensurePopupMenuCreated();
0N/A popupMenu.add(c);
0N/A return c;
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified component to this container at the given
0N/A * position. If <code>index</code> equals -1, the component will
0N/A * be appended to the end.
0N/A * @param c the <code>Component</code> to add
0N/A * @param index the position at which to insert the component
0N/A * @return the <code>Component</code> added
0N/A * @see #remove
0N/A * @see java.awt.Container#add(Component, int)
0N/A */
0N/A public Component add(Component c, int index) {
0N/A ensurePopupMenuCreated();
0N/A popupMenu.add(c, index);
0N/A return c;
0N/A }
0N/A
0N/A /**
0N/A * Creates a new menu item with the specified text and appends
0N/A * it to the end of this menu.
0N/A *
0N/A * @param s the string for the menu item to be added
0N/A */
0N/A public JMenuItem add(String s) {
0N/A return add(new JMenuItem(s));
0N/A }
0N/A
0N/A /**
0N/A * Creates a new menu item attached to the specified
0N/A * <code>Action</code> object and appends it to the end of this menu.
0N/A *
0N/A * @param a the <code>Action</code> for the menu item to be added
0N/A * @see Action
0N/A */
0N/A public JMenuItem add(Action a) {
0N/A JMenuItem mi = createActionComponent(a);
0N/A mi.setAction(a);
0N/A add(mi);
0N/A return mi;
0N/A }
0N/A
0N/A /**
0N/A * Factory method which creates the <code>JMenuItem</code> for
0N/A * <code>Action</code>s added to the <code>JMenu</code>.
0N/A *
0N/A * @param a the <code>Action</code> for the menu item to be added
0N/A * @return the new menu item
0N/A * @see Action
0N/A *
0N/A * @since 1.3
0N/A */
0N/A protected JMenuItem createActionComponent(Action a) {
0N/A JMenuItem mi = new JMenuItem() {
0N/A protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
0N/A PropertyChangeListener pcl = createActionChangeListener(this);
0N/A if (pcl == null) {
0N/A pcl = super.createActionPropertyChangeListener(a);
0N/A }
0N/A return pcl;
0N/A }
0N/A };
0N/A mi.setHorizontalTextPosition(JButton.TRAILING);
0N/A mi.setVerticalTextPosition(JButton.CENTER);
0N/A return mi;
0N/A }
0N/A
0N/A /**
0N/A * Returns a properly configured <code>PropertyChangeListener</code>
0N/A * which updates the control as changes to the <code>Action</code> occur.
0N/A */
0N/A protected PropertyChangeListener createActionChangeListener(JMenuItem b) {
0N/A return b.createActionPropertyChangeListener0(b.getAction());
0N/A }
0N/A
0N/A /**
0N/A * Appends a new separator to the end of the menu.
0N/A */
0N/A public void addSeparator()
0N/A {
0N/A ensurePopupMenuCreated();
0N/A popupMenu.addSeparator();
0N/A }
0N/A
0N/A /**
0N/A * Inserts a new menu item with the specified text at a
0N/A * given position.
0N/A *
0N/A * @param s the text for the menu item to add
0N/A * @param pos an integer specifying the position at which to add the
0N/A * new menu item
0N/A * @exception IllegalArgumentException when the value of
0N/A * <code>pos</code> < 0
0N/A */
0N/A public void insert(String s, int pos) {
0N/A if (pos < 0) {
0N/A throw new IllegalArgumentException("index less than zero.");
0N/A }
0N/A
0N/A ensurePopupMenuCreated();
0N/A popupMenu.insert(new JMenuItem(s), pos);
0N/A }
0N/A
0N/A /**
0N/A * Inserts the specified <code>JMenuitem</code> at a given position.
0N/A *
0N/A * @param mi the <code>JMenuitem</code> to add
0N/A * @param pos an integer specifying the position at which to add the
0N/A * new <code>JMenuitem</code>
0N/A * @return the new menu item
0N/A * @exception IllegalArgumentException if the value of
0N/A * <code>pos</code> < 0
0N/A */
0N/A public JMenuItem insert(JMenuItem mi, int pos) {
0N/A if (pos < 0) {
0N/A throw new IllegalArgumentException("index less than zero.");
0N/A }
0N/A ensurePopupMenuCreated();
0N/A popupMenu.insert(mi, pos);
0N/A return mi;
0N/A }
0N/A
0N/A /**
0N/A * Inserts a new menu item attached to the specified <code>Action</code>
0N/A * object at a given position.
0N/A *
0N/A * @param a the <code>Action</code> object for the menu item to add
0N/A * @param pos an integer specifying the position at which to add the
0N/A * new menu item
0N/A * @exception IllegalArgumentException if the value of
0N/A * <code>pos</code> < 0
0N/A */
0N/A public JMenuItem insert(Action a, int pos) {
0N/A if (pos < 0) {
0N/A throw new IllegalArgumentException("index less than zero.");
0N/A }
0N/A
0N/A ensurePopupMenuCreated();
0N/A JMenuItem mi = new JMenuItem(a);
0N/A mi.setHorizontalTextPosition(JButton.TRAILING);
0N/A mi.setVerticalTextPosition(JButton.CENTER);
0N/A popupMenu.insert(mi, pos);
0N/A return mi;
0N/A }
0N/A
0N/A /**
0N/A * Inserts a separator at the specified position.
0N/A *
0N/A * @param index an integer specifying the position at which to
0N/A * insert the menu separator
0N/A * @exception IllegalArgumentException if the value of
0N/A * <code>index</code> < 0
0N/A */
0N/A public void insertSeparator(int index) {
0N/A if (index < 0) {
0N/A throw new IllegalArgumentException("index less than zero.");
0N/A }
0N/A
0N/A ensurePopupMenuCreated();
0N/A popupMenu.insert( new JPopupMenu.Separator(), index );
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>JMenuItem</code> at the specified position.
0N/A * If the component at <code>pos</code> is not a menu item,
0N/A * <code>null</code> is returned.
0N/A * This method is included for AWT compatibility.
0N/A *
0N/A * @param pos an integer specifying the position
0N/A * @exception IllegalArgumentException if the value of
0N/A * <code>pos</code> < 0
0N/A * @return the menu item at the specified position; or <code>null</code>
0N/A * if the item as the specified position is not a menu item
0N/A */
0N/A public JMenuItem getItem(int pos) {
0N/A if (pos < 0) {
0N/A throw new IllegalArgumentException("index less than zero.");
0N/A }
0N/A
0N/A Component c = getMenuComponent(pos);
0N/A if (c instanceof JMenuItem) {
0N/A JMenuItem mi = (JMenuItem) c;
0N/A return mi;
0N/A }
0N/A
0N/A // 4173633
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of items on the menu, including separators.
0N/A * This method is included for AWT compatibility.
0N/A *
0N/A * @return an integer equal to the number of items on the menu
0N/A * @see #getMenuComponentCount
0N/A */
0N/A public int getItemCount() {
0N/A return getMenuComponentCount();
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the menu can be torn off. This method is not
0N/A * yet implemented.
0N/A *
0N/A * @return true if the menu can be torn off, else false
0N/A * @exception Error if invoked -- this method is not yet implemented
0N/A */
0N/A public boolean isTearOff() {
0N/A throw new Error("boolean isTearOff() {} not yet implemented");
0N/A }
0N/A
0N/A /**
0N/A * Removes the specified menu item from this menu. If there is no
0N/A * popup menu, this method will have no effect.
0N/A *
0N/A * @param item the <code>JMenuItem</code> to be removed from the menu
0N/A */
0N/A public void remove(JMenuItem item) {
0N/A if (popupMenu != null)
0N/A popupMenu.remove(item);
0N/A }
0N/A
0N/A /**
0N/A * Removes the menu item at the specified index from this menu.
0N/A *
0N/A * @param pos the position of the item to be removed
0N/A * @exception IllegalArgumentException if the value of
0N/A * <code>pos</code> < 0, or if <code>pos</code>
0N/A * is greater than the number of menu items
0N/A */
0N/A public void remove(int pos) {
0N/A if (pos < 0) {
0N/A throw new IllegalArgumentException("index less than zero.");
0N/A }
0N/A if (pos > getItemCount()) {
0N/A throw new IllegalArgumentException("index greater than the number of items.");
0N/A }
0N/A if (popupMenu != null)
0N/A popupMenu.remove(pos);
0N/A }
0N/A
0N/A /**
0N/A * Removes the component <code>c</code> from this menu.
0N/A *
0N/A * @param c the component to be removed
0N/A */
0N/A public void remove(Component c) {
0N/A if (popupMenu != null)
0N/A popupMenu.remove(c);
0N/A }
0N/A
0N/A /**
0N/A * Removes all menu items from this menu.
0N/A */
0N/A public void removeAll() {
0N/A if (popupMenu != null)
0N/A popupMenu.removeAll();
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of components on the menu.
0N/A *
0N/A * @return an integer containing the number of components on the menu
0N/A */
0N/A public int getMenuComponentCount() {
0N/A int componentCount = 0;
0N/A if (popupMenu != null)
0N/A componentCount = popupMenu.getComponentCount();
0N/A return componentCount;
0N/A }
0N/A
0N/A /**
0N/A * Returns the component at position <code>n</code>.
0N/A *
0N/A * @param n the position of the component to be returned
0N/A * @return the component requested, or <code>null</code>
0N/A * if there is no popup menu
0N/A *
0N/A */
0N/A public Component getMenuComponent(int n) {
0N/A if (popupMenu != null)
0N/A return popupMenu.getComponent(n);
0N/A
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of <code>Component</code>s of the menu's
0N/A * subcomponents. Note that this returns all <code>Component</code>s
0N/A * in the popup menu, including separators.
0N/A *
0N/A * @return an array of <code>Component</code>s or an empty array
0N/A * if there is no popup menu
0N/A */
0N/A public Component[] getMenuComponents() {
0N/A if (popupMenu != null)
0N/A return popupMenu.getComponents();
0N/A
0N/A return new Component[0];
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the menu is a 'top-level menu', that is, if it is
0N/A * the direct child of a menubar.
0N/A *
0N/A * @return true if the menu is activated from the menu bar;
0N/A * false if the menu is activated from a menu item
0N/A * on another menu
0N/A */
0N/A public boolean isTopLevelMenu() {
625N/A return getParent() instanceof JMenuBar;
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the specified component exists in the
0N/A * submenu hierarchy.
0N/A *
0N/A * @param c the <code>Component</code> to be tested
0N/A * @return true if the <code>Component</code> exists, false otherwise
0N/A */
0N/A public boolean isMenuComponent(Component c) {
0N/A // Are we in the MenuItem part of the menu
0N/A if (c == this)
0N/A return true;
0N/A // Are we in the PopupMenu?
0N/A if (c instanceof JPopupMenu) {
0N/A JPopupMenu comp = (JPopupMenu) c;
0N/A if (comp == this.getPopupMenu())
0N/A return true;
0N/A }
0N/A // Are we in a Component on the PopupMenu
0N/A int ncomponents = this.getMenuComponentCount();
0N/A Component[] component = this.getMenuComponents();
0N/A for (int i = 0 ; i < ncomponents ; i++) {
0N/A Component comp = component[i];
0N/A // Are we in the current component?
0N/A if (comp == c)
0N/A return true;
0N/A // Hmmm, what about Non-menu containers?
0N/A
0N/A // Recursive call for the Menu case
0N/A if (comp instanceof JMenu) {
0N/A JMenu subMenu = (JMenu) comp;
0N/A if (subMenu.isMenuComponent(c))
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Returns a point in the coordinate space of this menu's popupmenu
0N/A * which corresponds to the point <code>p</code> in the menu's
0N/A * coordinate space.
0N/A *
0N/A * @param p the point to be translated
0N/A * @return the point in the coordinate space of this menu's popupmenu
0N/A */
0N/A private Point translateToPopupMenu(Point p) {
0N/A return translateToPopupMenu(p.x, p.y);
0N/A }
0N/A
0N/A /*
0N/A * Returns a point in the coordinate space of this menu's popupmenu
0N/A * which corresponds to the point (x,y) in the menu's coordinate space.
0N/A *
0N/A * @param x the x coordinate of the point to be translated
0N/A * @param y the y coordinate of the point to be translated
0N/A * @return the point in the coordinate space of this menu's popupmenu
0N/A */
0N/A private Point translateToPopupMenu(int x, int y) {
0N/A int newX;
0N/A int newY;
0N/A
0N/A if (getParent() instanceof JPopupMenu) {
0N/A newX = x - getSize().width;
0N/A newY = y;
0N/A } else {
0N/A newX = x;
0N/A newY = y - getSize().height;
0N/A }
0N/A
0N/A return new Point(newX, newY);
0N/A }
0N/A
0N/A /**
0N/A * Returns the popupmenu associated with this menu. If there is
0N/A * no popupmenu, it will create one.
0N/A */
0N/A public JPopupMenu getPopupMenu() {
0N/A ensurePopupMenuCreated();
0N/A return popupMenu;
0N/A }
0N/A
0N/A /**
0N/A * Adds a listener for menu events.
0N/A *
0N/A * @param l the listener to be added
0N/A */
0N/A public void addMenuListener(MenuListener l) {
0N/A listenerList.add(MenuListener.class, l);
0N/A }
0N/A
0N/A /**
0N/A * Removes a listener for menu events.
0N/A *
0N/A * @param l the listener to be removed
0N/A */
0N/A public void removeMenuListener(MenuListener l) {
0N/A listenerList.remove(MenuListener.class, l);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the <code>MenuListener</code>s added
0N/A * to this JMenu with addMenuListener().
0N/A *
0N/A * @return all of the <code>MenuListener</code>s added or an empty
0N/A * array if no listeners have been added
0N/A * @since 1.4
0N/A */
0N/A public MenuListener[] getMenuListeners() {
625N/A return listenerList.getListeners(MenuListener.class);
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type. The event instance
0N/A * is created lazily.
0N/A *
0N/A * @exception Error if there is a <code>null</code> listener
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireMenuSelected() {
0N/A if (DEBUG) {
0N/A System.out.println("In JMenu.fireMenuSelected");
0N/A }
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuListener.class) {
0N/A if (listeners[i+1]== null) {
0N/A throw new Error(getText() +" has a NULL Listener!! " + i);
0N/A } else {
0N/A // Lazily create the event:
0N/A if (menuEvent == null)
0N/A menuEvent = new MenuEvent(this);
0N/A ((MenuListener)listeners[i+1]).menuSelected(menuEvent);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type. The event instance
0N/A * is created lazily.
0N/A *
0N/A * @exception Error if there is a <code>null</code> listener
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireMenuDeselected() {
0N/A if (DEBUG) {
0N/A System.out.println("In JMenu.fireMenuDeselected");
0N/A }
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuListener.class) {
0N/A if (listeners[i+1]== null) {
0N/A throw new Error(getText() +" has a NULL Listener!! " + i);
0N/A } else {
0N/A // Lazily create the event:
0N/A if (menuEvent == null)
0N/A menuEvent = new MenuEvent(this);
0N/A ((MenuListener)listeners[i+1]).menuDeselected(menuEvent);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type. The event instance
0N/A * is created lazily.
0N/A *
0N/A * @exception Error if there is a <code>null</code> listener
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireMenuCanceled() {
0N/A if (DEBUG) {
0N/A System.out.println("In JMenu.fireMenuCanceled");
0N/A }
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuListener.class) {
0N/A if (listeners[i+1]== null) {
0N/A throw new Error(getText() +" has a NULL Listener!! "
0N/A + i);
0N/A } else {
0N/A // Lazily create the event:
0N/A if (menuEvent == null)
0N/A menuEvent = new MenuEvent(this);
0N/A ((MenuListener)listeners[i+1]).menuCanceled(menuEvent);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Overriden to do nothing, JMenu doesn't support an accelerator
0N/A void configureAcceleratorFromAction(Action a) {
0N/A }
0N/A
0N/A class MenuChangeListener implements ChangeListener, Serializable {
0N/A boolean isSelected = false;
0N/A public void stateChanged(ChangeEvent e) {
0N/A ButtonModel model = (ButtonModel) e.getSource();
0N/A boolean modelSelected = model.isSelected();
0N/A
0N/A if (modelSelected != isSelected) {
0N/A if (modelSelected == true) {
0N/A fireMenuSelected();
0N/A } else {
0N/A fireMenuDeselected();
0N/A }
0N/A isSelected = modelSelected;
0N/A }
0N/A }
0N/A }
0N/A
0N/A private ChangeListener createMenuChangeListener() {
0N/A return new MenuChangeListener();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Creates a window-closing listener for the popup.
0N/A *
0N/A * @param p the <code>JPopupMenu</code>
0N/A * @return the new window-closing listener
0N/A *
0N/A * @see WinListener
0N/A */
0N/A protected WinListener createWinListener(JPopupMenu p) {
0N/A return new WinListener(p);
0N/A }
0N/A
0N/A /**
0N/A * A listener class that watches for a popup window closing.
0N/A * When the popup is closing, the listener deselects the menu.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class WinListener extends WindowAdapter implements Serializable {
0N/A JPopupMenu popupMenu;
0N/A /**
0N/A * Create the window listener for the specified popup.
0N/A * @since 1.4
0N/A */
0N/A public WinListener(JPopupMenu p) {
0N/A this.popupMenu = p;
0N/A }
0N/A /**
0N/A * Deselect the menu when the popup is closed from outside.
0N/A */
0N/A public void windowClosing(WindowEvent e) {
0N/A setSelected(false);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Messaged when the menubar selection changes to activate or
0N/A * deactivate this menu.
0N/A * Overrides <code>JMenuItem.menuSelectionChanged</code>.
0N/A *
0N/A * @param isIncluded true if this menu is active, false if
0N/A * it is not
0N/A */
0N/A public void menuSelectionChanged(boolean isIncluded) {
0N/A if (DEBUG) {
0N/A System.out.println("In JMenu.menuSelectionChanged to " + isIncluded);
0N/A }
0N/A setSelected(isIncluded);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of <code>MenuElement</code>s containing the submenu
0N/A * for this menu component. If popup menu is <code>null</code> returns
0N/A * an empty array. This method is required to conform to the
0N/A * <code>MenuElement</code> interface. Note that since
0N/A * <code>JSeparator</code>s do not conform to the <code>MenuElement</code>
0N/A * interface, this array will only contain <code>JMenuItem</code>s.
0N/A *
0N/A * @return an array of <code>MenuElement</code> objects
0N/A */
0N/A public MenuElement[] getSubElements() {
0N/A if(popupMenu == null)
0N/A return new MenuElement[0];
0N/A else {
0N/A MenuElement result[] = new MenuElement[1];
0N/A result[0] = popupMenu;
0N/A return result;
0N/A }
0N/A }
0N/A
0N/A
0N/A // implements javax.swing.MenuElement
0N/A /**
0N/A * Returns the <code>java.awt.Component</code> used to
0N/A * paint this <code>MenuElement</code>.
0N/A * The returned component is used to convert events and detect if
0N/A * an event is inside a menu component.
0N/A */
0N/A public Component getComponent() {
0N/A return this;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the <code>ComponentOrientation</code> property of this menu
0N/A * and all components contained within it. This includes all
0N/A * components returned by {@link #getMenuComponents getMenuComponents}.
0N/A *
0N/A * @param o the new component orientation of this menu and
0N/A * the components contained within it.
0N/A * @exception NullPointerException if <code>orientation</code> is null.
0N/A * @see java.awt.Component#setComponentOrientation
0N/A * @see java.awt.Component#getComponentOrientation
0N/A * @since 1.4
0N/A */
0N/A public void applyComponentOrientation(ComponentOrientation o) {
0N/A super.applyComponentOrientation(o);
0N/A
0N/A if ( popupMenu != null ) {
0N/A int ncomponents = getMenuComponentCount();
0N/A for (int i = 0 ; i < ncomponents ; ++i) {
0N/A getMenuComponent(i).applyComponentOrientation(o);
0N/A }
0N/A popupMenu.setComponentOrientation(o);
0N/A }
0N/A }
0N/A
0N/A public void setComponentOrientation(ComponentOrientation o) {
0N/A super.setComponentOrientation(o);
0N/A if ( popupMenu != null ) {
0N/A popupMenu.setComponentOrientation(o);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * <code>setAccelerator</code> is not defined for <code>JMenu</code>.
0N/A * Use <code>setMnemonic</code> instead.
0N/A * @param keyStroke the keystroke combination which will invoke
0N/A * the <code>JMenuItem</code>'s actionlisteners
0N/A * without navigating the menu hierarchy
0N/A * @exception Error if invoked -- this method is not defined for JMenu.
0N/A * Use <code>setMnemonic</code> instead
0N/A *
0N/A * @beaninfo
0N/A * description: The keystroke combination which will invoke the JMenuItem's
0N/A * actionlisteners without navigating the menu hierarchy
0N/A * hidden: true
0N/A */
0N/A public void setAccelerator(KeyStroke keyStroke) {
0N/A throw new Error("setAccelerator() is not defined for JMenu. Use setMnemonic() instead.");
0N/A }
0N/A
0N/A /**
0N/A * Processes key stroke events such as mnemonics and accelerators.
0N/A *
0N/A * @param evt the key event to be processed
0N/A */
0N/A protected void processKeyEvent(KeyEvent evt) {
0N/A MenuSelectionManager.defaultManager().processKeyEvent(evt);
0N/A if (evt.isConsumed())
0N/A return;
0N/A
0N/A super.processKeyEvent(evt);
0N/A }
0N/A
0N/A /**
0N/A * Programmatically performs a "click". This overrides the method
0N/A * <code>AbstractButton.doClick</code> in order to make the menu pop up.
0N/A * @param pressTime indicates the number of milliseconds the
0N/A * button was pressed for
0N/A */
0N/A public void doClick(int pressTime) {
0N/A MenuElement me[] = buildMenuElementArray(this);
0N/A MenuSelectionManager.defaultManager().setSelectedPath(me);
0N/A }
0N/A
0N/A /*
0N/A * Build an array of menu elements - from <code>PopupMenu</code> to
0N/A * the root <code>JMenuBar</code>.
0N/A * @param leaf the leaf node from which to start building up the array
0N/A * @return the array of menu items
0N/A */
0N/A private MenuElement[] buildMenuElementArray(JMenu leaf) {
625N/A Vector<MenuElement> elements = new Vector<MenuElement>();
0N/A Component current = leaf.getPopupMenu();
0N/A JPopupMenu pop;
0N/A JMenu menu;
0N/A JMenuBar bar;
0N/A
0N/A while (true) {
0N/A if (current instanceof JPopupMenu) {
0N/A pop = (JPopupMenu) current;
0N/A elements.insertElementAt(pop, 0);
0N/A current = pop.getInvoker();
0N/A } else if (current instanceof JMenu) {
0N/A menu = (JMenu) current;
0N/A elements.insertElementAt(menu, 0);
0N/A current = menu.getParent();
0N/A } else if (current instanceof JMenuBar) {
0N/A bar = (JMenuBar) current;
0N/A elements.insertElementAt(bar, 0);
0N/A MenuElement me[] = new MenuElement[elements.size()];
0N/A elements.copyInto(me);
0N/A return me;
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * See <code>readObject</code> and <code>writeObject</code> in
0N/A * <code>JComponent</code> for more
0N/A * information about serialization in Swing.
0N/A */
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A byte count = JComponent.getWriteObjCounter(this);
0N/A JComponent.setWriteObjCounter(this, --count);
0N/A if (count == 0 && ui != null) {
0N/A ui.installUI(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string representation of this <code>JMenu</code>. This
0N/A * method is intended to be used only for debugging purposes, and the
0N/A * content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not
0N/A * be <code>null</code>.
0N/A *
0N/A * @return a string representation of this JMenu.
0N/A */
0N/A protected String paramString() {
0N/A return super.paramString();
0N/A }
0N/A
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A /**
0N/A * Gets the AccessibleContext associated with this JMenu.
0N/A * For JMenus, the AccessibleContext takes the form of an
0N/A * AccessibleJMenu.
0N/A * A new AccessibleJMenu instance is created if necessary.
0N/A *
0N/A * @return an AccessibleJMenu that serves as the
0N/A * AccessibleContext of this JMenu
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleJMenu();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>JMenu</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to menu user-interface elements.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class AccessibleJMenu extends AccessibleJMenuItem
0N/A implements AccessibleSelection {
0N/A
0N/A /**
0N/A * Returns the number of accessible children in the object. If all
0N/A * of the children of this object implement Accessible, than this
0N/A * method should return the number of children of this object.
0N/A *
0N/A * @return the number of accessible children in the object.
0N/A */
0N/A public int getAccessibleChildrenCount() {
0N/A Component[] children = getMenuComponents();
0N/A int count = 0;
625N/A for (Component child : children) {
625N/A if (child instanceof Accessible) {
0N/A count++;
0N/A }
0N/A }
0N/A return count;
0N/A }
0N/A
0N/A /**
0N/A * Returns the nth Accessible child of the object.
0N/A *
0N/A * @param i zero-based index of child
0N/A * @return the nth Accessible child of the object
0N/A */
0N/A public Accessible getAccessibleChild(int i) {
0N/A Component[] children = getMenuComponents();
0N/A int count = 0;
625N/A for (Component child : children) {
625N/A if (child instanceof Accessible) {
0N/A if (count == i) {
625N/A if (child instanceof JComponent) {
0N/A // FIXME: [[[WDW - probably should set this when
0N/A // the component is added to the menu. I tried
0N/A // to do this in most cases, but the separators
0N/A // added by addSeparator are hard to get to.]]]
625N/A AccessibleContext ac = child.getAccessibleContext();
0N/A ac.setAccessibleParent(JMenu.this);
0N/A }
625N/A return (Accessible) child;
0N/A } else {
0N/A count++;
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the
0N/A * object
0N/A * @see AccessibleRole
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.MENU;
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleSelection associated with this object. In the
0N/A * implementation of the Java Accessibility API for this class,
0N/A * return this object, which is responsible for implementing the
0N/A * AccessibleSelection interface on behalf of itself.
0N/A *
0N/A * @return this object
0N/A */
0N/A public AccessibleSelection getAccessibleSelection() {
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Returns 1 if a sub-menu is currently selected in this menu.
0N/A *
0N/A * @return 1 if a menu is currently selected, else 0
0N/A */
0N/A public int getAccessibleSelectionCount() {
0N/A MenuElement me[] =
0N/A MenuSelectionManager.defaultManager().getSelectedPath();
0N/A if (me != null) {
0N/A for (int i = 0; i < me.length; i++) {
0N/A if (me[i] == JMenu.this) { // this menu is selected
0N/A if (i+1 < me.length) {
0N/A return 1;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns the currently selected sub-menu if one is selected,
0N/A * otherwise null (there can only be one selection, and it can
0N/A * only be a sub-menu, as otherwise menu items don't remain
0N/A * selected).
0N/A */
0N/A public Accessible getAccessibleSelection(int i) {
0N/A // if i is a sub-menu & popped, return it
0N/A if (i < 0 || i >= getItemCount()) {
0N/A return null;
0N/A }
0N/A MenuElement me[] =
0N/A MenuSelectionManager.defaultManager().getSelectedPath();
0N/A if (me != null) {
0N/A for (int j = 0; j < me.length; j++) {
0N/A if (me[j] == JMenu.this) { // this menu is selected
0N/A // so find the next JMenuItem in the MenuElement
0N/A // array, and return it!
0N/A while (++j < me.length) {
0N/A if (me[j] instanceof JMenuItem) {
0N/A return (Accessible) me[j];
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the current child of this object is selected
0N/A * (that is, if this child is a popped-up submenu).
0N/A *
0N/A * @param i the zero-based index of the child in this Accessible
0N/A * object.
0N/A * @see AccessibleContext#getAccessibleChild
0N/A */
0N/A public boolean isAccessibleChildSelected(int i) {
0N/A // if i is a sub-menu and is pop-ed up, return true, else false
0N/A MenuElement me[] =
0N/A MenuSelectionManager.defaultManager().getSelectedPath();
0N/A if (me != null) {
0N/A JMenuItem mi = JMenu.this.getItem(i);
0N/A for (int j = 0; j < me.length; j++) {
0N/A if (me[j] == mi) {
0N/A return true;
0N/A }
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Selects the <code>i</code>th menu in the menu.
0N/A * If that item is a submenu,
0N/A * it will pop up in response. If a different item is already
0N/A * popped up, this will force it to close. If this is a sub-menu
0N/A * that is already popped up (selected), this method has no
0N/A * effect.
0N/A *
0N/A * @param i the index of the item to be selected
0N/A * @see #getAccessibleStateSet
0N/A */
0N/A public void addAccessibleSelection(int i) {
0N/A if (i < 0 || i >= getItemCount()) {
0N/A return;
0N/A }
0N/A JMenuItem mi = getItem(i);
0N/A if (mi != null) {
0N/A if (mi instanceof JMenu) {
0N/A MenuElement me[] = buildMenuElementArray((JMenu) mi);
0N/A MenuSelectionManager.defaultManager().setSelectedPath(me);
0N/A } else {
0N/A MenuSelectionManager.defaultManager().setSelectedPath(null);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes the nth item from the selection. In general, menus
0N/A * can only have one item within them selected at a time
0N/A * (e.g. one sub-menu popped open).
0N/A *
0N/A * @param i the zero-based index of the selected item
0N/A */
0N/A public void removeAccessibleSelection(int i) {
0N/A if (i < 0 || i >= getItemCount()) {
0N/A return;
0N/A }
0N/A JMenuItem mi = getItem(i);
0N/A if (mi != null && mi instanceof JMenu) {
625N/A if (mi.isSelected()) {
0N/A MenuElement old[] =
0N/A MenuSelectionManager.defaultManager().getSelectedPath();
0N/A MenuElement me[] = new MenuElement[old.length-2];
0N/A for (int j = 0; j < old.length -2; j++) {
0N/A me[j] = old[j];
0N/A }
0N/A MenuSelectionManager.defaultManager().setSelectedPath(me);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Clears the selection in the object, so that nothing in the
0N/A * object is selected. This will close any open sub-menu.
0N/A */
0N/A public void clearAccessibleSelection() {
0N/A // if this menu is selected, reset selection to only go
0N/A // to this menu; else do nothing
0N/A MenuElement old[] =
0N/A MenuSelectionManager.defaultManager().getSelectedPath();
0N/A if (old != null) {
0N/A for (int j = 0; j < old.length; j++) {
0N/A if (old[j] == JMenu.this) { // menu is in the selection!
0N/A MenuElement me[] = new MenuElement[j+1];
0N/A System.arraycopy(old, 0, me, 0, j);
0N/A me[j] = JMenu.this.getPopupMenu();
0N/A MenuSelectionManager.defaultManager().setSelectedPath(me);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Normally causes every selected item in the object to be selected
0N/A * if the object supports multiple selections. This method
0N/A * makes no sense in a menu bar, and so does nothing.
0N/A */
0N/A public void selectAllAccessibleSelection() {
0N/A }
0N/A } // inner class AccessibleJMenu
0N/A
0N/A}