KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicButtonUI


1 /*
2  * @(#)BasicButtonUI.java 1.112 04/01/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.basic;
9
10 import com.sun.java.swing.SwingUtilities2;
11 import java.awt.*;
12 import java.awt.event.*;
13 import java.io.Serializable JavaDoc;
14 import javax.swing.*;
15 import javax.swing.border.*;
16 import java.awt.*;
17 import java.awt.event.*;
18 import javax.swing.plaf.ButtonUI JavaDoc;
19 import javax.swing.plaf.UIResource JavaDoc;
20 import javax.swing.plaf.ComponentUI JavaDoc;
21 import javax.swing.text.View JavaDoc;
22
23 /**
24  * BasicButton implementation
25  *
26  * @version 1.112 01/19/04
27  * @author Jeff Dinkins
28  */

29 public class BasicButtonUI extends ButtonUI JavaDoc{
30     // Shared UI object
31
private final static BasicButtonUI JavaDoc buttonUI = new BasicButtonUI JavaDoc();
32
33     // Visual constants
34
// NOTE: This is not used or set any where. Were we allowed to remove
35
// fields, this would be removed.
36
protected int defaultTextIconGap;
37     
38     // Amount to offset text, the value of this comes from
39
// defaultTextShiftOffset once setTextShiftOffset has been invoked.
40
private int shiftOffset = 0;
41     // Value that is set in shiftOffset once setTextShiftOffset has been
42
// invoked. The value of this comes from the defaults table.
43
protected int defaultTextShiftOffset;
44
45     private final static String JavaDoc propertyPrefix = "Button" + ".";
46
47     // ********************************
48
// Create PLAF
49
// ********************************
50
public static ComponentUI JavaDoc createUI(JComponent c) {
51         return buttonUI;
52     }
53
54     protected String JavaDoc getPropertyPrefix() {
55         return propertyPrefix;
56     }
57
58
59     // ********************************
60
// Install PLAF
61
// ********************************
62
public void installUI(JComponent c) {
63         installDefaults((AbstractButton) c);
64         installListeners((AbstractButton) c);
65         installKeyboardActions((AbstractButton) c);
66     BasicHTML.updateRenderer(c, ((AbstractButton) c).getText());
67     }
68
69     protected void installDefaults(AbstractButton b) {
70         // load shared instance defaults
71
String JavaDoc pp = getPropertyPrefix();
72
73         defaultTextShiftOffset = UIManager.getInt(pp + "textShiftOffset");
74
75         // set the following defaults on the button
76
if (b.isContentAreaFilled()) {
77             LookAndFeel.installProperty(b, "opaque", Boolean.TRUE);
78         } else {
79             LookAndFeel.installProperty(b, "opaque", Boolean.FALSE);
80         }
81
82         if(b.getMargin() == null || (b.getMargin() instanceof UIResource JavaDoc)) {
83             b.setMargin(UIManager.getInsets(pp + "margin"));
84         }
85
86     LookAndFeel.installColorsAndFont(b, pp + "background",
87                                          pp + "foreground", pp + "font");
88         LookAndFeel.installBorder(b, pp + "border");
89
90         Object JavaDoc rollover = UIManager.get(pp + "rollover");
91         if (rollover != null) {
92             LookAndFeel.installProperty(b, "rolloverEnabled", rollover);
93         }
94     }
95
96     protected void installListeners(AbstractButton b) {
97         BasicButtonListener JavaDoc listener = createButtonListener(b);
98         if(listener != null) {
99             b.addMouseListener(listener);
100             b.addMouseMotionListener(listener);
101             b.addFocusListener(listener);
102             b.addPropertyChangeListener(listener);
103             b.addChangeListener(listener);
104         }
105     }
106     
107     protected void installKeyboardActions(AbstractButton b){
108         BasicButtonListener JavaDoc listener = getButtonListener(b);
109
110         if(listener != null) {
111             listener.installKeyboardActions(b);
112         }
113     }
114
115         
116     // ********************************
117
// Uninstall PLAF
118
// ********************************
119
public void uninstallUI(JComponent c) {
120         uninstallKeyboardActions((AbstractButton) c);
121         uninstallListeners((AbstractButton) c);
122         uninstallDefaults((AbstractButton) c);
123     BasicHTML.updateRenderer(c, "");
124     }
125
126     protected void uninstallKeyboardActions(AbstractButton b) {
127         BasicButtonListener JavaDoc listener = getButtonListener(b);
128         if(listener != null) {
129             listener.uninstallKeyboardActions(b);
130         }
131     }
132
133     protected void uninstallListeners(AbstractButton b) {
134         BasicButtonListener JavaDoc listener = getButtonListener(b);
135         if(listener != null) {
136             b.removeMouseListener(listener);
137             b.removeMouseListener(listener);
138             b.removeMouseMotionListener(listener);
139             b.removeFocusListener(listener);
140             b.removeChangeListener(listener);
141             b.removePropertyChangeListener(listener);
142         }
143     }
144
145     protected void uninstallDefaults(AbstractButton b) {
146         LookAndFeel.uninstallBorder(b);
147     }
148   
149     // ********************************
150
// Create Listeners
151
// ********************************
152
protected BasicButtonListener JavaDoc createButtonListener(AbstractButton b) {
153         return new BasicButtonListener JavaDoc(b);
154     }
155
156     public int getDefaultTextIconGap(AbstractButton b) {
157         return defaultTextIconGap;
158     }
159
160     /* These rectangles/insets are allocated once for all
161      * ButtonUI.paint() calls. Re-using rectangles rather than
162      * allocating them in each paint call substantially reduced the time
163      * it took paint to run. Obviously, this method can't be re-entered.
164      */

165     private static Rectangle viewRect = new Rectangle();
166     private static Rectangle textRect = new Rectangle();
167     private static Rectangle iconRect = new Rectangle();
168
169     // ********************************
170
// Paint Methods
171
// ********************************
172

173     public void paint(Graphics g, JComponent c)
174     {
175         AbstractButton b = (AbstractButton) c;
176         ButtonModel model = b.getModel();
177
178         FontMetrics fm = SwingUtilities2.getFontMetrics(b, g);
179
180         Insets i = c.getInsets();
181
182         viewRect.x = i.left;
183         viewRect.y = i.top;
184         viewRect.width = b.getWidth() - (i.right + viewRect.x);
185         viewRect.height = b.getHeight() - (i.bottom + viewRect.y);
186
187         textRect.x = textRect.y = textRect.width = textRect.height = 0;
188         iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
189
190         Font f = c.getFont();
191         g.setFont(f);
192
193         // layout the text and icon
194
String JavaDoc text = SwingUtilities.layoutCompoundLabel(
195             c, fm, b.getText(), b.getIcon(),
196             b.getVerticalAlignment(), b.getHorizontalAlignment(),
197             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
198             viewRect, iconRect, textRect,
199         b.getText() == null ? 0 : b.getIconTextGap());
200
201         clearTextShiftOffset();
202
203         // perform UI specific press action, e.g. Windows L&F shifts text
204
if (model.isArmed() && model.isPressed()) {
205             paintButtonPressed(g,b);
206         }
207
208         // Paint the Icon
209
if(b.getIcon() != null) {
210             paintIcon(g,c,iconRect);
211         }
212
213         if (text != null && !text.equals("")){
214         View JavaDoc v = (View JavaDoc) c.getClientProperty(BasicHTML.propertyKey);
215         if (v != null) {
216         v.paint(g, textRect);
217         } else {
218         paintText(g, b, textRect, text);
219         }
220         }
221
222         if (b.isFocusPainted() && b.hasFocus()) {
223             // paint UI specific focus
224
paintFocus(g,b,viewRect,textRect,iconRect);
225         }
226     }
227     
228     protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect){
229             AbstractButton b = (AbstractButton) c;
230             ButtonModel model = b.getModel();
231             Icon icon = b.getIcon();
232             Icon tmpIcon = null;
233
234         if(icon == null) {
235            return;
236         }
237
238             if(!model.isEnabled()) {
239         if(model.isSelected()) {
240                    tmpIcon = (Icon) b.getDisabledSelectedIcon();
241         } else {
242                    tmpIcon = (Icon) b.getDisabledIcon();
243         }
244             } else if(model.isPressed() && model.isArmed()) {
245                 tmpIcon = (Icon) b.getPressedIcon();
246                 if(tmpIcon != null) {
247                     // revert back to 0 offset
248
clearTextShiftOffset();
249                 }
250             } else if(b.isRolloverEnabled() && model.isRollover()) {
251         if(model.isSelected()) {
252                    tmpIcon = (Icon) b.getRolloverSelectedIcon();
253         } else {
254                    tmpIcon = (Icon) b.getRolloverIcon();
255         }
256             } else if(model.isSelected()) {
257                 tmpIcon = (Icon) b.getSelectedIcon();
258         }
259               
260         if(tmpIcon != null) {
261             icon = tmpIcon;
262         }
263                
264             if(model.isPressed() && model.isArmed()) {
265                 icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(),
266                         iconRect.y + getTextShiftOffset());
267             } else {
268                 icon.paintIcon(c, g, iconRect.x, iconRect.y);
269             }
270
271     }
272
273     /**
274      * As of Java 2 platform v 1.4 this method should not be used or overriden.
275      * Use the paintText method which takes the AbstractButton argument.
276      */

