OSDN Git Service

64a1deca572705ed3acfd50523f3c220997bf2ea
[pf3gnuchains/gcc-fork.git] / libjava / classpath / javax / swing / plaf / basic / BasicRadioButtonUI.java
1 /* BasicRadioButtonUI.java
2    Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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.
20
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.
25
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. */
37
38
39 package javax.swing.plaf.basic;
40
41 import java.awt.Color;
42 import java.awt.Dimension;
43 import java.awt.Font;
44 import java.awt.Graphics;
45 import java.awt.Rectangle;
46
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;
54
55 /**
56  * The BasicLookAndFeel UI implementation for
57  * {@link javax.swing.JRadioButton}s.
58  */
59 public class BasicRadioButtonUI extends BasicToggleButtonUI
60 {
61   /**
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.
65    */
66   protected Icon icon;
67
68   /**
69    * Creates and returns a new instance of <code>BasicRadioButtonUI</code>.
70    *
71    * @return a new instance of <code>BasicRadioButtonUI</code>
72    */
73   public static ComponentUI createUI(final JComponent c)  {
74     return new BasicRadioButtonUI();
75   }
76
77   /**
78    * Creates a new instance of <code>BasicButtonUI</code>.
79    */
80   public BasicRadioButtonUI()
81   {
82     icon = getDefaultIcon();
83   }
84
85   /**
86    * Installs defaults from the Look &amp; Feel table on the specified
87    * button.
88    *
89    * @param b the button on which to install the defaults
90    */
91   protected void installDefaults(AbstractButton b)
92   {
93     super.installDefaults(b);
94     if (b.getIcon() == null)
95       b.setIcon(icon);
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);
102   }
103
104   /**
105    * Returns the prefix used for UIDefaults properties. This is
106    * <code>RadioButton</code> in this case.
107    *
108    * @return the prefix used for UIDefaults properties
109    */
110   protected String getPropertyPrefix()
111   {
112     return "RadioButton.";
113   }
114
115   /**
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.
120    *
121    * @return the default icon for JRadioButtons
122    */
123   public Icon getDefaultIcon()
124   {
125     return UIManager.getIcon(getPropertyPrefix() + "icon");
126   }
127
128   /**
129    * Paints the RadioButton.
130    *
131    * @param g the Graphics context to paint with
132    * @param c the button to paint
133    */
134   public void paint(Graphics g, JComponent c)
135   {
136     AbstractButton b = (AbstractButton) c;
137
138     Rectangle tr = new Rectangle();
139     Rectangle ir = new Rectangle();
140     Rectangle vr = new Rectangle();
141
142     Font f = c.getFont();
143
144     g.setFont(f);
145
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();
156
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);
163     
164     if (currentIcon != null)
165       {
166         currentIcon.paintIcon(c, g, ir.x, ir.y);
167       }
168     if (text != null)
169       paintText(g, b, tr, text);
170     if (b.hasFocus() && b.isFocusPainted() && m.isEnabled())
171       paintFocus(g, tr, c.getSize());
172   }
173
174   /**
175    * Paints the focus indicator for JRadioButtons.
176    *
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.
180    */
181   protected void paintFocus(Graphics g, Rectangle tr, Dimension size)
182   {
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);
187     g.setColor(saved);
188   }
189 }