blob: c6a8ce8ffccbe0f31714650421dec4ddaf3be0e9 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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
26package javax.swing.plaf.metal;
27
28import sun.swing.SwingUtilities2;
29import java.awt.*;
30import java.awt.event.*;
31import java.lang.ref.*;
32import java.util.*;
33import javax.swing.plaf.basic.BasicToggleButtonUI;
34
35import javax.swing.*;
36import javax.swing.border.*;
37import javax.swing.plaf.*;
38import javax.swing.*;
39
40import java.io.Serializable;
41
42/**
43 * MetalToggleButton implementation
44 * <p>
45 * <strong>Warning:</strong>
46 * Serialized objects of this class will not be compatible with
47 * future Swing releases. The current serialization support is
48 * appropriate for short term storage or RMI between applications running
49 * the same version of Swing. As of 1.4, support for long term storage
50 * of all JavaBeans<sup><font size="-2">TM</font></sup>
51 * has been added to the <code>java.beans</code> package.
52 * Please see {@link java.beans.XMLEncoder}.
53 *
54 * @author Tom Santos
55 */
56public class MetalToggleButtonUI extends BasicToggleButtonUI {
57
58 private static final MetalToggleButtonUI metalToggleButtonUI = new MetalToggleButtonUI();
59
60 protected Color focusColor;
61 protected Color selectColor;
62 protected Color disabledTextColor;
63
64 private boolean defaults_initialized = false;
65
66 // ********************************
67 // Create PLAF
68 // ********************************
69 public static ComponentUI createUI(JComponent b) {
70 return metalToggleButtonUI;
71 }
72
73 // ********************************
74 // Install Defaults
75 // ********************************
76 public void installDefaults(AbstractButton b) {
77 super.installDefaults(b);
78 if(!defaults_initialized) {
79 focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
80 selectColor = UIManager.getColor(getPropertyPrefix() + "select");
81 disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
82 defaults_initialized = true;
83 }
84 }
85
86 protected void uninstallDefaults(AbstractButton b) {
87 super.uninstallDefaults(b);
88 defaults_initialized = false;
89 }
90
91 // ********************************
92 // Default Accessors
93 // ********************************
94 protected Color getSelectColor() {
95 return selectColor;
96 }
97
98 protected Color getDisabledTextColor() {
99 return disabledTextColor;
100 }
101
102 protected Color getFocusColor() {
103 return focusColor;
104 }
105
106
107 // ********************************
108 // Paint Methods
109 // ********************************
110 /**
111 * If necessary paints the background of the component, then invokes
112 * <code>paint</code>.
113 *
114 * @param g Graphics to paint to
115 * @param c JComponent painting on
116 * @throws NullPointerException if <code>g</code> or <code>c</code> is
117 * null
118 * @see javax.swing.plaf.ComponentUI#update
119 * @see javax.swing.plaf.ComponentUI#paint
120 * @since 1.5
121 */
122 public void update(Graphics g, JComponent c) {
123 AbstractButton button = (AbstractButton)c;
124 if ((c.getBackground() instanceof UIResource) &&
125 button.isContentAreaFilled() && c.isEnabled()) {
126 ButtonModel model = button.getModel();
127 if (!MetalUtils.isToolBarButton(c)) {
128 if (!model.isArmed() && !model.isPressed() &&
129 MetalUtils.drawGradient(
130 c, g, "ToggleButton.gradient", 0, 0, c.getWidth(),
131 c.getHeight(), true)) {
132 paint(g, c);
133 return;
134 }
135 }
136 else if ((model.isRollover() || model.isSelected()) &&
137 MetalUtils.drawGradient(c, g, "ToggleButton.gradient",
138 0, 0, c.getWidth(), c.getHeight(), true)) {
139 paint(g, c);
140 return;
141 }
142 }
143 super.update(g, c);
144 }
145
146 protected void paintButtonPressed(Graphics g, AbstractButton b) {
147 if ( b.isContentAreaFilled() ) {
148 g.setColor(getSelectColor());
149 g.fillRect(0, 0, b.getWidth(), b.getHeight());
150 }
151 }
152
153 protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
154 AbstractButton b = (AbstractButton) c;
155 ButtonModel model = b.getModel();
156 FontMetrics fm = SwingUtilities2.getFontMetrics(b, g);
157 int mnemIndex = b.getDisplayedMnemonicIndex();
158
159 /* Draw the Text */
160 if(model.isEnabled()) {
161 /*** paint the text normally */
162 g.setColor(b.getForeground());
163 }
164 else {
165 /*** paint the text disabled ***/
166 if (model.isSelected()) {
167 g.setColor(c.getBackground());
168 } else {
169 g.setColor(getDisabledTextColor());
170 }
171 }
172 SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemIndex,
173 textRect.x, textRect.y + fm.getAscent());
174 }
175
176 protected void paintFocus(Graphics g, AbstractButton b,
177 Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
178
179 Rectangle focusRect = new Rectangle();
180 String text = b.getText();
181 boolean isIcon = b.getIcon() != null;
182
183 // If there is text
184 if ( text != null && !text.equals( "" ) ) {
185 if ( !isIcon ) {
186 focusRect.setBounds( textRect );
187 }
188 else {
189 focusRect.setBounds( iconRect.union( textRect ) );
190 }
191 }
192 // If there is an icon and no text
193 else if ( isIcon ) {
194 focusRect.setBounds( iconRect );
195 }
196
197 g.setColor(getFocusColor());
198 g.drawRect((focusRect.x-1), (focusRect.y-1),
199 focusRect.width+1, focusRect.height+1);
200
201 }
202
203 /**
204 * Paints the appropriate icon of the button <code>b</code> in the
205 * space <code>iconRect</code>.
206 *
207 * @param g Graphics to paint to
208 * @param b Button to render for
209 * @param iconRect space to render in
210 * @throws NullPointerException if any of the arguments are null.
211 * @since 1.5
212 */
213 protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
214 super.paintIcon(g, b, iconRect);
215 }
216}