KickJava   Java API By Example, From Geeks To Geeks.

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


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

34 public class MetalButtonUI extends BasicButtonUI {
35
36     private final static MetalButtonUI JavaDoc metalButtonUI = new MetalButtonUI JavaDoc();
37
38     // NOTE: These are not really needed, but at this point we can't pull
39
// them. Their values are updated purely for historical reasons.
40
protected Color focusColor;
41     protected Color selectColor;
42     protected Color disabledTextColor;
43  
44     // ********************************
45
// Create PLAF
46
// ********************************
47
public static ComponentUI createUI(JComponent c) {
48         return metalButtonUI;
49     }
50  
51     // ********************************
52
// Install
53
// ********************************
54
public void installDefaults(AbstractButton b) {
55         super.installDefaults(b);
56     }
57
58     public void uninstallDefaults(AbstractButton b) {
59     super.uninstallDefaults(b);
60     }
61
62     // ********************************
63
// Create Listeners
64
// ********************************
65
protected BasicButtonListener createButtonListener(AbstractButton b) {
66         return super.createButtonListener(b);
67     }
68
69     
70     // ********************************
71
// Default Accessors
72
// ********************************
73
protected Color getSelectColor() {
74         selectColor = UIManager.getColor(getPropertyPrefix() + "select");
75     return selectColor;
76     }
77
78     protected Color getDisabledTextColor() {
79         disabledTextColor = UIManager.getColor(getPropertyPrefix() +
80                                                "disabledText");
81     return disabledTextColor;
82     }
83
84     protected Color getFocusColor() {
85         focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
86     return focusColor;
87     }
88
89     // ********************************
90
// Paint
91
// ********************************
92
/**
93      * If necessary paints the background of the component, then
94      * invokes <code>paint</code>.
95      *
96      * @param g Graphics to paint to
97      * @param c JComponent painting on
98      * @throws NullPointerException if <code>g</code> or <code>c</code> is
99      * null
100      * @see javax.swing.plaf.ComponentUI#update
101      * @see javax.swing.plaf.ComponentUI#paint
102      * @since 1.5
103      */

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