KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > SynthButtonUI


1 /*
2  * @(#)SynthButtonUI.java 1.20 04/04/16
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.synth;
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import java.io.Serializable JavaDoc;
13 import javax.swing.*;
14 import javax.swing.border.*;
15 import java.awt.*;
16 import java.awt.event.*;
17 import java.beans.*;
18 import javax.swing.plaf.*;
19 import javax.swing.plaf.basic.BasicButtonUI JavaDoc;
20 import javax.swing.plaf.basic.BasicHTML JavaDoc;
21 import javax.swing.text.View JavaDoc;
22 import sun.swing.plaf.synth.SynthUI;
23 import sun.swing.plaf.synth.DefaultSynthStyle;
24
25 /**
26  * Synth's ButtonUI implementation.
27  *
28  * @version 1.20, 04/16/04
29  * @author Scott Violet
30  */

31 class SynthButtonUI extends BasicButtonUI JavaDoc implements
32                                  PropertyChangeListener, SynthUI {
33     private SynthStyle JavaDoc style;
34
35     public static ComponentUI createUI(JComponent c) {
36         return new SynthButtonUI JavaDoc();
37     }
38
39     protected void installDefaults(AbstractButton b) {
40         updateStyle(b);
41
42         LookAndFeel.installProperty(b, "rolloverEnabled", Boolean.TRUE);
43     }
44
45     protected void installListeners(AbstractButton b) {
46         super.installListeners(b);
47         b.addPropertyChangeListener(this);
48     }
49
50     void updateStyle(AbstractButton b) {
51         SynthContext JavaDoc context = getContext(b, SynthConstants.ENABLED);
52         SynthStyle JavaDoc oldStyle = style;
53         style = SynthLookAndFeel.updateStyle(context, this);
54         if (style != oldStyle) {
55             if (b.getMargin() == null ||
56                                 (b.getMargin() instanceof UIResource)) {
57                 Insets margin = (Insets)style.get(context,getPropertyPrefix() +
58                                                   "margin");
59
60                 if (margin == null) {
61                     // Some places assume margins are non-null.
62
margin = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
63                 }
64                 b.setMargin(margin);
65             }
66
67             Object JavaDoc value = style.get(context, getPropertyPrefix() + "iconTextGap");
68             if (value != null) {
69                 LookAndFeel.installProperty(b, "iconTextGap", value);
70             }
71
72             value = style.get(context, getPropertyPrefix() + "contentAreaFilled");
73             LookAndFeel.installProperty(b, "contentAreaFilled",
74                                         value != null? value : Boolean.TRUE);
75
76             if (oldStyle != null) {
77                 uninstallKeyboardActions(b);
78                 installKeyboardActions(b);
79             }
80
81         }
82         context.dispose();
83     }
84
85     protected void uninstallListeners(AbstractButton b) {
86         super.uninstallListeners(b);
87         b.removePropertyChangeListener(this);
88     }
89
90     protected void uninstallDefaults(AbstractButton b) {
91         SynthContext JavaDoc context = getContext(b, ENABLED);
92
93         style.uninstallDefaults(context);
94         context.dispose();
95         style = null;
96     }
97
98     public SynthContext JavaDoc getContext(JComponent c) {
99         return getContext(c, getComponentState(c));
100     }
101
102     SynthContext JavaDoc getContext(JComponent c, int state) {
103         Region JavaDoc region = getRegion(c);
104         return SynthContext.getContext(SynthContext JavaDoc.class, c, region,
105                                        style, state);
106     }
107
108     private Region JavaDoc getRegion(JComponent c) {
109         return SynthLookAndFeel.getRegion(c);
110     }
111
112     /**
113      * Returns the current state of the passed in <code>AbstractButton</code>.
114      */

115     private int getComponentState(JComponent c) {
116         int state = ENABLED;
117
118         if (!c.isEnabled()) {
119             state = DISABLED;
120         }
121         if (SynthLookAndFeel.selectedUI == this) {
122             return SynthLookAndFeel.selectedUIState | SynthConstants.ENABLED;
123         }
124         ButtonModel model = ((AbstractButton)c).getModel();
125
126         if (model.isPressed()) {
127             if (model.isArmed()) {
128                 state = PRESSED;
129             }
130             else {
131                 state = MOUSE_OVER;
132             }
133         }
134         else if (model.isRollover()) {
135             state = MOUSE_OVER;
136         }
137         if (model.isSelected()) {
138             state |= SELECTED;
139         }
140         if (c.isFocusOwner()) {
141             state |= FOCUSED;
142         }
143         if ((c instanceof JButton) && ((JButton)c).isDefaultButton()) {
144             state |= DEFAULT;
145         }
146         return state;
147     }
148
149     // ********************************
150
// Paint Methods
151
// ********************************
152

153     public void update(Graphics g, JComponent c) {
154         SynthContext JavaDoc context = getContext(c);
155
156         SynthLookAndFeel.update(context, g);
157         paintBackground(context, g, c);
158         paint(context, g);
159         context.dispose();
160     }
161
162     public void paint(Graphics g, JComponent c) {
163         SynthContext JavaDoc context = getContext(c);
164
165         paint(context, g);
166         context.dispose();
167     }
168
169     protected void paint(SynthContext JavaDoc context, Graphics g) {
170         AbstractButton b = (AbstractButton)context.getComponent();
171
172         g.setColor(context.getStyle().getColor(context,
173                                                ColorType.TEXT_FOREGROUND));
174         g.setFont(style.getFont(context));
175         context.getStyle().getGraphicsUtils(context).paintText(
176             context, g, b.getText(), getIcon(b),
177             b.getHorizontalAlignment(), b.getVerticalAlignment(),
178             b.getHorizontalTextPosition(), b.getVerticalTextPosition(),
179             b.getIconTextGap(), b.getDisplayedMnemonicIndex(),
180             getTextShiftOffset(context));
181     }
182
183     void paintBackground(SynthContext JavaDoc context, Graphics g, JComponent c) {
184         context.getPainter().paintButtonBackground(context, g, 0, 0,
185                                                 c.getWidth(), c.getHeight());
186     }
187
188     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
189                             int y, int w, int h) {
190         context.getPainter().paintButtonBorder(context, g, x, y, w, h);
191     }
192
193     /**
194      * Returns the default icon. This should NOT callback
195      * to the JComponent.
196      *
197      * @param b AbstractButton the iocn is associated with
198      * @return default icon
199      */

