KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicMenuBarUI


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
8 package javax.swing.plaf.basic;
9
10 import sun.swing.DefaultLookup;
11 import sun.swing.UIAction;
12 import javax.swing.*;
13 import javax.swing.event.*;
14 import java.awt.Color JavaDoc;
15 import java.awt.Component JavaDoc;
16 import java.awt.Container JavaDoc;
17 import java.awt.Dimension JavaDoc;
18 import java.awt.Graphics JavaDoc;
19 import java.awt.Insets JavaDoc;
20 import java.awt.Point JavaDoc;
21 import java.awt.Rectangle JavaDoc;
22 import java.awt.event.*;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25
26 import javax.swing.border.*;
27 import javax.swing.plaf.*;
28
29
30 /**
31  * A default L&F implementation of MenuBarUI. This implementation
32  * is a "combined" view/controller.
33  *
34  * @version 1.80 12/19/03
35  * @author Georges Saab
36  * @author David Karlton
37  * @author Arnaud Weber
38  */

39 public class BasicMenuBarUI extends MenuBarUI {
40     protected JMenuBar menuBar = null;
41     protected ContainerListener containerListener;
42     protected ChangeListener JavaDoc changeListener;
43     private Handler handler;
44
45     public static ComponentUI createUI(JComponent x) {
46     return new BasicMenuBarUI JavaDoc();
47     }
48
49     static void loadActionMap(LazyActionMap JavaDoc map) {
50         map.put(new Actions(Actions.TAKE_FOCUS));
51     }
52
53     public void installUI(JComponent c) {
54     menuBar = (JMenuBar) c;
55
56     installDefaults();
57         installListeners();
58         installKeyboardActions();
59
60     }
61
62     protected void installDefaults() {
63     if (menuBar.getLayout() == null ||
64         menuBar.getLayout() instanceof UIResource) {
65             menuBar.setLayout(new DefaultMenuLayout JavaDoc(menuBar,BoxLayout.LINE_AXIS));
66         }
67   
68     LookAndFeel.installProperty(menuBar, "opaque", Boolean.TRUE);
69     LookAndFeel.installBorder(menuBar,"MenuBar.border");
70     LookAndFeel.installColorsAndFont(menuBar,
71                           "MenuBar.background",
72                           "MenuBar.foreground",
73                           "MenuBar.font");
74     }
75
76     protected void installListeners() {
77         containerListener = createContainerListener();
78         changeListener = createChangeListener();
79     
80         for (int i = 0; i < menuBar.getMenuCount(); i++) {
81             JMenu menu = menuBar.getMenu(i);
82         if (menu!=null)
83         menu.getModel().addChangeListener(changeListener);
84     }
85     menuBar.addContainerListener(containerListener);
86     }
87
88     protected void installKeyboardActions() {
89     InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
90
91     SwingUtilities.replaceUIInputMap(menuBar,
92                JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
93
94         LazyActionMap.installLazyActionMap(menuBar, BasicMenuBarUI JavaDoc.class,
95                                            "MenuBar.actionMap");
96     }
97
98     InputMap getInputMap(int condition) {
99     if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
100         Object JavaDoc[] bindings = (Object JavaDoc[])DefaultLookup.get
101                         (menuBar, this, "MenuBar.windowBindings");
102         if (bindings != null) {
103         return LookAndFeel.makeComponentInputMap(menuBar, bindings);
104         }
105     }
106     return null;
107     }
108
109     public void uninstallUI(JComponent c) {
110         uninstallDefaults();
111         uninstallListeners();
112         uninstallKeyboardActions();
113
114     menuBar = null;
115     }
116
117     protected void uninstallDefaults() {
118     if (menuBar!=null) {
119         LookAndFeel.uninstallBorder(menuBar);
120     }
121     }
122
123     protected void uninstallListeners() {
124     menuBar.removeContainerListener(containerListener);
125
126         for (int i = 0; i < menuBar.getMenuCount(); i++) {
127         JMenu menu = menuBar.getMenu(i);
128         if (menu !=null)
129         menu.getModel().removeChangeListener(changeListener);
130         }
131
132     containerListener = null;
133     changeListener = null;
134         handler = null;
135     }
136
137     protected void uninstallKeyboardActions() {
138     SwingUtilities.replaceUIInputMap(menuBar, JComponent.
139                        WHEN_IN_FOCUSED_WINDOW, null);
140     SwingUtilities.replaceUIActionMap(menuBar, null);
141     }
142
143     protected ContainerListener createContainerListener() {
144     return getHandler();
145     }
146
147     protected ChangeListener JavaDoc createChangeListener() {
148         return getHandler();
149     }
150
151     private Handler getHandler() {
152         if (handler == null) {
153             handler = new Handler();
154         }
155         return handler;
156     }
157
158
159     public Dimension JavaDoc getMinimumSize(JComponent c) {
160         return null;
161     }
162
163     public Dimension JavaDoc getMaximumSize(JComponent c) {
164         return null;
165     }
166
167     private class Handler implements ChangeListener JavaDoc, ContainerListener {
168         //
169
// ChangeListener
170
//
171
public void stateChanged(ChangeEvent JavaDoc e) {
172         int i,c;
173         for(i=0,c = menuBar.getMenuCount() ; i < c ; i++) {
174         JMenu menu = menuBar.getMenu(i);
175         if(menu !=null && menu.isSelected()) {
176             menuBar.getSelectionModel().setSelectedIndex(i);
177             break;
178         }
179         }
180     }
181
182         //
183
// ContainerListener
184
//
185
public void componentAdded(ContainerEvent e) {
186         Component JavaDoc c = e.getChild();
187         if (c instanceof JMenu)
188         ((JMenu)c).getModel().addChangeListener(changeListener);
189     }
190     public void componentRemoved(ContainerEvent e) {
191         Component JavaDoc c = e.getChild();
192         if (c instanceof JMenu)
193         ((JMenu)c).getModel().removeChangeListener(changeListener);
194     }
195     }
196
197
198     private static class Actions extends UIAction {
199         private static final String JavaDoc TAKE_FOCUS = "takeFocus";
200
201         Actions(String JavaDoc key) {
202             super(key);
203     }
204
205     public void actionPerformed(ActionEvent e) {
206             // TAKE_FOCUS
207
JMenuBar menuBar = (JMenuBar)e.getSource();
208             MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager();
209         MenuElement me[];
210         MenuElement subElements[];
211         JMenu menu = menuBar.getMenu(0);
212         if (menu!=null) {
213             me = new MenuElement[3];
214             me[0] = (MenuElement) menuBar;
215             me[1] = (MenuElement) menu;
216             me[2] = (MenuElement) menu.getPopupMenu();
217             defaultManager.setSelectedPath(me);
218         }
219     }
220     }
221 }
222
223
224
Popular Tags