KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)WindowsMenuUI.java 1.26 07/01/22
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 java.awt.*;
11 import java.awt.event.MouseEvent JavaDoc;
12
13 import javax.swing.plaf.ComponentUI JavaDoc;
14 import javax.swing.plaf.basic.BasicMenuUI JavaDoc;
15 import javax.swing.event.MouseInputListener JavaDoc;
16 import javax.swing.*;
17
18 import com.sun.java.swing.plaf.windows.TMSchema.Part;
19 import com.sun.java.swing.plaf.windows.TMSchema.State;
20
21 /**
22  * Windows rendition of the component.
23  * <p>
24  * <strong>Warning:</strong>
25  * Serialized objects of this class will not be compatible with
26  * future Swing releases. The current serialization support is appropriate
27  * for short term storage or RMI between applications running the same
28  * version of Swing. A future release of Swing will provide support for
29  * long term persistence.
30  */

31 public class WindowsMenuUI extends BasicMenuUI JavaDoc {
32
33     final WindowsMenuItemUIAccessor accessor =
34         new WindowsMenuItemUIAccessor() {
35
36             public JMenuItem getMenuItem() {
37                 return menuItem;
38             }
39
40             public State getState(JMenuItem menu) {
41                 State state = menu.isEnabled() ? State.NORMAL
42                         : State.DISABLED;
43                 ButtonModel model = menu.getModel();
44                 if (model.isArmed() || model.isSelected()) {
45                     state = (menu.isEnabled()) ? State.PUSHED
46                             : State.DISABLEDPUSHED;
47                 } else if (model.isRollover()
48                            && ((JMenu) menu).isTopLevelMenu()) {
49                     /*
50                      * Only paint rollover if no other menu on menubar is
51                      * selected
52                      */

53                     State stateTmp = state;
54                     state = (menu.isEnabled()) ? State.HOT
55                             : State.DISABLEDHOT;
56                     for (MenuElement menuElement :
57                         ((JMenuBar) menu.getParent()).getSubElements()) {
58                         if (((JMenuItem) menuElement).isSelected()) {
59                             state = stateTmp;
60                             break;
61                         }
62                     }
63                 }
64
65                 //non top level menus have HOT state instead of PUSHED
66
if (!((JMenu) menu).isTopLevelMenu()) {
67                     if (state == State.PUSHED) {
68                         state = State.HOT;
69                     } else if (state == State.DISABLEDPUSHED) {
70                         state = State.DISABLEDHOT;
71                     }
72                 }
73
74                 /*
75                  * on Vista top level menu for non active frame looks disabled
76                  */

77                 if (((JMenu) menu).isTopLevelMenu() && WindowsMenuItemUI.isVistaPainting()) {
78                     if (! WindowsMenuBarUI.isActive(menu)) {
79                         state = State.DISABLED;
80                     }
81                 }
82                 return state;
83             }
84
85             public Part getPart(JMenuItem menuItem) {
86                 return ((JMenu) menuItem).isTopLevelMenu() ? Part.MP_BARITEM
87                         : Part.MP_POPUPITEM;
88             }
89     };
90     public static ComponentUI JavaDoc createUI(JComponent x) {
91     return new WindowsMenuUI();
92     }
93
94     protected void installDefaults() {
95     super.installDefaults();
96     if (!WindowsLookAndFeel.isClassicWindows()) {
97         menuItem.setRolloverEnabled(true);
98     }
99     }
100
101     /**
102      * Draws the background of the menu.
103      * @since 1.4
104      */

105     protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) {
106         if (WindowsMenuItemUI.isVistaPainting()) {
107             WindowsMenuItemUI.paintBackground(accessor, g, menuItem, bgColor);
108             return;
109         }
110         
111     JMenu menu = (JMenu)menuItem;
112     ButtonModel model = menu.getModel();
113
114     // Use superclass method for the old Windows LAF,
115
// for submenus, and for XP toplevel if selected or pressed
116
if (WindowsLookAndFeel.isClassicWindows() ||
117         !menu.isTopLevelMenu() ||
118         (XPStyle.getXP() != null && (model.isArmed() || model.isSelected()))) {
119
120         super.paintBackground(g, menu, bgColor);
121         return;
122     }
123
124     Color oldColor = g.getColor();
125         int menuWidth = menu.getWidth();
126         int menuHeight = menu.getHeight();
127
128     UIDefaults table = UIManager.getLookAndFeelDefaults();
129     Color highlight = table.getColor("controlLtHighlight");
130     Color shadow = table.getColor("controlShadow");
131
132     g.setColor(menu.getBackground());
133     g.fillRect(0,0, menuWidth, menuHeight);
134
135         if (menu.isOpaque()) {
136             if (model.isArmed() || model.isSelected()) {
137         // Draw a lowered bevel border
138
g.setColor(shadow);
139         g.drawLine(0,0, menuWidth - 1,0);
140         g.drawLine(0,0, 0,menuHeight - 2);
141
142         g.setColor(highlight);
143         g.drawLine(menuWidth - 1,0, menuWidth - 1,menuHeight - 2);
144         g.drawLine(0,menuHeight - 2, menuWidth - 1,menuHeight - 2);
145             } else if (model.isRollover() && model.isEnabled()) {
146         // Only paint rollover if no other menu on menubar is selected
147
boolean otherMenuSelected = false;
148         MenuElement[] menus = ((JMenuBar)menu.getParent()).getSubElements();
149         for (int i = 0; i < menus.length; i++) {
150             if (((JMenuItem)menus[i]).isSelected()) {
151             otherMenuSelected = true;
152             break;
153             }
154         }
155         if (!otherMenuSelected) {
156             if (XPStyle.getXP() != null) {
157             g.setColor(selectionBackground); // Uses protected field.
158
g.fillRect(0, 0, menuWidth, menuHeight);
159             } else {
160             // Draw a raised bevel border
161
g.setColor(highlight);
162             g.drawLine(0,0, menuWidth - 1,0);
163             g.drawLine(0,0, 0,menuHeight - 2);
164
165             g.setColor(shadow);
166             g.drawLine(menuWidth - 1,0, menuWidth - 1,menuHeight - 2);
167             g.drawLine(0,menuHeight - 2, menuWidth - 1,menuHeight - 2);
168             }
169         }
170             }
171         }
172     g.setColor(oldColor);
173     }
174
175     /**
176      * Method which renders the text of the current menu item.
177      * <p>
178      * @param g Graphics context
179      * @param menuItem Current menu item to render
180      * @param textRect Bounding rectangle to render the text.
181      * @param text String to render
182      * @since 1.4
183      */

