001 /*
002 * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.plaf.metal;
027
028 import sun.swing.SwingUtilities2;
029 import java.awt.*;
030 import java.awt.event.*;
031 import java.lang.ref.*;
032 import java.util.*;
033 import javax.swing.plaf.basic.BasicToggleButtonUI;
034
035 import javax.swing.*;
036 import javax.swing.border.*;
037 import javax.swing.plaf.*;
038 import javax.swing.*;
039
040 import java.io.Serializable;
041
042 /**
043 * MetalToggleButton implementation
044 * <p>
045 * <strong>Warning:</strong>
046 * Serialized objects of this class will not be compatible with
047 * future Swing releases. The current serialization support is
048 * appropriate for short term storage or RMI between applications running
049 * the same version of Swing. As of 1.4, support for long term storage
050 * of all JavaBeans<sup><font size="-2">TM</font></sup>
051 * has been added to the <code>java.beans</code> package.
052 * Please see {@link java.beans.XMLEncoder}.
053 *
054 * @version 1.36 05/05/07
055 * @author Tom Santos
056 */
057 public class MetalToggleButtonUI extends BasicToggleButtonUI {
058
059 private static final MetalToggleButtonUI metalToggleButtonUI = new MetalToggleButtonUI();
060
061 protected Color focusColor;
062 protected Color selectColor;
063 protected Color disabledTextColor;
064
065 private boolean defaults_initialized = false;
066
067 // ********************************
068 // Create PLAF
069 // ********************************
070 public static ComponentUI createUI(JComponent b) {
071 return metalToggleButtonUI;
072 }
073
074 // ********************************
075 // Install Defaults
076 // ********************************
077 public void installDefaults(AbstractButton b) {
078 super .installDefaults(b);
079 if (!defaults_initialized) {
080 focusColor = UIManager.getColor(getPropertyPrefix()
081 + "focus");
082 selectColor = UIManager.getColor(getPropertyPrefix()
083 + "select");
084 disabledTextColor = UIManager.getColor(getPropertyPrefix()
085 + "disabledText");
086 defaults_initialized = true;
087 }
088 }
089
090 protected void uninstallDefaults(AbstractButton b) {
091 super .uninstallDefaults(b);
092 defaults_initialized = false;
093 }
094
095 // ********************************
096 // Default Accessors
097 // ********************************
098 protected Color getSelectColor() {
099 return selectColor;
100 }
101
102 protected Color getDisabledTextColor() {
103 return disabledTextColor;
104 }
105
106 protected Color getFocusColor() {
107 return focusColor;
108 }
109
110 // ********************************
111 // Paint Methods
112 // ********************************
113 /**
114 * If necessary paints the background of the component, then invokes
115 * <code>paint</code>.
116 *
117 * @param g Graphics to paint to
118 * @param c JComponent painting on
119 * @throws NullPointerException if <code>g</code> or <code>c</code> is
120 * null
121 * @see javax.swing.plaf.ComponentUI#update
122 * @see javax.swing.plaf.ComponentUI#paint
123 * @since 1.5
124 */
125 public void update(Graphics g, JComponent c) {
126 AbstractButton button = (AbstractButton) c;
127 if ((c.getBackground() instanceof UIResource)
128 && button.isContentAreaFilled() && c.isEnabled()) {
129 ButtonModel model = button.getModel();
130 if (!MetalUtils.isToolBarButton(c)) {
131 if (!model.isArmed()
132 && !model.isPressed()
133 && MetalUtils.drawGradient(c, g,
134 "ToggleButton.gradient", 0, 0, c
135 .getWidth(), c.getHeight(),
136 true)) {
137 paint(g, c);
138 return;
139 }
140 } else if ((model.isRollover() || model.isSelected())
141 && MetalUtils.drawGradient(c, g,
142 "ToggleButton.gradient", 0, 0,
143 c.getWidth(), c.getHeight(), true)) {
144 paint(g, c);
145 return;
146 }
147 }
148 super .update(g, c);
149 }
150
151 protected void paintButtonPressed(Graphics g, AbstractButton b) {
152 if (b.isContentAreaFilled()) {
153 g.setColor(getSelectColor());
154 g.fillRect(0, 0, b.getWidth(), b.getHeight());
155 }
156 }
157
158 protected void paintText(Graphics g, JComponent c,
159 Rectangle textRect, String text) {
160 AbstractButton b = (AbstractButton) c;
161 ButtonModel model = b.getModel();
162 FontMetrics fm = SwingUtilities2.getFontMetrics(b, g);
163 int mnemIndex = b.getDisplayedMnemonicIndex();
164
165 /* Draw the Text */
166 if (model.isEnabled()) {
167 /*** paint the text normally */
168 g.setColor(b.getForeground());
169 } else {
170 /*** paint the text disabled ***/
171 if (model.isSelected()) {
172 g.setColor(c.getBackground());
173 } else {
174 g.setColor(getDisabledTextColor());
175 }
176 }
177 SwingUtilities2.drawStringUnderlineCharAt(c, g, text,
178 mnemIndex, textRect.x, textRect.y + fm.getAscent());
179 }
180
181 protected void paintFocus(Graphics g, AbstractButton b,
182 Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
183
184 Rectangle focusRect = new Rectangle();
185 String text = b.getText();
186 boolean isIcon = b.getIcon() != null;
187
188 // If there is text
189 if (text != null && !text.equals("")) {
190 if (!isIcon) {
191 focusRect.setBounds(textRect);
192 } else {
193 focusRect.setBounds(iconRect.union(textRect));
194 }
195 }
196 // If there is an icon and no text
197 else if (isIcon) {
198 focusRect.setBounds(iconRect);
199 }
200
201 g.setColor(getFocusColor());
202 g.drawRect((focusRect.x - 1), (focusRect.y - 1),
203 focusRect.width + 1, focusRect.height + 1);
204
205 }
206
207 /**
208 * Paints the appropriate icon of the button <code>b</code> in the
209 * space <code>iconRect</code>.
210 *
211 * @param g Graphics to paint to
212 * @param b Button to render for
213 * @param iconRect space to render in
214 * @throws NullPointerException if any of the arguments are null.
215 * @since 1.5
216 */
217 protected void paintIcon(Graphics g, AbstractButton b,
218 Rectangle iconRect) {
219 super.paintIcon(g, b, iconRect);
220 }
221 }
|