KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)WindowsToggleButtonUI.java 1.31 06/08/25
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 import java.beans.PropertyChangeEvent JavaDoc;
17
18
19
20 /**
21  * A Windows toggle 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.31 08/25/06
31  * @author Jeff Dinkins
32  */

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

89     protected void paintButtonPressed(Graphics g, AbstractButton b) {
90         if (XPStyle.getXP() == null &&
91         b.isContentAreaFilled() &&
92         !(b.getBorder() instanceof UIResource)) {
93         // This is a special case in which the toggle button in the
94
// Rollover JToolBar will render the button in a pressed state
95
Color oldColor = g.getColor();
96
97             int w = b.getWidth();
98         int h = b.getHeight();
99         UIDefaults table = UIManager.getLookAndFeelDefaults();
100
101         Color shade = table.getColor("ToggleButton.shadow");
102         Component p = b.getParent();
103         if (p != null && p.getBackground().equals(shade)) {
104         shade = table.getColor("ToggleButton.darkShadow");
105         }
106         g.setColor(shade);
107         g.drawRect(0, 0, w-1, h-1);
108         g.setColor(table.getColor("ToggleButton.highlight"));
109         g.drawLine(w-1, 0, w-1, h-1);
110         g.drawLine(0, h-1, w-1, h-1);
111         g.setColor(oldColor);
112     }
113     }
114
115     public void paint(Graphics g, JComponent c) {
116     if (XPStyle.getXP() != null) {
117         WindowsButtonUI.paintXPButtonBackground(g, c);
118     }
119     super.paint(g, c);
120     }
121
122
123     /**
124      * Overridden method to render the text without the mnemonic
125      */

126     protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String JavaDoc text) {
127     WindowsGraphicsUtils.paintText(g, b, textRect, text, getTextShiftOffset());
128     }
129
130     protected void paintFocus(Graphics g, AbstractButton b,
131                   Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
132     if (b.getParent() instanceof JToolBar) {
133         // Windows doesn't draw the focus rect for buttons in a toolbar.
134
return;
135     }
136     g.setColor(getFocusColor());
137     BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY,
138                       b.getWidth() - dashedRectGapWidth,
139                       b.getHeight() - dashedRectGapHeight);
140     }
141
142     // ********************************
143
// Layout Methods
144
// ********************************
145
public Dimension getPreferredSize(JComponent c) {
146     Dimension d = super.getPreferredSize(c);
147
148     /* Ensure that the width and height of the button is odd,
149      * to allow for the focus line if focus is painted
150      */

151         AbstractButton b = (AbstractButton)c;
152     if (d != null && b.isFocusPainted()) {
153         if(d.width % 2 == 0) { d.width += 1; }
154         if(d.height % 2 == 0) { d.height += 1; }
155     }
156     return d;
157     }
158 }
159
160
Popular Tags