KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)WindowsButtonUI.java 1.36 06/03/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 javax.swing.plaf.basic.*;
11 import javax.swing.border.*;
12 import javax.swing.plaf.*;
13 import javax.swing.*;
14
15 import java.awt.*;
16
17 import com.sun.java.swing.plaf.windows.TMSchema.*;
18 import com.sun.java.swing.plaf.windows.XPStyle.Skin;
19
20 /**
21  * Windows button.
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.36 03/22/06
31  * @author Jeff Dinkins
32  *
33  */

34 public class WindowsButtonUI extends BasicButtonUI
35 {
36     private final static WindowsButtonUI windowsButtonUI = new WindowsButtonUI();
37
38     protected int dashedRectGapX;
39     protected int dashedRectGapY;
40     protected int dashedRectGapWidth;
41     protected int dashedRectGapHeight;
42
43     protected Color focusColor;
44
45     private boolean defaults_initialized = false;
46     
47
48     // ********************************
49
// Create PLAF
50
// ********************************
51
public static ComponentUI createUI(JComponent c){
52     return windowsButtonUI;
53     }
54     
55
56     // ********************************
57
// Defaults
58
// ********************************
59
protected void installDefaults(AbstractButton b) {
60     super.installDefaults(b);
61     if(!defaults_initialized) {
62         String JavaDoc pp = getPropertyPrefix();
63         dashedRectGapX = UIManager.getInt(pp + "dashedRectGapX");
64         dashedRectGapY = UIManager.getInt(pp + "dashedRectGapY");
65         dashedRectGapWidth = UIManager.getInt(pp + "dashedRectGapWidth");
66         dashedRectGapHeight = UIManager.getInt(pp + "dashedRectGapHeight");
67         focusColor = UIManager.getColor(pp + "focus");
68         defaults_initialized = true;
69     }
70
71     XPStyle xp = XPStyle.getXP();
72     if (xp != null) {
73             b.setBorder(xp.getBorder(b, getXPButtonType(b)));
74             LookAndFeel.installProperty(b, "rolloverEnabled", Boolean.TRUE);
75     }
76     }
77     
78     protected void uninstallDefaults(AbstractButton b) {
79     super.uninstallDefaults(b);
80     defaults_initialized = false;
81     }
82
83     protected Color getFocusColor() {
84     return focusColor;
85     }
86     
87     // ********************************
88
// Paint Methods
89
// ********************************
90

91     /**
92      * Overridden method to render the text without the mnemonic
93      */

94     protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String JavaDoc text) {
95     WindowsGraphicsUtils.paintText(g, b, textRect, text, getTextShiftOffset());
96     }
97     
98     protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
99     if (b.getParent() instanceof JToolBar) {
100         // Windows doesn't draw the focus rect for buttons in a toolbar.
101
return;
102     }
103         
104     if (XPStyle.getXP() != null) {
105         return;
106     }
107
108     // focus painted same color as text on Basic??
109
int width = b.getWidth();
110     int height = b.getHeight();
111     g.setColor(getFocusColor());
112     BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY,
113                       width - dashedRectGapWidth, height - dashedRectGapHeight);
114     }
115     
116     protected void paintButtonPressed(Graphics g, AbstractButton b){
117     setTextShiftOffset();
118     }
119
120     // ********************************
121
// Layout Methods
122
// ********************************
123
public Dimension getPreferredSize(JComponent c) {
124     Dimension d = super.getPreferredSize(c);
125     
126     /* Ensure that the width and height of the button is odd,
127      * to allow for the focus line if focus is painted
128      */

129         AbstractButton b = (AbstractButton)c;
130     if (d != null && b.isFocusPainted()) {
131         if(d.width % 2 == 0) { d.width += 1; }
132         if(d.height % 2 == 0) { d.height += 1; }
133     }
134     return d;
135     }
136
137
138     /* These rectangles/insets are allocated once for all
139      * ButtonUI.paint() calls. Re-using rectangles rather than
140      * allocating them in each paint call substantially reduced the time
141      * it took paint to run. Obviously, this method can't be re-entered.
142      */

