Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / swing / plaf / metal / MetalToggleButtonUI.java
blob0b56d59144286ecbe81abc63c3bd5b4ff76d70c1
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.JComponent;
49 import javax.swing.JToggleButton;
50 import javax.swing.SwingConstants;
51 import javax.swing.SwingUtilities;
52 import javax.swing.UIManager;
53 import javax.swing.plaf.ComponentUI;
54 import javax.swing.plaf.basic.BasicButtonUI;
55 import javax.swing.plaf.basic.BasicToggleButtonUI;
57 /**
58 * A UI delegate for the {@link JToggleButton} component.
60 public class MetalToggleButtonUI
61 extends BasicToggleButtonUI
64 /** The color for the focus border. */
65 protected Color focusColor;
67 /** The color that indicates a selected button. */
68 protected Color selectColor;
70 /** The color for disabled button labels. */
71 protected Color disabledTextColor;
73 /**
74 * Returns a new instance of <code>MetalToggleButtonUI</code>.
76 * @param component the component for which we return an UI instance
78 * @return A new instance of <code>MetalToggleButtonUI</code>.
80 public static ComponentUI createUI(JComponent component)
82 return new MetalToggleButtonUI();
85 /**
86 * Constructs a new instance of <code>MetalToggleButtonUI</code>.
88 public MetalToggleButtonUI()
90 super();
93 /**
94 * Returns the color for the focus border.
96 * @return the color for the focus border
98 protected Color getFocusColor()
100 return focusColor;
104 * Returns the color that indicates a selected button.
106 * @return the color that indicates a selected button
108 protected Color getSelectColor()
110 return selectColor;
114 * Returns the color for the text label of disabled buttons. The value
115 * is initialised in the {@link #installDefaults(AbstractButton)} method
116 * by reading the <code>ToggleButton.disabledText</code> item from the UI
117 * defaults.
119 * @return The color for the text label of disabled buttons.
121 protected Color getDisabledTextColor()
123 return disabledTextColor;
127 * Updates the button with the defaults for this look and feel.
129 * @param b the button.
131 public void installDefaults(AbstractButton b)
133 super.installDefaults(b);
134 focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
135 selectColor = UIManager.getColor(getPropertyPrefix() + "select");
136 disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
140 * Paints the button background when it is pressed/selected.
142 * @param g the graphics device.
143 * @param b the button.
145 protected void paintButtonPressed(Graphics g, AbstractButton b)
147 if (b.isContentAreaFilled() && b.isOpaque())
149 Color saved = g.getColor();
150 Rectangle bounds = SwingUtilities.getLocalBounds(b);
151 g.setColor(selectColor);
152 g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
153 g.setColor(saved);
158 * Paints the text for the button.
160 * @param g the graphics device.
161 * @param c the component.
162 * @param textRect the bounds for the text.
163 * @param text the text.
165 * @deprecated 1.4 Use {@link BasicButtonUI#paintText(java.awt.Graphics,
166 * javax.swing.AbstractButton, java.awt.Rectangle, java.lang.String)}.
168 protected void paintText(Graphics g, JComponent c, Rectangle textRect,
169 String text)
171 Font savedFont = g.getFont();
172 Color savedColor = g.getColor();
173 g.setFont(c.getFont());
174 if (c.isEnabled())
175 g.setColor(c.getForeground());
176 else
177 g.setColor(disabledTextColor);
178 FontMetrics fm = g.getFontMetrics(c.getFont());
179 int ascent = fm.getAscent();
180 g.drawString(text, textRect.x, textRect.y + ascent);
181 g.setFont(savedFont);
182 g.setColor(savedColor);
186 * Draws the focus highlight around the text and icon.
188 * @param g the graphics device.
189 * @param b the button.
191 protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
192 Rectangle textRect, Rectangle iconRect)
194 if (!b.hasFocus())
195 return;
196 Color saved = g.getColor();
197 g.setColor(focusColor);
198 Rectangle fr = iconRect.union(textRect);
199 g.drawRect(fr.x - 1, fr.y - 1, fr.width + 1, fr.height + 1);
200 g.setColor(saved);
204 * If the property <code>ToggleButton.gradient</code> is set, then a gradient
205 * is painted as background, otherwise the normal superclass behaviour is
206 * called.
208 public void update(Graphics g, JComponent c)
210 if (c.isOpaque() && UIManager.get(getPropertyPrefix() + "gradient") != null)
212 MetalUtils.paintGradient(g, 0, 0, c.getWidth(), c.getHeight(),
213 SwingConstants.VERTICAL,
214 getPropertyPrefix() + "gradient");
215 paint(g, c);
217 else
218 super.update(g, c);