Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / plaf / basic / BasicMenuItemUI.java
blobff48a74f07474bde886a683123399155cad1e6c0
1 /* BasicMenuItemUI.java --
2 Copyright (C) 2002, 2004 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)
9 any later version.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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
24 combination.
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. */
39 package javax.swing.plaf.basic;
41 import java.awt.Color;
42 import java.awt.Component;
43 import java.awt.Dimension;
44 import java.awt.Font;
45 import java.awt.FontMetrics;
46 import java.awt.Graphics;
47 import java.awt.Insets;
48 import java.awt.Rectangle;
49 import java.awt.event.KeyEvent;
50 import java.awt.event.MouseEvent;
51 import java.beans.PropertyChangeEvent;
52 import java.beans.PropertyChangeListener;
53 import java.util.ArrayList;
55 import javax.swing.Icon;
56 import javax.swing.JComponent;
57 import javax.swing.JMenu;
58 import javax.swing.JMenuItem;
59 import javax.swing.JPopupMenu;
60 import javax.swing.KeyStroke;
61 import javax.swing.MenuElement;
62 import javax.swing.MenuSelectionManager;
63 import javax.swing.SwingConstants;
64 import javax.swing.SwingUtilities;
65 import javax.swing.UIDefaults;
66 import javax.swing.UIManager;
67 import javax.swing.event.MenuDragMouseEvent;
68 import javax.swing.event.MenuDragMouseListener;
69 import javax.swing.event.MenuKeyEvent;
70 import javax.swing.event.MenuKeyListener;
71 import javax.swing.event.MouseInputListener;
72 import javax.swing.plaf.ComponentUI;
73 import javax.swing.plaf.MenuItemUI;
75 /**
76 * UI Delegate for JMenuItem.
78 public class BasicMenuItemUI extends MenuItemUI
80 /**
81 * Font to be used when displaying menu item's accelerator.
83 protected Font acceleratorFont;
85 /**
86 * Color to be used when displaying menu item's accelerator.
88 protected Color acceleratorForeground;
90 /**
91 * Color to be used when displaying menu item's accelerator when menu item is
92 * selected.
94 protected Color acceleratorSelectionForeground;
96 /**
97 * Icon that is displayed after the text to indicated that this menu contains
98 * submenu.
100 protected Icon arrowIcon;
103 * Icon that is displayed before the text. This icon is only used in
104 * JCheckBoxMenuItem or JRadioBoxMenuItem.
106 protected Icon checkIcon;
109 * Number of spaces between icon and text.
111 protected int defaultTextIconGap = 4;
114 * Color of the text when menu item is disabled
116 protected Color disabledForeground;
119 * The menu Drag mouse listener listening to the menu item.
121 protected MenuDragMouseListener menuDragMouseListener;
124 * The menu item itself
126 protected JMenuItem menuItem;
129 * Menu Key listener listening to the menu item.
131 protected MenuKeyListener menuKeyListener;
134 * mouse input listener listening to menu item.
136 protected MouseInputListener mouseInputListener;
139 * Indicates if border should be painted
141 protected boolean oldBorderPainted;
144 * Color of text that is used when menu item is selected
146 protected Color selectionBackground;
149 * Color of the text that is used when menu item is selected.
151 protected Color selectionForeground;
154 * String that separates description of the modifiers and the key
156 private String acceleratorDelimiter;
159 * PropertyChangeListener to listen for property changes in the menu item
161 private PropertyChangeListener propertyChangeListener;
164 * Number of spaces between accelerator and menu item's label.
166 private int defaultAcceleratorLabelGap = 4;
169 * Creates a new BasicMenuItemUI object.
171 public BasicMenuItemUI()
173 mouseInputListener = createMouseInputListener(menuItem);
174 menuDragMouseListener = createMenuDragMouseListener(menuItem);
175 menuKeyListener = createMenuKeyListener(menuItem);
176 propertyChangeListener = new PropertyChangeHandler();
180 * Create MenuDragMouseListener to listen for mouse dragged events.
182 * @param c menu item to listen to
184 * @return The MenuDragMouseListener
186 protected MenuDragMouseListener createMenuDragMouseListener(JComponent c)
188 return new MenuDragMouseHandler();
192 * Creates MenuKeyListener to listen to key events occuring when menu item
193 * is visible on the screen.
195 * @param c menu item to listen to
197 * @return The MenuKeyListener
199 protected MenuKeyListener createMenuKeyListener(JComponent c)
201 return new MenuKeyHandler();
205 * Handles mouse input events occuring for this menu item
207 * @param c menu item to listen to
209 * @return The MouseInputListener
211 protected MouseInputListener createMouseInputListener(JComponent c)
213 return new MouseInputHandler();
217 * Factory method to create a BasicMenuItemUI for the given {@link
218 * JComponent}, which should be a {@link JMenuItem}.
220 * @param c The {@link JComponent} a UI is being created for.
222 * @return A BasicMenuItemUI for the {@link JComponent}.
224 public static ComponentUI createUI(JComponent c)
226 return new BasicMenuItemUI();
230 * Programatically clicks menu item.
232 * @param msm MenuSelectionManager for the menu hierarchy
234 protected void doClick(MenuSelectionManager msm)
236 menuItem.doClick();
237 msm.clearSelectedPath();
241 * Returns maximum size for the specified menu item
243 * @param c component for which to get maximum size
245 * @return Maximum size for the specified menu item.
247 public Dimension getMaximumSize(JComponent c)
249 return null;
253 * Returns minimum size for the specified menu item
255 * @param c component for which to get minimum size
257 * @return Minimum size for the specified menu item.
259 public Dimension getMinimumSize(JComponent c)
261 return null;
265 * Returns path to this menu item.
267 * @return $MenuElement[]$ Returns array of menu elements
268 * that constitute a path to this menu item.
270 public MenuElement[] getPath()
272 ArrayList path = new ArrayList();
274 // Path to menu should also include its popup menu.
275 if (menuItem instanceof JMenu)
276 path.add(((JMenu) menuItem).getPopupMenu());
278 Component c = menuItem;
279 while (c instanceof MenuElement)
281 path.add(0, (MenuElement) c);
283 if (c instanceof JPopupMenu)
284 c = ((JPopupMenu) c).getInvoker();
285 else
286 c = c.getParent();
289 MenuElement[] pathArray = new MenuElement[path.size()];
290 path.toArray(pathArray);
291 return pathArray;
295 * Returns preferred size for the given menu item.
297 * @param c menu item for which to get preferred size
298 * @param checkIcon chech icon displayed in the given menu item
299 * @param arrowIcon arrow icon displayed in the given menu item
300 * @param defaultTextIconGap space between icon and text in the given menuItem
302 * @return $Dimension$ preferred size for the given menu item
304 protected Dimension getPreferredMenuItemSize(JComponent c, Icon checkIcon,
305 Icon arrowIcon,
306 int defaultTextIconGap)
308 JMenuItem m = (JMenuItem) c;
309 Dimension d = BasicGraphicsUtils.getPreferredButtonSize(m,
310 defaultTextIconGap);
312 // if menu item has accelerator then take accelerator's size into account
313 // when calculating preferred size.
314 KeyStroke accelerator = m.getAccelerator();
315 Rectangle rect;
317 if (accelerator != null)
319 rect = getAcceleratorRect(accelerator,
320 m.getToolkit().getFontMetrics(acceleratorFont));
322 // add width of accelerator's text
323 d.width = d.width + rect.width + defaultAcceleratorLabelGap;
325 // adjust the heigth of the preferred size if necessary
326 if (d.height < rect.height)
327 d.height = rect.height;
330 if (checkIcon != null)
332 d.width = d.width + checkIcon.getIconWidth() + defaultTextIconGap;
334 if (checkIcon.getIconHeight() > d.height)
335 d.height = checkIcon.getIconHeight();
338 if (arrowIcon != null && (c instanceof JMenu))
340 d.width = d.width + arrowIcon.getIconWidth() + defaultTextIconGap;
342 if (arrowIcon.getIconHeight() > d.height)
343 d.height = arrowIcon.getIconHeight();
346 return d;
350 * Returns preferred size of the given component
352 * @param c component for which to return preferred size
354 * @return $Dimension$ preferred size for the given component
356 public Dimension getPreferredSize(JComponent c)
358 return getPreferredMenuItemSize(c, checkIcon, arrowIcon, defaultTextIconGap);
361 protected String getPropertyPrefix()
363 return null;
367 * This method installs the components for this {@link JMenuItem}.
369 * @param menuItem The {@link JMenuItem} to install components for.
371 protected void installComponents(JMenuItem menuItem)
373 // FIXME: Need to implement
377 * This method installs the defaults that are defined in the Basic look and
378 * feel for this {@link JMenuItem}.
380 protected void installDefaults()
382 UIDefaults defaults = UIManager.getLookAndFeelDefaults();
384 menuItem.setBackground(defaults.getColor("MenuItem.background"));
385 menuItem.setBorder(defaults.getBorder("MenuItem.border"));
386 menuItem.setFont(defaults.getFont("MenuItem.font"));
387 menuItem.setForeground(defaults.getColor("MenuItem.foreground"));
388 menuItem.setMargin(defaults.getInsets("MenuItem.margin"));
389 menuItem.setOpaque(true);
390 acceleratorFont = defaults.getFont("MenuItem.acceleratorFont");
391 acceleratorForeground = defaults.getColor("MenuItem.acceleratorForeground");
392 acceleratorSelectionForeground = defaults.getColor("MenuItem.acceleratorSelectionForeground");
393 selectionBackground = defaults.getColor("MenuItem.selectionBackground");
394 selectionForeground = defaults.getColor("MenuItem.selectionForeground");
395 acceleratorDelimiter = defaults.getString("MenuItem.acceleratorDelimiter");
397 menuItem.setHorizontalTextPosition(SwingConstants.TRAILING);
398 menuItem.setHorizontalAlignment(SwingConstants.LEADING);
402 * This method installs the keyboard actions for this {@link JMenuItem}.
404 protected void installKeyboardActions()
406 // FIXME: Need to implement
410 * This method installs the listeners for the {@link JMenuItem}.
412 protected void installListeners()
414 menuItem.addMouseListener(mouseInputListener);
415 menuItem.addMouseMotionListener(mouseInputListener);
416 menuItem.addMenuDragMouseListener(menuDragMouseListener);
417 menuItem.addMenuKeyListener(menuKeyListener);
418 menuItem.addPropertyChangeListener(propertyChangeListener);
422 * Installs and initializes all fields for this UI delegate. Any properties
423 * of the UI that need to be initialized and/or set to defaults will be
424 * done now. It will also install any listeners necessary.
426 * @param c The {@link JComponent} that is having this UI installed.
428 public void installUI(JComponent c)
430 super.installUI(c);
431 menuItem = (JMenuItem) c;
432 installDefaults();
433 installComponents(menuItem);
434 installListeners();
438 * Paints given menu item using specified graphics context
440 * @param g The graphics context used to paint this menu item
441 * @param c Menu Item to paint
443 public void paint(Graphics g, JComponent c)
445 paintMenuItem(g, c, checkIcon, arrowIcon, c.getBackground(),
446 c.getForeground(), defaultTextIconGap);
450 * Paints background of the menu item
452 * @param g The graphics context used to paint this menu item
453 * @param menuItem menu item to paint
454 * @param bgColor Background color to use when painting menu item
456 protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor)
458 Dimension size = getPreferredSize(menuItem);
459 Color foreground = g.getColor();
460 g.setColor(bgColor);
461 g.drawRect(0, 0, size.width, size.height);
462 g.setColor(foreground);
466 * Paints specified menu item
468 * @param g The graphics context used to paint this menu item
469 * @param c menu item to paint
470 * @param checkIcon check icon to use when painting menu item
471 * @param arrowIcon arrow icon to use when painting menu item
472 * @param background Background color of the menu item
473 * @param foreground Foreground color of the menu item
474 * @param defaultTextIconGap space to use between icon and
475 * text when painting menu item
477 protected void paintMenuItem(Graphics g, JComponent c, Icon checkIcon,
478 Icon arrowIcon, Color background,
479 Color foreground, int defaultTextIconGap)
481 JMenuItem m = (JMenuItem) c;
482 Rectangle tr = new Rectangle(); // text rectangle
483 Rectangle ir = new Rectangle(); // icon rectangle
484 Rectangle vr = new Rectangle(); // view rectangle
485 Rectangle br = new Rectangle(); // border rectangle
486 Rectangle ar = new Rectangle(); // accelerator rectangle
487 Rectangle cr = new Rectangle(); // checkIcon rectangle
489 int vertAlign = m.getVerticalAlignment();
490 int horAlign = m.getHorizontalAlignment();
491 int vertTextPos = m.getVerticalTextPosition();
492 int horTextPos = m.getHorizontalTextPosition();
494 Font f = m.getFont();
495 g.setFont(f);
496 FontMetrics fm = g.getFontMetrics(f);
497 SwingUtilities.calculateInnerArea(m, br);
498 SwingUtilities.calculateInsetArea(br, m.getInsets(), vr);
499 paintBackground(g, m, m.getBackground());
501 /* MenuItems insets are equal to menuItems margin, space between text and
502 menuItems border. We need to paint insets region as well. */
503 Insets insets = m.getInsets();
504 br.x -= insets.left;
505 br.y -= insets.top;
506 br.width += insets.right + insets.left;
507 br.height += insets.top + insets.bottom;
509 /* Menu item is considered to be highlighted when it is selected.
510 It is considered to be selected if menu item is inside some menu
511 and is armed or if it is both armed and pressed */
512 if (m.getModel().isArmed()
513 && (m.getParent() instanceof MenuElement || m.getModel().isPressed()))
515 if (m.isContentAreaFilled())
517 g.setColor(selectionBackground);
518 g.fillRect(br.x, br.y, br.width, br.height);
521 else
523 if (m.isContentAreaFilled())
525 g.setColor(m.getBackground());
526 g.fillRect(br.x, br.y, br.width, br.height);
530 // If this menu item is a JCheckBoxMenuItem then paint check icon
531 if (checkIcon != null)
533 SwingUtilities.layoutCompoundLabel(m, fm, null, checkIcon, vertAlign,
534 horAlign, vertTextPos, horTextPos,
535 vr, cr, tr, defaultTextIconGap);
536 checkIcon.paintIcon(m, g, cr.x, cr.y);
538 // We need to calculate position of the menu text and position of
539 // user menu icon if there exists one relative to the check icon.
540 // So we need to adjust view rectangle s.t. its starting point is at
541 // checkIcon.width + defaultTextIconGap.
542 vr.x = cr.x + cr.width + defaultTextIconGap;
545 // if this is a submenu, then paint arrow icon to indicate it.
546 if (arrowIcon != null && (c instanceof JMenu))
548 if (! ((JMenu) c).isTopLevelMenu())
550 int width = arrowIcon.getIconWidth();
551 int height = arrowIcon.getIconHeight();
553 arrowIcon.paintIcon(m, g, vr.width - width + defaultTextIconGap,
554 vr.y + 2);
558 // paint text and user menu icon if it exists
559 Icon i = m.getIcon();
560 SwingUtilities.layoutCompoundLabel(c, fm, m.getText(), i,
561 vertAlign, horAlign, vertTextPos,
562 horTextPos, vr, ir, tr,
563 defaultTextIconGap);
564 if (i != null)
565 i.paintIcon(c, g, ir.x, ir.y);
567 paintText(g, m, tr, m.getText());
569 // paint accelerator
570 String acceleratorText = "";
572 if (m.getAccelerator() != null)
574 acceleratorText = getAcceleratorText(m.getAccelerator());
575 fm = g.getFontMetrics(acceleratorFont);
576 ar.width = fm.stringWidth(acceleratorText);
577 ar.x = br.width - ar.width;
578 vr.x = br.width - ar.width;
580 SwingUtilities.layoutCompoundLabel(m, fm, acceleratorText, null,
581 vertAlign, horAlign, vertTextPos,
582 horTextPos, vr, ir, ar,
583 defaultTextIconGap);
585 paintAccelerator(g, m, ar, acceleratorText);
590 * Paints label for the given menu item
592 * @param g The graphics context used to paint this menu item
593 * @param menuItem menu item for which to draw its label
594 * @param textRect rectangle specifiying position of the text relative to
595 * the given menu item
596 * @param text label of the menu item
598 protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect,
599 String text)
601 Font f = menuItem.getFont();
602 g.setFont(f);
603 FontMetrics fm = g.getFontMetrics(f);
605 if (text != null && ! text.equals(""))
607 if (menuItem.isEnabled())
608 g.setColor(menuItem.getForeground());
609 else
610 // FIXME: should fix this to use 'disabledForeground', but its
611 // default value in BasicLookAndFeel is null.
612 g.setColor(Color.gray);
614 int mnemonicIndex = menuItem.getDisplayedMnemonicIndex();
616 if (mnemonicIndex != -1)
617 BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemonicIndex,
618 textRect.x,
619 textRect.y
620 + fm.getAscent());
621 else
622 BasicGraphicsUtils.drawString(g, text, 0, textRect.x,
623 textRect.y + fm.getAscent());
628 * This method uninstalls the components for this {@link JMenuItem}.
630 * @param menuItem The {@link JMenuItem} to uninstall components for.
632 protected void uninstallComponents(JMenuItem menuItem)
634 // FIXME: need to implement
638 * This method uninstalls the defaults and sets any objects created during
639 * install to null
641 protected void uninstallDefaults()
643 menuItem.setForeground(null);
644 menuItem.setBackground(null);
645 menuItem.setBorder(null);
646 menuItem.setMargin(null);
647 menuItem.setBackground(null);
648 menuItem.setBorder(null);
649 menuItem.setFont(null);
650 menuItem.setForeground(null);
651 menuItem.setMargin(null);
652 acceleratorFont = null;
653 acceleratorForeground = null;
654 acceleratorSelectionForeground = null;
655 arrowIcon = null;
656 selectionBackground = null;
657 selectionForeground = null;
658 acceleratorDelimiter = null;
662 * Uninstalls any keyboard actions.
664 protected void uninstallKeyboardActions()
666 // FIXME: need to implement
670 * Unregisters all the listeners that this UI delegate was using.
672 protected void uninstallListeners()
674 menuItem.removeMouseListener(mouseInputListener);
675 menuItem.removeMenuDragMouseListener(menuDragMouseListener);
676 menuItem.removeMenuKeyListener(menuKeyListener);
677 menuItem.removePropertyChangeListener(propertyChangeListener);
681 * Performs the opposite of installUI. Any properties or resources that need
682 * to be cleaned up will be done now. It will also uninstall any listeners
683 * it has. In addition, any properties of this UI will be nulled.
685 * @param c The {@link JComponent} that is having this UI uninstalled.
687 public void uninstallUI(JComponent c)
689 uninstallListeners();
690 uninstallDefaults();
691 uninstallComponents(menuItem);
692 menuItem = null;
696 * This method calls paint.
698 * @param g The graphics context used to paint this menu item
699 * @param c The menu item to paint
701 public void update(Graphics g, JComponent c)
703 paint(g, c);
707 * Return text representation of the specified accelerator
709 * @param accelerator Accelerator for which to return string representation
711 * @return $String$ Text representation of the given accelerator
713 private String getAcceleratorText(KeyStroke accelerator)
715 // convert keystroke into string format
716 String modifiersText = "";
717 int modifiers = accelerator.getModifiers();
718 char keyChar = accelerator.getKeyChar();
719 int keyCode = accelerator.getKeyCode();
721 if (modifiers != 0)
722 modifiersText = KeyEvent.getKeyModifiersText(modifiers)
723 + acceleratorDelimiter;
725 if (keyCode == KeyEvent.VK_UNDEFINED)
726 return modifiersText + keyChar;
727 else
728 return modifiersText + KeyEvent.getKeyText(keyCode);
732 * Calculates and return rectange in which accelerator should be displayed
734 * @param accelerator accelerator for which to return the display rectangle
735 * @param fm The font metrics used to measure the text
737 * @return $Rectangle$ reactangle which will be used to display accelerator
739 private Rectangle getAcceleratorRect(KeyStroke accelerator, FontMetrics fm)
741 int width = fm.stringWidth(getAcceleratorText(accelerator));
742 int height = fm.getHeight();
743 return new Rectangle(0, 0, width, height);
747 * Paints accelerator inside menu item
749 * @param g The graphics context used to paint the border
750 * @param menuItem Menu item for which to draw accelerator
751 * @param acceleratorRect rectangle representing position
752 * of the accelerator relative to the menu item
753 * @param acceleratorText accelerator's text
755 private void paintAccelerator(Graphics g, JMenuItem menuItem,
756 Rectangle acceleratorRect,
757 String acceleratorText)
759 g.setFont(acceleratorFont);
760 FontMetrics fm = g.getFontMetrics(acceleratorFont);
762 if (menuItem.isEnabled())
763 g.setColor(acceleratorForeground);
764 else
765 // FIXME: should fix this to use 'disabledForeground', but its
766 // default value in BasicLookAndFeel is null.
767 g.setColor(Color.gray);
769 BasicGraphicsUtils.drawString(g, acceleratorText, 0, acceleratorRect.x,
770 acceleratorRect.y + fm.getAscent());
774 * This class handles mouse events occuring inside the menu item.
775 * Most of the events are forwarded for processing to MenuSelectionManager
776 * of the current menu hierarchy.
779 protected class MouseInputHandler implements MouseInputListener
782 * Creates a new MouseInputHandler object.
784 protected MouseInputHandler()
789 * This method is called when mouse is clicked on the menu item.
790 * It forwards this event to MenuSelectionManager.
792 * @param e A {@link MouseEvent}.
794 public void mouseClicked(MouseEvent e)
796 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
797 manager.processMouseEvent(e);
801 * This method is called when mouse is dragged inside the menu item.
802 * It forwards this event to MenuSelectionManager.
804 * @param e A {@link MouseEvent}.
806 public void mouseDragged(MouseEvent e)
808 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
809 manager.processMouseEvent(e);
813 * This method is called when mouse enters menu item.
814 * When this happens menu item is considered to be selected and selection path
815 * in MenuSelectionManager is set. This event is also forwarded to MenuSelection
816 * Manager for further processing.
818 * @param e A {@link MouseEvent}.
820 public void mouseEntered(MouseEvent e)
822 Component source = (Component) e.getSource();
823 if (source.getParent() instanceof MenuElement)
825 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
826 manager.setSelectedPath(getPath());
827 manager.processMouseEvent(e);
832 * This method is called when mouse exits menu item. The event is
833 * forwarded to MenuSelectionManager for processing.
835 * @param e A {@link MouseEvent}.
837 public void mouseExited(MouseEvent e)
839 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
840 manager.processMouseEvent(e);
844 * This method is called when mouse is inside the menu item.
845 * This event is forwarder to MenuSelectionManager for further processing.
847 * @param e A {@link MouseEvent}.
849 public void mouseMoved(MouseEvent e)
851 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
852 manager.processMouseEvent(e);
856 * This method is called when mouse is pressed. This event is forwarded to
857 * MenuSelectionManager for further processing.
859 * @param e A {@link MouseEvent}.
861 public void mousePressed(MouseEvent e)
863 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
864 manager.processMouseEvent(e);
868 * This method is called when mouse is released. If the mouse is released
869 * inside this menuItem, then this menu item is considered to be chosen and
870 * the menu hierarchy should be closed.
872 * @param e A {@link MouseEvent}.
874 public void mouseReleased(MouseEvent e)
876 Rectangle size = menuItem.getBounds();
877 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
878 if (e.getX() > 0 && e.getX() < size.width && e.getY() > 0
879 && e.getY() < size.height)
881 manager.clearSelectedPath();
882 menuItem.doClick();
885 else
886 manager.processMouseEvent(e);
891 * This class handles mouse dragged events.
893 protected class MenuDragMouseHandler implements MenuDragMouseListener
896 * Tbis method is invoked when mouse is dragged over the menu item.
898 * @param e The MenuDragMouseEvent
900 public void menuDragMouseDragged(MenuDragMouseEvent e)
902 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
903 manager.setSelectedPath(e.getPath());
907 * Tbis method is invoked when mouse enters the menu item while it is
908 * being dragged.
910 * @param e The MenuDragMouseEvent
912 public void menuDragMouseEntered(MenuDragMouseEvent e)
914 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
915 manager.setSelectedPath(e.getPath());
919 * Tbis method is invoked when mouse exits the menu item while
920 * it is being dragged
922 * @param e The MenuDragMouseEvent
924 public void menuDragMouseExited(MenuDragMouseEvent e)
929 * Tbis method is invoked when mouse was dragged and released
930 * inside the menu item.
932 * @param e The MenuDragMouseEvent
934 public void menuDragMouseReleased(MenuDragMouseEvent e)
936 MenuElement[] path = e.getPath();
938 if (path[path.length - 1] instanceof JMenuItem)
939 ((JMenuItem) path[path.length - 1]).doClick();
941 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
942 manager.clearSelectedPath();
947 * This class handles key events occuring when menu item is visible on the
948 * screen.
950 protected class MenuKeyHandler implements MenuKeyListener
953 * This method is invoked when key has been pressed
955 * @param e A {@link MenuKeyEvent}.
957 public void menuKeyPressed(MenuKeyEvent e)
962 * This method is invoked when key has been pressed
964 * @param e A {@link MenuKeyEvent}.
966 public void menuKeyReleased(MenuKeyEvent e)
971 * This method is invoked when key has been typed
972 * It handles the mnemonic key for the menu item.
974 * @param e A {@link MenuKeyEvent}.
976 public void menuKeyTyped(MenuKeyEvent e)
982 * Helper class that listens for changes to the properties of the {@link
983 * JMenuItem}.
985 protected class PropertyChangeHandler implements PropertyChangeListener
988 * This method is called when one of the menu item's properties change.
990 * @param evt A {@link PropertyChangeEvent}.
992 public void propertyChange(PropertyChangeEvent evt)
994 menuItem.revalidate();
995 menuItem.repaint();