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.Rectangle;
47 import javax.swing.AbstractButton;
48 import javax.swing.ButtonModel;
49 import javax.swing.Icon;
50 import javax.swing.JComponent;
51 import javax.swing.SwingUtilities;
52 import javax.swing.UIManager;
53 import javax.swing.plaf.ComponentUI;
56 * The BasicLookAndFeel UI implementation for
57 * {@link javax.swing.JRadioButton}s.
59 public class BasicRadioButtonUI extends BasicToggleButtonUI
62 * The default icon for JRadioButtons. The default icon displays the usual
63 * RadioButton and is sensible to the selection state of the button,
64 * and can be used both as normal icon as well as selectedIcon.
69 * Creates and returns a new instance of <code>BasicRadioButtonUI</code>.
71 * @return a new instance of <code>BasicRadioButtonUI</code>
73 public static ComponentUI createUI(final JComponent c) {
74 return new BasicRadioButtonUI();
78 * Creates a new instance of <code>BasicButtonUI</code>.
80 public BasicRadioButtonUI()
82 icon = getDefaultIcon();
86 * Installs defaults from the Look & Feel table on the specified
89 * @param b the button on which to install the defaults
91 protected void installDefaults(AbstractButton b)
93 super.installDefaults(b);
94 if (b.getIcon() == null)
96 if (b.getSelectedIcon() == null)
97 b.setSelectedIcon(icon);
98 if (b.getDisabledIcon() == null)
99 b.setDisabledIcon(icon);
100 if (b.getDisabledSelectedIcon() == null)
101 b.setDisabledSelectedIcon(icon);
105 * Returns the prefix used for UIDefaults properties. This is
106 * <code>RadioButton</code> in this case.
108 * @return the prefix used for UIDefaults properties
110 protected String getPropertyPrefix()
112 return "RadioButton.";
116 * Returns the default icon for JRadioButtons.
117 * The default icon displays the usual
118 * RadioButton and is sensible to the selection state of the button,
119 * and can be used both as normal icon as well as selectedIcon.
121 * @return the default icon for JRadioButtons
123 public Icon getDefaultIcon()
125 return UIManager.getIcon(getPropertyPrefix() + "icon");
129 * Paints the RadioButton.
131 * @param g the Graphics context to paint with
132 * @param c the button to paint
134 public void paint(Graphics g, JComponent c)
136 AbstractButton b = (AbstractButton) c;
138 Rectangle tr = new Rectangle();
139 Rectangle ir = new Rectangle();
140 Rectangle vr = new Rectangle();
142 Font f = c.getFont();
146 ButtonModel m = b.getModel();
147 Icon currentIcon = null;
148 if (m.isSelected() && m.isEnabled())
149 currentIcon = b.getSelectedIcon();
150 else if (! m.isSelected() && m.isEnabled())
151 currentIcon = b.getIcon();
152 else if (m.isSelected() && ! m.isEnabled())
153 currentIcon = b.getDisabledSelectedIcon();
154 else // (!m.isSelected() && ! m.isEnabled())
155 currentIcon = b.getDisabledIcon();
157 SwingUtilities.calculateInnerArea(b, vr);
158 String text = SwingUtilities.layoutCompoundLabel
159 (c, g.getFontMetrics(f), b.getText(), currentIcon,
160 b.getVerticalAlignment(), b.getHorizontalAlignment(),
161 b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
162 vr, ir, tr, b.getIconTextGap() + defaultTextShiftOffset);
164 if (currentIcon != null)
166 currentIcon.paintIcon(c, g, ir.x, ir.y);
169 paintText(g, b, tr, text);
170 if (b.hasFocus() && b.isFocusPainted() && m.isEnabled())
171 paintFocus(g, tr, c.getSize());
175 * Paints the focus indicator for JRadioButtons.
177 * @param g the graphics context
178 * @param tr the rectangle for the text label
179 * @param size the size of the <code>JRadioButton</code> component.
181 protected void paintFocus(Graphics g, Rectangle tr, Dimension size)
183 Color focusColor = UIManager.getColor(getPropertyPrefix() + ".focus");
184 Color saved = g.getColor();
185 g.setColor(focusColor);
186 g.drawRect(tr.x, tr.y, tr.width, tr.height);