277     protected void paintText(Graphics g, JComponent c, Rectangle textRect, String JavaDoc text) {
278         AbstractButton b = (AbstractButton) c;
279         ButtonModel model = b.getModel();
280         FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
281         int mnemonicIndex = b.getDisplayedMnemonicIndex();
282
283     /* Draw the Text */
284     if(model.isEnabled()) {
285         /*** paint the text normally */
286         g.setColor(b.getForeground());
287         SwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex,
288                       textRect.x + getTextShiftOffset(),
289                       textRect.y + fm.getAscent() + getTextShiftOffset());
290     }
291     else {
292         /*** paint the text disabled ***/
293         g.setColor(b.getBackground().brighter());
294         SwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex,
295                       textRect.x, textRect.y + fm.getAscent());
296         g.setColor(b.getBackground().darker());
297         SwingUtilities2.drawStringUnderlineCharAt(c, g,text, mnemonicIndex,
298                       textRect.x - 1, textRect.y + fm.getAscent() - 1);
299     }
300     }
301
302     /**
303      * Method which renders the text of the current button.
304      * <p>
305      * @param g Graphics context
306      * @param b Current button to render
307      * @param textRect Bounding rectangle to render the text.
308      * @param text String to render
309      * @since 1.4
310      */

311     protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String JavaDoc text) {
312     paintText(g, (JComponent)b, textRect, text);
313     }
314
315     // Method signature defined here overriden in subclasses.
316
// Perhaps this class should be abstract?
317
protected void paintFocus(Graphics g, AbstractButton b,
318                               Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
319     }
320   
321
322
323     protected void paintButtonPressed(Graphics g, AbstractButton b){
324     }
325
326     protected void clearTextShiftOffset(){
327         this.shiftOffset = 0;
328     }
329
330     protected void setTextShiftOffset(){
331         this.shiftOffset = defaultTextShiftOffset;
332     }
333
334     protected int getTextShiftOffset() {
335         return shiftOffset;
336     }
337
338     // ********************************
339
// Layout Methods
340
// ********************************
341
public Dimension getMinimumSize(JComponent c) {
342         Dimension d = getPreferredSize(c);
343     View JavaDoc v = (View JavaDoc) c.getClientProperty(BasicHTML.propertyKey);
344     if (v != null) {
345         d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
346     }
347     return d;
348     }
349
350     public Dimension getPreferredSize(JComponent c) {
351         AbstractButton b = (AbstractButton)c;
352         return BasicGraphicsUtils.getPreferredButtonSize(b, b.getIconTextGap());
353     }
354
355     public Dimension getMaximumSize(JComponent c) {
356         Dimension d = getPreferredSize(c);
357     View JavaDoc v = (View JavaDoc) c.getClientProperty(BasicHTML.propertyKey);
358     if (v != null) {
359         d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
360     }
361     return d;
362     }
363
364     /**
365      * Returns the ButtonListener for the passed in Button, or null if one
366      * could not be found.
367      */

368     private BasicButtonListener JavaDoc getButtonListener(AbstractButton b) {
369         MouseMotionListener[] listeners = b.getMouseMotionListeners();
370
371         if (listeners != null) {
372             for (int counter = 0; counter < listeners.length; counter++) {
373                 if (listeners[counter] instanceof BasicButtonListener JavaDoc) {
374                     return (BasicButtonListener JavaDoc)listeners[counter];
375                 }
376             }
377         }
378         return null;
379     }
380
381 }
382
Popular Tags