1 /*
2 * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.swing.plaf.basic;
27
28 import sun.swing.DefaultLookup;
29 import sun.swing.UIAction;
30 import javax.swing;
31 import javax.swing.event;
32 import java.awt.Color;
33 import java.awt.Component;
34 import java.awt.Container;
35 import java.awt.Dimension;
36 import java.awt.Graphics;
37 import java.awt.Insets;
38 import java.awt.Point;
39 import java.awt.Rectangle;
40 import java.awt.event;
41 import java.beans.PropertyChangeEvent;
42 import java.beans.PropertyChangeListener;
43
44 import javax.swing.border;
45 import javax.swing.plaf;
46
47
48 /**
49 * A default L&F implementation of MenuBarUI. This implementation
50 * is a "combined" view/controller.
51 *
52 * @author Georges Saab
53 * @author David Karlton
54 * @author Arnaud Weber
55 */
56 public class BasicMenuBarUI extends MenuBarUI {
57 protected JMenuBar menuBar = null;
58 protected ContainerListener containerListener;
59 protected ChangeListener changeListener;
60 private Handler handler;
61
62 public static ComponentUI createUI(JComponent x) {
63 return new BasicMenuBarUI();
64 }
65
66 static void loadActionMap(LazyActionMap map) {
67 map.put(new Actions(Actions.TAKE_FOCUS));
68 }
69
70 public void installUI(JComponent c) {
71 menuBar = (JMenuBar) c;
72
73 installDefaults();
74 installListeners();
75 installKeyboardActions();
76
77 }
78
79 protected void installDefaults() {
80 if (menuBar.getLayout() == null ||
81 menuBar.getLayout() instanceof UIResource) {
82 menuBar.setLayout(new DefaultMenuLayout(menuBar,BoxLayout.LINE_AXIS));
83 }
84
85 LookAndFeel.installProperty(menuBar, "opaque", Boolean.TRUE);
86 LookAndFeel.installBorder(menuBar,"MenuBar.border");
87 LookAndFeel.installColorsAndFont(menuBar,
88 "MenuBar.background",
89 "MenuBar.foreground",
90 "MenuBar.font");
91 }
92
93 protected void installListeners() {
94 containerListener = createContainerListener();
95 changeListener = createChangeListener();
96
97 for (int i = 0; i < menuBar.getMenuCount(); i++) {
98 JMenu menu = menuBar.getMenu(i);
99 if (menu!=null)
100 menu.getModel().addChangeListener(changeListener);
101 }
102 menuBar.addContainerListener(containerListener);
103 }
104
105 protected void installKeyboardActions() {
106 InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
107
108 SwingUtilities.replaceUIInputMap(menuBar,
109 JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
110
111 LazyActionMap.installLazyActionMap(menuBar, BasicMenuBarUI.class,
112 "MenuBar.actionMap");
113 }
114
115 InputMap getInputMap(int condition) {
116 if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
117 Object[] bindings = (Object[])DefaultLookup.get
118 (menuBar, this, "MenuBar.windowBindings");
119 if (bindings != null) {
120 return LookAndFeel.makeComponentInputMap(menuBar, bindings);
121 }
122 }
123 return null;
124 }
125
126 public void uninstallUI(JComponent c) {
127 uninstallDefaults();
128 uninstallListeners();
129 uninstallKeyboardActions();
130
131 menuBar = null;
132 }
133
134 protected void uninstallDefaults() {
135 if (menuBar!=null) {
136 LookAndFeel.uninstallBorder(menuBar);
137 }
138 }
139
140 protected void uninstallListeners() {
141 menuBar.removeContainerListener(containerListener);
142
143 for (int i = 0; i < menuBar.getMenuCount(); i++) {
144 JMenu menu = menuBar.getMenu(i);
145 if (menu !=null)
146 menu.getModel().removeChangeListener(changeListener);
147 }
148
149 containerListener = null;
150 changeListener = null;
151 handler = null;
152 }
153
154 protected void uninstallKeyboardActions() {
155 SwingUtilities.replaceUIInputMap(menuBar, JComponent.
156 WHEN_IN_FOCUSED_WINDOW, null);
157 SwingUtilities.replaceUIActionMap(menuBar, null);
158 }
159
160 protected ContainerListener createContainerListener() {
161 return getHandler();
162 }
163
164 protected ChangeListener createChangeListener() {
165 return getHandler();
166 }
167
168 private Handler getHandler() {
169 if (handler == null) {
170 handler = new Handler();
171 }
172 return handler;
173 }
174
175
176 public Dimension getMinimumSize(JComponent c) {
177 return null;
178 }
179
180 public Dimension getMaximumSize(JComponent c) {
181 return null;
182 }
183
184 private class Handler implements ChangeListener, ContainerListener {
185 //
186 // ChangeListener
187 //
188 public void stateChanged(ChangeEvent e) {
189 int i,c;
190 for(i=0,c = menuBar.getMenuCount() ; i < c ; i++) {
191 JMenu menu = menuBar.getMenu(i);
192 if(menu !=null && menu.isSelected()) {
193 menuBar.getSelectionModel().setSelectedIndex(i);
194 break;
195 }
196 }
197 }
198
199 //
200 // ContainerListener
201 //
202 public void componentAdded(ContainerEvent e) {
203 Component c = e.getChild();
204 if (c instanceof JMenu)
205 ((JMenu)c).getModel().addChangeListener(changeListener);
206 }
207 public void componentRemoved(ContainerEvent e) {
208 Component c = e.getChild();
209 if (c instanceof JMenu)
210 ((JMenu)c).getModel().removeChangeListener(changeListener);
211 }
212 }
213
214
215 private static class Actions extends UIAction {
216 private static final String TAKE_FOCUS = "takeFocus";
217
218 Actions(String key) {
219 super(key);
220 }
221
222 public void actionPerformed(ActionEvent e) {
223 // TAKE_FOCUS
224 JMenuBar menuBar = (JMenuBar)e.getSource();
225 MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager();
226 MenuElement me[];
227 MenuElement subElements[];
228 JMenu menu = menuBar.getMenu(0);
229 if (menu!=null) {
230 me = new MenuElement[3];
231 me[0] = (MenuElement) menuBar;
232 me[1] = (MenuElement) menu;
233 me[2] = (MenuElement) menu.getPopupMenu();
234 defaultManager.setSelectedPath(me);
235 }
236 }
237 }
238 }