Debian Codegraph @

src/com/jidesoft/plaf/eclipse/EclipseMenuUI.java

raw
1 /*
2 * @(#)vsnetMenuUI.java
3 *
4 * Copyright 2002 JIDE Software Inc. All rights reserved.
5 */
6
7 package com.jidesoft.plaf.eclipse;
8
9 import com.jidesoft.plaf.LookAndFeelFactory;
10 import com.jidesoft.plaf.UIDefaultsLookup;
11 import com.jidesoft.plaf.windows.WindowsGraphicsUtilsPort;
12 import com.jidesoft.swing.JideSwingUtilities;
13 import com.jidesoft.swing.TopLevelMenuContainer;
14
15 import javax.swing.*;
16 import javax.swing.border.Border;
17 import javax.swing.event.*;
18 import javax.swing.plaf.ComponentUI;
19 import java.awt.*;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.KeyEvent;
22 import java.awt.event.MouseEvent;
23 import java.awt.event.MouseMotionListener;
24 import java.beans.PropertyChangeEvent;
25 import java.beans.PropertyChangeListener;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28
29
30 /**
31 * Menu UI implementation
32 */
33 public class EclipseMenuUI extends EclipseMenuItemUI {
34 protected ChangeListener changeListener;
35 protected PropertyChangeListener propertyChangeListener;
36 protected MenuListener menuListener;
37
38 private int lastMnemonic = 0;
39
40 /* diagnostic aids -- should be false for production builds. */
41 // private static final boolean TRACE = false; // trace creates and disposes
42 // private static final boolean VERBOSE = false; // show reuse hits/misses
43 // private static final boolean DEBUG = false; // show bad params, misc.
44
45 private static boolean crossMenuMnemonic = true;
46
47 private boolean isMouseOver = false;
48
49 public static ComponentUI createUI(JComponent x) {
50 return new EclipseMenuUI();
51 }
52
53 @Override
54 protected void installDefaults() {
55 super.installDefaults();
56 ((JMenu) menuItem).setDelay(200);
57 crossMenuMnemonic = UIDefaultsLookup.getBoolean("Menu.crossMenuMnemonic");
58 }
59
60 @Override
61 protected String getPropertyPrefix() {
62 return "Menu";
63 }
64
65 /**
66 * Draws the background of the menu item.
67 *
68 * @param g the paint graphics
69 * @param menuItem menu item to be painted
70 * @param bgColor selection background color
71 * @since 1.4
72 */
73 @Override
74 protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) {
75 if (!(menuItem instanceof JMenu) || !((JMenu) menuItem).isTopLevelMenu()) {
76 super.paintBackground(g, menuItem, bgColor);
77 return;
78 }
79
80 ButtonModel model = menuItem.getModel();
81 Color oldColor = g.getColor();
82 int menuWidth = 0;
83 int menuHeight = 0;
84 if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL) {
85 menuWidth = menuItem.getWidth();
86 menuHeight = menuItem.getHeight();
87 }
88 else {
89 menuWidth = menuItem.getHeight();
90 menuHeight = menuItem.getWidth();
91 }
92
93 Color darkShadow = UIDefaultsLookup.getColor("controlDkShadow");
94 Color mouseHoverBackground = UIDefaultsLookup.getColor("Menu.mouseHoverBackground");
95 Border mouseHoverBorder = UIDefaultsLookup.getBorder("Menu.mouseHoverBorder");
96 Border mouseSelectedBorder = UIDefaultsLookup.getBorder("Menu.mouseSelectedBorder");
97
98 if (menuItem.isOpaque()) {
99 if (menuItem.getParent() != null) {
100 g.setColor(menuItem.getParent().getBackground());
101 }
102 else {
103 g.setColor(menuItem.getBackground());
104 }
105 g.fillRect(0, 0, menuWidth, menuHeight);
106 }
107
108 // if (menuItem.isOpaque()) {
109 if (model.isArmed() || (menuItem instanceof JMenu && model.isSelected())) {
110 // Draw a dark shadow border without bottom
111 if (mouseSelectedBorder != null) {
112 mouseSelectedBorder.paintBorder(menuItem, g, 0, 0, menuWidth - 1, menuHeight);
113 }
114 g.setColor(mouseHoverBackground);
115 g.fillRect(1, 1, menuWidth - 3, menuHeight - 2);
116 }
117 else {
118 if (isMouseOver() && model.isEnabled()) {
119 // Draw a line border with background
120 if (mouseHoverBorder != null) {
121 mouseHoverBorder.paintBorder(menuItem, g, 0, 0, menuWidth - 1, menuHeight);
122 }
123 g.setColor(mouseHoverBackground);
124 g.fillRect(1, 1, menuWidth - 3, menuHeight - 2);
125 }
126 else {
127 // normal menu without mouse hovering over
128 // g.setColor(menuItem.getBackground());
129 // g.fillRect(0, 0, menuWidth, menuHeight);
130 }
131 }
132 // }
133
134 if (isDownArrowVisible(menuItem.getParent())) {
135 int middle = menuWidth - 9;
136 if (model.isSelected() || model.isArmed() || model.isPressed() || isMouseOver()) {
137 JideSwingUtilities.paintArrow(g, selectionForeground, middle - 2, menuHeight / 2 - 1, 5, SwingConstants.HORIZONTAL);
138 }
139 else {
140 JideSwingUtilities.paintArrow(g, menuItem.getForeground(), middle - 2, menuHeight / 2 - 1, 5, SwingConstants.HORIZONTAL);
141 }
142 }
143 g.setColor(oldColor);
144 }
145
146 @Override
147 protected void installListeners() {
148 super.installListeners();
149
150 if (changeListener == null)
151 changeListener = createChangeListener(menuItem);
152
153 if (changeListener != null)
154 menuItem.addChangeListener(changeListener);
155
156 if (propertyChangeListener == null)
157 propertyChangeListener = createPropertyChangeListener(menuItem);
158
159 if (propertyChangeListener != null)
160 menuItem.addPropertyChangeListener(propertyChangeListener);
161
162 if (menuListener == null)
163 menuListener = createMenuListener(menuItem);
164
165 if (menuListener != null)
166 ((JMenu) menuItem).addMenuListener(menuListener);
167 }
168
169 @Override
170 protected void installKeyboardActions() {
171 super.installKeyboardActions();
172 updateMnemonicBinding();
173 }
174
175 protected void updateMnemonicBinding() {
176 int mnemonic = menuItem.getModel().getMnemonic();
177 int[] shortcutKeys = (int[]) UIDefaultsLookup.get("Menu.shortcutKeys");
178 if (shortcutKeys == null) {
179 shortcutKeys = new int[]{KeyEvent.ALT_MASK};
180 }
181 if (mnemonic == lastMnemonic) {
182 return;
183 }
184 if (lastMnemonic != 0 && windowInputMap != null) {
185 for (int shortcutKey : shortcutKeys) {
186 windowInputMap.remove(KeyStroke.getKeyStroke
187 (lastMnemonic, shortcutKey, false));
188 windowInputMap.remove(KeyStroke.getKeyStroke
189 (lastMnemonic, shortcutKey, true));
190 }
191 }
192 if (mnemonic != 0) {
193 if (windowInputMap == null) {
194 windowInputMap = createInputMap(JComponent.
195 WHEN_IN_FOCUSED_WINDOW);
196 SwingUtilities.replaceUIInputMap(menuItem, JComponent.
197 WHEN_IN_FOCUSED_WINDOW, windowInputMap);
198 }
199 for (int shortcutKey : shortcutKeys) {
200 windowInputMap.put(KeyStroke.getKeyStroke(mnemonic,
201 shortcutKey, false), "selectMenu");
202 }
203 }
204 lastMnemonic = mnemonic;
205 }
206
207 @Override
208 protected void uninstallKeyboardActions() {
209 super.uninstallKeyboardActions();
210 }
211
212 /**
213 * The ActionMap for BasicMenUI can not be shared, this is subclassed to create a new one for each invocation.
214 */
215 @Override
216 protected ActionMap getActionMap() {
217 return createActionMap();
218 }
219
220 /**
221 * Invoked to create the ActionMap.
222 */
223 @Override
224 protected ActionMap createActionMap() {
225 ActionMap am = super.createActionMap();
226 if (am != null) {
227 am.put("selectMenu", new PostAction((JMenu) menuItem, true));
228 }
229 return am;
230 }
231
232 @Override
233 protected MouseInputListener createMouseInputListener(JComponent c) {
234 return new MouseInputHandler();
235 }
236
237 protected MenuListener createMenuListener(JComponent c) {
238 return new MenuHandler();
239 }
240
241 protected ChangeListener createChangeListener(JComponent c) {
242 return null;
243 }
244
245 protected PropertyChangeListener createPropertyChangeListener(JComponent c) {
246 return new PropertyChangeHandler();
247 }
248
249 @Override
250 protected void uninstallDefaults() {
251 menuItem.setArmed(false);
252 menuItem.setSelected(false);
253 menuItem.resetKeyboardActions();
254 super.uninstallDefaults();
255 }
256
257 @Override
258 protected void uninstallListeners() {
259 super.uninstallListeners();
260
261 if (changeListener != null)
262 menuItem.removeChangeListener(changeListener);
263
264 if (propertyChangeListener != null)
265 menuItem.removePropertyChangeListener(propertyChangeListener);
266
267 if (menuListener != null)
268 ((JMenu) menuItem).removeMenuListener(menuListener);
269
270 changeListener = null;
271 propertyChangeListener = null;
272 menuListener = null;
273 }
274
275 @Override
276 protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) {
277 return new MenuDragMouseHandler();
278 }
279
280 @Override
281 protected MenuKeyListener createMenuKeyListener(JComponent c) {
282 return new MenuKeyHandler();
283 }
284
285 @Override
286 public Dimension getMaximumSize(JComponent c) {
287 if (((JMenu) menuItem).isTopLevelMenu() == true) {
288 Dimension d = c.getPreferredSize();
289 return new Dimension(d.width, Short.MAX_VALUE);
290 }
291 return null;
292 }
293
294 public static void setupPostTimer(JMenu menu) {
295 Timer timer = new Timer(menu.getDelay(), new PostAction(menu, false));
296 timer.setRepeats(false);
297 timer.start();
298 }
299
300 protected static void appendPath(MenuElement[] path, MenuElement elem) {
301 MenuElement newPath[] = new MenuElement[path.length + 1];
302 System.arraycopy(path, 0, newPath, 0, path.length);
303 newPath[path.length] = elem;
304 MenuSelectionManager.defaultManager().setSelectedPath(newPath);
305 }
306
307 private static class PostAction extends AbstractAction {
308 JMenu menu;
309 boolean force = false;
310
311 PostAction(JMenu menu, boolean shouldForce) {
312 this.menu = menu;
313 this.force = shouldForce;
314 }
315
316 public void actionPerformed(ActionEvent e) {
317 if (!crossMenuMnemonic) {
318 JPopupMenu pm = getActivePopupMenu();
319 if (pm != null && pm != menu.getParent()) {
320 return;
321 }
322 }
323
324 final MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager();
325 if (force) {
326 Container cnt = menu.getParent();
327 if (cnt != null && cnt instanceof JMenuBar) {
328 MenuElement me[];
329 MenuElement subElements[];
330
331 subElements = menu.getPopupMenu().getSubElements();
332 if (subElements.length > 0) {
333 me = new MenuElement[4];
334 me[0] = (MenuElement) cnt;
335 me[1] = menu;
336 me[2] = menu.getPopupMenu();
337 me[3] = subElements[0];
338 }
339 else {
340 me = new MenuElement[3];
341 me[0] = (MenuElement) cnt;
342 me[1] = menu;
343 me[2] = menu.getPopupMenu();
344 }
345 defaultManager.setSelectedPath(me);
346 }
347 }
348 else {
349 MenuElement path[] = defaultManager.getSelectedPath();
350 if (path.length > 0 && path[path.length - 1] == menu) {
351 appendPath(path, menu.getPopupMenu());
352 }
353 }
354 }
355
356 @Override
357 public boolean isEnabled() {
358 return menu.getModel().isEnabled();
359 }
360 }
361
362
363 private class PropertyChangeHandler implements PropertyChangeListener {
364 public void propertyChange(PropertyChangeEvent e) {
365 String prop = e.getPropertyName();
366 if (prop.equals(AbstractButton.MNEMONIC_CHANGED_PROPERTY)) {
367 updateMnemonicBinding();
368 }
369 }
370 }
371
372 /**
373 * Instantiated and used by a menu item to handle the current menu selection from mouse events. A MouseInputHandler
374 * processes and forwards all mouse events to a shared instance of the MenuSelectionManager.
375 * <p/>
376 * This class is protected so that it can be subclassed by other look and feels to implement their own mouse
377 * handling behavior. All overridden methods should call the parent methods so that the menu selection is correct.
378 *
379 * @see MenuSelectionManager
380 * @since 1.4
381 */
382 protected class MouseInputHandler implements MouseInputListener {
383 public void mouseClicked(MouseEvent e) {
384 }
385
386 public void mousePressed(MouseEvent e) {
387 if (!(menuItem instanceof JMenu)) {
388 return;
389 }
390
391 JMenu menu = (JMenu) menuItem;
392 if (!menu.isEnabled())
393 return;
394
395 MenuSelectionManager manager =
396 MenuSelectionManager.defaultManager();
397 if (menu.getParent() instanceof JMenuBar || menu.getParent() instanceof TopLevelMenuContainer) {
398 if (menu.isSelected()) {
399 manager.clearSelectedPath();
400 }
401 else {
402 //Container cnt = menu.getParent();
403 Container cnt = getFirstParentMenuElement(menu);
404
405 if (cnt != null && cnt instanceof MenuElement) {
406 ArrayList<Component> parents = new ArrayList<Component>();
407 while (cnt instanceof MenuElement) {
408 parents.add(0, cnt);
409 if (cnt instanceof JPopupMenu) {
410 cnt = (Container) ((JPopupMenu) cnt).getInvoker();
411 }
412 else {
413 //cnt = cnt.getParent();
414 cnt = getFirstParentMenuElement(cnt);
415 }
416 }
417
418 MenuElement me[] = new MenuElement[parents.size() + 1];
419 for (int i = 0; i < parents.size(); i++) {
420 Container container = (Container) parents.get(i);
421 me[i] = (MenuElement) container;
422 }
423 me[parents.size()] = menu;
424 manager.setSelectedPath(me);
425 }
426 else {
427 MenuElement me[] = new MenuElement[1];
428 me[0] = menu;
429 manager.setSelectedPath(me);
430 }
431 }
432 }
433
434 MenuElement selectedPath[] = manager.getSelectedPath();
435 if (selectedPath.length > 0 &&
436 selectedPath[selectedPath.length - 1] != menu.getPopupMenu()) {
437
438 if (menu.isTopLevelMenu() ||
439 menu.getDelay() == 0) {
440 appendPath(selectedPath, menu.getPopupMenu());
441 }
442 else {
443 setupPostTimer(menu);
444 }
445 }
446 }
447
448 protected Container getFirstParentMenuElement(Component comp) {
449 Container parent = comp.getParent();
450
451 while (parent != null) {
452 if (parent instanceof MenuElement)
453 return parent;
454
455 parent = parent.getParent();
456 }
457
458 return null;
459 }
460
461 /**
462 * Invoked when the mouse has been released on the menu. Delegates the mouse event to the MenuSelectionManager.
463 *
464 * @param e the mouse event
465 */
466 public void mouseReleased(MouseEvent e) {
467 if (!(menuItem instanceof JMenu)) {
468 return;
469 }
470
471 JMenu menu = (JMenu) menuItem;
472 if (!menu.isEnabled())
473 return;
474 MenuSelectionManager manager =
475 MenuSelectionManager.defaultManager();
476 manager.processMouseEvent(e);
477 if (!e.isConsumed())
478 manager.clearSelectedPath();
479 }
480
481 /**
482 * Invoked when the cursor enters the menu. This method sets the selected path for the MenuSelectionManager and
483 * handles the case in which a menu item is used to pop up an additional menu, as in a hierarchical menu
484 * system.
485 *
486 * @param e the mouse event; not used
487 */
488 public void mouseEntered(MouseEvent e) {
489 if (!(menuItem instanceof JMenu)) {
490 return;
491 }
492 JMenu menu = (JMenu) menuItem;
493 if (!menu.isEnabled())
494 return;
495
496 MenuSelectionManager manager =
497 MenuSelectionManager.defaultManager();
498 MenuElement selectedPath[] = manager.getSelectedPath();
499 if (!menu.isTopLevelMenu()) {
500 if (!(selectedPath.length > 0 &&
501 selectedPath[selectedPath.length - 1] ==
502 menu.getPopupMenu())) {
503 if (menu.getDelay() == 0) {
504 appendPath(getPath(), menu.getPopupMenu());
505 }
506 else {
507 manager.setSelectedPath(getPath());
508 setupPostTimer(menu);
509 }
510 }
511 }
512 else {
513 if (selectedPath.length > 0 &&
514 selectedPath[0] == menu.getParent()) {
515 MenuElement newPath[] = new MenuElement[3];
516 // A top level menu's parent is by definition
517 // a JMenuBar
518 newPath[0] = (MenuElement) menu.getParent();
519 newPath[1] = menu;
520 newPath[2] = menu.getPopupMenu();
521 manager.setSelectedPath(newPath);
522 }
523 }
524
525 setMouseOver(true);
526 menuItem.repaint();
527 }
528
529 public void mouseExited(MouseEvent e) {
530 setMouseOver(false);
531 menuItem.repaint();
532 }
533
534 /**
535 * Invoked when a mouse button is pressed on the menu and then dragged. Delegates the mouse event to the
536 * MenuSelectionManager.
537 *
538 * @param e the mouse event
539 * @see MouseMotionListener#mouseDragged
540 */
541 public void mouseDragged(MouseEvent e) {
542 if (!(menuItem instanceof JMenu)) {
543 return;
544 }
545 JMenu menu = (JMenu) menuItem;
546 if (!menu.isEnabled())
547 return;
548 MenuSelectionManager.defaultManager().processMouseEvent(e);
549 }
550
551 public void mouseMoved(MouseEvent e) {
552 }
553 }
554
555 private static class MenuHandler implements MenuListener {
556 public void menuSelected(MenuEvent e) {
557 }
558
559 public void menuDeselected(MenuEvent e) {
560 }
561
562 public void menuCanceled(MenuEvent e) {
563 JMenu m = (JMenu) e.getSource();
564 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
565 if (manager.isComponentPartOfCurrentMenu(m))
566 MenuSelectionManager.defaultManager().clearSelectedPath();
567 }
568
569 }
570
571 /**
572 * As of Java 2 platform 1.4, this previously undocumented class is now obsolete. KeyBindings are now managed by the
573 * popup menu.
574 */
575 public class ChangeHandler implements ChangeListener {
576 public JMenu menu;
577 public EclipseMenuUI ui;
578 public boolean isSelected = false;
579 public Component wasFocused;
580
581 public ChangeHandler(JMenu m, EclipseMenuUI ui) {
582 menu = m;
583 this.ui = ui;
584 }
585
586 public void stateChanged(ChangeEvent e) {
587 }
588 }
589
590 private class MenuDragMouseHandler implements MenuDragMouseListener {
591 public void menuDragMouseEntered(MenuDragMouseEvent e) {
592 }
593
594 public void menuDragMouseDragged(MenuDragMouseEvent e) {
595 if (!(menuItem instanceof JMenu)) {
596 return;
597 }
598 if (menuItem.isEnabled() == false)
599 return;
600
601 MenuSelectionManager manager = e.getMenuSelectionManager();
602 MenuElement path[] = e.getPath();
603
604 Point p = e.getPoint();
605 if (p.x >= 0 && p.x < menuItem.getWidth() &&
606 p.y >= 0 && p.y < menuItem.getHeight()) {
607 JMenu menu = (JMenu) menuItem;
608 MenuElement selectedPath[] = manager.getSelectedPath();
609 if (!(selectedPath.length > 0 &&
610 selectedPath[selectedPath.length - 1] ==
611 menu.getPopupMenu())) {
612 if (menu.isTopLevelMenu() ||
613 menu.getDelay() == 0 ||
614 e.getID() == MouseEvent.MOUSE_DRAGGED) {
615 appendPath(path, menu.getPopupMenu());
616 }
617 else {
618 manager.setSelectedPath(path);
619 setupPostTimer(menu);
620 }
621 }
622 }
623 else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
624 Component comp = manager.componentForPoint(e.getComponent(), e.getPoint());
625 if (comp == null)
626 manager.clearSelectedPath();
627 }
628
629 }
630
631 public void menuDragMouseExited(MenuDragMouseEvent e) {
632 }
633
634 public void menuDragMouseReleased(MenuDragMouseEvent e) {
635 }
636 }
637
638 static JPopupMenu getActivePopupMenu() {
639 MenuElement[] path = MenuSelectionManager.defaultManager().
640 getSelectedPath();
641 for (int i = path.length - 1; i >= 0; i--) {
642 MenuElement elem = path[i];
643 if (elem instanceof JPopupMenu) {
644 return (JPopupMenu) elem;
645 }
646 }
647 return null;
648 }
649
650 /**
651 * Handles the mnemonic handling for the JMenu and JMenuItems.
652 */
653 private class MenuKeyHandler implements MenuKeyListener {
654
655 /**
656 * Opens the SubMenu
657 */
658 public void menuKeyTyped(MenuKeyEvent e) {
659 // if (DEBUG) {
660 // System.out.println("in BasicMenuUI.menuKeyTyped for " + menuItem.getText());
661 // }
662 if (!crossMenuMnemonic) {
663 JPopupMenu pm = getActivePopupMenu();
664 if (pm != null && pm != menuItem.getParent()) {
665 return;
666 }
667 }
668
669 int key = menuItem.getMnemonic();
670 if (key == 0)
671 return;
672 MenuElement path[] = e.getPath();
673 if (lower((char) key) == lower(e.getKeyChar())) {
674 JPopupMenu popupMenu = ((JMenu) menuItem).getPopupMenu();
675 ArrayList newList = new ArrayList(Arrays.asList(path));
676 newList.add(popupMenu);
677 MenuElement sub[] = popupMenu.getSubElements();
678 if (sub.length > 0) {
679 newList.add(sub[0]);
680 }
681 MenuSelectionManager manager = e.getMenuSelectionManager();
682 MenuElement newPath[] = new MenuElement[0];
683 newPath = (MenuElement[]) newList.toArray(newPath);
684 manager.setSelectedPath(newPath);
685 e.consume();
686 }
687 }
688
689 /**
690 * Handles the mnemonics for the menu items. Will also handle duplicate mnemonics. Perhaps this should be moved
691 * into BasicPopupMenuUI. See 4670831
692 */
693 public void menuKeyPressed(MenuKeyEvent e) {
694 // if (DEBUG) {
695 // System.out.println("in BasicMenuUI.menuKeyPressed for " + menuItem.getText());
696 // }
697 // Handle the case for Escape or Enter...
698 char keyChar = e.getKeyChar();
699 if (!Character.isLetterOrDigit(keyChar))
700 return;
701
702 MenuSelectionManager manager = e.getMenuSelectionManager();
703 MenuElement path[] = e.getPath();
704 MenuElement selectedPath[] = manager.getSelectedPath();
705
706 for (int i = selectedPath.length - 1; i >= 0; i--) {
707 if (selectedPath[i] == menuItem) {
708 JPopupMenu popupMenu = ((JMenu) menuItem).getPopupMenu();
709 if (!popupMenu.isVisible()) {
710 return; // Do not invoke items from invisible popup
711 }
712 MenuElement items[] = popupMenu.getSubElements();
713
714 MenuElement currentItem = selectedPath[selectedPath.length - 1];
715 int currentIndex = -1;
716 int matches = 0;
717 int firstMatch = -1;
718 int indexes[] = null;
719
720 for (int j = 0; j < items.length; j++) {
721 int key = ((JMenuItem) items[j]).getMnemonic();
722 if (lower((char) key) == lower(keyChar)) {
723 if (matches == 0) {
724 firstMatch = j;
725 matches++;
726 }
727 else {
728 if (indexes == null) {
729 indexes = new int[items.length];
730 indexes[0] = firstMatch;
731 }
732 indexes[matches++] = j;
733 }
734 }
735 if (currentItem == items[j]) {
736 currentIndex = matches - 1;
737 }
738 }
739
740 if (matches == 0) {
741 ; // no op (consume)
742 }
743 else if (matches == 1) {
744 // Invoke the menu action
745 JMenuItem item = (JMenuItem) items[firstMatch];
746 if (!(item instanceof JMenu)) {
747 // Let Submenus be handled by menuKeyTyped
748 manager.clearSelectedPath();
749 item.doClick();
750 }
751 }
752 else {
753 // Select the menu item with the matching mnemonic. If
754 // the same mnemonic has been invoked then select the next
755 // menu item in the cycle.
756 MenuElement newItem = null;
757 if (indexes != null) {
758 newItem = items[indexes[(currentIndex + 1) % matches]];
759 }
760
761 MenuElement newPath[] = new MenuElement[path.length + 2];
762 System.arraycopy(path, 0, newPath, 0, path.length);
763 newPath[path.length] = popupMenu;
764 newPath[path.length + 1] = newItem;
765 manager.setSelectedPath(newPath);
766 }
767 e.consume();
768 return;
769 }
770 }
771 }
772
773 public void menuKeyReleased(MenuKeyEvent e) {
774 }
775
776 private char lower(char keyChar) {
777 return Character.toLowerCase(keyChar);
778 }
779 }
780
781 /**
782 * Method which renders the text of the current menu item.
783 * <p/>
784 *
785 * @param g Graphics context
786 * @param menuItem Current menu item to render
787 * @param textRect Bounding rectangle to render the text.
788 * @param text String to render
789 * @since 1.4
790 */
791 @Override
792 protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text) {
793 // Note: This method is almost identical to the same method in WindowsMenuItemUI
794 ButtonModel model = menuItem.getModel();
795
796 if (!(menuItem instanceof JMenu) || !((JMenu) menuItem).isTopLevelMenu()) {
797 if (menuItem.getComponentOrientation().isLeftToRight()) {
798 int defaultTextIconGap = UIDefaultsLookup.getInt("MenuItem.textIconGap");
799 int defaultShadowWidth = UIDefaultsLookup.getInt("MenuItem.shadowWidth");
800 textRect.x = defaultShadowWidth + defaultTextIconGap;
801 }
802 else {
803 // isLeftToRight is false
804 }
805 }
806
807 if (!model.isEnabled()) {
808 // *** paint the text disabled
809 textRect.y++;
810 WindowsGraphicsUtilsPort.paintText(g, menuItem, textRect, text, 0);
811 }
812 else {
813 FontMetrics fm = g.getFontMetrics();
814 int mnemonicIndex = menuItem.getDisplayedMnemonicIndex();
815 // W2K Feature: Check to see if the Underscore should be rendered.
816 if (LookAndFeelFactory.isMnemonicHidden()) {
817 mnemonicIndex = -1;
818 }
819
820 Color oldColor = g.getColor();
821
822 if (model.isArmed() || (menuItem instanceof JMenu && model.isSelected()) || isMouseOver()) {
823 g.setColor(selectionForeground); // Uses protected field.
824 }
825 JideSwingUtilities.drawStringUnderlineCharAt(menuItem, g, text,
826 mnemonicIndex,
827 textRect.x,
828 textRect.y + fm.getAscent() - 1);
829 g.setColor(oldColor);
830 }
831 }
832
833 /**
834 * Set the temporary flag to indicate if the mouse has entered the menu.
835 */
836 protected void setMouseOver(boolean over) {
837 isMouseOver = over;
838 menuItem.getModel().setRollover(isMouseOver);
839 }
840
841 /**
842 * Get the temporary flag to indicate if the mouse has entered the menu.
843 */
844 protected boolean isMouseOver() {
845 return isMouseOver;
846 }
847
848 @Override
849 public Dimension getPreferredSize(JComponent c) {
850 Dimension size = super.getPreferredSize(c);
851 if (menuItem instanceof JMenu && ((JMenu) menuItem).isTopLevelMenu() &&
852 isDownArrowVisible(menuItem.getParent())) {
853 if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL)
854 size.width += 11;
855 else
856 size.height += 11;
857 }
858 return size;
859 }
860 }
861
862
863
864