KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > metal > MetalRadioButtonUI


1 /*
2  * @(#)MetalRadioButtonUI.java 1.29 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.metal;
9
10 import com.sun.java.swing.SwingUtilities2;
11 import java.awt.*;
12 import java.awt.event.*;
13 import javax.swing.*;
14 import javax.swing.plaf.basic.*;
15 import javax.swing.border.*;
16 import javax.swing.plaf.*;
17 import java.io.Serializable JavaDoc;
18 import javax.swing.text.View JavaDoc;
19
20
21 /**
22  * RadioButtonUI implementation for MetalRadioButtonUI
23  * <p>
24  * <strong>Warning:</strong>
25  * Serialized objects of this class will not be compatible with
26  * future Swing releases. The current serialization support is
27  * appropriate for short term storage or RMI between applications running
28  * the same version of Swing. As of 1.4, support for long term storage
29  * of all JavaBeans<sup><font size="-2">TM</font></sup>
30  * has been added to the <code>java.beans</code> package.
31  * Please see {@link java.beans.XMLEncoder}.
32  *
33  * @version 1.29 12/19/03
34  * @author Michael C. Albers (Metal modifications)
35  * @author Jeff Dinkins (original BasicRadioButtonCode)
36  */

37 public class MetalRadioButtonUI extends BasicRadioButtonUI {
38
39     private static final MetalRadioButtonUI JavaDoc metalRadioButtonUI = new MetalRadioButtonUI JavaDoc();
40
41     protected Color focusColor;
42     protected Color selectColor;
43     protected Color disabledTextColor;
44
45     private boolean defaults_initialized = false;
46
47     // ********************************
48
// Create PlAF
49
// ********************************
50
public static ComponentUI createUI(JComponent c) {
51         return metalRadioButtonUI;
52     }
53
54     // ********************************
55
// Install Defaults
56
// ********************************
57
public void installDefaults(AbstractButton b) {
58         super.installDefaults(b);
59         if(!defaults_initialized) {
60             focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
61             selectColor = UIManager.getColor(getPropertyPrefix() + "select");
62             disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
63             defaults_initialized = true;
64         }
65         LookAndFeel.installProperty(b, "opaque", Boolean.TRUE);
66     }
67
68     protected void uninstallDefaults(AbstractButton b) {
69         super.uninstallDefaults(b);
70         defaults_initialized = false;
71     }
72
73     // ********************************
74
// Default Accessors
75
// ********************************
76
protected Color getSelectColor() {
77         return selectColor;
78     }
79
80     protected Color getDisabledTextColor() {
81         return disabledTextColor;
82     }
83
84     protected Color getFocusColor() {
85         return focusColor;
86     }
87
88
89     // ********************************
90
// Paint Methods
91
// ********************************
92
public synchronized void paint(Graphics g, JComponent c) {
93
94         AbstractButton b = (AbstractButton) c;
95         ButtonModel model = b.getModel();
96         
97         Dimension size = c.getSize();
98
99         int w = size.width;
100         int h = size.height;
101
102         Font f = c.getFont();
103         g.setFont(f);
104         FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f);
105
106         Rectangle viewRect = new Rectangle(size);
107         Rectangle iconRect = new Rectangle();
108         Rectangle textRect = new Rectangle();
109
110         Insets i = c.getInsets();
111     viewRect.x += i.left;
112         viewRect.y += i.top;
113         viewRect.width -= (i.right + viewRect.x);
114         viewRect.height -= (i.bottom + viewRect.y);
115
116         Icon altIcon = b.getIcon();
117         Icon selectedIcon = null;
118         Icon disabledIcon = null;
119         
120         String JavaDoc text = SwingUtilities.layoutCompoundLabel(
121             c, fm, b.getText(), altIcon != null ? altIcon : getDefaultIcon(),
122             b.getVerticalAlignment(), b.getHorizontalAlignment(),
123             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
124             viewRect, iconRect, textRect, b.getIconTextGap());
125         
126         // fill background
127
if(c.isOpaque()) {
128             g.setColor(b.getBackground());
129             g.fillRect(0,0, size.width, size.height);
130         }
131
132         
133         // Paint the radio button
134
if(altIcon != null) {
135
136             if(!model.isEnabled()) {
137             if(model.isSelected()) {
138                    altIcon = b.getDisabledSelectedIcon();
139         } else {
140                    altIcon = b.getDisabledIcon();
141         }
142             } else if(model.isPressed() && model.isArmed()) {
143                 altIcon = b.getPressedIcon();
144                 if(altIcon == null) {
145                     // Use selected icon
146
altIcon = b.getSelectedIcon();
147                 }
148             } else if(model.isSelected()) {
149                 if(b.isRolloverEnabled() && model.isRollover()) {
150                         altIcon = (Icon) b.getRolloverSelectedIcon();
151                         if (altIcon == null) {
152                                 altIcon = (Icon) b.getSelectedIcon();
153                         }
154                 } else {
155                         altIcon = (Icon) b.getSelectedIcon();
156                 }
157             } else if(b.isRolloverEnabled() && model.isRollover()) {
158                 altIcon = (Icon) b.getRolloverIcon();
159             }
160               
161             if(altIcon == null) {
162                 altIcon = b.getIcon();
163             }
164                
165             altIcon.paintIcon(c, g, iconRect.x, iconRect.y);
166
167         } else {
168             getDefaultIcon().paintIcon(c, g, iconRect.x, iconRect.y);
169         }
170
171
172         // Draw the Text
173
if(text != null) {
174             View JavaDoc v = (View JavaDoc) c.getClientProperty(BasicHTML.propertyKey);
175             if (v != null) {
176                 v.paint(g, textRect);
177             } else {
178                int mnemIndex = b.getDisplayedMnemonicIndex();
179                if(model.isEnabled()) {
180                    // *** paint the text normally
181
g.setColor(b.getForeground());
182                } else {
183                    // *** paint the text disabled
184
g.setColor(getDisabledTextColor());
185                }
186                SwingUtilities2.drawStringUnderlineCharAt(c,g,text,
187                        mnemIndex, textRect.x, textRect.y + fm.getAscent());
188        }
189        if(b.hasFocus() && b.isFocusPainted() &&
190           textRect.width > 0 && textRect.height > 0 ) {
191            paintFocus(g,textRect,size);
192        }
193         }
194     }
195
196     protected void paintFocus(Graphics g, Rectangle t, Dimension d){
197         g.setColor(getFocusColor());
198         g.drawRect(t.x-1, t.y-1, t.width+1, t.height+1);
199     }
200 }
201
Popular Tags