39 |
package javax.swing.plaf.metal; |
package javax.swing.plaf.metal; |
40 |
|
|
41 |
import java.awt.Color; |
import java.awt.Color; |
42 |
|
import java.awt.Font; |
43 |
|
import java.awt.FontMetrics; |
44 |
|
import java.awt.Graphics; |
45 |
|
import java.awt.Rectangle; |
46 |
|
|
47 |
|
import javax.swing.AbstractButton; |
48 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
49 |
|
import javax.swing.JToggleButton; |
50 |
|
import javax.swing.SwingUtilities; |
51 |
import javax.swing.UIDefaults; |
import javax.swing.UIDefaults; |
52 |
import javax.swing.UIManager; |
import javax.swing.UIManager; |
53 |
import javax.swing.plaf.ComponentUI; |
import javax.swing.plaf.ComponentUI; |
54 |
|
import javax.swing.plaf.basic.BasicButtonUI; |
55 |
import javax.swing.plaf.basic.BasicToggleButtonUI; |
import javax.swing.plaf.basic.BasicToggleButtonUI; |
56 |
|
|
57 |
|
/** |
58 |
|
* A UI delegate for {@link JToggleButton} components. |
59 |
|
*/ |
60 |
public class MetalToggleButtonUI |
public class MetalToggleButtonUI |
61 |
extends BasicToggleButtonUI |
extends BasicToggleButtonUI |
62 |
{ |
{ |
70 |
/** The color for disabled button labels. */ |
/** The color for disabled button labels. */ |
71 |
protected Color disabledTextColor; |
protected Color disabledTextColor; |
72 |
|
|
73 |
/** The shared UI instance for MetalToggleButtonUIs */ |
/** |
74 |
private static MetalToggleButtonUI instance = null; |
* Returns an instance of MetalToggleButtonUI. |
75 |
|
* |
76 |
|
* @param component the component for which we return an UI instance |
77 |
|
* |
78 |
|
* @return an instance of MetalToggleButtonUI |
79 |
|
*/ |
80 |
|
public static ComponentUI createUI(JComponent component) |
81 |
|
{ |
82 |
|
return new MetalToggleButtonUI(); |
83 |
|
} |
84 |
|
|
85 |
/** |
/** |
86 |
* Constructs a new instance of MetalToggleButtonUI. |
* Constructs a new instance of MetalToggleButtonUI. |
88 |
public MetalToggleButtonUI() |
public MetalToggleButtonUI() |
89 |
{ |
{ |
90 |
super(); |
super(); |
91 |
focusColor = getFocusColor(); |
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
92 |
selectColor = getSelectColor(); |
focusColor = defaults.getColor(getPropertyPrefix() + "focus"); |
93 |
disabledTextColor = getDisabledTextColor(); |
selectColor = defaults.getColor(getPropertyPrefix() + "select"); |
94 |
|
disabledTextColor = defaults.getColor(getPropertyPrefix() + "disabledText"); |
95 |
} |
} |
96 |
|
|
|
|
|
97 |
/** |
/** |
98 |
* Returns the color for the focus border. |
* Returns the color for the focus border. |
99 |
* |
* |
101 |
*/ |
*/ |
102 |
protected Color getFocusColor() |
protected Color getFocusColor() |
103 |
{ |
{ |
104 |
UIDefaults def = UIManager.getLookAndFeelDefaults(); |
return focusColor; |
|
return def.getColor(getPropertyPrefix() + ".focus"); |
|
105 |
} |
} |
106 |
|
|
107 |
/** |
/** |
111 |
*/ |
*/ |
112 |
protected Color getSelectColor() |
protected Color getSelectColor() |
113 |
{ |
{ |
114 |
UIDefaults def = UIManager.getLookAndFeelDefaults(); |
return selectColor; |
|
return def.getColor(getPropertyPrefix() + ".select"); |
|
115 |
} |
} |
116 |
|
|
117 |
/** |
/** |
121 |
*/ |
*/ |
122 |
protected Color getDisabledTextColor() |
protected Color getDisabledTextColor() |
123 |
{ |
{ |
124 |
UIDefaults def = UIManager.getLookAndFeelDefaults(); |
return disabledTextColor; |
|
return def.getColor(getPropertyPrefix() + ".disabledText"); |
|
125 |
} |
} |
126 |
|
|
127 |
/** |
/** |
128 |
* Returns an instance of MetalToggleButtonUI. |
* Updates the button with the defaults for this look and feel. |
129 |
* |
* |
130 |
* @param component the component for which we return an UI instance |
* @param b the button. |
|
* |
|
|
* @return an instance of MetalToggleButtonUI |
|
131 |
*/ |
*/ |
132 |
public static ComponentUI createUI(JComponent component) |
public void installDefaults(AbstractButton b) |
133 |
|
{ |
134 |
|
// FIXME: for now, this override just changes the visibility of the method |
135 |
|
// in the super-class, to satisfy japi...but there must be something else. |
136 |
|
super.installDefaults(b); |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
|
* Paints the button background when it is pressed/selected. |
141 |
|
* |
142 |
|
* @param g the graphics device. |
143 |
|
* @param b the button. |
144 |
|
*/ |
145 |
|
protected void paintButtonPressed(Graphics g, AbstractButton b) |
146 |
|
{ |
147 |
|
Color saved = g.getColor(); |
148 |
|
Rectangle bounds = SwingUtilities.getLocalBounds(b); |
149 |
|
g.setColor(selectColor); |
150 |
|
g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height); |
151 |
|
g.setColor(saved); |
152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
|
* Paints the text for the button. |
156 |
|
* |
157 |
|
* @param g the graphics device. |
158 |
|
* @param c the component. |
159 |
|
* @param textRect the bounds for the text. |
160 |
|
* @param text the text. |
161 |
|
* |
162 |
|
* @deprecated 1.4 Use {@link BasicButtonUI#paintText(java.awt.Graphics, |
163 |
|
* javax.swing.AbstractButton, java.awt.Rectangle, java.lang.String)}. |
164 |
|
*/ |
165 |
|
protected void paintText(Graphics g, JComponent c, Rectangle textRect, |
166 |
|
String text) |
167 |
|
{ |
168 |
|
Font savedFont = g.getFont(); |
169 |
|
Color savedColor = g.getColor(); |
170 |
|
g.setFont(c.getFont()); |
171 |
|
if (c.isEnabled()) |
172 |
|
g.setColor(c.getForeground()); |
173 |
|
else |
174 |
|
g.setColor(disabledTextColor); |
175 |
|
FontMetrics fm = g.getFontMetrics(c.getFont()); |
176 |
|
int ascent = fm.getAscent(); |
177 |
|
g.drawString(text, textRect.x, textRect.y + ascent); |
178 |
|
g.setFont(savedFont); |
179 |
|
g.setColor(savedColor); |
180 |
|
} |
181 |
|
|
182 |
|
/** |
183 |
|
* Draws the focus highlight around the text and icon. |
184 |
|
* |
185 |
|
* @param g the graphics device. |
186 |
|
* @param b the button. |
187 |
|
*/ |
188 |
|
protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, |
189 |
|
Rectangle textRect, Rectangle iconRect) |
190 |
{ |
{ |
191 |
if (instance == null) |
if (!b.hasFocus()) |
192 |
instance = new MetalToggleButtonUI(); |
return; |
193 |
return instance; |
Color saved = g.getColor(); |
194 |
|
g.setColor(focusColor); |
195 |
|
Rectangle fr = iconRect.union(textRect); |
196 |
|
g.drawRect(fr.x - 1, fr.y - 1, fr.width + 1, fr.height + 1); |
197 |
|
g.setColor(saved); |
198 |
} |
} |
199 |
|
|
200 |
} |
} |