KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > plastic > PlasticToggleButtonUI


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.plastic;
32
33 import java.awt.*;
34 import java.beans.PropertyChangeListener JavaDoc;
35
36 import javax.swing.AbstractButton JavaDoc;
37 import javax.swing.ButtonModel JavaDoc;
38 import javax.swing.JComponent JavaDoc;
39 import javax.swing.JToolBar JavaDoc;
40 import javax.swing.SwingUtilities JavaDoc;
41 import javax.swing.UIManager JavaDoc;
42 import javax.swing.border.EmptyBorder JavaDoc;
43 import javax.swing.plaf.ComponentUI JavaDoc;
44 import javax.swing.plaf.basic.BasicHTML JavaDoc;
45 import javax.swing.plaf.metal.MetalToggleButtonUI JavaDoc;
46 import javax.swing.text.View JavaDoc;
47
48 import com.jgoodies.looks.LookUtils;
49 import com.jgoodies.looks.Options;
50 import com.jgoodies.looks.common.ButtonMarginListener;
51
52 /**
53  * The JGoodies Plastic L&amp;F implementation of <code>ToggleButtonUI</code>.
54  * It differs from its superclass in that it can add a pseudo 3D effect,
55  * and that it listens to the <code>jgoodies.isNarrow</code> property to
56  * choose an appropriate margin.
57  *
58  * @author Karsten Lentzsch
59  * @version $Revision: 1.3 $
60  *
61  * @see com.jgoodies.looks.Options#IS_NARROW_KEY
62  */

