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.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.Serializable;
0N/Aimport java.beans.*;
0N/A
0N/Aimport java.util.Locale;
0N/Aimport java.util.Vector;
0N/Aimport java.util.Hashtable;
0N/Aimport javax.accessibility.*;
0N/Aimport javax.swing.plaf.PopupMenuUI;
0N/Aimport javax.swing.plaf.ComponentUI;
0N/Aimport javax.swing.plaf.basic.BasicComboPopup;
0N/Aimport javax.swing.event.*;
4930N/A
4930N/Aimport sun.awt.SunToolkit;
341N/Aimport sun.security.util.SecurityConstants;
0N/A
0N/Aimport java.applet.Applet;
0N/A
0N/A/**
0N/A * An implementation of a popup menu -- a small window that pops up
0N/A * and displays a series of choices. A <code>JPopupMenu</code> is used for the
0N/A * menu that appears when the user selects an item on the menu bar.
0N/A * It is also used for "pull-right" menu that appears when the
0N/A * selects a menu item that activates it. Finally, a <code>JPopupMenu</code>
0N/A * can also be used anywhere else you want a menu to appear. For
0N/A * example, when the user right-clicks in a specified area.
0N/A * <p>
0N/A * For information and examples of using popup menus, see
0N/A * <a
0N/A href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>
0N/A * 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 false
0N/A * description: A small window that pops up and displays a series of choices.
0N/A *
0N/A * @author Georges Saab
0N/A * @author David Karlton
0N/A * @author Arnaud Weber
0N/A */
0N/Apublic class JPopupMenu extends JComponent implements Accessible,MenuElement {
0N/A
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "PopupMenuUI";
0N/A
0N/A /**
0N/A * Key used in AppContext to determine if light way popups are the default.
0N/A */
0N/A private static final Object defaultLWPopupEnabledKey =
0N/A new StringBuffer("JPopupMenu.defaultLWPopupEnabledKey");
0N/A
0N/A /** Bug#4425878-Property javax.swing.adjustPopupLocationToFit introduced */
0N/A static boolean popupPostionFixDisabled = false;
0N/A
0N/A static {
0N/A popupPostionFixDisabled = java.security.AccessController.doPrivileged(
0N/A new sun.security.action.GetPropertyAction(
0N/A "javax.swing.adjustPopupLocationToFit","")).equals("false");
0N/A
0N/A }
0N/A
0N/A transient Component invoker;
0N/A transient Popup popup;
0N/A transient Frame frame;
0N/A private int desiredLocationX,desiredLocationY;
0N/A
0N/A private String label = null;
0N/A private boolean paintBorder = true;
0N/A private Insets margin = null;
0N/A
0N/A /**
0N/A * Used to indicate if lightweight popups should be used.
0N/A */
0N/A private boolean lightWeightPopup = true;
0N/A
0N/A /*
0N/A * Model for the selected subcontrol.
0N/A */
0N/A private SingleSelectionModel selectionModel;
0N/A
0N/A /* Lock object used in place of class object for synchronization.
0N/A * (4187686)
0N/A */
0N/A private static final Object classLock = new Object();
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 * Sets the default value of the <code>lightWeightPopupEnabled</code>
0N/A * property.
0N/A *
0N/A * @param aFlag <code>true</code> if popups can be lightweight,
0N/A * otherwise <code>false</code>
0N/A * @see #getDefaultLightWeightPopupEnabled
0N/A * @see #setLightWeightPopupEnabled
0N/A */
0N/A public static void setDefaultLightWeightPopupEnabled(boolean aFlag) {
0N/A SwingUtilities.appContextPut(defaultLWPopupEnabledKey,
0N/A Boolean.valueOf(aFlag));
0N/A }
0N/A
0N/A /**
0N/A * Gets the <code>defaultLightWeightPopupEnabled</code> property,
0N/A * which by default is <code>true</code>.
0N/A *
0N/A * @return the value of the <code>defaultLightWeightPopupEnabled</code>
0N/A * property
0N/A *
0N/A * @see #setDefaultLightWeightPopupEnabled
0N/A */
0N/A public static boolean getDefaultLightWeightPopupEnabled() {
0N/A Boolean b = (Boolean)
0N/A SwingUtilities.appContextGet(defaultLWPopupEnabledKey);
0N/A if (b == null) {
0N/A SwingUtilities.appContextPut(defaultLWPopupEnabledKey,
0N/A Boolean.TRUE);
0N/A return true;
0N/A }
0N/A return b.booleanValue();
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>JPopupMenu</code> without an "invoker".
0N/A */
0N/A public JPopupMenu() {
0N/A this(null);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>JPopupMenu</code> with the specified title.
0N/A *
0N/A * @param label the string that a UI may use to display as a title
0N/A * for the popup menu.
0N/A */
0N/A public JPopupMenu(String label) {
0N/A this.label = label;
0N/A lightWeightPopup = getDefaultLightWeightPopupEnabled();
0N/A setSelectionModel(new DefaultSingleSelectionModel());
0N/A enableEvents(AWTEvent.MOUSE_EVENT_MASK);
0N/A setFocusTraversalKeysEnabled(false);
0N/A updateUI();
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Returns the look and feel (L&F) object that renders this component.
0N/A *
0N/A * @return the <code>PopupMenuUI</code> object that renders this component
0N/A */
0N/A public PopupMenuUI getUI() {
0N/A return (PopupMenuUI)ui;
0N/A }
0N/A
0N/A /**
0N/A * Sets the L&F object that renders this component.
0N/A *
0N/A * @param ui the new <code>PopupMenuUI</code> L&F object
0N/A * @see UIDefaults#getUI
0N/A * @beaninfo
0N/A * bound: true
0N/A * hidden: true
0N/A * attribute: visualUpdate true
0N/A * description: The UI object that implements the Component's LookAndFeel.
0N/A */
0N/A public void setUI(PopupMenuUI ui) {
0N/A super.setUI(ui);
0N/A }
0N/A
0N/A /**
0N/A * Resets the UI property to 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((PopupMenuUI)UIManager.getUI(this));
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 "PopupMenuUI"
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 protected void processFocusEvent(FocusEvent evt) {
0N/A super.processFocusEvent(evt);
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 /**
0N/A * Returns the model object that handles single selections.
0N/A *
0N/A * @return the <code>selectionModel</code> property
0N/A * @see SingleSelectionModel
0N/A */
0N/A public SingleSelectionModel getSelectionModel() {
0N/A return selectionModel;
0N/A }
0N/A
0N/A /**
0N/A * Sets the model object to handle single selections.
0N/A *
0N/A * @param model the new <code>SingleSelectionModel</code>
0N/A * @see SingleSelectionModel
0N/A * @beaninfo
0N/A * description: The selection model for the popup menu
0N/A * expert: true
0N/A */
0N/A public void setSelectionModel(SingleSelectionModel model) {
0N/A selectionModel = model;
0N/A }
0N/A
0N/A /**
0N/A * Appends the specified menu item to the end of this menu.
0N/A *
0N/A * @param menuItem the <code>JMenuItem</code> to add
0N/A * @return the <code>JMenuItem</code> added
0N/A */
0N/A public JMenuItem add(JMenuItem menuItem) {
0N/A super.add(menuItem);
0N/A return menuItem;
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 * Appends a new menu item to the end of the menu which
0N/A * dispatches the specified <code>Action</code> object.
0N/A *
0N/A * @param a the <code>Action</code> to add to the menu
0N/A * @return the new menu item
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 * Returns an point which has been adjusted to take into account of the
0N/A * desktop bounds, taskbar and multi-monitor configuration.
0N/A * <p>
0N/A * This adustment may be cancelled by invoking the application with
0N/A * -Djavax.swing.adjustPopupLocationToFit=false
0N/A */
341N/A Point adjustPopupLocationToFitScreen(int xPosition, int yPosition) {
341N/A Point popupLocation = new Point(xPosition, yPosition);
341N/A
341N/A if(popupPostionFixDisabled == true || GraphicsEnvironment.isHeadless()) {
341N/A return popupLocation;
341N/A }
0N/A
341N/A // Get screen bounds
341N/A Rectangle scrBounds;
341N/A GraphicsConfiguration gc = getCurrentGraphicsConfiguration(popupLocation);
341N/A Toolkit toolkit = Toolkit.getDefaultToolkit();
341N/A if(gc != null) {
341N/A // If we have GraphicsConfiguration use it to get screen bounds
341N/A scrBounds = gc.getBounds();
341N/A } else {
341N/A // If we don't have GraphicsConfiguration use primary screen
341N/A scrBounds = new Rectangle(toolkit.getScreenSize());
341N/A }
0N/A
341N/A // Calculate the screen size that popup should fit
341N/A Dimension popupSize = JPopupMenu.this.getPreferredSize();
4096N/A long popupRightX = (long)popupLocation.x + (long)popupSize.width;
4096N/A long popupBottomY = (long)popupLocation.y + (long)popupSize.height;
341N/A int scrWidth = scrBounds.width;
341N/A int scrHeight = scrBounds.height;
4930N/A
341N/A if (!canPopupOverlapTaskBar()) {
341N/A // Insets include the task bar. Take them into account.
341N/A Insets scrInsets = toolkit.getScreenInsets(gc);
341N/A scrBounds.x += scrInsets.left;
341N/A scrBounds.y += scrInsets.top;
341N/A scrWidth -= scrInsets.left + scrInsets.right;
341N/A scrHeight -= scrInsets.top + scrInsets.bottom;
341N/A }
341N/A int scrRightX = scrBounds.x + scrWidth;
341N/A int scrBottomY = scrBounds.y + scrHeight;
341N/A
341N/A // Ensure that popup menu fits the screen
4911N/A if (popupRightX > (long) scrRightX) {
341N/A popupLocation.x = scrRightX - popupSize.width;
4911N/A }
4911N/A
4911N/A if (popupBottomY > (long) scrBottomY) {
4911N/A popupLocation.y = scrBottomY - popupSize.height;
341N/A }
4911N/A
4911N/A if (popupLocation.x < scrBounds.x) {
4911N/A popupLocation.x = scrBounds.x;
4911N/A }
4911N/A
4911N/A if (popupLocation.y < scrBounds.y) {
4911N/A popupLocation.y = scrBounds.y;
341N/A }
341N/A
341N/A return popupLocation;
341N/A }
341N/A
341N/A /**
341N/A * Tries to find GraphicsConfiguration
341N/A * that contains the mouse cursor position.
341N/A * Can return null.
341N/A */
341N/A private GraphicsConfiguration getCurrentGraphicsConfiguration(
341N/A Point popupLocation) {
0N/A GraphicsConfiguration gc = null;
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();
341N/A if(dgc.getBounds().contains(popupLocation)) {
0N/A gc = dgc;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A // If not found and we have invoker, ask invoker about his gc
0N/A if(gc == null && getInvoker() != null) {
0N/A gc = getInvoker().getGraphicsConfiguration();
0N/A }
341N/A return gc;
341N/A }
0N/A
341N/A /**
4930N/A * Returns whether popup is allowed to be shown above the task bar.
341N/A */
341N/A static boolean canPopupOverlapTaskBar() {
341N/A boolean result = true;
4930N/A
4930N/A Toolkit tk = Toolkit.getDefaultToolkit();
4930N/A if (tk instanceof SunToolkit) {
4930N/A result = ((SunToolkit)tk).canPopupOverlapTaskBar();
341N/A }
4930N/A
341N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Factory method which creates the <code>JMenuItem</code> for
0N/A * <code>Actions</code> added to the <code>JPopupMenu</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 * Removes the component at the specified index from this popup 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 the value of
0N/A * <code>pos</code> is greater than the
0N/A * number of 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 > getComponentCount() -1) {
0N/A throw new IllegalArgumentException("index greater than the number of items.");
0N/A }
0N/A super.remove(pos);
0N/A }
0N/A
0N/A /**
0N/A * Sets the value of the <code>lightWeightPopupEnabled</code> property,
0N/A * which by default is <code>true</code>.
0N/A * By default, when a look and feel displays a popup,
0N/A * it can choose to
0N/A * use a lightweight (all-Java) popup.
0N/A * Lightweight popup windows are more efficient than heavyweight
0N/A * (native peer) windows,
0N/A * but lightweight and heavyweight components do not mix well in a GUI.
0N/A * If your application mixes lightweight and heavyweight components,
0N/A * you should disable lightweight popups.
0N/A * Some look and feels might always use heavyweight popups,
0N/A * no matter what the value of this property.
0N/A *
0N/A * @param aFlag <code>false</code> to disable lightweight popups
0N/A * @beaninfo
0N/A * description: Determines whether lightweight popups are used when possible
0N/A * expert: true
0N/A *
0N/A * @see #isLightWeightPopupEnabled
0N/A */
0N/A public void setLightWeightPopupEnabled(boolean aFlag) {
0N/A // NOTE: this use to set the flag on a shared JPopupMenu, which meant
0N/A // this effected ALL JPopupMenus.
0N/A lightWeightPopup = aFlag;
0N/A }
0N/A
0N/A /**
0N/A * Gets the <code>lightWeightPopupEnabled</code> property.
0N/A *
0N/A * @return the value of the <code>lightWeightPopupEnabled</code> property
0N/A * @see #setLightWeightPopupEnabled
0N/A */
0N/A public boolean isLightWeightPopupEnabled() {
0N/A return lightWeightPopup;
0N/A }
0N/A
0N/A /**
0N/A * Returns the popup menu's label
0N/A *
0N/A * @return a string containing the popup menu's label
0N/A * @see #setLabel
0N/A */
0N/A public String getLabel() {
0N/A return label;
0N/A }
0N/A
0N/A /**
0N/A * Sets the popup menu's label. Different look and feels may choose
0N/A * to display or not display this.
0N/A *
0N/A * @param label a string specifying the label for the popup menu
0N/A *
0N/A * @see #setLabel
0N/A * @beaninfo
0N/A * description: The label for the popup menu.
0N/A * bound: true
0N/A */
0N/A public void setLabel(String label) {
0N/A String oldValue = this.label;
0N/A this.label = label;
0N/A firePropertyChange("label", oldValue, label);
0N/A if (accessibleContext != null) {
0N/A accessibleContext.firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
0N/A oldValue, label);
0N/A }
0N/A invalidate();
0N/A repaint();
0N/A }
0N/A
0N/A /**
0N/A * Appends a new separator at the end of the menu.
0N/A */
0N/A public void addSeparator() {
0N/A add( new JPopupMenu.Separator() );
0N/A }
0N/A
0N/A /**
0N/A * Inserts a menu item for the specified <code>Action</code> object at
0N/A * a given position.
0N/A *
0N/A * @param a the <code>Action</code> object to insert
0N/A * @param index specifies the position at which to insert the
0N/A * <code>Action</code>, where 0 is the first
0N/A * @exception IllegalArgumentException if <code>index</code> < 0
0N/A * @see Action
0N/A */
0N/A public void insert(Action a, int index) {
0N/A JMenuItem mi = createActionComponent(a);
0N/A mi.setAction(a);
0N/A insert(mi, index);
0N/A }
0N/A
0N/A /**
0N/A * Inserts the specified component into the menu at a given
0N/A * position.
0N/A *
0N/A * @param component the <code>Component</code> to insert
0N/A * @param index specifies the position at which
0N/A * to insert the component, where 0 is the first
0N/A * @exception IllegalArgumentException if <code>index</code> < 0
0N/A */
0N/A public void insert(Component component, int index) {
0N/A if (index < 0) {
0N/A throw new IllegalArgumentException("index less than zero.");
0N/A }
0N/A
0N/A int nitems = getComponentCount();
0N/A // PENDING(ges): Why not use an array?
625N/A Vector<Component> tempItems = new Vector<Component>();
0N/A
0N/A /* Remove the item at index, nitems-index times
0N/A storing them in a temporary vector in the
0N/A order they appear on the menu.
0N/A */
0N/A for (int i = index ; i < nitems; i++) {
0N/A tempItems.addElement(getComponent(index));
0N/A remove(index);
0N/A }
0N/A
0N/A add(component);
0N/A
0N/A /* Add the removed items back to the menu, they are
0N/A already in the correct order in the temp vector.
0N/A */
625N/A for (Component tempItem : tempItems) {
625N/A add(tempItem);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds a <code>PopupMenu</code> listener.
0N/A *
0N/A * @param l the <code>PopupMenuListener</code> to add
0N/A */
0N/A public void addPopupMenuListener(PopupMenuListener l) {
0N/A listenerList.add(PopupMenuListener.class,l);
0N/A }
0N/A
0N/A /**
0N/A * Removes a <code>PopupMenu</code> listener.
0N/A *
0N/A * @param l the <code>PopupMenuListener</code> to remove
0N/A */
0N/A public void removePopupMenuListener(PopupMenuListener l) {
0N/A listenerList.remove(PopupMenuListener.class,l);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the <code>PopupMenuListener</code>s added
0N/A * to this JMenuItem with addPopupMenuListener().
0N/A *
0N/A * @return all of the <code>PopupMenuListener</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 PopupMenuListener[] getPopupMenuListeners() {
625N/A return listenerList.getListeners(PopupMenuListener.class);
0N/A }
0N/A
0N/A /**
0N/A * Adds a <code>MenuKeyListener</code> to the popup menu.
0N/A *
0N/A * @param l the <code>MenuKeyListener</code> to be added
0N/A * @since 1.5
0N/A */
0N/A public void addMenuKeyListener(MenuKeyListener l) {
0N/A listenerList.add(MenuKeyListener.class, l);
0N/A }
0N/A
0N/A /**
0N/A * Removes a <code>MenuKeyListener</code> from the popup menu.
0N/A *
0N/A * @param l the <code>MenuKeyListener</code> to be removed
0N/A * @since 1.5
0N/A */
0N/A public void removeMenuKeyListener(MenuKeyListener l) {
0N/A listenerList.remove(MenuKeyListener.class, l);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the <code>MenuKeyListener</code>s added
0N/A * to this JPopupMenu with addMenuKeyListener().
0N/A *
0N/A * @return all of the <code>MenuKeyListener</code>s added or an empty
0N/A * array if no listeners have been added
0N/A * @since 1.5
0N/A */
0N/A public MenuKeyListener[] getMenuKeyListeners() {
625N/A return listenerList.getListeners(MenuKeyListener.class);
0N/A }
0N/A
0N/A /**
0N/A * Notifies <code>PopupMenuListener</code>s that this popup menu will
0N/A * become visible.
0N/A */
0N/A protected void firePopupMenuWillBecomeVisible() {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A PopupMenuEvent e=null;
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==PopupMenuListener.class) {
0N/A if (e == null)
0N/A e = new PopupMenuEvent(this);
0N/A ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeVisible(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies <code>PopupMenuListener</code>s that this popup menu will
0N/A * become invisible.
0N/A */
0N/A protected void firePopupMenuWillBecomeInvisible() {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A PopupMenuEvent e=null;
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==PopupMenuListener.class) {
0N/A if (e == null)
0N/A e = new PopupMenuEvent(this);
0N/A ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeInvisible(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies <code>PopupMenuListeners</code> that this popup menu is
0N/A * cancelled.
0N/A */
0N/A protected void firePopupMenuCanceled() {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A PopupMenuEvent e=null;
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==PopupMenuListener.class) {
0N/A if (e == null)
0N/A e = new PopupMenuEvent(this);
0N/A ((PopupMenuListener)listeners[i+1]).popupMenuCanceled(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Always returns true since popups, by definition, should always
0N/A * be on top of all other windows.
0N/A * @return true
0N/A */
0N/A // package private
0N/A boolean alwaysOnTop() {
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Lays out the container so that it uses the minimum space
0N/A * needed to display its contents.
0N/A */
0N/A public void pack() {
0N/A if(popup != null) {
0N/A Dimension pref = getPreferredSize();
0N/A
0N/A if (pref == null || pref.width != getWidth() ||
0N/A pref.height != getHeight()) {
0N/A popup = getPopup();
0N/A } else {
0N/A validate();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the visibility of the popup menu.
0N/A *
0N/A * @param b true to make the popup visible, or false to
0N/A * hide it
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Makes the popup visible
0N/A */
0N/A public void setVisible(boolean b) {
0N/A if (DEBUG) {
0N/A System.out.println("JPopupMenu.setVisible " + b);
0N/A }
0N/A
0N/A // Is it a no-op?
0N/A if (b == isVisible())
0N/A return;
0N/A
0N/A // if closing, first close all Submenus
0N/A if (b == false) {
0N/A
0N/A // 4234793: This is a workaround because JPopupMenu.firePopupMenuCanceled is
0N/A // a protected method and cannot be called from BasicPopupMenuUI directly
0N/A // The real solution could be to make
0N/A // firePopupMenuCanceled public and call it directly.
0N/A Boolean doCanceled = (Boolean)getClientProperty("JPopupMenu.firePopupMenuCanceled");
0N/A if (doCanceled != null && doCanceled == Boolean.TRUE) {
0N/A putClientProperty("JPopupMenu.firePopupMenuCanceled", Boolean.FALSE);
0N/A firePopupMenuCanceled();
0N/A }
0N/A getSelectionModel().clearSelection();
0N/A
0N/A } else {
0N/A // This is a popup menu with MenuElement children,
0N/A // set selection path before popping up!
0N/A if (isPopupMenu()) {
0N/A MenuElement me[] = new MenuElement[1];
625N/A me[0] = this;
0N/A MenuSelectionManager.defaultManager().setSelectedPath(me);
0N/A }
0N/A }
0N/A
0N/A if(b) {
0N/A firePopupMenuWillBecomeVisible();
0N/A popup = getPopup();
0N/A firePropertyChange("visible", Boolean.FALSE, Boolean.TRUE);
0N/A
0N/A
0N/A } else if(popup != null) {
0N/A firePopupMenuWillBecomeInvisible();
0N/A popup.hide();
0N/A popup = null;
0N/A firePropertyChange("visible", Boolean.TRUE, Boolean.FALSE);
0N/A // 4694797: When popup menu is made invisible, selected path
0N/A // should be cleared
0N/A if (isPopupMenu()) {
0N/A MenuSelectionManager.defaultManager().clearSelectedPath();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>Popup</code> instance from the
0N/A * <code>PopupMenuUI</code> that has had <code>show</code> invoked on
0N/A * it. If the current <code>popup</code> is non-null,
0N/A * this will invoke <code>dispose</code> of it, and then
0N/A * <code>show</code> the new one.
0N/A * <p>
0N/A * This does NOT fire any events, it is up the caller to dispatch
0N/A * the necessary events.
0N/A */
0N/A private Popup getPopup() {
0N/A Popup oldPopup = popup;
0N/A
0N/A if (oldPopup != null) {
0N/A oldPopup.hide();
0N/A }
0N/A PopupFactory popupFactory = PopupFactory.getSharedInstance();
0N/A
0N/A if (isLightWeightPopupEnabled()) {
0N/A popupFactory.setPopupType(PopupFactory.LIGHT_WEIGHT_POPUP);
0N/A }
0N/A else {
5077N/A popupFactory.setPopupType(PopupFactory.HEAVY_WEIGHT_POPUP);
0N/A }
0N/A
0N/A // adjust the location of the popup
0N/A Point p = adjustPopupLocationToFitScreen(desiredLocationX,desiredLocationY);
0N/A desiredLocationX = p.x;
0N/A desiredLocationY = p.y;
0N/A
0N/A Popup newPopup = getUI().getPopup(this, desiredLocationX,
0N/A desiredLocationY);
0N/A
0N/A popupFactory.setPopupType(PopupFactory.LIGHT_WEIGHT_POPUP);
0N/A newPopup.show();
0N/A return newPopup;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the popup menu is visible (currently
0N/A * being displayed).
0N/A */
0N/A public boolean isVisible() {
625N/A return popup != null;
0N/A }
0N/A
0N/A /**
0N/A * Sets the location of the upper left corner of the
0N/A * popup menu using x, y coordinates.
0N/A *
0N/A * @param x the x coordinate of the popup's new position
0N/A * in the screen's coordinate space
0N/A * @param y the y coordinate of the popup's new position
0N/A * in the screen's coordinate space
0N/A * @beaninfo
0N/A * description: The location of the popup menu.
0N/A */
0N/A public void setLocation(int x, int y) {
0N/A int oldX = desiredLocationX;
0N/A int oldY = desiredLocationY;
0N/A
0N/A desiredLocationX = x;
0N/A desiredLocationY = y;
0N/A if(popup != null && (x != oldX || y != oldY)) {
0N/A popup = getPopup();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the popup menu is a standalone popup menu
0N/A * rather than the submenu of a <code>JMenu</code>.
0N/A *
0N/A * @return true if this menu is a standalone popup menu, otherwise false
0N/A */
0N/A private boolean isPopupMenu() {
0N/A return ((invoker != null) && !(invoker instanceof JMenu));
0N/A }
0N/A
0N/A /**
0N/A * Returns the component which is the 'invoker' of this
0N/A * popup menu.
0N/A *
0N/A * @return the <code>Component</code> in which the popup menu is displayed
0N/A */
0N/A public Component getInvoker() {
0N/A return this.invoker;
0N/A }
0N/A
0N/A /**
0N/A * Sets the invoker of this popup menu -- the component in which
0N/A * the popup menu menu is to be displayed.
0N/A *
0N/A * @param invoker the <code>Component</code> in which the popup
0N/A * menu is displayed
0N/A * @beaninfo
0N/A * description: The invoking component for the popup menu
0N/A * expert: true
0N/A */
0N/A public void setInvoker(Component invoker) {
0N/A Component oldInvoker = this.invoker;
0N/A this.invoker = invoker;
0N/A if ((oldInvoker != this.invoker) && (ui != null)) {
0N/A ui.uninstallUI(this);
0N/A ui.installUI(this);
0N/A }
0N/A invalidate();
0N/A }
0N/A
0N/A /**
0N/A * Displays the popup menu at the position x,y in the coordinate
0N/A * space of the component invoker.
0N/A *
0N/A * @param invoker the component in whose space the popup menu is to appear
0N/A * @param x the x coordinate in invoker's coordinate space at which
0N/A * the popup menu is to be displayed
0N/A * @param y the y coordinate in invoker's coordinate space at which
0N/A * the popup menu is to be displayed
0N/A */
0N/A public void show(Component invoker, int x, int y) {
0N/A if (DEBUG) {
0N/A System.out.println("in JPopupMenu.show " );
0N/A }
0N/A setInvoker(invoker);
0N/A Frame newFrame = getFrame(invoker);
0N/A if (newFrame != frame) {
0N/A // Use the invoker's frame so that events
0N/A // are propagated properly
0N/A if (newFrame!=null) {
0N/A this.frame = newFrame;
0N/A if(popup != null) {
0N/A setVisible(false);
0N/A }
0N/A }
0N/A }
0N/A Point invokerOrigin;
0N/A if (invoker != null) {
0N/A invokerOrigin = invoker.getLocationOnScreen();
0N/A
0N/A // To avoid integer overflow
0N/A long lx, ly;
0N/A lx = ((long) invokerOrigin.x) +
0N/A ((long) x);
0N/A ly = ((long) invokerOrigin.y) +
0N/A ((long) y);
0N/A if(lx > Integer.MAX_VALUE) lx = Integer.MAX_VALUE;
0N/A if(lx < Integer.MIN_VALUE) lx = Integer.MIN_VALUE;
0N/A if(ly > Integer.MAX_VALUE) ly = Integer.MAX_VALUE;
0N/A if(ly < Integer.MIN_VALUE) ly = Integer.MIN_VALUE;
0N/A
0N/A setLocation((int) lx, (int) ly);
0N/A } else {
0N/A setLocation(x, y);
0N/A }
0N/A setVisible(true);
0N/A }
0N/A
0N/A /**
0N/A * Returns the popup menu which is at the root of the menu system
0N/A * for this popup menu.
0N/A *
0N/A * @return the topmost grandparent <code>JPopupMenu</code>
0N/A */
0N/A JPopupMenu getRootPopupMenu() {
0N/A JPopupMenu mp = this;
0N/A while((mp!=null) && (mp.isPopupMenu()!=true) &&
0N/A (mp.getInvoker() != null) &&
0N/A (mp.getInvoker().getParent() != null) &&
0N/A (mp.getInvoker().getParent() instanceof JPopupMenu)
0N/A ) {
0N/A mp = (JPopupMenu) mp.getInvoker().getParent();
0N/A }
0N/A return mp;
0N/A }
0N/A
0N/A /**
0N/A * Returns the component at the specified index.
0N/A *
0N/A * @param i the index of the component, where 0 is the first
0N/A * @return the <code>Component</code> at that index
0N/A * @deprecated replaced by {@link java.awt.Container#getComponent(int)}
0N/A */
0N/A @Deprecated
0N/A public Component getComponentAtIndex(int i) {
0N/A return getComponent(i);
0N/A }
0N/A
0N/A /**
0N/A * Returns the index of the specified component.
0N/A *
0N/A * @param c the <code>Component</code> to find
0N/A * @return the index of the component, where 0 is the first;
0N/A * or -1 if the component is not found
0N/A */
0N/A public int getComponentIndex(Component c) {
0N/A int ncomponents = this.getComponentCount();
0N/A Component[] component = this.getComponents();
0N/A for (int i = 0 ; i < ncomponents ; i++) {
0N/A Component comp = component[i];
0N/A if (comp == c)
0N/A return i;
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Sets the size of the Popup window using a <code>Dimension</code> object.
0N/A * This is equivalent to <code>setPreferredSize(d)</code>.
0N/A *
0N/A * @param d the <code>Dimension</code> specifying the new size
0N/A * of this component.
0N/A * @beaninfo
0N/A * description: The size of the popup menu
0N/A */
0N/A public void setPopupSize(Dimension d) {
0N/A Dimension oldSize = getPreferredSize();
0N/A
0N/A setPreferredSize(d);
0N/A if (popup != null) {
0N/A Dimension newSize = getPreferredSize();
0N/A
0N/A if (!oldSize.equals(newSize)) {
0N/A popup = getPopup();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the size of the Popup window to the specified width and
0N/A * height. This is equivalent to
0N/A * <code>setPreferredSize(new Dimension(width, height))</code>.
0N/A *
0N/A * @param width the new width of the Popup in pixels
0N/A * @param height the new height of the Popup in pixels
0N/A * @beaninfo
0N/A * description: The size of the popup menu
0N/A */
0N/A public void setPopupSize(int width, int height) {
0N/A setPopupSize(new Dimension(width, height));
0N/A }
0N/A
0N/A /**
0N/A * Sets the currently selected component, This will result
0N/A * in a change to the selection model.
0N/A *
0N/A * @param sel the <code>Component</code> to select
0N/A * @beaninfo
0N/A * description: The selected component on the popup menu
0N/A * expert: true
0N/A * hidden: true
0N/A */
0N/A public void setSelected(Component sel) {
0N/A SingleSelectionModel model = getSelectionModel();
0N/A int index = getComponentIndex(sel);
0N/A model.setSelectedIndex(index);
0N/A }
0N/A
0N/A /**
0N/A * Checks whether the border should be painted.
0N/A *
0N/A * @return true if the border is painted, false otherwise
0N/A * @see #setBorderPainted
0N/A */
0N/A public boolean isBorderPainted() {
0N/A return paintBorder;
0N/A }
0N/A
0N/A /**
0N/A * Sets whether the border should be painted.
0N/A *
0N/A * @param b if true, the border is painted.
0N/A * @see #isBorderPainted
0N/A * @beaninfo
0N/A * description: Is the border of the popup menu painted
0N/A */
0N/A public void setBorderPainted(boolean b) {
0N/A paintBorder = b;
0N/A repaint();
0N/A }
0N/A
0N/A /**
0N/A * Paints the popup menu's border if the <code>borderPainted</code>
0N/A * property is <code>true</code>.
0N/A * @param g the <code>Graphics</code> object
0N/A *
0N/A * @see JComponent#paint
0N/A * @see JComponent#setBorder
0N/A */
0N/A protected void paintBorder(Graphics g) {
0N/A if (isBorderPainted()) {
0N/A super.paintBorder(g);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the margin, in pixels, between the popup menu's border and
0N/A * its containees.
0N/A *
0N/A * @return an <code>Insets</code> object containing the margin values.
0N/A */
0N/A public Insets getMargin() {
0N/A if(margin == null) {
0N/A return new Insets(0,0,0,0);
0N/A } else {
0N/A return margin;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Examines the list of menu items to determine whether
0N/A * <code>popup</code> is a popup menu.
0N/A *
0N/A * @param popup a <code>JPopupMenu</code>
0N/A * @return true if <code>popup</code>
0N/A */
0N/A boolean isSubPopupMenu(JPopupMenu popup) {
0N/A int ncomponents = this.getComponentCount();
0N/A Component[] component = this.getComponents();
0N/A for (int i = 0 ; i < ncomponents ; i++) {
0N/A Component comp = component[i];
0N/A if (comp instanceof JMenu) {
0N/A JMenu menu = (JMenu)comp;
0N/A JPopupMenu subPopup = menu.getPopupMenu();
0N/A if (subPopup == popup)
0N/A return true;
0N/A if (subPopup.isSubPopupMenu(popup))
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A
0N/A private static Frame getFrame(Component c) {
0N/A Component w = c;
0N/A
0N/A while(!(w instanceof Frame) && (w!=null)) {
0N/A w = w.getParent();
0N/A }
0N/A return (Frame)w;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string representation of this <code>JPopupMenu</code>.
0N/A * This method
0N/A * 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 <code>JPopupMenu</code>.
0N/A */
0N/A protected String paramString() {
0N/A String labelString = (label != null ?
0N/A label : "");
0N/A String paintBorderString = (paintBorder ?
0N/A "true" : "false");
0N/A String marginString = (margin != null ?
0N/A margin.toString() : "");
0N/A String lightWeightPopupEnabledString = (isLightWeightPopupEnabled() ?
0N/A "true" : "false");
0N/A return super.paramString() +
0N/A ",desiredLocationX=" + desiredLocationX +
0N/A ",desiredLocationY=" + desiredLocationY +
0N/A ",label=" + labelString +
0N/A ",lightWeightPopupEnabled=" + lightWeightPopupEnabledString +
0N/A ",margin=" + marginString +
0N/A ",paintBorder=" + paintBorderString;
0N/A }
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A /**
0N/A * Gets the AccessibleContext associated with this JPopupMenu.
0N/A * For JPopupMenus, the AccessibleContext takes the form of an
0N/A * AccessibleJPopupMenu.
0N/A * A new AccessibleJPopupMenu instance is created if necessary.
0N/A *
0N/A * @return an AccessibleJPopupMenu that serves as the
0N/A * AccessibleContext of this JPopupMenu
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleJPopupMenu();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>JPopupMenu</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to popup menu user-interface
0N/A * elements.
0N/A */
0N/A protected class AccessibleJPopupMenu extends AccessibleJComponent
0N/A implements PropertyChangeListener {
0N/A
0N/A /**
0N/A * AccessibleJPopupMenu constructor
0N/A *
0N/A * @since 1.5
0N/A */
0N/A protected AccessibleJPopupMenu() {
0N/A JPopupMenu.this.addPropertyChangeListener(this);
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
0N/A * the object
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.POPUP_MENU;
0N/A }
0N/A
0N/A /**
0N/A * This method gets called when a bound property is changed.
0N/A * @param e A <code>PropertyChangeEvent</code> object describing
0N/A * the event source and the property that has changed. Must not be null.
0N/A *
0N/A * @throws NullPointerException if the parameter is null.
0N/A * @since 1.5
0N/A */
0N/A public void propertyChange(PropertyChangeEvent e) {
0N/A String propertyName = e.getPropertyName();
0N/A if (propertyName == "visible") {
0N/A if (e.getOldValue() == Boolean.FALSE &&
0N/A e.getNewValue() == Boolean.TRUE) {
0N/A handlePopupIsVisibleEvent(true);
0N/A
0N/A } else if (e.getOldValue() == Boolean.TRUE &&
0N/A e.getNewValue() == Boolean.FALSE) {
0N/A handlePopupIsVisibleEvent(false);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Handles popup "visible" PropertyChangeEvent
0N/A */
0N/A private void handlePopupIsVisibleEvent(boolean visible) {
0N/A if (visible) {
0N/A // notify listeners that the popup became visible
0N/A firePropertyChange(ACCESSIBLE_STATE_PROPERTY,
0N/A null, AccessibleState.VISIBLE);
0N/A // notify listeners that a popup list item is selected
0N/A fireActiveDescendant();
0N/A } else {
0N/A // notify listeners that the popup became hidden
0N/A firePropertyChange(ACCESSIBLE_STATE_PROPERTY,
0N/A AccessibleState.VISIBLE, null);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Fires AccessibleActiveDescendant PropertyChangeEvent to notify listeners
0N/A * on the popup menu invoker that a popup list item has been selected
0N/A */
0N/A private void fireActiveDescendant() {
0N/A if (JPopupMenu.this instanceof BasicComboPopup) {
0N/A // get the popup list
0N/A JList popupList = ((BasicComboPopup)JPopupMenu.this).getList();
0N/A if (popupList == null) {
0N/A return;
0N/A }
0N/A
0N/A // get the first selected item
0N/A AccessibleContext ac = popupList.getAccessibleContext();
0N/A AccessibleSelection selection = ac.getAccessibleSelection();
0N/A if (selection == null) {
0N/A return;
0N/A }
0N/A Accessible a = selection.getAccessibleSelection(0);
0N/A if (a == null) {
0N/A return;
0N/A }
0N/A AccessibleContext selectedItem = a.getAccessibleContext();
0N/A
0N/A // fire the event with the popup invoker as the source.
0N/A if (selectedItem != null && invoker != null) {
0N/A AccessibleContext invokerContext = invoker.getAccessibleContext();
0N/A if (invokerContext != null) {
0N/A // Check invokerContext because Component.getAccessibleContext
0N/A // returns null. Classes that extend Component are responsible
0N/A // for returning a non-null AccessibleContext.
0N/A invokerContext.firePropertyChange(
0N/A ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
0N/A null, selectedItem);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A } // inner class AccessibleJPopupMenu
0N/A
0N/A
0N/A////////////
0N/A// Serialization support.
0N/A////////////
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
625N/A Vector<Object> values = new Vector<Object>();
0N/A
0N/A s.defaultWriteObject();
0N/A // Save the invoker, if its Serializable.
0N/A if(invoker != null && invoker instanceof Serializable) {
0N/A values.addElement("invoker");
0N/A values.addElement(invoker);
0N/A }
0N/A // Save the popup, if its Serializable.
0N/A if(popup != null && popup instanceof Serializable) {
0N/A values.addElement("popup");
0N/A values.addElement(popup);
0N/A }
0N/A s.writeObject(values);
0N/A
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 // implements javax.swing.MenuElement
0N/A private void readObject(ObjectInputStream s)
0N/A throws IOException, ClassNotFoundException {
0N/A s.defaultReadObject();
0N/A
0N/A Vector values = (Vector)s.readObject();
0N/A int indexCounter = 0;
0N/A int maxCounter = values.size();
0N/A
0N/A if(indexCounter < maxCounter && values.elementAt(indexCounter).
0N/A equals("invoker")) {
0N/A invoker = (Component)values.elementAt(++indexCounter);
0N/A indexCounter++;
0N/A }
0N/A if(indexCounter < maxCounter && values.elementAt(indexCounter).
0N/A equals("popup")) {
0N/A popup = (Popup)values.elementAt(++indexCounter);
0N/A indexCounter++;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * This method is required to conform to the
0N/A * <code>MenuElement</code> interface, but it not implemented.
0N/A * @see MenuElement#processMouseEvent(MouseEvent, MenuElement[], MenuSelectionManager)
0N/A */
0N/A public void processMouseEvent(MouseEvent event,MenuElement path[],MenuSelectionManager manager) {}
0N/A
0N/A /**
0N/A * Processes a key event forwarded from the
0N/A * <code>MenuSelectionManager</code> and changes the menu selection,
0N/A * if necessary, by using <code>MenuSelectionManager</code>'s API.
0N/A * <p>
0N/A * Note: you do not have to forward the event to sub-components.
0N/A * This is done automatically by the <code>MenuSelectionManager</code>.
0N/A *
0N/A * @param e a <code>KeyEvent</code>
0N/A * @param path the <code>MenuElement</code> path array
0N/A * @param manager the <code>MenuSelectionManager</code>
0N/A */
0N/A public void processKeyEvent(KeyEvent e, MenuElement path[],
0N/A MenuSelectionManager manager) {
0N/A MenuKeyEvent mke = new MenuKeyEvent(e.getComponent(), e.getID(),
0N/A e.getWhen(), e.getModifiers(),
0N/A e.getKeyCode(), e.getKeyChar(),
0N/A path, manager);
0N/A processMenuKeyEvent(mke);
0N/A
0N/A if (mke.isConsumed()) {
0N/A e.consume();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Handles a keystroke in a menu.
0N/A *
0N/A * @param e a <code>MenuKeyEvent</code> object
0N/A * @since 1.5
0N/A */
0N/A private void processMenuKeyEvent(MenuKeyEvent e) {
0N/A switch (e.getID()) {
0N/A case KeyEvent.KEY_PRESSED:
0N/A fireMenuKeyPressed(e); break;
0N/A case KeyEvent.KEY_RELEASED:
0N/A fireMenuKeyReleased(e); break;
0N/A case KeyEvent.KEY_TYPED:
0N/A fireMenuKeyTyped(e); break;
0N/A default:
0N/A break;
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.
0N/A *
0N/A * @param event a <code>MenuKeyEvent</code>
0N/A * @see EventListenerList
0N/A */
0N/A private void fireMenuKeyPressed(MenuKeyEvent event) {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuKeyListener.class) {
0N/A ((MenuKeyListener)listeners[i+1]).menuKeyPressed(event);
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.
0N/A *
0N/A * @param event a <code>MenuKeyEvent</code>
0N/A * @see EventListenerList
0N/A */
0N/A private void fireMenuKeyReleased(MenuKeyEvent event) {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuKeyListener.class) {
0N/A ((MenuKeyListener)listeners[i+1]).menuKeyReleased(event);
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.
0N/A *
0N/A * @param event a <code>MenuKeyEvent</code>
0N/A * @see EventListenerList
0N/A */
0N/A private void fireMenuKeyTyped(MenuKeyEvent event) {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuKeyListener.class) {
0N/A ((MenuKeyListener)listeners[i+1]).menuKeyTyped(event);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Messaged when the menubar selection changes to activate or
0N/A * deactivate this menu. This implements the
0N/A * <code>javax.swing.MenuElement</code> interface.
0N/A * Overrides <code>MenuElement.menuSelectionChanged</code>.
0N/A *
0N/A * @param isIncluded true if this menu is active, false if
0N/A * it is not
0N/A * @see MenuElement#menuSelectionChanged(boolean)
0N/A */
0N/A public void menuSelectionChanged(boolean isIncluded) {
0N/A if (DEBUG) {
0N/A System.out.println("In JPopupMenu.menuSelectionChanged " + isIncluded);
0N/A }
0N/A if(invoker instanceof JMenu) {
0N/A JMenu m = (JMenu) invoker;
0N/A if(isIncluded)
0N/A m.setPopupMenuVisible(true);
0N/A else
0N/A m.setPopupMenuVisible(false);
0N/A }
0N/A if (isPopupMenu() && !isIncluded)
0N/A setVisible(false);
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. It will only return items conforming to
0N/A * the <code>JMenuElement</code> interface.
0N/A * 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.
0N/A *
0N/A * @return an array of <code>MenuElement</code> objects
0N/A * @see MenuElement#getSubElements
0N/A */
0N/A public MenuElement[] getSubElements() {
0N/A MenuElement result[];
625N/A Vector<MenuElement> tmp = new Vector<MenuElement>();
0N/A int c = getComponentCount();
0N/A int i;
0N/A Component m;
0N/A
0N/A for(i=0 ; i < c ; i++) {
0N/A m = getComponent(i);
0N/A if(m instanceof MenuElement)
625N/A tmp.addElement((MenuElement) m);
0N/A }
0N/A
0N/A result = new MenuElement[tmp.size()];
0N/A for(i=0,c=tmp.size() ; i < c ; i++)
625N/A result[i] = tmp.elementAt(i);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Returns this <code>JPopupMenu</code> component.
0N/A * @return this <code>JPopupMenu</code> object
0N/A * @see MenuElement#getComponent
0N/A */
0N/A public Component getComponent() {
0N/A return this;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * A popup menu-specific separator.
0N/A */
0N/A static public class Separator extends JSeparator
0N/A {
0N/A public Separator( )
0N/A {
0N/A super( JSeparator.HORIZONTAL );
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 "PopupMenuSeparatorUI"
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A */
0N/A public String getUIClassID()
0N/A {
0N/A return "PopupMenuSeparatorUI";
0N/A
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the <code>MouseEvent</code> is considered a popup trigger
0N/A * by the <code>JPopupMenu</code>'s currently installed UI.
0N/A *
0N/A * @return true if the mouse event is a popup trigger
0N/A * @since 1.3
0N/A */
0N/A public boolean isPopupTrigger(MouseEvent e) {
0N/A return getUI().isPopupTrigger(e);
0N/A }
0N/A}