KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > windows > WindowsMenuBarUI


1 /*
2  * @(#)WindowsMenuBarUI.java 1.15 07/01/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.windows;
9
10 import javax.swing.plaf.basic.*;
11 import javax.swing.*;
12 import javax.swing.plaf.ActionMapUIResource JavaDoc;
13 import javax.swing.plaf.ComponentUI JavaDoc;
14 import java.awt.event.ActionEvent JavaDoc;
15 import java.awt.event.HierarchyEvent JavaDoc;
16 import java.awt.event.HierarchyListener JavaDoc;
17 import java.awt.event.WindowAdapter JavaDoc;
18 import java.awt.event.WindowEvent JavaDoc;
19 import java.awt.event.WindowListener JavaDoc;
20 import java.awt.event.WindowStateListener JavaDoc;
21
22 import java.awt.*;
23
24 import com.sun.java.swing.plaf.windows.TMSchema.*;
25 import com.sun.java.swing.plaf.windows.XPStyle.*;
26
27 /**
28  * Windows rendition of the component.
29  * <p>
30  * <strong>Warning:</strong>
31  * Serialized objects of this class will not be compatible with
32  * future Swing releases. The current serialization support is appropriate
33  * for short term storage or RMI between applications running the same
34  * version of Swing. A future release of Swing will provide support for
35  * long term persistence.
36  */

37 public class WindowsMenuBarUI extends BasicMenuBarUI
38 {
39     /* to be accessed on the EDT only */
40     private WindowListener JavaDoc windowListener = null;
41     private HierarchyListener JavaDoc hierarchyListener = null;
42     private Window window = null;
43     
44     public static ComponentUI JavaDoc createUI(JComponent x) {
45     return new WindowsMenuBarUI();
46     }
47     
48     @Override JavaDoc
49     protected void uninstallListeners() {
50         uninstallWindowListener();
51         if (hierarchyListener != null) {
52             menuBar.removeHierarchyListener(hierarchyListener);
53             hierarchyListener = null;
54         }
55         super.uninstallListeners();
56     }
57     private void installWindowListener() {
58         if (windowListener == null) {
59             Component component = menuBar.getTopLevelAncestor();
60             if (component instanceof Window) {
61                 window = (Window) component;
62                 windowListener = new WindowAdapter JavaDoc() {
63                     @Override JavaDoc
64                     public void windowActivated(WindowEvent JavaDoc e) {
65                         menuBar.repaint();
66                     }
67                     @Override JavaDoc
68                     public void windowDeactivated(WindowEvent JavaDoc e) {
69                         menuBar.repaint();
70                     }
71                 };
72                 ((Window) component).addWindowListener(windowListener);
73             }
74         }
75     }
76     private void uninstallWindowListener() {
77         if (windowListener != null && window != null) {
78             window.removeWindowListener(windowListener);
79         }
80         window = null;
81         windowListener = null;
82     }
83     @Override JavaDoc
84     protected void installListeners() {
85         if (WindowsLookAndFeel.isOnVista()) {
86             installWindowListener();
87             hierarchyListener =
88                 new HierarchyListener JavaDoc() {
89                     public void hierarchyChanged(HierarchyEvent JavaDoc e) {
90                         if ((e.getChangeFlags()
91                                 & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
92                             if (menuBar.isDisplayable()) {
93                                 installWindowListener();
94                             } else {
95                                 uninstallWindowListener();
96                             }
97                         }
98                     }
99             };
100             menuBar.addHierarchyListener(hierarchyListener);
101         }
102         super.installListeners();
103     }
104     
105     protected void installKeyboardActions() {
106         super.installKeyboardActions();
107     ActionMap map = SwingUtilities.getUIActionMap(menuBar);
108         if (map == null) {
109             map = new ActionMapUIResource JavaDoc();
110             SwingUtilities.replaceUIActionMap(menuBar, map);
111         }
112         map.put("takeFocus", new TakeFocus());
113     }
114
115     /**
116      * Action that activates the menu (e.g. when F10 is pressed).
117      * Unlike BasicMenuBarUI.TakeFocus, this Action will not show menu popup.
118      */

119     private static class TakeFocus extends AbstractAction {
120     public void actionPerformed(ActionEvent JavaDoc e) {
121         JMenuBar menuBar = (JMenuBar)e.getSource();
122         JMenu menu = menuBar.getMenu(0);
123         if (menu != null) {
124                 MenuSelectionManager msm =
125                     MenuSelectionManager.defaultManager();
126                 MenuElement path[] = new MenuElement[2];
127                 path[0] = (MenuElement)menuBar;
128                 path[1] = (MenuElement)menu;
129                 msm.setSelectedPath(path);
130
131                 // show mnemonics
132
WindowsLookAndFeel.setMnemonicHidden(false);
133                 WindowsLookAndFeel.repaintRootPane(menuBar);
134         }
135     }
136     }
137
138     @Override JavaDoc
139     public void paint(Graphics g, JComponent c) {
140         if (WindowsMenuItemUI.isVistaPainting()) {
141             XPStyle xp = XPStyle.getXP();
142             Skin skin;
143             skin = xp.getSkin(c, Part.MP_BARBACKGROUND);
144             int width = c.getWidth();
145             int height = c.getHeight();
146             State state = isActive(c) ? State.ACTIVE : State.INACTIVE;
147             skin.paintSkin(g, 0, 0, width, height, state);
148         } else {
149             super.paint(g, c);
150         }
151     }
152     
153     /**
154      * Checks if component belongs to an active window.
155      * @param c component to check
156      * @return true if component belongs to an active window
157      */

158     static boolean isActive(JComponent c) {
159         JRootPane rootPane = c.getRootPane();
160         if (rootPane != null) {
161             Component component = rootPane.getParent();
162             if (component instanceof Window) {
163                 return ((Window) component).isActive();
164             }
165         }
166         return true;
167     }
168 }
169
170
Popular Tags