200    
201     protected Icon getDefaultIcon(AbstractButton b) {
202         SynthContext JavaDoc context = getContext(b);
203         Icon icon = context.getStyle().getIcon(context, getPropertyPrefix() + "icon");
204         context.dispose();
205         return icon;
206     }
207     
208     /**
209      * Returns the Icon to use in painting the button.
210      */

211     protected Icon getIcon(AbstractButton b) {
212         Icon icon = getEnabledIcon(b);
213
214         ButtonModel model = b.getModel();
215         Icon tmpIcon = null;
216
217         if (!model.isEnabled()) {
218             tmpIcon = getSynthDisabledIcon(b);
219         } else if (model.isPressed() && model.isArmed()) {
220             tmpIcon = getPressedIcon(b);
221         } else if (b.isRolloverEnabled() && model.isRollover()) {
222             tmpIcon = getRolloverIcon(b);
223         } else if (model.isSelected()) {
224             tmpIcon = getSelectedIcon(b);
225         }
226         if (tmpIcon != null) {
227             icon = tmpIcon;
228         }
229         if(icon == null) {
230             return getDefaultIcon(b);
231         }
232         return icon;
233     }
234
235     private Icon getSynthIcon(AbstractButton b, int synthConstant) {
236         return style.getIcon(getContext(b, synthConstant), getPropertyPrefix() + "icon");
237     }
238     
239     private Icon getEnabledIcon(AbstractButton b) {
240         Icon tmpIcon = b.getIcon();
241         if(tmpIcon == null) {
242             tmpIcon = getSynthIcon(b, SynthConstants.ENABLED);
243         }
244         return tmpIcon;
245     }
246     
247     private Icon getSelectedIcon(AbstractButton b) {
248         Icon tmpIcon = b.getSelectedIcon();
249         if(tmpIcon == null) {
250             tmpIcon = getSynthIcon(b, SynthConstants.SELECTED);
251         }
252         return tmpIcon;
253     }
254
255     private Icon getRolloverIcon(AbstractButton b) {
256         ButtonModel model = b.getModel();
257         Icon tmpIcon;
258         if (model.isSelected()) {
259             tmpIcon = b.getRolloverSelectedIcon();
260             if (tmpIcon == null) {
261                 tmpIcon = getSynthIcon(b, SynthConstants.SELECTED);
262                 if (tmpIcon == null) {
263                   tmpIcon = getSelectedIcon(b);
264                 }
265             }
266         } else {
267             tmpIcon = b.getRolloverIcon();
268             if (tmpIcon == null) {
269               tmpIcon = getSynthIcon(b, SynthConstants.MOUSE_OVER);
270             }
271         }
272         return tmpIcon;
273     }
274
275     private Icon getPressedIcon(AbstractButton b) {
276         Icon tmpIcon;
277         tmpIcon = b.getPressedIcon();
278         if (tmpIcon == null) {
279             tmpIcon = getSynthIcon(b, SynthConstants.PRESSED);
280             if (tmpIcon == null) {
281               tmpIcon = getSelectedIcon(b);
282             }
283         }
284         return tmpIcon;
285     }
286
287     private Icon getSynthDisabledIcon(AbstractButton b) {
288         ButtonModel model = b.getModel();
289         Icon tmpIcon;
290         if (model.isSelected()) {
291             tmpIcon = b.getDisabledSelectedIcon();
292             if(tmpIcon == null) {
293               tmpIcon = getSynthIcon(b, SynthConstants.DISABLED|SynthConstants.SELECTED);
294             }
295         } else {
296             tmpIcon = b.getDisabledIcon();
297             if(tmpIcon == null) {
298                 tmpIcon = getSynthIcon(b, SynthConstants.DISABLED);
299             }
300         }
301         return tmpIcon;
302     }
303
304     /**
305      * Returns the amount to shift the text/icon when painting.
306      */

