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; |
import javax.swing.AbstractButton; |
48 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
|
import javax.swing.JToolBar; |
|
49 |
import javax.swing.UIDefaults; |
import javax.swing.UIDefaults; |
50 |
import javax.swing.UIManager; |
import javax.swing.UIManager; |
51 |
import javax.swing.plaf.ComponentUI; |
import javax.swing.plaf.ComponentUI; |
52 |
|
import javax.swing.plaf.UIResource; |
53 |
|
import javax.swing.plaf.basic.BasicButtonListener; |
54 |
import javax.swing.plaf.basic.BasicButtonUI; |
import javax.swing.plaf.basic.BasicButtonUI; |
55 |
|
|
56 |
/** |
/** |
63 |
extends BasicButtonUI |
extends BasicButtonUI |
64 |
{ |
{ |
65 |
|
|
66 |
/** The cached MetalButtonUI instance. */ |
/** The color used to draw the focus rectangle around the text and/or icon. */ |
|
private static MetalButtonUI instance = null; |
|
|
|
|
|
/** The color for the focus border. */ |
|
67 |
protected Color focusColor; |
protected Color focusColor; |
68 |
|
|
69 |
/** The color that indicates a selected button. */ |
/** The background color for the button when it is pressed. */ |
70 |
protected Color selectColor; |
protected Color selectColor; |
71 |
|
|
72 |
/** The color for disabled button labels. */ |
/** The color for disabled button labels. */ |
73 |
protected Color disabledTextColor; |
protected Color disabledTextColor; |
74 |
|
|
75 |
/** |
/** |
76 |
* Creates a new instance of MetalButtonUI. |
* Creates a new instance. |
77 |
*/ |
*/ |
78 |
public MetalButtonUI() |
public MetalButtonUI() |
79 |
{ |
{ |
80 |
super(); |
super(); |
81 |
focusColor = getFocusColor(); |
UIDefaults def = UIManager.getLookAndFeelDefaults(); |
82 |
selectColor = getSelectColor(); |
focusColor = def.getColor(getPropertyPrefix() + "focus"); |
83 |
disabledTextColor = getDisabledTextColor(); |
selectColor = def.getColor(getPropertyPrefix() + "select"); |
84 |
|
disabledTextColor = def.getColor(getPropertyPrefix() + "disabledText"); |
85 |
} |
} |
86 |
|
|
87 |
/** |
/** |
91 |
*/ |
*/ |
92 |
protected Color getFocusColor() |
protected Color getFocusColor() |
93 |
{ |
{ |
94 |
UIDefaults def = UIManager.getLookAndFeelDefaults(); |
return focusColor; |
|
return def.getColor(getPropertyPrefix() + ".focus"); |
|
95 |
} |
} |
96 |
|
|
97 |
/** |
/** |
101 |
*/ |
*/ |
102 |
protected Color getSelectColor() |
protected Color getSelectColor() |
103 |
{ |
{ |
104 |
UIDefaults def = UIManager.getLookAndFeelDefaults(); |
return selectColor; |
|
return def.getColor(getPropertyPrefix() + ".select"); |
|
105 |
} |
} |
106 |
|
|
107 |
/** |
/** |
111 |
*/ |
*/ |
112 |
protected Color getDisabledTextColor() |
protected Color getDisabledTextColor() |
113 |
{ |
{ |
114 |
UIDefaults def = UIManager.getLookAndFeelDefaults(); |
return disabledTextColor; |
|
return def.getColor(getPropertyPrefix() + ".disabledText"); |
|
115 |
} |
} |
116 |
|
|
117 |
/** |
/** |
118 |
* Returns an instance of MetalButtonUI. |
* Returns a UI delegate for the specified component. |
119 |
* |
* |
120 |
* @param component a button for which a UI instance should be returned |
* @param c the component (should be a subclass of {@link AbstractButton}). |
121 |
|
* |
122 |
|
* @return A new instance of <code>MetalButtonUI</code>. |
123 |
*/ |
*/ |
124 |
public static ComponentUI createUI(JComponent component) |
public static ComponentUI createUI(JComponent c) { |
125 |
{ |
return new MetalButtonUI(); |
|
if (instance == null) |
|
|
instance = new MetalButtonUI(); |
|
|
return instance; |
|
126 |
} |
} |
127 |
|
|
128 |
/** |
/** |
129 |
* Install the Look & Feel defaults for Buttons. |
* Installs the default settings for the specified button. |
130 |
* |
* |
131 |
* @param button the button for which to install the Look & Feel |
* @param button the button. |
132 |
|
* |
133 |
|
* @see #uninstallDefaults(AbstractButton) |
134 |
*/ |
*/ |
135 |
public void installDefaults(AbstractButton button) |
public void installDefaults(AbstractButton button) |
136 |
{ |
{ |
137 |
super.installDefaults(button); |
super.installDefaults(button); |
138 |
|
if (button.isRolloverEnabled()) |
139 |
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
{ |
140 |
button.setFont(defaults.getFont("Button.font")); |
if (button.getBorder() instanceof UIResource) |
141 |
|
button.setBorder(MetalBorders.getRolloverBorder()); |
142 |
if (button.getParent() instanceof JToolBar) |
} |
143 |
button.setBorder(MetalBorders.getToolbarButtonBorder()); |
} |
144 |
|
|
145 |
|
/** |
146 |
|
* Removes the defaults added by {@link #installDefaults(AbstractButton)}. |
147 |
|
*/ |
148 |
|
public void uninstallDefaults(AbstractButton button) |
149 |
|
{ |
150 |
|
super.uninstallDefaults(button); |
151 |
|
if (button.getBorder() instanceof UIResource) |
152 |
|
button.setBorder(null); |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
|
* Returns a button listener for the specified button. |
157 |
|
* |
158 |
|
* @param button the button. |
159 |
|
* |
160 |
|
* @return A button listener. |
161 |
|
*/ |
162 |
|
protected BasicButtonListener createButtonListener(AbstractButton button) |
163 |
|
{ |
164 |
|
return new MetalButtonListener(button); |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
|
* Paints the background of the button to indicate that it is in the "pressed" |
169 |
|
* state. |
170 |
|
* |
171 |
|
* @param g the graphics context. |
172 |
|
* @param b the button. |
173 |
|
*/ |
174 |
|
protected void paintButtonPressed(Graphics g, AbstractButton b) |
175 |
|
{ |
176 |
|
if (b.isContentAreaFilled()) |
177 |
|
{ |
178 |
|
Rectangle area = b.getVisibleRect(); |
179 |
|
g.setColor(selectColor); |
180 |
|
g.fillRect(area.x, area.y, area.width, area.height); |
181 |
|
} |
182 |
|
} |
183 |
|
|
184 |
|
/** |
185 |
|
* Paints the focus rectangle around the button text and/or icon. |
186 |
|
* |
187 |
|
* @param g the graphics context. |
188 |
|
* @param b the button. |
189 |
|
* @param viewRect the button bounds. |
190 |
|
* @param textRect the text bounds. |
191 |
|
* @param iconRect the icon bounds. |
192 |
|
*/ |
193 |
|
protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, |
194 |
|
Rectangle textRect, Rectangle iconRect) { |
195 |
|
if (b.hasFocus() && b.isFocusPainted()) |
196 |
|
{ |
197 |
|
Color savedColor = g.getColor(); |
198 |
|
g.setColor(getFocusColor()); |
199 |
|
Rectangle focusRect = iconRect.union(textRect); |
200 |
|
g.drawRect(focusRect.x - 1, focusRect.y - 1, |
201 |
|
focusRect.width + 1, focusRect.height + 1); |
202 |
|
g.setColor(savedColor); |
203 |
|
} |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
|
* Paints the button text. |
208 |
|
* |
209 |
|
* @param g the graphics context. |
210 |
|
* @param c the button. |
211 |
|
* @param textRect the text bounds. |
212 |
|
* @param text the text to display. |
213 |
|
*/ |
214 |
|
protected void paintText(Graphics g, JComponent c, Rectangle textRect, |
215 |
|
String text) |
216 |
|
{ |
217 |
|
AbstractButton b = (AbstractButton) c; |
218 |
|
Font f = b.getFont(); |
219 |
|
g.setFont(f); |
220 |
|
FontMetrics fm = g.getFontMetrics(f); |
221 |
|
|
222 |
|
if (b.isEnabled()) |
223 |
|
{ |
224 |
|
g.setColor(b.getForeground()); |
225 |
|
g.drawString(text, textRect.x, textRect.y + fm.getAscent()); |
226 |
|
} |
227 |
|
else |
228 |
|
{ |
229 |
|
g.setColor(getDisabledTextColor()); |
230 |
|
g.drawString(text, textRect.x, textRect.y + fm.getAscent()); |
231 |
|
} |
232 |
} |
} |
|
|
|
233 |
} |
} |