KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)MetalToggleButtonUI.java 1.28 04/04/02
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 java.lang.ref.*;
14 import java.util.*;
15 import javax.swing.plaf.basic.BasicToggleButtonUI JavaDoc;
16
17 import javax.swing.*;
18 import javax.swing.border.*;
19 import javax.swing.plaf.*;
20 import javax.swing.*;
21
22 import java.io.Serializable JavaDoc;
23
24 /**
25  * MetalToggleButton implementation
26  * <p>
27  * <strong>Warning:</strong>
28  * Serialized objects of this class will not be compatible with
29  * future Swing releases. The current serialization support is
30  * appropriate for short term storage or RMI between applications running
31  * the same version of Swing. As of 1.4, support for long term storage
32  * of all JavaBeans<sup><font size="-2">TM</font></sup>
33  * has been added to the <code>java.beans</code> package.
34  * Please see {@link java.beans.XMLEncoder}.
35  *
36  * @version 1.28 04/02/04
37  * @author Tom Santos
38  */

39 public class MetalToggleButtonUI extends BasicToggleButtonUI JavaDoc {
40
41     private static final MetalToggleButtonUI JavaDoc metalToggleButtonUI = new MetalToggleButtonUI JavaDoc();
42
43     protected Color focusColor;
44     protected Color selectColor;
45     protected Color disabledTextColor;
46
47     private boolean defaults_initialized = false;
48
49     // ********************************
50
// Create PLAF
51
// ********************************
52
public static ComponentUI createUI(JComponent b) {
53         return metalToggleButtonUI;
54     }
55
56     // ********************************
57
// Install Defaults
58
// ********************************
59
public void installDefaults(AbstractButton b) {
60         super.installDefaults(b);
61     if(!defaults_initialized) {
62         focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
63         selectColor = UIManager.getColor(getPropertyPrefix() + "select");
64         disabledTextColor = UIManager.getColor(getPropertyPrefix() + "disabledText");
65         defaults_initialized = true;
66     }
67     }
68
69     protected void uninstallDefaults(AbstractButton b) {
70     super.uninstallDefaults(b);
71     defaults_initialized = false;
72     }
73
74     // ********************************
75
// Default Accessors
76
// ********************************
77
protected Color getSelectColor() {
78     return selectColor;
79     }
80
81     protected Color getDisabledTextColor() {
82     return disabledTextColor;
83     }
84
85     protected Color getFocusColor() {
86     return focusColor;
87     }
88
89
90     // ********************************
91
// Paint Methods
92
// ********************************
93
/**
94      * If necessary paints the background of the component, then invokes
95      * <code>paint</code>.
96      *
97      * @param g Graphics to paint to
98      * @param c JComponent painting on
99      * @throws NullPointerException if <code>g</code> or <code>c</code> is
100      * null
101      * @see javax.swing.plaf.ComponentUI#update
102      * @see javax.swing.plaf.ComponentUI#paint
103      * @since 1.5
104      */

105     public void update(Graphics g, JComponent c) {
106         AbstractButton button = (AbstractButton)c;
107         if ((c.getBackground() instanceof UIResource) &&
108                         button.isContentAreaFilled() && c.isEnabled()) {
109             ButtonModel model = button.getModel();
110             if (!MetalUtils.isToolBarButton(c)) {
111                 if (!model.isArmed() && !model.isPressed() &&
112                         MetalUtils.drawGradient(
113                         c, g, "ToggleButton.gradient", 0, 0, c.getWidth(),
114                         c.getHeight(), true)) {
115                     paint(g, c);
116                     return;
117                 }
118             }
119             else if ((model.isRollover() || model.isSelected()) &&
120                         MetalUtils.drawGradient(c, g, "ToggleButton.gradient",
121                         0, 0, c.getWidth(), c.getHeight(), true)) {
122                 paint(g, c);
123                 return;
124             }
125         }
126         super.update(g, c);
127     }
128
129     protected void paintButtonPressed(Graphics g, AbstractButton b) {
130         if ( b.isContentAreaFilled() ) {
131         g.setColor(getSelectColor());
132         g.fillRect(0, 0, b.getWidth(), b.getHeight());
133     }
134     }
135
136     protected void paintText(Graphics g, JComponent c, Rectangle textRect, String JavaDoc text) {
137     AbstractButton b = (AbstractButton) c;
138     ButtonModel model = b.getModel();
139     FontMetrics fm = SwingUtilities2.getFontMetrics(b, g);
140         int mnemIndex = b.getDisplayedMnemonicIndex();
141
142     /* Draw the Text */
143     if(model.isEnabled()) {
144         /*** paint the text normally */
145         g.setColor(b.getForeground());
146     }
147     else {
148         /*** paint the text disabled ***/
149         if (model.isSelected()) {
150         g.setColor(c.getBackground());
151         } else {
152             g.setColor(getDisabledTextColor());
153         }
154         }
155         SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemIndex,
156                 textRect.x, textRect.y + fm.getAscent());
157     }
158
159     protected void paintFocus(Graphics g, AbstractButton b,
160                   Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
161
162         Rectangle focusRect = new Rectangle();
163     String JavaDoc text = b.getText();
164     boolean isIcon = b.getIcon() != null;
165
166         // If there is text
167
if ( text != null && !text.equals( "" ) ) {
168         if ( !isIcon ) {
169             focusRect.setBounds( textRect );
170         }
171         else {
172             focusRect.setBounds( iconRect.union( textRect ) );
173         }
174         }
175         // If there is an icon and no text
176
else if ( isIcon ) {
177         focusRect.setBounds( iconRect );
178         }
179
180         g.setColor(getFocusColor());
181     g.drawRect((focusRect.x-1), (focusRect.y-1),
182           focusRect.width+1, focusRect.height+1);
183     
184     }
185
186     /**
187      * Paints the appropriate icon of the button <code>b</code> in the
188      * space <code>iconRect</code>.
189      *
190      * @param g Graphics to paint to
191      * @param b Button to render for
192      * @param iconRect space to render in
193      * @throws NullPointerException if any of the arguments are null.
194      * @since 1.5
195      */

196     protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
197         super.paintIcon(g, b, iconRect);
198     }
199 }
200
Popular Tags