1 /* BasicRadioButtonUI.java
2 Copyright (C) 2002, 2004, 2006, 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.basic;
41 import java.awt.Color;
42 import java.awt.Dimension;
44 import java.awt.Graphics;
45 import java.awt.Insets;
46 import java.awt.Rectangle;
48 import javax.swing.AbstractButton;
49 import javax.swing.ButtonModel;
50 import javax.swing.Icon;
51 import javax.swing.JComponent;
52 import javax.swing.SwingUtilities;
53 import javax.swing.UIManager;
54 import javax.swing.plaf.ComponentUI;
55 import javax.swing.text.View;
58 * The BasicLookAndFeel UI implementation for
59 * {@link javax.swing.JRadioButton}s.
61 public class BasicRadioButtonUI extends BasicToggleButtonUI
64 * The default icon for JRadioButtons. The default icon displays the usual
65 * RadioButton and is sensible to the selection state of the button,
66 * and can be used both as normal icon as well as selectedIcon.
71 * Creates and returns a new instance of <code>BasicRadioButtonUI</code>.
73 * @return a new instance of <code>BasicRadioButtonUI</code>
75 public static ComponentUI createUI(final JComponent c)
77 return new BasicRadioButtonUI();
81 * Creates a new instance of <code>BasicButtonUI</code>.
83 public BasicRadioButtonUI()
89 * Installs defaults from the Look & Feel table on the specified
92 * @param b the button on which to install the defaults
94 protected void installDefaults(AbstractButton b)
96 super.installDefaults(b);
97 icon = UIManager.getIcon(getPropertyPrefix() + "icon");
101 * Returns the prefix used for UIDefaults properties. This is
102 * <code>RadioButton</code> in this case.
104 * @return the prefix used for UIDefaults properties
106 protected String getPropertyPrefix()
108 return "RadioButton.";
112 * Returns the default icon for JRadioButtons.
113 * The default icon displays the usual
114 * RadioButton and is sensible to the selection state of the button,
115 * and can be used both as normal icon as well as selectedIcon.
117 * @return the default icon for JRadioButtons
119 public Icon getDefaultIcon()
125 * Paints the RadioButton.
127 * @param g the Graphics context to paint with
128 * @param c the button to paint
130 public void paint(Graphics g, JComponent c)
132 AbstractButton b = (AbstractButton) c;
133 Dimension size = c.getSize();
134 Insets i = b.getInsets();
145 viewR.width = size.width - i.left - i.right;
146 viewR.height = size.height - i.top - i.bottom;
148 Font f = c.getFont();
152 ButtonModel m = b.getModel();
154 // This is the icon that we use for layout.
155 Icon icon = b.getIcon();
157 icon = getDefaultIcon();
160 String text = SwingUtilities.layoutCompoundLabel(c, g.getFontMetrics(f),
162 b.getVerticalAlignment(), b.getHorizontalAlignment(),
163 b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
164 viewR, iconR, textR, b.getIconTextGap());
166 // Figure out the correct icon.
169 icon = getDefaultIcon();
175 icon = b.getDisabledSelectedIcon();
177 icon = b.getDisabledIcon();
179 else if (m.isArmed() && m.isPressed())
181 icon = b.getPressedIcon();
183 icon = b.getSelectedIcon();
185 else if (m.isSelected())
187 if (b.isRolloverEnabled() && m.isRollover())
189 icon = b.getRolloverSelectedIcon();
191 icon = b.getSelectedIcon();
194 icon = b.getSelectedIcon();
196 else if (b.isRolloverEnabled() && m.isRollover())
197 icon = b.getRolloverIcon();
202 icon.paintIcon(c, g, iconR.x, iconR.y);
204 // Paint text and focus indicator.
207 // Maybe render HTML in the radio button.
208 View v = (View) c.getClientProperty(BasicHTML.propertyKey);
212 paintText(g, b, textR, text);
214 // Paint focus indicator if necessary.
215 if (b.hasFocus() && b.isFocusPainted()
216 && textR.width > 0 && textR.height > 0)
217 paintFocus(g, textR, size);
221 public Dimension getPreferredSize(JComponent c)
223 // This is basically the same code as in
224 // BasicGraphicsUtils.getPreferredButtonSize() but takes the default icon
225 // property into account. JRadioButton and subclasses always have an icon:
226 // the check box. If the user explicitly changes it with setIcon() that
227 // one will be used for layout calculations and painting instead.
228 // The other icon properties are ignored.
229 AbstractButton b = (AbstractButton) c;
231 Insets insets = b.getInsets();
233 String text = b.getText();
234 Icon i = b.getIcon();
236 i = getDefaultIcon();
248 viewR.width = Short.MAX_VALUE;
249 viewR.height = Short.MAX_VALUE;
251 SwingUtilities.layoutCompoundLabel(b, // for the component orientation
252 b.getFontMetrics(b.getFont()),
253 text, i, b.getVerticalAlignment(),
254 b.getHorizontalAlignment(),
255 b.getVerticalTextPosition(),
256 b.getHorizontalTextPosition(),
258 text == null ? 0 : b.getIconTextGap());
260 Rectangle r = SwingUtilities.computeUnion(textR.x, textR.y, textR.width,
261 textR.height, iconR);
263 return new Dimension(insets.left + r.width + insets.right,
264 insets.top + r.height + insets.bottom);
268 * Paints the focus indicator for JRadioButtons.
270 * @param g the graphics context
271 * @param tr the rectangle for the text label
272 * @param size the size of the <code>JRadioButton</code> component.
274 protected void paintFocus(Graphics g, Rectangle tr, Dimension size)
276 Color focusColor = UIManager.getColor(getPropertyPrefix() + ".focus");
277 Color saved = g.getColor();
278 g.setColor(focusColor);
279 g.drawRect(tr.x, tr.y, tr.width, tr.height);