184     protected void paintText(Graphics g, JMenuItem menuItem,
185                              Rectangle textRect, String JavaDoc text) {
186         if (WindowsMenuItemUI.isVistaPainting()) {
187             WindowsMenuItemUI.paintText(accessor, g, menuItem, textRect, text);
188             return;
189         }
190     JMenu menu = (JMenu)menuItem;
191     ButtonModel model = menuItem.getModel();
192         Color oldColor = g.getColor();
193
194     // Only paint rollover if no other menu on menubar is selected
195
boolean paintRollover = model.isRollover();
196     if (paintRollover && menu.isTopLevelMenu()) {
197         MenuElement[] menus = ((JMenuBar)menu.getParent()).getSubElements();
198         for (int i = 0; i < menus.length; i++) {
199         if (((JMenuItem)menus[i]).isSelected()) {
200             paintRollover = false;
201             break;
202         }
203         }
204     }
205
206     if ((model.isSelected() && (WindowsLookAndFeel.isClassicWindows() ||
207                     !menu.isTopLevelMenu())) ||
208         (XPStyle.getXP() != null && (paintRollover ||
209                      model.isArmed() ||
210                      model.isSelected()))) {
211         g.setColor(selectionForeground); // Uses protected field.
212
}
213
214         WindowsGraphicsUtils.paintText(g, menuItem, textRect, text, 0);
215  
216         g.setColor(oldColor);
217     }
218
219     protected MouseInputListener JavaDoc createMouseInputListener(JComponent c) {
220         return new WindowsMouseInputHandler();
221     }
222
223     /**
224      * This class implements a mouse handler that sets the rollover flag to
225      * true when the mouse enters the menu and false when it exits.
226      * @since 1.4
227      */

228     protected class WindowsMouseInputHandler extends BasicMenuUI.MouseInputHandler JavaDoc {
229     public void mouseEntered(MouseEvent JavaDoc evt) {
230         super.mouseEntered(evt);
231
232         JMenu menu = (JMenu)evt.getSource();
233         ButtonModel model = menu.getModel();
234         if (menu.isRolloverEnabled()) {
235         model.setRollover(true);
236         menuItem.repaint();
237         }
238     }
239
240     public void mouseExited(MouseEvent JavaDoc evt) {
241         super.mouseExited(evt);
242
243         JMenu menu = (JMenu)evt.getSource();
244         ButtonModel model = menu.getModel();
245         if (menu.isRolloverEnabled()) {
246         model.setRollover(false);
247         menuItem.repaint();
248         }
249     }
250     }
251 }
252
253
Popular Tags