307     protected int getTextShiftOffset(SynthContext JavaDoc state) {
308         AbstractButton button = (AbstractButton)state.getComponent();
309         ButtonModel model = button.getModel();
310
311         if (model.isArmed() && model.isPressed() &&
312                                button.getPressedIcon() == null) {
313             return state.getStyle().getInt(state, getPropertyPrefix() +
314                                            "textShiftOffset", 0);
315         }
316         return 0;
317     }
318
319     // ********************************
320
// Layout Methods
321
// ********************************
322
public Dimension getMinimumSize(JComponent c) {
323         if (c.getComponentCount() > 0 && c.getLayout() != null) {
324             return null;
325         }
326         AbstractButton b = (AbstractButton)c;
327         SynthContext JavaDoc ss = getContext(c);
328         Dimension size = ss.getStyle().getGraphicsUtils(ss).getMinimumSize(
329                ss, ss.getStyle().getFont(ss), b.getText(), getSizingIcon(b),
330                b.getHorizontalAlignment(), b.getVerticalAlignment(),
331                b.getHorizontalTextPosition(),
332                b.getVerticalTextPosition(), b.getIconTextGap(),
333                b.getDisplayedMnemonicIndex());
334
335         ss.dispose();
336         return size;
337     }
338
339     public Dimension getPreferredSize(JComponent c) {
340         if (c.getComponentCount() > 0 && c.getLayout() != null) {
341             return null;
342         }
343         AbstractButton b = (AbstractButton)c;
344         SynthContext JavaDoc ss = getContext(c);
345         Dimension size = ss.getStyle().getGraphicsUtils(ss).getPreferredSize(
346                ss, ss.getStyle().getFont(ss), b.getText(), getSizingIcon(b),
347                b.getHorizontalAlignment(), b.getVerticalAlignment(),
348                b.getHorizontalTextPosition(),
349                b.getVerticalTextPosition(), b.getIconTextGap(),
350                b.getDisplayedMnemonicIndex());
351
352         ss.dispose();
353         return size;
354     }
355
356     public Dimension getMaximumSize(JComponent c) {
357         if (c.getComponentCount() > 0 && c.getLayout() != null) {
358             return null;
359         }
360
361         AbstractButton b = (AbstractButton)c;
362         SynthContext JavaDoc ss = getContext(c);
363         Dimension size = ss.getStyle().getGraphicsUtils(ss).getMaximumSize(
364                ss, ss.getStyle().getFont(ss), b.getText(), getSizingIcon(b),
365                b.getHorizontalAlignment(), b.getVerticalAlignment(),
366                b.getHorizontalTextPosition(),
367                b.getVerticalTextPosition(), b.getIconTextGap(),
368                b.getDisplayedMnemonicIndex());
369
370         ss.dispose();
371         return size;
372     }
373
374     /**
375      * Returns the Icon used in calculating the pref/min/max size.
376      */

377     protected Icon getSizingIcon(AbstractButton b) {
378         // NOTE: this is slightly different than BasicButtonUI, where it
379
// would just use getIcon, but this should be ok.
380
return (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon();
381     }
382
383     public void propertyChange(PropertyChangeEvent e) {
384         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
385             updateStyle((AbstractButton)e.getSource());
386         }
387     }
388 }
389
Popular Tags