Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / plaf / metal / MetalToggleButtonUI.java
blob8c7a46e3a86ee34e30c8865f773fdb7d7b314290
1 /* MetalToggleButtonUI.java
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.plaf.metal;
41 import java.awt.Color;
42 import java.awt.Font;
43 import java.awt.FontMetrics;
44 import java.awt.Graphics;
45 import java.awt.Rectangle;
47 import javax.swing.AbstractButton;
48 import javax.swing.ButtonModel;
49 import javax.swing.JComponent;
50 import javax.swing.JToggleButton;
51 import javax.swing.SwingConstants;
52 import javax.swing.SwingUtilities;
53 import javax.swing.UIManager;
54 import javax.swing.plaf.ComponentUI;
55 import javax.swing.plaf.UIResource;
56 import javax.swing.plaf.basic.BasicButtonUI;
57 import javax.swing.plaf.basic.BasicToggleButtonUI;
59 /**
60 * A UI delegate for the {@link JToggleButton} component.
62 public class MetalToggleButtonUI
63 extends BasicToggleButtonUI
66 /** The color for the focus border. */
67 protected Color focusColor;
69 /** The color that indicates a selected button. */
70 protected Color selectColor;
72 /** The color for disabled button labels. */
73 protected Color disabledTextColor;
75 /**
76 * Returns a new instance of <code>MetalToggleButtonUI</code>.
78 * @param component the component for which we return an UI instance
80 * @return A new instance of <code>MetalToggleButtonUI</code>.
82 public static ComponentUI createUI(JComponent component)
84 return new MetalToggleButtonUI();
87 /**
88 * Constructs a new instance of <code>MetalToggleButtonUI</code>.
90 public MetalToggleButtonUI()
92 super();
95 /**
96 * Returns the color for the focus border.
98 * @return the color for the focus border
100 protected Color getFocusColor()
102 return focusColor;
106 * Returns the color that indicates a selected button.
108 * @return the color that indicates a selected button
110 protected Color getSelectColor()
112 return selectColor;
116 * Returns the color for the text label of disabled buttons. The value
117 * is initialised in the {@link #installDefaults(AbstractButton)} method
118 * by reading the <code>ToggleButton.disabledText</code> item from the UI
119 * defaults.
121 * @return The color for the text label of disabled buttons.
123 protected Color getDisabledTextColor()
125 return disabledTextColor;
129 * Updates the button with the defaults for this look and feel.
131 * @param b the button.
133 public void installDefaults(AbstractButton b)
135 super.installDefaults(b);
136 focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
137 selectColor = UIManager.getColor(getPropertyPrefix() + "select");
138 disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
142 * Paints the button background when it is pressed/selected.
144 * @param g the graphics device.
145 * @param b the button.
147 protected void paintButtonPressed(Graphics g, AbstractButton b)
149 if (b.isContentAreaFilled() && b.isOpaque())
151 Color saved = g.getColor();
152 Rectangle bounds = SwingUtilities.getLocalBounds(b);
153 g.setColor(selectColor);
154 g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
155 g.setColor(saved);
160 * Paints the text for the button.
162 * As of JDK 1.4 this method is obsolete.
163 * Use {@link BasicButtonUI#paintText(java.awt.Graphics,
164 * javax.swing.AbstractButton, java.awt.Rectangle, java.lang.String)}.
166 * @param g the graphics device.
167 * @param c the component.
168 * @param textRect the bounds for the text.
169 * @param text the text.
172 protected void paintText(Graphics g, JComponent c, Rectangle textRect,
173 String text)
175 Font savedFont = g.getFont();
176 Color savedColor = g.getColor();
177 g.setFont(c.getFont());
178 if (c.isEnabled())
179 g.setColor(c.getForeground());
180 else
181 g.setColor(disabledTextColor);
182 FontMetrics fm = g.getFontMetrics(c.getFont());
183 int ascent = fm.getAscent();
184 g.drawString(text, textRect.x, textRect.y + ascent);
185 g.setFont(savedFont);
186 g.setColor(savedColor);
190 * Draws the focus highlight around the text and icon.
192 * @param g the graphics device.
193 * @param b the button.
195 protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
196 Rectangle textRect, Rectangle iconRect)
198 if (!b.hasFocus())
199 return;
200 Color saved = g.getColor();
201 g.setColor(focusColor);
202 Rectangle fr = iconRect.union(textRect);
203 g.drawRect(fr.x - 1, fr.y - 1, fr.width + 1, fr.height + 1);
204 g.setColor(saved);
208 * If the property <code>ToggleButton.gradient</code> is set, then a gradient
209 * is painted as background, otherwise the normal superclass behaviour is
210 * called.
212 public void update(Graphics g, JComponent c)
214 AbstractButton b = (AbstractButton) c;
215 ButtonModel m = b.getModel();
216 if (b.getBackground() instanceof UIResource
217 && b.isContentAreaFilled()
218 && b.isEnabled() && ! m.isArmed() && ! m.isPressed()
219 && UIManager.get(getPropertyPrefix() + "gradient") != null)
221 MetalUtils.paintGradient(g, 0, 0, c.getWidth(), c.getHeight(),
222 SwingConstants.VERTICAL,
223 getPropertyPrefix() + "gradient");
224 paint(g, c);
226 else
227 super.update(g, c);