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)
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
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
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.Component;
44 import java.awt.FontMetrics;
45 import java.awt.Graphics;
46 import java.awt.Rectangle;
48 import javax.swing.AbstractButton;
49 import javax.swing.ButtonModel;
50 import javax.swing.JButton;
51 import javax.swing.JComponent;
52 import javax.swing.JToolBar;
53 import javax.swing.SwingConstants;
54 import javax.swing.UIManager;
55 import javax.swing.plaf.ComponentUI;
56 import javax.swing.plaf.UIResource;
57 import javax.swing.plaf.basic.BasicButtonUI;
60 * A UI delegate for the {@link JButton} component.
62 * @author Roman Kennke (roman@kennke.org)
64 public class MetalButtonUI
69 * The shared button UI.
71 private static MetalButtonUI sharedUI;
74 * The color used to draw the focus rectangle around the text and/or icon.
76 protected Color focusColor;
79 * The background color for the button when it is pressed.
81 protected Color selectColor;
84 * The color for disabled button labels.
86 protected Color disabledTextColor;
89 * Returns a UI delegate for the specified component.
91 * @param c the component (should be a subclass of {@link AbstractButton}).
93 * @return A new instance of <code>MetalButtonUI</code>.
95 public static ComponentUI createUI(JComponent c)
98 sharedUI = new MetalButtonUI();
103 * Creates a new instance.
105 public MetalButtonUI()
111 * Returns the color for the focus border.
113 * @return the color for the focus border
115 protected Color getFocusColor()
117 focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
122 * Returns the color that indicates a selected button.
124 * @return the color that indicates a selected button
126 protected Color getSelectColor()
128 selectColor = UIManager.getColor(getPropertyPrefix() + "select");
133 * Returns the color for the text label of disabled buttons.
135 * @return the color for the text label of disabled buttons
137 protected Color getDisabledTextColor()
139 disabledTextColor = UIManager.getColor(getPropertyPrefix()
141 return disabledTextColor;
145 * Installs the default settings for the specified button.
147 * @param button the button.
149 * @see #uninstallDefaults(AbstractButton)
151 public void installDefaults(AbstractButton button)
153 // This is overridden to be public, for whatever reason.
154 super.installDefaults(button);
158 * Removes the defaults added by {@link #installDefaults(AbstractButton)}.
160 public void uninstallDefaults(AbstractButton button)
162 // This is overridden to be public, for whatever reason.
163 super.uninstallDefaults(button);
167 * Paints the background of the button to indicate that it is in the
170 * @param g the graphics context.
171 * @param b the button.
173 protected void paintButtonPressed(Graphics g, AbstractButton b)
175 if (b.isContentAreaFilled())
177 Rectangle area = b.getVisibleRect();
178 g.setColor(getSelectColor());
179 g.fillRect(area.x, area.y, area.width, area.height);
184 * Paints the focus rectangle around the button text and/or icon.
186 * @param g the graphics context.
187 * @param b the button.
188 * @param viewRect the button bounds.
189 * @param textRect the text bounds.
190 * @param iconRect the icon bounds.
192 protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
193 Rectangle textRect, Rectangle iconRect)
195 if (b.isEnabled() && b.hasFocus() && b.isFocusPainted())
197 Color savedColor = g.getColor();
198 g.setColor(getFocusColor());
199 Rectangle focusRect = iconRect.union(textRect);
200 g.drawRect(focusRect.x - 1, focusRect.y,
201 focusRect.width + 1, focusRect.height);
202 g.setColor(savedColor);
207 * Paints the button text.
209 * @param g the graphics context.
210 * @param c the button.
211 * @param textRect the text bounds.
212 * @param text the text to display.
214 protected void paintText(Graphics g, JComponent c, Rectangle textRect,
217 AbstractButton b = (AbstractButton) c;
218 Font f = b.getFont();
220 FontMetrics fm = g.getFontMetrics(f);
224 g.setColor(b.getForeground());
225 g.drawString(text, textRect.x, textRect.y + fm.getAscent());
229 g.setColor(getDisabledTextColor());
230 g.drawString(text, textRect.x, textRect.y + fm.getAscent());
235 * If the property <code>Button.gradient</code> is set, then a gradient is
236 * painted as background, otherwise the normal superclass behaviour is
239 public void update(Graphics g, JComponent c)
241 AbstractButton b = (AbstractButton) c;
242 if ((b.getBackground() instanceof UIResource)
243 && b.isContentAreaFilled() && b.isEnabled())
245 ButtonModel m = b.getModel();
246 String uiKey = "Button.gradient";
247 if (! isToolbarButton(b))
249 if (! m.isArmed() && ! m.isPressed() && isDrawingGradient(uiKey))
251 MetalUtils.paintGradient(g, 0, 0, b.getWidth(), b.getHeight(),
252 SwingConstants.VERTICAL,
258 else if (m.isRollover() && isDrawingGradient(uiKey))
260 MetalUtils.paintGradient(g, 0, 0, b.getWidth(), b.getHeight(),
261 SwingConstants.VERTICAL,
267 // Fallback if we didn't have any of the two above cases.
272 * Returns <code>true</code> when the button is a toolbar button,
273 * <code>false</code> otherwise.
275 * @param b the button component to test
277 * @return <code>true</code> when the button is a toolbar button,
278 * <code>false</code> otherwise
280 private boolean isToolbarButton(Component b)
282 Component parent = b.getParent();
283 return parent instanceof JToolBar;
287 * Returns <code>true</code> if we should draw the button gradient,
288 * <code>false</code> otherwise.
290 * @param uiKey the UIManager key for the gradient
292 * @return <code>true</code> if we should draw the button gradient,
293 * <code>false</code> otherwise
295 private boolean isDrawingGradient(String uiKey)
297 return (UIManager.get(uiKey) != null);