143     private static Rectangle viewRect = new Rectangle();
144
145     public void paint(Graphics g, JComponent c) {
146     if (XPStyle.getXP() != null) {
147         WindowsButtonUI.paintXPButtonBackground(g, c);
148     }
149     super.paint(g, c);
150     }
151
152     static Part getXPButtonType(AbstractButton b) {
153         boolean toolbar = (b.getParent() instanceof JToolBar);
154         return toolbar ? Part.TP_BUTTON : Part.BP_PUSHBUTTON;
155     }
156
157     static void paintXPButtonBackground(Graphics g, JComponent c) {
158     AbstractButton b = (AbstractButton)c;
159
160     XPStyle xp = XPStyle.getXP();
161
162     boolean toolbar = (b.getParent() instanceof JToolBar);
163         Part part = getXPButtonType(b);
164
165     if (b.isContentAreaFilled() && xp != null) {
166
167         ButtonModel model = b.getModel();
168             Skin skin = xp.getSkin(b, part);
169
170         // normal, rollover/activated/focus, pressed, disabled, default
171
State state = State.NORMAL;
172         if (toolbar) {
173                 if (model.isArmed() && model.isPressed()) {
174                     state = State.PRESSED;
175                 } else if (!model.isEnabled()) {
176                     state = State.DISABLED;
177                 } else if (model.isSelected() && model.isRollover()) {
178                     state = State.HOTCHECKED;
179                 } else if (model.isSelected()) {
180                     state = State.CHECKED;
181                 } else if (model.isRollover()) {
182                     state = State.HOT;
183                 }
184         } else {
185         if (model.isArmed() && model.isPressed() || model.isSelected()) {
186                     state = State.PRESSED;
187                 } else if (!model.isEnabled()) {
188                     state = State.DISABLED;
189                 } else if (model.isRollover() || model.isPressed()) {
190                     state = State.HOT;
191                 } else if (b instanceof JButton && ((JButton)b).isDefaultButton()) {
192                     state = State.DEFAULTED;
193                 } else if (c.hasFocus()) {
194                     state = State.HOT;
195                 }
196         }
197         Dimension d = c.getSize();
198         int dx = 0;
199         int dy = 0;
200         int dw = d.width;
201         int dh = d.height;
202
203         Border border = c.getBorder();
204         Insets insets;
205         if (border != null) {
206         // Note: The border may be compound, containing an outer
207
// opaque border (supplied by the application), plus an
208
// inner transparent margin border. We want to size the
209
// background to fill the transparent part, but stay
210
// inside the opaque part.
211
insets = WindowsButtonUI.getOpaqueInsets(border, c);
212         } else {
213         insets = c.getInsets();
214         }
215         if (insets != null) {
216         dx += insets.left;
217         dy += insets.top;
218         dw -= (insets.left + insets.right);
219         dh -= (insets.top + insets.bottom);
220         }
221             skin.paintSkin(g, dx, dy, dw, dh, state);
222     }
223     }
224
225     /**
226      * returns - b.getBorderInsets(c) if border is opaque
227      * - null if border is completely non-opaque
228      * - somewhere inbetween if border is compound and
229      * outside border is opaque and inside isn't
230      */

231     private static Insets getOpaqueInsets(Border b, Component c) {
232     if (b == null) {
233         return null;
234     }
235     if (b.isBorderOpaque()) {
236         return b.getBorderInsets(c);
237     } else if (b instanceof CompoundBorder) {
238         CompoundBorder cb = (CompoundBorder)b;
239         Insets iOut = getOpaqueInsets(cb.getOutsideBorder(), c);
240         if (iOut != null && iOut.equals(cb.getOutsideBorder().getBorderInsets(c))) {
241         // Outside border is opaque, keep looking
242
Insets iIn = getOpaqueInsets(cb.getInsideBorder(), c);
243         if (iIn == null) {
244             // Inside is non-opaque, use outside insets
245
return iOut;
246         } else {
247             // Found non-opaque somewhere in the inside (which is
248
// also compound).
249
return new Insets(iOut.top + iIn.top, iOut.left + iIn.left,
250                       iOut.bottom + iIn.bottom, iOut.right + iIn.right);
251         }
252         } else {
253         // Outside is either all non-opaque or has non-opaque
254
// border inside another compound border
255
return iOut;
256         }
257     } else {
258         return null;
259     }
260     }
261 }
262
263
Popular Tags