OSDN Git Service

Merged gcj-eclipse branch to trunk.
[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.Insets;
46 import java.awt.Rectangle;
47
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;
56
57 /**
58  * The BasicLookAndFeel UI implementation for
59  * {@link javax.swing.JRadioButton}s.
60  */
61 public class BasicRadioButtonUI extends BasicToggleButtonUI
62 {
63   /**
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.
67    */
68   protected Icon icon;
69
70   /**
71    * Creates and returns a new instance of <code>BasicRadioButtonUI</code>.
72    *
73    * @return a new instance of <code>BasicRadioButtonUI</code>
74    */
75   public static ComponentUI createUI(final JComponent c)  
76   {
77     return new BasicRadioButtonUI();
78   }
79
80   /**
81    * Creates a new instance of <code>BasicButtonUI</code>.
82    */
83   public BasicRadioButtonUI()
84   {
85     // nothing to do
86   }
87
88   /**
89    * Installs defaults from the Look &amp; Feel table on the specified
90    * button.
91    *
92    * @param b the button on which to install the defaults
93    */
94   protected void installDefaults(AbstractButton b)
95   {
96     super.installDefaults(b);
97     icon = UIManager.getIcon(getPropertyPrefix() + "icon");
98   }
99
100   /**
101    * Returns the prefix used for UIDefaults properties. This is
102    * <code>RadioButton</code> in this case.
103    *
104    * @return the prefix used for UIDefaults properties
105    */
106   protected String getPropertyPrefix()
107   {
108     return "RadioButton.";
109   }
110
111   /**
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.
116    *
117    * @return the default icon for JRadioButtons
118    */
119   public Icon getDefaultIcon()
120   {
121     return icon;
122   }
123
124   /**
125    * Paints the RadioButton.
126    *
127    * @param g the Graphics context to paint with
128    * @param c the button to paint
129    */
130   public void paint(Graphics g, JComponent c)
131   {
132     AbstractButton b = (AbstractButton) c;
133     Dimension size = c.getSize();
134     Insets i = b.getInsets();
135     textR.x = 0;
136     textR.y = 0;
137     textR.width = 0;
138     textR.height = 0;
139     iconR.x = 0;
140     iconR.y = 0;
141     iconR.width = 0;
142     iconR.height = 0;
143     viewR.x = i.left;
144     viewR.y = i.right;
145     viewR.width = size.width - i.left - i.right;
146     viewR.height = size.height - i.top - i.bottom;
147
148     Font f = c.getFont();
149
150     g.setFont(f);
151
152     ButtonModel m = b.getModel();
153
154     // This is the icon that we use for layout.
155     Icon icon = b.getIcon();
156     if (icon == null)
157       icon = getDefaultIcon();
158
159     // Do the layout.
160     String text = SwingUtilities.layoutCompoundLabel(c, g.getFontMetrics(f), 
161        b.getText(), icon,
162        b.getVerticalAlignment(), b.getHorizontalAlignment(),
163        b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
164        viewR, iconR, textR, b.getIconTextGap());
165
166     // Figure out the correct icon.
167     icon = b.getIcon();
168     if (icon == null)
169       icon = getDefaultIcon();
170     else
171       {
172         if (! m.isEnabled())
173           {
174             if (m.isSelected())
175               icon = b.getDisabledSelectedIcon();
176             else
177               icon = b.getDisabledIcon();
178           }
179         else if (m.isArmed() && m.isPressed())
180           {
181             icon = b.getPressedIcon();
182             if (icon == null)
183               icon = b.getSelectedIcon();
184           }
185         else if (m.isSelected())
186           {
187             if (b.isRolloverEnabled() && m.isRollover())
188               {
189                 icon = b.getRolloverSelectedIcon();
190                 if (icon == null)
191                   icon = b.getSelectedIcon();
192               }
193             else
194               icon = b.getSelectedIcon();
195           }
196         else if (b.isRolloverEnabled() && m.isRollover())
197           icon = b.getRolloverIcon();
198         if (icon == null)
199           icon = b.getIcon();
200       }
201     // .. and paint it.
202     icon.paintIcon(c, g, iconR.x, iconR.y);
203
204     // Paint text and focus indicator.
205     if (text != null)
206       {
207         // Maybe render HTML in the radio button.
208         View v = (View) c.getClientProperty(BasicHTML.propertyKey);
209         if (v != null)
210           v.paint(g, textR);
211         else
212           paintText(g, b, textR, text);
213
214         // Paint focus indicator if necessary.
215         if (b.hasFocus() && b.isFocusPainted()
216             && textR.width > 0 && textR.height > 0)
217           paintFocus(g, textR, size);
218       }
219   }
220   
221   public Dimension getPreferredSize(JComponent c)
222   {
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;
230     
231     Insets insets = b.getInsets();
232
233     String text = b.getText();
234     Icon i = b.getIcon();
235     if (i == null)
236       i = getDefaultIcon(); 
237     
238     textR.x = 0;
239     textR.y = 0;
240     textR.width = 0;
241     textR.height = 0;
242     iconR.x = 0;
243     iconR.y = 0;
244     iconR.width = 0;
245     iconR.height = 0;
246     viewR.x = 0;
247     viewR.y = 0;
248     viewR.width = Short.MAX_VALUE;
249     viewR.height = Short.MAX_VALUE;
250
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(),
257                                        viewR, iconR, textR,
258                                        text == null ? 0 : b.getIconTextGap());
259
260     Rectangle r = SwingUtilities.computeUnion(textR.x, textR.y, textR.width,
261                                               textR.height, iconR);
262
263     return new Dimension(insets.left + r.width + insets.right,
264                          insets.top + r.height + insets.bottom);
265   }
266
267   /**
268    * Paints the focus indicator for JRadioButtons.
269    *
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.
273    */
274   protected void paintFocus(Graphics g, Rectangle tr, Dimension size)
275   {
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);
280     g.setColor(saved);
281   }
282 }