KickJava   Java API By Example, From Geeks To Geeks.

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


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

33
34 public class WindowsMenuItemUI extends BasicMenuItemUI {
35     final WindowsMenuItemUIAccessor accessor =
36         new WindowsMenuItemUIAccessor() {
37         
38             public JMenuItem getMenuItem() {
39                 return menuItem;
40             }
41
42             public State getState(JMenuItem menuItem) {
43                 return WindowsMenuItemUI.getState(this, menuItem);
44             }
45
46             public Part getPart(JMenuItem menuItem) {
47                 return WindowsMenuItemUI.getPart(this, menuItem);
48             }
49     };
50     public static ComponentUI createUI(JComponent c) {
51     return new WindowsMenuItemUI();
52     }
53
54     /**
55      * Method which renders the text of the current menu item.
56      * <p>
57      * @param g Graphics context
58      * @param menuItem Current menu item to render
59      * @param textRect Bounding rectangle to render the text.
60      * @param text String to render
61      */

62     protected void paintText(Graphics g, JMenuItem menuItem,
63                              Rectangle textRect, String JavaDoc text) {
64         if (WindowsMenuItemUI.isVistaPainting()) {
65             WindowsMenuItemUI.paintText(accessor, g, menuItem, textRect, text);
66             return;
67         }
68     ButtonModel model = menuItem.getModel();
69         Color oldColor = g.getColor();
70
71         if(model.isEnabled() &&
72             (model.isArmed() || (menuItem instanceof JMenu &&
73              model.isSelected()))) {
74             g.setColor(selectionForeground); // Uses protected field.
75
}
76
77         WindowsGraphicsUtils.paintText(g, menuItem, textRect, text, 0);
78
79         g.setColor(oldColor);
80     }
81     
82     @Override JavaDoc
83     protected void paintBackground(Graphics g, JMenuItem menuItem,
84             Color bgColor) {
85         if (WindowsMenuItemUI.isVistaPainting()) {
86             WindowsMenuItemUI.paintBackground(accessor, g, menuItem, bgColor);
87             return;
88         }
89         super.paintBackground(g, menuItem, bgColor);
90     }
91     
92     static void paintBackground(WindowsMenuItemUIAccessor menuItemUI,
93             Graphics g, JMenuItem menuItem, Color bgColor) {
94         assert isVistaPainting();
95         if (isVistaPainting()) {
96             int menuWidth = menuItem.getWidth();
97             int menuHeight = menuItem.getHeight();
98             if (menuItem.isOpaque()) {
99                 Color oldColor = g.getColor();
100                 g.setColor(menuItem.getBackground());
101                 g.fillRect(0,0, menuWidth, menuHeight);
102                 g.setColor(oldColor);
103             }
104             XPStyle xp = XPStyle.getXP();
105             Part part = menuItemUI.getPart(menuItem);
106             Skin skin = xp.getSkin(menuItem, part);
107             skin.paintSkin(g, 0 , 0,
108                 menuWidth,
109                 menuHeight,
110                 menuItemUI.getState(menuItem));
111         }
112     }
113
114     static void paintText(WindowsMenuItemUIAccessor menuItemUI, Graphics g,
115                                 JMenuItem menuItem, Rectangle textRect,
116                                 String JavaDoc text) {
117         assert isVistaPainting();
118         if (isVistaPainting()) {
119             State state = menuItemUI.getState(menuItem);
120        
121             /* part of it copied from 1.6 WindowsGraphicsUtils.java */
122             FontMetrics fm = SwingUtilities2.getFontMetrics(menuItem, g);
123             int mnemIndex = menuItem.getDisplayedMnemonicIndex();
124             // W2K Feature: Check to see if the Underscore should be rendered.
125
if (WindowsLookAndFeel.isMnemonicHidden() == true) {
126                 mnemIndex = -1;
127             }
128             XPStyle xp = XPStyle.getXP();
129             Color textColor = menuItem.getForeground();
130             if (textColor instanceof UIResource) {
131                 Part part = menuItemUI.getPart(menuItem);
132                 textColor = xp.getColor(menuItem, part, state, Prop.TEXTCOLOR, textColor);
133             }
134             g.setColor(textColor);
135             SwingUtilities2.drawStringUnderlineCharAt(menuItem,
136                 g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent());
137         }
138     }
139     
140     static State getState(WindowsMenuItemUIAccessor menuItemUI, JMenuItem menuItem) {
141         State state;
142         ButtonModel model = menuItem.getModel();
143         if (model.isArmed()) {
144             state = (model.isEnabled()) ? State.HOT : State.DISABLEDHOT;
145         } else {
146             state = (model.isEnabled()) ? State.NORMAL : State.DISABLED;
147         }
148         return state;
149     }
150     
151     static Part getPart(WindowsMenuItemUIAccessor menuItemUI, JMenuItem menuItem) {
152         return Part.MP_POPUPITEM;
153     }
154     
155     /*
156      * TODO idk can we use XPStyle.isVista?
157      * is it possible that in some theme some Vista parts are not defined while
158      * others are?
159      */

160     static boolean isVistaPainting() {
161         XPStyle xp = XPStyle.getXP();
162         return xp != null && xp.isSkinDefined(null, Part.MP_POPUPITEM);
163     }
164 }
165
166
Popular Tags