JPopupMenu.java revision 2362
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 * 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 * 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 * 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. 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 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 * For information and examples of using popup menus, see 0N/A * in <em>The Java Tutorial.</em> 0N/A * <strong>Warning:</strong> Swing is not thread safe. For more 0N/A * information see <a 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 * attribute: isContainer false 0N/A * description: A small window that pops up and displays a series of choices. 0N/A * @author Georges Saab 0N/A * @author David Karlton 0N/A * @author Arnaud Weber 0N/A * @see #getUIClassID 0N/A * Key used in AppContext to determine if light way popups are the default. 0N/A /** Bug#4425878-Property javax.swing.adjustPopupLocationToFit introduced */ 0N/A "javax.swing.adjustPopupLocationToFit",
"")).
equals(
"false");
0N/A * Used to indicate if lightweight popups should be used. 0N/A * Model for the selected subcontrol. 0N/A /* Lock object used in place of class object for synchronization. 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 DEBUG =
false;
// show bad params, misc. 0N/A * Sets the default value of the <code>lightWeightPopupEnabled</code> 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 * Gets the <code>defaultLightWeightPopupEnabled</code> property, 0N/A * which by default is <code>true</code>. 0N/A * @return the value of the <code>defaultLightWeightPopupEnabled</code> 0N/A * @see #setDefaultLightWeightPopupEnabled 0N/A * Constructs a <code>JPopupMenu</code> without an "invoker". 0N/A * Constructs a <code>JPopupMenu</code> with the specified title. 0N/A * @param label the string that a UI may use to display as a title 0N/A * for the popup menu. 0N/A * Returns the look and feel (L&F) object that renders this component. 0N/A * @return the <code>PopupMenuUI</code> object that renders this component 0N/A * Sets the L&F object that renders this component. 0N/A * @param ui the new <code>PopupMenuUI</code> L&F object 0N/A * @see UIDefaults#getUI 0N/A * attribute: visualUpdate true 0N/A * description: The UI object that implements the Component's LookAndFeel. 0N/A * Resets the UI property to a value from the current look and feel. 0N/A * @see JComponent#updateUI 0N/A * Returns the name of the L&F class that renders this component. 0N/A * @return the string "PopupMenuUI" 0N/A * @see JComponent#getUIClassID 0N/A * @see UIDefaults#getUI 0N/A * Processes key stroke events such as mnemonics and accelerators. 0N/A * @param evt the key event to be processed 0N/A * Returns the model object that handles single selections. 0N/A * @return the <code>selectionModel</code> property 0N/A * @see SingleSelectionModel 0N/A * Sets the model object to handle single selections. 0N/A * @param model the new <code>SingleSelectionModel</code> 0N/A * @see SingleSelectionModel 0N/A * description: The selection model for the popup menu 0N/A * Appends the specified menu item to the end of this menu. 0N/A * @param menuItem the <code>JMenuItem</code> to add 0N/A * @return the <code>JMenuItem</code> added 0N/A * Creates a new menu item with the specified text and appends 0N/A * it to the end of this menu. 0N/A * @param s the string for the menu item to be added 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 * @param a the <code>Action</code> to add to the menu 0N/A * @return the new menu item 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 * This adustment may be cancelled by invoking the application with 0N/A * -Djavax.swing.adjustPopupLocationToFit=false 341N/A // If we have GraphicsConfiguration use it to get screen bounds 341N/A // If we don't have GraphicsConfiguration use primary screen 341N/A // Calculate the screen size that popup should fit 341N/A // Insets include the task bar. Take them into account. 341N/A // Ensure that popup menu fits the screen 341N/A * Tries to find GraphicsConfiguration 341N/A * that contains the mouse cursor position. 0N/A // If not found and we have invoker, ask invoker about his gc 341N/A * Checks that there are enough security permissions 341N/A * to make popup "always on top", which allows to show it above the task bar. 341N/A // There is no permission to show popups over the task bar 0N/A * Factory method which creates the <code>JMenuItem</code> for 0N/A * <code>Actions</code> added to the <code>JPopupMenu</code>. 0N/A * @param a the <code>Action</code> for the menu item to be added 0N/A * @return the new menu item 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 * Removes the component at the specified index from this popup menu. 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 * 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 * 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 * @param aFlag <code>false</code> to disable lightweight popups 0N/A * description: Determines whether lightweight popups are used when possible 0N/A * @see #isLightWeightPopupEnabled 0N/A // NOTE: this use to set the flag on a shared JPopupMenu, which meant 0N/A // this effected ALL JPopupMenus. 0N/A * Gets the <code>lightWeightPopupEnabled</code> property. 0N/A * @return the value of the <code>lightWeightPopupEnabled</code> property 0N/A * @see #setLightWeightPopupEnabled 0N/A * Returns the popup menu's label 0N/A * @return a string containing the popup menu's label 0N/A * Sets the popup menu's label. Different look and feels may choose 0N/A * to display or not display this. 0N/A * @param label a string specifying the label for the popup menu 0N/A * description: The label for the popup menu. 0N/A * Appends a new separator at the end of the menu. 0N/A * Inserts a menu item for the specified <code>Action</code> object at 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 * Inserts the specified component into the menu at a given 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 // PENDING(ges): Why not use an array? 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 /* Add the removed items back to the menu, they are 0N/A already in the correct order in the temp vector. 0N/A * Adds a <code>PopupMenu</code> listener. 0N/A * @param l the <code>PopupMenuListener</code> to add 0N/A * Removes a <code>PopupMenu</code> listener. 0N/A * @param l the <code>PopupMenuListener</code> to remove 0N/A * Returns an array of all the <code>PopupMenuListener</code>s added 0N/A * to this JMenuItem with addPopupMenuListener(). 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 * Adds a <code>MenuKeyListener</code> to the popup menu. 0N/A * @param l the <code>MenuKeyListener</code> to be added 0N/A * Removes a <code>MenuKeyListener</code> from the popup menu. 0N/A * @param l the <code>MenuKeyListener</code> to be removed 0N/A * Returns an array of all the <code>MenuKeyListener</code>s added 0N/A * to this JPopupMenu with addMenuKeyListener(). 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 * Notifies <code>PopupMenuListener</code>s that this popup menu will 0N/A * Notifies <code>PopupMenuListener</code>s that this popup menu will 0N/A * Notifies <code>PopupMenuListeners</code> that this popup menu is 0N/A * Always returns true since popups, by definition, should always 0N/A * be on top of all other windows. 0N/A * Lays out the container so that it uses the minimum space 0N/A * needed to display its contents. 0N/A * Sets the visibility of the popup menu. 0N/A * @param b true to make the popup visible, or false to 0N/A * description: Makes the popup visible 0N/A // if closing, first close all Submenus 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 // This is a popup menu with MenuElement children, 0N/A // set selection path before popping up! 0N/A // 4694797: When popup menu is made invisible, selected path 0N/A // should be cleared 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 * This does NOT fire any events, it is up the caller to dispatch 0N/A * the necessary events. 0N/A // adjust the location of the popup 0N/A * Returns true if the popup menu is visible (currently 0N/A * Sets the location of the upper left corner of the 0N/A * popup menu using x, y coordinates. 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 * description: The location of the popup menu. 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 * @return true if this menu is a standalone popup menu, otherwise false 0N/A * Returns the component which is the 'invoker' of this 0N/A * @return the <code>Component</code> in which the popup menu is displayed 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 * @param invoker the <code>Component</code> in which the popup 0N/A * description: The invoking component for the popup menu 0N/A * Displays the popup menu at the position x,y in the coordinate 0N/A * space of the component invoker. 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 // Use the invoker's frame so that events 0N/A // are propagated properly 0N/A // To avoid integer overflow 0N/A * Returns the popup menu which is at the root of the menu system 0N/A * for this popup menu. 0N/A * @return the topmost grandparent <code>JPopupMenu</code> 0N/A * Returns the component at the specified index. 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 * Returns the index of the specified component. 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 * 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 * @param d the <code>Dimension</code> specifying the new size 0N/A * of this component. 0N/A * description: The size of the popup menu 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 * @param width the new width of the Popup in pixels 0N/A * @param height the new height of the Popup in pixels 0N/A * description: The size of the popup menu 0N/A * Sets the currently selected component, This will result 0N/A * in a change to the selection model. 0N/A * @param sel the <code>Component</code> to select 0N/A * description: The selected component on the popup menu 0N/A * Checks whether the border should be painted. 0N/A * @return true if the border is painted, false otherwise 0N/A * @see #setBorderPainted 0N/A * Sets whether the border should be painted. 0N/A * @param b if true, the border is painted. 0N/A * @see #isBorderPainted 0N/A * description: Is the border of the popup menu painted 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 * @see JComponent#paint 0N/A * @see JComponent#setBorder 0N/A * Returns the margin, in pixels, between the popup menu's border and 0N/A * @return an <code>Insets</code> object containing the margin values. 0N/A * Examines the list of menu items to determine whether 0N/A * <code>popup</code> is a popup menu. 0N/A * @param popup a <code>JPopupMenu</code> 0N/A * @return true if <code>popup</code> 0N/A * Returns a string representation of this <code>JPopupMenu</code>. 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 * @return a string representation of this <code>JPopupMenu</code>. 0N/A// Accessibility support 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 * @return an AccessibleJPopupMenu that serves as the 0N/A * AccessibleContext of this JPopupMenu 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 * AccessibleJPopupMenu constructor 0N/A * Get the role of this object. 0N/A * @return an instance of AccessibleRole describing the role of 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 * @throws NullPointerException if the parameter is null. 0N/A * Handles popup "visible" PropertyChangeEvent 0N/A // notify listeners that the popup became visible 0N/A // notify listeners that a popup list item is selected 0N/A // notify listeners that the popup became hidden 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 // get the popup list 0N/A // get the first selected item 0N/A // fire the event with the popup invoker as the source. 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 }
// inner class AccessibleJPopupMenu 0N/A// Serialization support. 0N/A // Save the invoker, if its Serializable. 0N/A // Save the popup, if its Serializable. 0N/A // implements javax.swing.MenuElement 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 * 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 * 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 * @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 * Handles a keystroke in a menu. 0N/A * @param e a <code>MenuKeyEvent</code> object 0N/A * Notifies all listeners that have registered interest for 0N/A * notification on this event type. 0N/A * @param event a <code>MenuKeyEvent</code> 0N/A * @see EventListenerList 0N/A * Notifies all listeners that have registered interest for 0N/A * notification on this event type. 0N/A * @param event a <code>MenuKeyEvent</code> 0N/A * @see EventListenerList 0N/A * Notifies all listeners that have registered interest for 0N/A * notification on this event type. 0N/A * @param event a <code>MenuKeyEvent</code> 0N/A * @see EventListenerList 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 * @param isIncluded true if this menu is active, false if 0N/A * @see MenuElement#menuSelectionChanged(boolean) 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 * @return an array of <code>MenuElement</code> objects 0N/A * @see MenuElement#getSubElements 0N/A for(i=
0 ; i < c ; i++) {
0N/A * Returns this <code>JPopupMenu</code> component. 0N/A * @return this <code>JPopupMenu</code> object 0N/A * @see MenuElement#getComponent 0N/A * A popup menu-specific separator. 0N/A * Returns the name of the L&F class that renders this component. 0N/A * @return the string "PopupMenuSeparatorUI" 0N/A * @see JComponent#getUIClassID 0N/A * @see UIDefaults#getUI 0N/A return "PopupMenuSeparatorUI";
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 * @return true if the mouse event is a popup trigger