2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.Component;
42 import java.awt.Dimension;
43 import java.awt.Insets;
44 import java.awt.Point;
45 import java.awt.event.KeyEvent;
46 import java.awt.event.MouseEvent;
47 import java.beans.PropertyChangeEvent;
48 import java.beans.PropertyChangeListener;
49 import java.util.ArrayList;
50 import java.util.EventListener;
52 import javax.accessibility.Accessible;
53 import javax.accessibility.AccessibleContext;
54 import javax.accessibility.AccessibleRole;
55 import javax.swing.event.MenuKeyListener;
56 import javax.swing.event.PopupMenuEvent;
57 import javax.swing.event.PopupMenuListener;
58 import javax.swing.plaf.PopupMenuUI;
61 * JPopupMenu is a container that is used to display popup menu's menu
62 * items. By default JPopupMenu is a lightweight container, however if it
63 * is the case that JPopupMenu's bounds are outside of main window, then
64 * heawyweight container will be used to display menu items. It is also
65 * possible to change JPopupMenu's default behavior and set JPopupMenu
66 * to always use heavyweight container.
68 * JPopupMenu can be displayed anywhere; it is a floating free popup menu.
69 * However before JPopupMenu is diplayed, its invoker property should be set.
70 * JPopupMenu's invoker is a component relative to which popup menu is
73 * JPopupMenu fires PopupMenuEvents to its registered listeners. Whenever
74 * JPopupMenu becomes visible on the screen then PopupMenuEvent indicating
75 * that popup menu became visible will be fired. In the case when
76 * JPopupMenu becomes invisible or cancelled without selection, then
77 * popupMenuBecomeInvisible() or popupMenuCancelled() methods of
78 * PopupMenuListeners will be invoked.
80 * JPopupMenu also fires PropertyChangeEvents when its bound properties
81 * change.In addittion to inheritted bound properties, JPopupMenu has
82 * 'visible' bound property. When JPopupMenu becomes visible/invisible on
83 * the screen it fires PropertyChangeEvents to its registered
84 * PropertyChangeListeners.
86 public class JPopupMenu extends JComponent implements Accessible, MenuElement
88 private static final long serialVersionUID = -8336996630009646009L;
90 /* indicates if popup's menu border should be painted*/
91 private boolean borderPainted = true;
93 /** Flag indicating whether lightweight, mediumweight or heavyweight popup
94 is used to display menu items.
96 These are the possible cases:
98 1. if DefaultLightWeightPopupEnabled true
99 (i) use lightweight container if popup feets inside top-level window
100 (ii) only use heavyweight container (JDialog) if popup doesn't fit.
102 2. if DefaultLightWeightPopupEnabled false
103 (i) if popup fits, use awt.Panel (mediumWeight)
104 (ii) if popup doesn't fit, use JDialog (heavyWeight)
106 private static boolean DefaultLightWeightPopupEnabled = true;
108 /* Component that invokes popup menu. */
109 transient Component invoker;
111 /* Label for this popup menu. It is not used in most of the look and feel themes. */
112 private String label;
114 /*Amount of space between menuItem's in JPopupMenu and JPopupMenu's border */
115 private Insets margin;
117 /** Indicates whether ligthWeight container can be used to display popup
118 menu. This flag is the same as DefaultLightWeightPopupEnabled, but setting
119 this flag can change popup menu after creation of the object */
120 private boolean lightWeightPopupEnabled;
122 /** SelectionModel that keeps track of menu selection. */
123 protected SingleSelectionModel selectionModel;
125 /* Popup that is used to display JPopupMenu */
126 private transient Popup popup;
129 * Location of the popup, X coordinate.
131 private int popupLocationX;
134 * Location of the popup, Y coordinate.
136 private int popupLocationY;
138 /* Field indicating if popup menu is visible or not */
139 private boolean visible = false;
142 * Creates a new JPopupMenu object.
150 * Creates a new JPopupMenu with specified label
152 * @param label Label for popup menu.
154 public JPopupMenu(String label)
156 lightWeightPopupEnabled = getDefaultLightWeightPopupEnabled();
158 setSelectionModel(new DefaultSingleSelectionModel());
159 super.setVisible(false);
164 * Adds given menu item to the popup menu
166 * @param item menu item to add to the popup menu
168 * @return menu item that was added to the popup menu
170 public JMenuItem add(JMenuItem item)
172 this.insert(item, -1);
177 * Constructs menu item with a specified label and adds it to
180 * @param text label for the menu item to be added
182 * @return constructed menu item that was added to the popup menu
184 public JMenuItem add(String text)
186 JMenuItem item = new JMenuItem(text);
191 * Constructs menu item associated with the specified action
192 * and adds it to the popup menu
194 * @param action Action for the new menu item
196 * @return menu item that was added to the menu
198 public JMenuItem add(Action action)
200 JMenuItem item = createActionComponent(action);
203 action.addPropertyChangeListener(createActionChangeListener(item));
209 * Revomes component at the given index from the menu.
211 * @param index index of the component that will be removed in the menu
213 public void remove(int index)
220 * Create menu item associated with the given action
221 * and inserts it into the popup menu at the specified index
223 * @param action Action for the new menu item
224 * @param index index in the popup menu at which to insert new menu item.
226 public void insert(Action action, int index)
228 JMenuItem item = new JMenuItem(action);
229 this.insert(item, index);
233 * Insert given component to the popup menu at the
236 * @param component Component to insert
237 * @param index Index at which to insert given component
239 public void insert(Component component, int index)
241 super.add(component, index);
245 * Returns flag indicating if newly created JPopupMenu will use
246 * heavyweight or lightweight container to display its menu items
248 * @return true if JPopupMenu will use lightweight container to display
249 * menu items by default, and false otherwise.
251 public static boolean getDefaultLightWeightPopupEnabled()
253 return DefaultLightWeightPopupEnabled;
257 * Sets whether JPopupMenu should use ligthWeight container to
258 * display it menu items by default
260 * @param enabled true if JPopupMenu should use lightweight container
261 * for displaying its menu items, and false otherwise.
263 public static void setDefaultLightWeightPopupEnabled(boolean enabled)
265 DefaultLightWeightPopupEnabled = enabled;
269 * This method returns the UI used to display the JPopupMenu.
271 * @return The UI used to display the JPopupMenu.
273 public PopupMenuUI getUI()
275 return (PopupMenuUI) ui;
279 * Set the "UI" property of the menu item, which is a look and feel class
280 * responsible for handling popupMenu's input events and painting it.
282 * @param ui The new "UI" property
284 public void setUI(PopupMenuUI ui)
290 * This method sets this menuItem's UI to the UIManager's default for the
291 * current look and feel.
293 public void updateUI()
295 setUI((PopupMenuUI) UIManager.getUI(this));
299 * This method returns a name to identify which look and feel class will be
300 * the UI delegate for the menuItem.
302 * @return The Look and Feel classID. "PopupMenuUI"
304 public String getUIClassID()
306 return "PopupMenuUI";
310 * Returns selectionModel used by this popup menu to keep
311 * track of the selection.
313 * @return popup menu's selection model
315 public SingleSelectionModel getSelectionModel()
317 return selectionModel;
321 * Sets selection model for this popup menu
323 * @param model new selection model of this popup menu
325 public void setSelectionModel(SingleSelectionModel model)
327 selectionModel = model;
331 * Creates new menu item associated with a given action.
333 * @param action Action used to create new menu item
335 * @return new created menu item associated with a given action.
337 protected JMenuItem createActionComponent(Action action)
339 return new JMenuItem(action);
343 * Creates PropertyChangeListener that listens to PropertyChangeEvents
344 * occuring in the Action associated with given menu item in this popup menu.
346 * @param item MenuItem
348 * @return The PropertyChangeListener
350 protected PropertyChangeListener createActionChangeListener(JMenuItem item)
352 return new ActionChangeListener();
356 * Returns true if this popup menu will display its menu item in
357 * a lightweight container and false otherwise.
359 * @return true if this popup menu will display its menu items
360 * in a lightweight container and false otherwise.
362 public boolean isLightWeightPopupEnabled()
364 return lightWeightPopupEnabled;
370 * @param enabled DOCUMENT ME!
372 public void setLightWeightPopupEnabled(boolean enabled)
374 lightWeightPopupEnabled = enabled;
378 * Returns label for this popup menu
380 * @return label for this popup menu
382 public String getLabel()
388 * Sets label for this popup menu. This method fires PropertyChangeEvent
389 * when the label property is changed. Please note that most
390 * of the Look & Feel will ignore this property.
392 * @param label label for this popup menu
394 public void setLabel(String label)
396 if (label != this.label)
398 String oldLabel = this.label;
400 firePropertyChange("label", oldLabel, label);
405 * Adds separator to this popup menu
407 public void addSeparator()
409 // insert separator at the end of the list of menu items
410 this.insert(new Separator(), -1);
414 * Adds a MenuKeyListener to the popup.
416 * @param l - the listener to add.
418 public void addMenuKeyListener(MenuKeyListener l)
420 listenerList.add(MenuKeyListener.class, l);
424 * Removes a MenuKeyListener from the popup.
426 * @param l - the listener to remove.
428 public void removeMenuKeyListener(MenuKeyListener l)
430 listenerList.remove(MenuKeyListener.class, l);
434 * Returns array of getMenuKeyListeners that are listening to JPopupMenu.
436 * @return array of getMenuKeyListeners that are listening to JPopupMenu
438 public MenuKeyListener[] getMenuKeyListeners()
440 return ((MenuKeyListener[]) listenerList.getListeners(MenuKeyListener.class));
444 * Adds popupMenuListener to listen for PopupMenuEvents fired
447 * @param listener PopupMenuListener to add to JPopupMenu
449 public void addPopupMenuListener(PopupMenuListener listener)
451 listenerList.add(PopupMenuListener.class, listener);
455 * Removes PopupMenuListener from JPopupMenu's list of listeners
457 * @param listener PopupMenuListener which needs to be removed
459 public void removePopupMenuListener(PopupMenuListener listener)
461 listenerList.remove(PopupMenuListener.class, listener);
465 * Returns array of PopupMenuListeners that are listening to JPopupMenu
467 * @return Array of PopupMenuListeners that are listening to JPopupMenu
469 public PopupMenuListener[] getPopupMenuListeners()
471 return ((PopupMenuListener[]) listenerList.getListeners(PopupMenuListener.class));
475 * This method calls popupMenuWillBecomeVisible() of popup menu's
476 * PopupMenuListeners. This method is invoked just before popup menu
477 * will appear on the screen.
479 protected void firePopupMenuWillBecomeVisible()
481 EventListener[] ll = listenerList.getListeners(PopupMenuListener.class);
483 for (int i = 0; i < ll.length; i++)
484 ((PopupMenuListener) ll[i]).popupMenuWillBecomeVisible(new PopupMenuEvent(this));
488 * This method calls popupMenuWillBecomeInvisible() of popup
489 * menu's PopupMenuListeners. This method is invoked just before popup
490 * menu will disappear from the screen
492 protected void firePopupMenuWillBecomeInvisible()
494 EventListener[] ll = listenerList.getListeners(PopupMenuListener.class);
496 for (int i = 0; i < ll.length; i++)
497 ((PopupMenuListener) ll[i]).popupMenuWillBecomeInvisible(new PopupMenuEvent(this));
501 * This method calls popupMenuCanceled() of popup menu's PopupMenuListeners.
502 * This method is invoked just before popup menu is cancelled. This happens
503 * when popup menu is closed without selecting any of its menu items. This
504 * usually happens when the top-level window is resized or moved.
506 protected void firePopupMenuCanceled()
508 EventListener[] ll = listenerList.getListeners(PopupMenuListener.class);
510 for (int i = 0; i < ll.length; i++)
511 ((PopupMenuListener) ll[i]).popupMenuCanceled(new PopupMenuEvent(this));
515 * This methods sets popup menu's size to its' preferred size. If the
516 * popup menu's size is previously set it will be ignored.
520 // Hook up this call so that it gets executed on the event thread in order
521 // to avoid synchronization problems when calling the layout manager.
522 if (! SwingUtilities.isEventDispatchThread())
524 SwingUtilities.invokeLater(new Runnable()
533 setSize(getPreferredSize());
537 * Return visibility of the popup menu
539 * @return true if popup menu is visible on the screen and false otherwise.
541 public boolean isVisible()
547 * Sets visibility property of this popup menu. If the property is
548 * set to true then popup menu will be dispayed and popup menu will
549 * hide itself if visible property is set to false.
551 * @param visible true if popup menu will become visible and false otherwise.
553 public void setVisible(final boolean visible)
555 // Hook up this call so that it gets executed on the event thread in order
556 // to avoid synchronization problems when calling the layout manager.
557 if (! SwingUtilities.isEventDispatchThread())
559 SwingUtilities.invokeLater(new Runnable()
568 if (visible == isVisible())
571 boolean old = isVisible();
572 this.visible = visible;
573 if (old != isVisible())
577 if (invoker != null && !(invoker instanceof JMenu))
579 MenuElement[] menuEls;
580 if (getSubElements().length > 0)
582 menuEls = new MenuElement[2];
584 menuEls[1] = getSubElements()[0];
588 menuEls = new MenuElement[1];
591 MenuSelectionManager.defaultManager().setSelectedPath(menuEls);
593 firePopupMenuWillBecomeVisible();
594 PopupFactory pf = PopupFactory.getSharedInstance();
596 popup = pf.getPopup(invoker, this, popupLocationX, popupLocationY);
601 getSelectionModel().clearSelection();
602 firePopupMenuWillBecomeInvisible();
605 firePropertyChange("visible", old, isVisible());
610 * Sets location of the popup menu.
612 * @param x X coordinate of the popup menu's location
613 * @param y Y coordinate of the popup menu's location
615 public void setLocation(int x, int y)
619 // Handle the case when the popup is already showing. In this case we need
620 // to fetch a new popup from PopupFactory and use this. See the general
621 // contract of the PopupFactory.
625 * Returns popup menu's invoker.
627 * @return popup menu's invoker
629 public Component getInvoker()
635 * Sets popup menu's invoker.
637 * @param component The new invoker of this popup menu
639 public void setInvoker(Component component)
645 * This method displays JPopupMenu on the screen at the specified
646 * location. Note that x and y coordinates given to this method
647 * should be expressed in terms of the popup menus' invoker.
649 * @param component Invoker for this popup menu
650 * @param x x-coordinate of the popup menu relative to the specified invoker
651 * @param y y-coordiate of the popup menu relative to the specified invoker
653 public void show(Component component, int x, int y)
655 if (component.isShowing())
657 setInvoker(component);
658 Point p = new Point(x, y);
659 SwingUtilities.convertPointToScreen(p, component);
660 setLocation(p.x, p.y);
666 * Returns component located at the specified index in the popup menu
668 * @param index index of the component to return
670 * @return component located at the specified index in the popup menu
672 * @deprecated Replaced by getComponent(int)
674 public Component getComponentAtIndex(int index)
676 return getComponent(index);
680 * Returns index of the specified component in the popup menu
682 * @param component Component to look for
684 * @return index of the specified component in the popup menu
686 public int getComponentIndex(Component component)
688 Component[] items = getComponents();
690 for (int i = 0; i < items.length; i++)
692 if (items[i].equals(component))
700 * Sets size of the popup
702 * @param size Dimensions representing new size of the popup menu
704 public void setPopupSize(Dimension size)
710 * Sets size of the popup menu
712 * @param width width for the new size
713 * @param height height for the new size
715 public void setPopupSize(int width, int height)
717 super.setSize(width, height);
721 * Selects specified component in this popup menu.
723 * @param selected component to select
725 public void setSelected(Component selected)
727 int index = getComponentIndex(selected);
728 selectionModel.setSelectedIndex(index);
732 * Checks if this popup menu paints its border.
734 * @return true if this popup menu paints its border and false otherwise.
736 public boolean isBorderPainted()
738 return borderPainted;
742 * Sets if the border of the popup menu should be
745 * @param painted true if the border should be painted and false otherwise
747 public void setBorderPainted(boolean painted)
749 borderPainted = painted;
753 * Returns margin for this popup menu.
755 * @return margin for this popup menu.
757 public Insets getMargin()
763 * A string that describes this JPopupMenu. Normally only used
766 * @return A string describing this JMenuItem
768 protected String paramString()
770 StringBuffer sb = new StringBuffer();
771 sb.append(super.paramString());
772 sb.append(",label=");
773 if (getLabel() != null)
774 sb.append(getLabel());
775 sb.append(",lightWeightPopupEnabled=").append(isLightWeightPopupEnabled());
776 sb.append(",margin=");
777 if (getMargin() != null)
779 sb.append(",paintBorder=").append(isBorderPainted());
780 return sb.toString();
784 * Process mouse events forwarded from MenuSelectionManager. This method
785 * doesn't do anything. It is here to conform to the MenuElement interface.
787 * @param event event forwarded from MenuSelectionManager
788 * @param path path to the menu element from which event was generated
789 * @param manager MenuSelectionManager for the current menu hierarchy
791 public void processMouseEvent(MouseEvent event, MenuElement[] path,
792 MenuSelectionManager manager)
794 // Empty Implementation. This method is needed for the implementation
795 // of MenuElement interface
799 * Process key events forwarded from MenuSelectionManager. This method
800 * doesn't do anything. It is here to conform to the MenuElement interface.
802 * @param event event forwarded from MenuSelectionManager
803 * @param path path to the menu element from which event was generated
804 * @param manager MenuSelectionManager for the current menu hierarchy
807 public void processKeyEvent(KeyEvent event, MenuElement[] path,
808 MenuSelectionManager manager)
810 // Empty Implementation. This method is needed for the implementation
811 // of MenuElement interface
815 * Method of MenuElement Interface. It is invoked when
816 * popupMenu's selection has changed
818 * @param changed true if this popupMenu is part of current menu
819 * hierarchy and false otherwise.
821 public void menuSelectionChanged(boolean changed)
823 if (invoker instanceof JMenu)
825 // We need to special case this since the JMenu calculates the
826 // position etc of the popup.
827 JMenu menu = (JMenu) invoker;
828 menu.setPopupMenuVisible(changed);
835 * Return subcomonents of this popup menu. This method returns only
836 * components that implement the <code>MenuElement</code> interface.
838 * @return array of menu items belonging to this popup menu
840 public MenuElement[] getSubElements()
842 Component[] items = getComponents();
843 ArrayList subElements = new ArrayList();
845 for (int i = 0; i < items.length; i++)
846 if (items[i] instanceof MenuElement)
847 subElements.add(items[i]);
849 return (MenuElement[])
850 subElements.toArray(new MenuElement[subElements.size()]);
854 * Method of the MenuElement interface. Returns reference to itself.
856 * @return Returns reference to itself
858 public Component getComponent()
864 * Checks if observing mouse event should trigger popup
865 * menu to show on the screen.
867 * @param event MouseEvent to check
869 * @return true if the observing mouse event is popup trigger and false otherwise
871 public boolean isPopupTrigger(MouseEvent event)
873 return ((PopupMenuUI) getUI()).isPopupTrigger(event);
879 * @return DOCUMENT ME!
881 public AccessibleContext getAccessibleContext()
883 if (accessibleContext == null)
884 accessibleContext = new AccessibleJPopupMenu();
886 return accessibleContext;
890 * This is the separator that can be used in popup menu.
892 public static class Separator extends JSeparator
899 public String getUIClassID()
901 return "PopupMenuSeparatorUI";
906 * Returns <code>true</code> if the component is guaranteed to be painted
907 * on top of others. This returns false by default and is overridden by
908 * components like JMenuItem, JPopupMenu and JToolTip to return true for
911 * @return <code>true</code> if the component is guaranteed to be painted
919 protected class AccessibleJPopupMenu extends AccessibleJComponent
921 private static final long serialVersionUID = 7423261328879849768L;
923 protected AccessibleJPopupMenu()
925 // Nothing to do here.
928 public AccessibleRole getAccessibleRole()
930 return AccessibleRole.POPUP_MENU;
934 /* This class resizes popup menu and repaints popup menu appropriately if one
935 of item's action has changed */
936 private class ActionChangeListener implements PropertyChangeListener
938 public void propertyChange(PropertyChangeEvent evt)
940 // We used to have a revalidate() and repaint() call here. However I think
941 // this is not needed. Instead, a new Popup has to be fetched from the
942 // PopupFactory and used here.