1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50:
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72:
73:
76: public class BasicMenuUI extends BasicMenuItemUI
77: {
78:
81: class SelectMenuAction
82: extends AbstractAction
83: {
84:
87: public void actionPerformed(ActionEvent event)
88: {
89: JMenu menu = (JMenu) menuItem;
90: MenuSelectionManager defaultManager =
91: MenuSelectionManager.defaultManager();
92: MenuElement path[] = defaultManager.getSelectedPath();
93: if(path.length > 0 && path[path.length - 1] == menu)
94: {
95: MenuElement newPath[] = new MenuElement[path.length + 1];
96: System.arraycopy(path, 0, newPath, 0, path.length);
97: newPath[path.length] = menu.getPopupMenu();
98: defaultManager.setSelectedPath(newPath);
99: }
100: }
101:
102: }
103:
104: protected ChangeListener changeListener;
105:
106:
107: protected MenuListener menuListener;
108:
109:
110: protected PropertyChangeListener propertyChangeListener;
111:
112:
115: public BasicMenuUI()
116: {
117: mouseInputListener = createMouseInputListener((JMenu) menuItem);
118: menuListener = createMenuListener((JMenu) menuItem);
119: propertyChangeListener = createPropertyChangeListener((JMenu) menuItem);
120: }
121:
122:
127: protected ChangeListener createChangeListener(JComponent c)
128: {
129: return new ChangeHandler((JMenu) c, this);
130: }
131:
132:
140: protected MenuDragMouseListener createMenuDragMouseListener(JComponent c)
141: {
142: return new MenuDragMouseHandler();
143: }
144:
145:
152: protected MenuKeyListener createMenuKeyListener(JComponent c)
153: {
154: return new MenuKeyHandler();
155: }
156:
157:
165: protected MenuListener createMenuListener(JComponent c)
166: {
167: return new MenuHandler();
168: }
169:
170:
178: protected MouseInputListener createMouseInputListener(JComponent c)
179: {
180: return new MouseInputHandler();
181: }
182:
183:
191: protected PropertyChangeListener createPropertyChangeListener(JComponent c)
192: {
193: return new PropertyChangeHandler();
194: }
195:
196:
203: public static ComponentUI createUI(JComponent c)
204: {
205: return new BasicMenuUI();
206: }
207:
208:
215: public Dimension getMaximumSize(JComponent c)
216: {
217: return c.getPreferredSize();
218: }
219:
220:
225: protected String getPropertyPrefix()
226: {
227: return "Menu";
228: }
229:
230:
234: protected void installDefaults()
235: {
236:
237: LookAndFeel.installBorder(menuItem, "Menu.border");
238: LookAndFeel.installColorsAndFont(menuItem, "Menu.background",
239: "Menu.foreground", "Menu.font");
240: menuItem.setMargin(UIManager.getInsets("Menu.margin"));
241: acceleratorFont = UIManager.getFont("Menu.acceleratorFont");
242: acceleratorForeground = UIManager.getColor("Menu.acceleratorForeground");
243: acceleratorSelectionForeground = UIManager.getColor("Menu.acceleratorSelectionForeground");
244: selectionBackground = UIManager.getColor("Menu.selectionBackground");
245: selectionForeground = UIManager.getColor("Menu.selectionForeground");
246: arrowIcon = UIManager.getIcon("Menu.arrowIcon");
247: oldBorderPainted = UIManager.getBoolean("Menu.borderPainted");
248: ((JMenu) menuItem).setDelay(200);
249: }
250:
251:
256: protected void installKeyboardActions()
257: {
258: super.installKeyboardActions();
259: }
260:
261:
264: protected void installListeners()
265: {
266: super.installListeners();
267: ((JMenu) menuItem).addMenuListener(menuListener);
268: }
269:
270: protected void setupPostTimer(JMenu menu)
271: {
272: Timer timer = new Timer(menu.getDelay(), new SelectMenuAction());
273: timer.setRepeats(false);
274: timer.start();
275: }
276:
277:
281: protected void uninstallDefaults()
282: {
283: menuItem.setBackground(null);
284: menuItem.setBorder(null);
285: menuItem.setFont(null);
286: menuItem.setForeground(null);
287: menuItem.setMargin(null);
288: acceleratorFont = null;
289: acceleratorForeground = null;
290: acceleratorSelectionForeground = null;
291: selectionBackground = null;
292: selectionForeground = null;
293: arrowIcon = null;
294: }
295:
296:
300: protected void uninstallKeyboardActions()
301: {
302: super.installKeyboardActions();
303: }
304:
305:
309: protected void uninstallListeners()
310: {
311: super.uninstallListeners();
312: ((JMenu) menuItem).removeMenuListener(menuListener);
313: }
314:
315:
319: protected class MouseInputHandler implements MouseInputListener
320: {
321: public void mouseClicked(MouseEvent e)
322: {
323:
324: }
325:
326: public void mouseDragged(MouseEvent e)
327: {
328: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
329: manager.processMouseEvent(e);
330: }
331:
332: private boolean popupVisible()
333: {
334: JMenuBar mb = (JMenuBar) ((JMenu) menuItem).getParent();
335:
336:
337: if (!mb.isSelected())
338: return false;
339: for (int i = 0; i < mb.getMenuCount(); i++)
340: {
341: JMenu m = mb.getMenu(i);
342: if (m != null && m.isPopupMenuVisible())
343: return true;
344: }
345: return false;
346: }
347:
348: public void mouseEntered(MouseEvent e)
349: {
350: JMenu menu = (JMenu) menuItem;
351: if (menu.isEnabled())
352: {
353: MenuSelectionManager manager =
354: MenuSelectionManager.defaultManager();
355: MenuElement[] selectedPath = manager.getSelectedPath();
356: if (! menu.isTopLevelMenu())
357: {
358:
359:
360: if(! (selectedPath.length > 0
361: && selectedPath[selectedPath.length - 1] == menu.getPopupMenu()))
362: {
363: if(menu.getDelay() == 0)
364: {
365: MenuElement[] path = getPath();
366: MenuElement[] newPath = new MenuElement[path.length + 1];
367: System.arraycopy(path, 0, newPath, 0, path.length);
368: newPath[path.length] = menu.getPopupMenu();
369: manager.setSelectedPath(newPath);
370: }
371: else
372: {
373: manager.setSelectedPath(getPath());
374: setupPostTimer(menu);
375: }
376: }
377: }
378: else
379: {
380: if(selectedPath.length > 0
381: && selectedPath[0] == menu.getParent())
382: {
383: MenuElement[] newPath = new MenuElement[3];
384: newPath[0] = (MenuElement) menu.getParent();
385: newPath[1] = menu;
386: newPath[2] = menu.getPopupMenu();
387: manager.setSelectedPath(newPath);
388: }
389: }
390: }
391: }
392:
393: public void mouseExited(MouseEvent e)
394: {
395: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
396: manager.processMouseEvent(e);
397: }
398:
399: public void mouseMoved(MouseEvent e)
400: {
401:
402: }
403:
404: public void mousePressed(MouseEvent e)
405: {
406: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
407: JMenu menu = (JMenu) menuItem;
408: if (menu.isEnabled())
409: {
410:
411:
412: if (menu.isTopLevelMenu())
413: {
414: if (menu.isSelected())
415: manager.clearSelectedPath();
416: else
417: {
418: Container cnt = menu.getParent();
419: if (cnt != null && cnt instanceof JMenuBar)
420: {
421: MenuElement[] me = new MenuElement[2];
422: me[0] = (MenuElement) cnt;
423: me[1] = menu;
424: manager.setSelectedPath(me);
425: }
426: }
427: }
428:
429:
430:
431: MenuElement[] selectedPath = manager.getSelectedPath();
432: if (selectedPath.length > 0
433: && selectedPath[selectedPath.length - 1] != menu.getPopupMenu())
434: {
435: if(menu.isTopLevelMenu() || menu.getDelay() == 0)
436: {
437: MenuElement[] newPath =
438: new MenuElement[selectedPath.length + 1];
439: System.arraycopy(selectedPath, 0, newPath, 0,
440: selectedPath.length);
441: newPath[selectedPath.length] = menu.getPopupMenu();
442: manager.setSelectedPath(newPath);
443: }
444: else
445: {
446: setupPostTimer(menu);
447: }
448: }
449:
450: }
451: }
452:
453: public void mouseReleased(MouseEvent e)
454: {
455: MenuSelectionManager manager = MenuSelectionManager.defaultManager();
456: manager.processMouseEvent(e);
457: }
458: }
459:
460:
463: private class MenuHandler implements MenuListener
464: {
465:
472: public void menuCanceled(MenuEvent e)
473: {
474: menuDeselected(e);
475: }
476:
477:
483: public void menuDeselected(MenuEvent e)
484: {
485: JMenu menu = (JMenu) menuItem;
486: if (menu.getParent() != null)
487: {
488: if (menu.isTopLevelMenu())
489: ((JMenuBar) menu.getParent()).getSelectionModel().clearSelection();
490: else
491: ((JPopupMenu) menu.getParent()).getSelectionModel().clearSelection();
492: }
493: }
494:
495:
501: public void menuSelected(MenuEvent e)
502: {
503: JMenu menu = (JMenu) menuItem;
504: if (menu.isTopLevelMenu())
505: ((JMenuBar) menu.getParent()).setSelected(menu);
506: else
507: ((JPopupMenu) menu.getParent()).setSelected(menu);
508: }
509: }
510:
511:
514: public class ChangeHandler implements ChangeListener
515: {
516:
519: public boolean isSelected;
520:
521:
524: public JMenu menu;
525:
526:
529: public BasicMenuUI ui;
530:
531:
534: public Component wasFocused;
535:
536:
539: public ChangeHandler(JMenu m, BasicMenuUI ui)
540: {
541: menu = m;
542: this.ui = ui;
543: }
544:
545:
548: public void stateChanged(ChangeEvent e)
549: {
550:
551: }
552: }
553:
554:
557: private class MenuDragMouseHandler implements MenuDragMouseListener
558: {
559:
564: public void menuDragMouseDragged(MenuDragMouseEvent e)
565: {
566: if (menuItem.isEnabled())
567: {
568: MenuSelectionManager manager = e.getMenuSelectionManager();
569: MenuElement path[] = e.getPath();
570:
571: Point p = e.getPoint();
572: if(p.x >= 0 && p.x < menuItem.getWidth()
573: && p.y >= 0 && p.y < menuItem.getHeight())
574: {
575: JMenu menu = (JMenu) menuItem;
576: MenuElement[] selectedPath = manager.getSelectedPath();
577: if(! (selectedPath.length > 0
578: && selectedPath[selectedPath.length-1]
579: == menu.getPopupMenu()))
580: {
581: if(menu.isTopLevelMenu() || menu.getDelay() == 0
582: || e.getID() == MouseEvent.MOUSE_DRAGGED)
583: {
584: MenuElement[] newPath = new MenuElement[path.length + 1];
585: System.arraycopy(path, 0, newPath, 0, path.length);
586: newPath[path.length] = menu.getPopupMenu();
587: manager.setSelectedPath(newPath);
588: }
589: else
590: {
591: manager.setSelectedPath(path);
592: setupPostTimer(menu);
593: }
594: }
595: }
596: else if (e.getID() == MouseEvent.MOUSE_RELEASED)
597: {
598: Component comp = manager.componentForPoint(e.getComponent(),
599: e.getPoint());
600: if (comp == null)
601: manager.clearSelectedPath();
602: }
603: }
604: }
605:
606:
612: public void menuDragMouseEntered(MenuDragMouseEvent e)
613: {
614:
615: }
616:
617:
623: public void menuDragMouseExited(MenuDragMouseEvent e)
624: {
625:
626: }
627:
628:
634: public void menuDragMouseReleased(MenuDragMouseEvent e)
635: {
636:
637: }
638: }
639:
640:
644: private class MenuKeyHandler implements MenuKeyListener
645: {
646:
651: public void menuKeyPressed(MenuKeyEvent e)
652: {
653:
654: }
655:
656:
661: public void menuKeyReleased(MenuKeyEvent e)
662: {
663:
664: }
665:
666:
672: public void menuKeyTyped(MenuKeyEvent e)
673: throws NotImplementedException
674: {
675:
676: }
677: }
678: }