63 public class PlasticToggleButtonUI extends MetalToggleButtonUI JavaDoc {
64
65     private static final PlasticToggleButtonUI INSTANCE =
66         new PlasticToggleButtonUI();
67     
68     /*
69      * Implementation note: The protected visibility prevents
70      * the String value from being encrypted by the obfuscator.
71      * An encrypted String key would break the client property lookup
72      * in the #paint method below.
73      */

74     protected static final String JavaDoc HTML_KEY = BasicHTML.propertyKey;
75
76     private boolean borderPaintsFocus;
77     private PropertyChangeListener JavaDoc buttonMarginListener;
78
79     public static ComponentUI JavaDoc createUI(JComponent JavaDoc b) {
80         return INSTANCE;
81     }
82
83     /**
84      * Installs defaults and honors the client property <code>isNarrow</code>.
85      */

86     public void installDefaults(AbstractButton JavaDoc b) {
87         super.installDefaults(b);
88         LookUtils.installNarrowMargin(b, getPropertyPrefix());
89         borderPaintsFocus =
90             Boolean.TRUE.equals(
91                 UIManager.get("ToggleButton.borderPaintsFocus"));
92     }
93
94     /**
95      * Installs an extra listener for a change of the isNarrow property.
96      */

97     public void installListeners(AbstractButton JavaDoc b) {
98         super.installListeners(b);
99         if (buttonMarginListener == null) {
100             buttonMarginListener = new ButtonMarginListener(getPropertyPrefix());
101         }
102         b.addPropertyChangeListener(Options.IS_NARROW_KEY, buttonMarginListener);
103     }
104
105     /**
106      * Uninstalls the extra listener for a change of the isNarrow property.
107      */

108     public void uninstallListeners(AbstractButton JavaDoc b) {
109         super.uninstallListeners(b);
110         b.removePropertyChangeListener(buttonMarginListener);
111     }
112
113     // Painting ***************************************************************
114

115     public void update(Graphics g, JComponent JavaDoc c) {
116         AbstractButton JavaDoc b = (AbstractButton JavaDoc) c;
117         if (c.isOpaque()) {
118             if (isToolBarButton(b)) {
119                 c.setOpaque(false);
120             } else if (b.isContentAreaFilled()) {
121                 g.setColor(c.getBackground());
122                 g.fillRect(0, 0, c.getWidth(), c.getHeight());
123
124                 if (is3D(b)) {
125                     Rectangle r =
126                         new Rectangle(
127                             1,
128                             1,
129                             c.getWidth() - 2,
130                             c.getHeight() - 1);
131                     PlasticUtils.add3DEffekt(g, r);
132                 }
133             }
134         }
135         paint(g, c);
136     }
137
138     /**
139      * Paints the focus close to the button's border.
140      */

141     protected void paintFocus(
142         Graphics g,
143         AbstractButton JavaDoc b,
144         Rectangle viewRect,
145         Rectangle textRect,
146         Rectangle iconRect) {
147
148         if (borderPaintsFocus)
149             return;
150
151         boolean isDefault = false;
152         int topLeftInset = isDefault ? 3 : 2;
153         int width = b.getWidth() - 1 - topLeftInset * 2;
154         int height = b.getHeight() - 1 - topLeftInset * 2;
155
156         g.setColor(getFocusColor());
157         g.drawRect(topLeftInset, topLeftInset, width - 1, height - 1);
158     }
159
160     /**
161      * Unlike the BasicToggleButtonUI.paint, we don't fill the content area;
162      * this has been done by the update method before.
163      */

164     public void paint(Graphics g, JComponent JavaDoc c) {
165         AbstractButton JavaDoc b = (AbstractButton JavaDoc) c;
166         ButtonModel JavaDoc model = b.getModel();
167
168         Dimension size = b.getSize();
169         FontMetrics fm = g.getFontMetrics();
170
171         Insets i = c.getInsets();
172
173         Rectangle viewRect = new Rectangle(size);
174
175         viewRect.x += i.left;
176         viewRect.y += i.top;
177         viewRect.width -= (i.right + viewRect.x);
178         viewRect.height -= (i.bottom + viewRect.y);
179
180         Rectangle iconRect = new Rectangle();
181         Rectangle textRect = new Rectangle();
182
183         Font f = c.getFont();
184         g.setFont(f);
185
186         // layout the text and icon
187
String JavaDoc text =
188             SwingUtilities.layoutCompoundLabel(
189                 c,
190                 fm,
191                 b.getText(),
192                 b.getIcon(),
193                 b.getVerticalAlignment(),
194                 b.getHorizontalAlignment(),
195                 b.getVerticalTextPosition(),
196                 b.getHorizontalTextPosition(),
197                 viewRect,
198                 iconRect,
199                 textRect,
200                 b.getText() == null ? 0 : b.getIconTextGap());
201         
202         g.setColor(b.getBackground());
203
204         if (model.isArmed() && model.isPressed() || model.isSelected()) {
205             paintButtonPressed(g, b);
206         } /*else if (b.isOpaque() && b.isContentAreaFilled() && !(is3D(b))) {
207                     g.fillRect(0, 0, size.width, size.height);
208                     
209                     Insets insets = b.getInsets();
210                     Insets margin = b.getMargin();
211                 
212                     g.fillRect(insets.left - margin.left, insets.top - margin.top,
213                        size.width - (insets.left-margin.left) - (insets.right - margin.right),
214                        size.height - (insets.top-margin.top) - (insets.bottom - margin.bottom));
215                        
216                 }*/

217
218         // Paint the Icon
219
if (b.getIcon() != null) {
220             paintIcon(g, b, iconRect);
221         }
222
223         // Draw the Text
224
if (text != null && !text.equals("")) {
225             View JavaDoc v = (View JavaDoc) c.getClientProperty(HTML_KEY);
226             if (v != null) {
227                 v.paint(g, textRect);
228             } else {
229                 paintText(g, c, textRect, text);
230             }
231         }
232
233         // draw the dashed focus line.
234
if (b.isFocusPainted() && b.hasFocus()) {
235             paintFocus(g, b, viewRect, textRect, iconRect);
236         }
237     }
238
239     // Private Helper Code **************************************************************
240

241     /**
242      * Checks and answers if this is button is in a tool bar.
243      *
244      * @param b the button to check
245      * @return true if in tool bar, false otherwise
246      */

247     protected boolean isToolBarButton(AbstractButton JavaDoc b) {
248         Container parent = b.getParent();
249         return parent != null
250             && (parent instanceof JToolBar JavaDoc
251              || parent.getParent() instanceof JToolBar JavaDoc);
252     }
253
254     /**
255      * Checks and answers if this button shall use a pseudo 3D effect
256      *
257      * @param b the button to check
258      * @return true indicates a 3D effect, false flat
259      */

260     protected boolean is3D(AbstractButton JavaDoc b) {
261         if (PlasticUtils.force3D(b))
262             return true;
263         if (PlasticUtils.forceFlat(b))
264             return false;
265         ButtonModel JavaDoc model = b.getModel();
266         return PlasticUtils.is3D("ToggleButton.")
267             && b.isBorderPainted()
268             && model.isEnabled()
269             && !model.isPressed()
270             && !(b.getBorder() instanceof EmptyBorder JavaDoc);
271
272         /*
273          * Implementation note regarding the last line: instead of checking
274          * for the EmptyBorder in NetBeans, I'd prefer to just check the
275          * 'borderPainted' property. I'd recommend to the NetBeans developers,
276          * to switch this property on and off, instead of changing the border.
277          */

278     }
279
280 }
Popular Tags