1. /*
  2. * @(#)BasicMenuBarUI.java 1.80 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.basic;
  8. import sun.swing.DefaultLookup;
  9. import sun.swing.UIAction;
  10. import javax.swing.*;
  11. import javax.swing.event.*;
  12. import java.awt.Color;
  13. import java.awt.Component;
  14. import java.awt.Container;
  15. import java.awt.Dimension;
  16. import java.awt.Graphics;
  17. import java.awt.Insets;
  18. import java.awt.Point;
  19. import java.awt.Rectangle;
  20. import java.awt.event.*;
  21. import java.beans.PropertyChangeEvent;
  22. import java.beans.PropertyChangeListener;
  23. import javax.swing.border.*;
  24. import javax.swing.plaf.*;
  25. /**
  26. * A default L&F implementation of MenuBarUI. This implementation
  27. * is a "combined" view/controller.
  28. *
  29. * @version 1.80 12/19/03
  30. * @author Georges Saab
  31. * @author David Karlton
  32. * @author Arnaud Weber
  33. */
  34. public class BasicMenuBarUI extends MenuBarUI {
  35. protected JMenuBar menuBar = null;
  36. protected ContainerListener containerListener;
  37. protected ChangeListener changeListener;
  38. private Handler handler;
  39. public static ComponentUI createUI(JComponent x) {
  40. return new BasicMenuBarUI();
  41. }
  42. static void loadActionMap(LazyActionMap map) {
  43. map.put(new Actions(Actions.TAKE_FOCUS));
  44. }
  45. public void installUI(JComponent c) {
  46. menuBar = (JMenuBar) c;
  47. installDefaults();
  48. installListeners();
  49. installKeyboardActions();
  50. }
  51. protected void installDefaults() {
  52. if (menuBar.getLayout() == null ||
  53. menuBar.getLayout() instanceof UIResource) {
  54. menuBar.setLayout(new DefaultMenuLayout(menuBar,BoxLayout.LINE_AXIS));
  55. }
  56. LookAndFeel.installProperty(menuBar, "opaque", Boolean.TRUE);
  57. LookAndFeel.installBorder(menuBar,"MenuBar.border");
  58. LookAndFeel.installColorsAndFont(menuBar,
  59. "MenuBar.background",
  60. "MenuBar.foreground",
  61. "MenuBar.font");
  62. }
  63. protected void installListeners() {
  64. containerListener = createContainerListener();
  65. changeListener = createChangeListener();
  66. for (int i = 0; i < menuBar.getMenuCount(); i++) {
  67. JMenu menu = menuBar.getMenu(i);
  68. if (menu!=null)
  69. menu.getModel().addChangeListener(changeListener);
  70. }
  71. menuBar.addContainerListener(containerListener);
  72. }
  73. protected void installKeyboardActions() {
  74. InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
  75. SwingUtilities.replaceUIInputMap(menuBar,
  76. JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
  77. LazyActionMap.installLazyActionMap(menuBar, BasicMenuBarUI.class,
  78. "MenuBar.actionMap");
  79. }
  80. InputMap getInputMap(int condition) {
  81. if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
  82. Object[] bindings = (Object[])DefaultLookup.get
  83. (menuBar, this, "MenuBar.windowBindings");
  84. if (bindings != null) {
  85. return LookAndFeel.makeComponentInputMap(menuBar, bindings);
  86. }
  87. }
  88. return null;
  89. }
  90. public void uninstallUI(JComponent c) {
  91. uninstallDefaults();
  92. uninstallListeners();
  93. uninstallKeyboardActions();
  94. menuBar = null;
  95. }
  96. protected void uninstallDefaults() {
  97. if (menuBar!=null) {
  98. LookAndFeel.uninstallBorder(menuBar);
  99. }
  100. }
  101. protected void uninstallListeners() {
  102. menuBar.removeContainerListener(containerListener);
  103. for (int i = 0; i < menuBar.getMenuCount(); i++) {
  104. JMenu menu = menuBar.getMenu(i);
  105. if (menu !=null)
  106. menu.getModel().removeChangeListener(changeListener);
  107. }
  108. containerListener = null;
  109. changeListener = null;
  110. handler = null;
  111. }
  112. protected void uninstallKeyboardActions() {
  113. SwingUtilities.replaceUIInputMap(menuBar, JComponent.
  114. WHEN_IN_FOCUSED_WINDOW, null);
  115. SwingUtilities.replaceUIActionMap(menuBar, null);
  116. }
  117. protected ContainerListener createContainerListener() {
  118. return getHandler();
  119. }
  120. protected ChangeListener createChangeListener() {
  121. return getHandler();
  122. }
  123. private Handler getHandler() {
  124. if (handler == null) {
  125. handler = new Handler();
  126. }
  127. return handler;
  128. }
  129. public Dimension getMinimumSize(JComponent c) {
  130. return null;
  131. }
  132. public Dimension getMaximumSize(JComponent c) {
  133. return null;
  134. }
  135. private class Handler implements ChangeListener, ContainerListener {
  136. //
  137. // ChangeListener
  138. //
  139. public void stateChanged(ChangeEvent e) {
  140. int i,c;
  141. for(i=0,c = menuBar.getMenuCount() ; i < c ; i++) {
  142. JMenu menu = menuBar.getMenu(i);
  143. if(menu !=null && menu.isSelected()) {
  144. menuBar.getSelectionModel().setSelectedIndex(i);
  145. break;
  146. }
  147. }
  148. }
  149. //
  150. // ContainerListener
  151. //
  152. public void componentAdded(ContainerEvent e) {
  153. Component c = e.getChild();
  154. if (c instanceof JMenu)
  155. ((JMenu)c).getModel().addChangeListener(changeListener);
  156. }
  157. public void componentRemoved(ContainerEvent e) {
  158. Component c = e.getChild();
  159. if (c instanceof JMenu)
  160. ((JMenu)c).getModel().removeChangeListener(changeListener);
  161. }
  162. }
  163. private static class Actions extends UIAction {
  164. private static final String TAKE_FOCUS = "takeFocus";
  165. Actions(String key) {
  166. super(key);
  167. }
  168. public void actionPerformed(ActionEvent e) {
  169. // TAKE_FOCUS
  170. JMenuBar menuBar = (JMenuBar)e.getSource();
  171. MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager();
  172. MenuElement me[];
  173. MenuElement subElements[];
  174. JMenu menu = menuBar.getMenu(0);
  175. if (menu!=null) {
  176. me = new MenuElement[3];
  177. me[0] = (MenuElement) menuBar;
  178. me[1] = (MenuElement) menu;
  179. me[2] = (MenuElement) menu.getPopupMenu();
  180. defaultManager.setSelectedPath(me);
  181. }
  182. }
  183. }
  184. }