2
|
1 |
/*
|
|
2 |
* Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.swing.plaf.metal;
|
|
27 |
|
|
28 |
import sun.swing.SwingUtilities2;
|
4225
|
29 |
import sun.awt.AppContext;
|
|
30 |
|
2
|
31 |
import java.awt.*;
|
|
32 |
import java.awt.event.*;
|
|
33 |
import java.lang.ref.*;
|
|
34 |
import java.util.*;
|
|
35 |
import javax.swing.plaf.basic.BasicToggleButtonUI;
|
|
36 |
|
|
37 |
import javax.swing.*;
|
|
38 |
import javax.swing.border.*;
|
|
39 |
import javax.swing.plaf.*;
|
|
40 |
import javax.swing.*;
|
|
41 |
|
|
42 |
import java.io.Serializable;
|
|
43 |
|
|
44 |
/**
|
|
45 |
* MetalToggleButton implementation
|
|
46 |
* <p>
|
|
47 |
* <strong>Warning:</strong>
|
|
48 |
* Serialized objects of this class will not be compatible with
|
|
49 |
* future Swing releases. The current serialization support is
|
|
50 |
* appropriate for short term storage or RMI between applications running
|
|
51 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
52 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
53 |
* has been added to the <code>java.beans</code> package.
|
|
54 |
* Please see {@link java.beans.XMLEncoder}.
|
|
55 |
*
|
|
56 |
* @author Tom Santos
|
|
57 |
*/
|
|
58 |
public class MetalToggleButtonUI extends BasicToggleButtonUI {
|
|
59 |
|
4225
|
60 |
private static final Object METAL_TOGGLE_BUTTON_UI_KEY = new Object();
|
2
|
61 |
|
|
62 |
protected Color focusColor;
|
|
63 |
protected Color selectColor;
|
|
64 |
protected Color disabledTextColor;
|
|
65 |
|
|
66 |
private boolean defaults_initialized = false;
|
|
67 |
|
|
68 |
// ********************************
|
|
69 |
// Create PLAF
|
|
70 |
// ********************************
|
|
71 |
public static ComponentUI createUI(JComponent b) {
|
4225
|
72 |
AppContext appContext = AppContext.getAppContext();
|
|
73 |
MetalToggleButtonUI metalToggleButtonUI =
|
|
74 |
(MetalToggleButtonUI) appContext.get(METAL_TOGGLE_BUTTON_UI_KEY);
|
|
75 |
if (metalToggleButtonUI == null) {
|
|
76 |
metalToggleButtonUI = new MetalToggleButtonUI();
|
|
77 |
appContext.put(METAL_TOGGLE_BUTTON_UI_KEY, metalToggleButtonUI);
|
|
78 |
}
|
2
|
79 |
return metalToggleButtonUI;
|
|
80 |
}
|
|
81 |
|
|
82 |
// ********************************
|
|
83 |
// Install Defaults
|
|
84 |
// ********************************
|
|
85 |
public void installDefaults(AbstractButton b) {
|
|
86 |
super.installDefaults(b);
|
|
87 |
if(!defaults_initialized) {
|
|
88 |
focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
|
|
89 |
selectColor = UIManager.getColor(getPropertyPrefix() + "select");
|
|
90 |
disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
|
|
91 |
defaults_initialized = true;
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
protected void uninstallDefaults(AbstractButton b) {
|
|
96 |
super.uninstallDefaults(b);
|
|
97 |
defaults_initialized = false;
|
|
98 |
}
|
|
99 |
|
|
100 |
// ********************************
|
|
101 |
// Default Accessors
|
|
102 |
// ********************************
|
|
103 |
protected Color getSelectColor() {
|
|
104 |
return selectColor;
|
|
105 |
}
|
|
106 |
|
|
107 |
protected Color getDisabledTextColor() {
|
|
108 |
return disabledTextColor;
|
|
109 |
}
|
|
110 |
|
|
111 |
protected Color getFocusColor() {
|
|
112 |
return focusColor;
|
|
113 |
}
|
|
114 |
|
|
115 |
|
|
116 |
// ********************************
|
|
117 |
// Paint Methods
|
|
118 |
// ********************************
|
|
119 |
/**
|
|
120 |
* If necessary paints the background of the component, then invokes
|
|
121 |
* <code>paint</code>.
|
|
122 |
*
|
|
123 |
* @param g Graphics to paint to
|
|
124 |
* @param c JComponent painting on
|
|
125 |
* @throws NullPointerException if <code>g</code> or <code>c</code> is
|
|
126 |
* null
|
|
127 |
* @see javax.swing.plaf.ComponentUI#update
|
|
128 |
* @see javax.swing.plaf.ComponentUI#paint
|
|
129 |
* @since 1.5
|
|
130 |
*/
|
|
131 |
public void update(Graphics g, JComponent c) {
|
|
132 |
AbstractButton button = (AbstractButton)c;
|
|
133 |
if ((c.getBackground() instanceof UIResource) &&
|
|
134 |
button.isContentAreaFilled() && c.isEnabled()) {
|
|
135 |
ButtonModel model = button.getModel();
|
|
136 |
if (!MetalUtils.isToolBarButton(c)) {
|
|
137 |
if (!model.isArmed() && !model.isPressed() &&
|
|
138 |
MetalUtils.drawGradient(
|
|
139 |
c, g, "ToggleButton.gradient", 0, 0, c.getWidth(),
|
|
140 |
c.getHeight(), true)) {
|
|
141 |
paint(g, c);
|
|
142 |
return;
|
|
143 |
}
|
|
144 |
}
|
|
145 |
else if ((model.isRollover() || model.isSelected()) &&
|
|
146 |
MetalUtils.drawGradient(c, g, "ToggleButton.gradient",
|
|
147 |
0, 0, c.getWidth(), c.getHeight(), true)) {
|
|
148 |
paint(g, c);
|
|
149 |
return;
|
|
150 |
}
|
|
151 |
}
|
|
152 |
super.update(g, c);
|
|
153 |
}
|
|
154 |
|
|
155 |
protected void paintButtonPressed(Graphics g, AbstractButton b) {
|
|
156 |
if ( b.isContentAreaFilled() ) {
|
|
157 |
g.setColor(getSelectColor());
|
|
158 |
g.fillRect(0, 0, b.getWidth(), b.getHeight());
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
|
|
163 |
AbstractButton b = (AbstractButton) c;
|
|
164 |
ButtonModel model = b.getModel();
|
|
165 |
FontMetrics fm = SwingUtilities2.getFontMetrics(b, g);
|
|
166 |
int mnemIndex = b.getDisplayedMnemonicIndex();
|
|
167 |
|
|
168 |
/* Draw the Text */
|
|
169 |
if(model.isEnabled()) {
|
|
170 |
/*** paint the text normally */
|
|
171 |
g.setColor(b.getForeground());
|
|
172 |
}
|
|
173 |
else {
|
|
174 |
/*** paint the text disabled ***/
|
|
175 |
if (model.isSelected()) {
|
|
176 |
g.setColor(c.getBackground());
|
|
177 |
} else {
|
|
178 |
g.setColor(getDisabledTextColor());
|
|
179 |
}
|
|
180 |
}
|
|
181 |
SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemIndex,
|
|
182 |
textRect.x, textRect.y + fm.getAscent());
|
|
183 |
}
|
|
184 |
|
|
185 |
protected void paintFocus(Graphics g, AbstractButton b,
|
|
186 |
Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
|
|
187 |
|
|
188 |
Rectangle focusRect = new Rectangle();
|
|
189 |
String text = b.getText();
|
|
190 |
boolean isIcon = b.getIcon() != null;
|
|
191 |
|
|
192 |
// If there is text
|
|
193 |
if ( text != null && !text.equals( "" ) ) {
|
|
194 |
if ( !isIcon ) {
|
|
195 |
focusRect.setBounds( textRect );
|
|
196 |
}
|
|
197 |
else {
|
|
198 |
focusRect.setBounds( iconRect.union( textRect ) );
|
|
199 |
}
|
|
200 |
}
|
|
201 |
// If there is an icon and no text
|
|
202 |
else if ( isIcon ) {
|
|
203 |
focusRect.setBounds( iconRect );
|
|
204 |
}
|
|
205 |
|
|
206 |
g.setColor(getFocusColor());
|
|
207 |
g.drawRect((focusRect.x-1), (focusRect.y-1),
|
|
208 |
focusRect.width+1, focusRect.height+1);
|
|
209 |
|
|
210 |
}
|
|
211 |
|
|
212 |
/**
|
|
213 |
* Paints the appropriate icon of the button <code>b</code> in the
|
|
214 |
* space <code>iconRect</code>.
|
|
215 |
*
|
|
216 |
* @param g Graphics to paint to
|
|
217 |
* @param b Button to render for
|
|
218 |
* @param iconRect space to render in
|
|
219 |
* @throws NullPointerException if any of the arguments are null.
|
|
220 |
* @since 1.5
|
|
221 |
*/
|
|
222 |
protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
|
|
223 |
super.paintIcon(g, b, iconRect);
|
|
224 |
}
|
|
225 |
}
|