KickJava   Java API By Example, From Geeks To Geeks.

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


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.Container JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Rectangle JavaDoc;
36 import java.beans.PropertyChangeListener JavaDoc;
37
38 import javax.swing.AbstractButton JavaDoc;
39 import javax.swing.ButtonModel JavaDoc;
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JComponent JavaDoc;
42 import javax.swing.JToolBar JavaDoc;
43 import javax.swing.UIManager JavaDoc;
44 import javax.swing.border.EmptyBorder JavaDoc;
45 import javax.swing.plaf.ComponentUI JavaDoc;
46 import javax.swing.plaf.metal.MetalButtonUI 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>ButtonUI</code>.
54  * It differs from the superclass <code>MetalButtonUI</code> in that
55  * it can add a pseudo 3D effect, and that it listens to the
56  * <code>jgoodies.isNarrow</code> property to choose an appropriate margin.
57  *
58  * @author Karsten Lentzsch
59  * @version $Revision: 1.4 $
60  *
61  * @see com.jgoodies.looks.Options#IS_NARROW_KEY
62  */

63 public class PlasticButtonUI extends MetalButtonUI JavaDoc {
64
65     private static final PlasticButtonUI INSTANCE = new PlasticButtonUI();
66
67     private boolean borderPaintsFocus;
68     private PropertyChangeListener JavaDoc buttonMarginListener;
69
70     public static ComponentUI JavaDoc createUI(JComponent JavaDoc b) {
71         return INSTANCE;
72     }
73
74     /**
75      * Installs defaults and honors the client property <code>isNarrow</code>.
76      */

77     public void installDefaults(AbstractButton JavaDoc b) {
78         super.installDefaults(b);
79         LookUtils.installNarrowMargin(b, getPropertyPrefix());
80         borderPaintsFocus =
81             Boolean.TRUE.equals(UIManager.get("Button.borderPaintsFocus"));
82     }
83
84     /**
85      * Installs an extra listener for a change of the isNarrow property.
86      */

87     public void installListeners(AbstractButton JavaDoc b) {
88         super.installListeners(b);
89         if (buttonMarginListener == null) {
90             buttonMarginListener = new ButtonMarginListener(getPropertyPrefix());
91         }
92         b.addPropertyChangeListener(Options.IS_NARROW_KEY, buttonMarginListener);
93     }
94
95     /**
96      * Uninstalls the extra listener for a change of the isNarrow property.
97      */

98     public void uninstallListeners(AbstractButton JavaDoc b) {
99         super.uninstallListeners(b);
100         b.removePropertyChangeListener(buttonMarginListener);
101     }
102
103     // Painting ***************************************************************
104

105     public void update(Graphics JavaDoc g, JComponent JavaDoc c) {
106         if (c.isOpaque()) {
107             AbstractButton JavaDoc b = (AbstractButton JavaDoc) c;
108             if (isToolBarButton(b)) {
109                 c.setOpaque(false);
110             } else if (b.isContentAreaFilled()) {
111                 g.setColor(c.getBackground());
112                 g.fillRect(0, 0, c.getWidth(), c.getHeight());
113
114                 if (is3D(b)) {
115                     Rectangle JavaDoc r =
116                         new Rectangle JavaDoc(
117                             1,
118                             1,
119                             c.getWidth() - 2,
120                             c.getHeight() - 1);
121                     PlasticUtils.add3DEffekt(g, r);
122                 }
123             }
124         }
125         paint(g, c);
126     }
127
128     /**
129      * Paints the focus with close to the button's border.
130      */

131     protected void paintFocus(
132         Graphics JavaDoc g,
133         AbstractButton JavaDoc b,
134         Rectangle JavaDoc viewRect,
135         Rectangle JavaDoc textRect,
136         Rectangle JavaDoc iconRect) {
137
138         if (borderPaintsFocus) {
139             return;
140         }
141
142         boolean isDefault =
143             b instanceof JButton JavaDoc && ((JButton JavaDoc) b).isDefaultButton();
144         int topLeftInset = isDefault ? 3 : 2;
145         int width = b.getWidth() - 1 - topLeftInset * 2;
146         int height = b.getHeight() - 1 - topLeftInset * 2;
147
148         g.setColor(getFocusColor());
149         g.drawRect(topLeftInset, topLeftInset, width - 1, height - 1);
150     }
151
152     // Private Helper Code **************************************************************
153

154     /**
155      * Checks and answers if this is button is in a tool bar.
156      *
157      * @param b the button to check
158      * @return true if in tool bar, false otherwise
159      */

160     protected boolean isToolBarButton(AbstractButton JavaDoc b) {
161         Container JavaDoc parent = b.getParent();
162         return parent != null
163             && (parent instanceof JToolBar JavaDoc
164                 || parent.getParent() instanceof JToolBar JavaDoc);
165     }
166
167     /**
168      * Checks and answers if this button shall use a pseudo 3D effect
169      *
170      * @param b the button to check
171      * @return true indicates a 3D effect, false flat
172      */

173     protected boolean is3D(AbstractButton JavaDoc b) {
174         if (PlasticUtils.force3D(b))
175             return true;
176         if (PlasticUtils.forceFlat(b))
177             return false;
178         ButtonModel JavaDoc model = b.getModel();
179         return PlasticUtils.is3D("Button.")
180             && b.isBorderPainted()
181             && model.isEnabled()
182             && !(model.isPressed() && model.isArmed())
183             && !(b.getBorder() instanceof EmptyBorder JavaDoc);
184
185         /*
186          * Implementation note regarding the last line: instead of checking
187          * for the EmptyBorder in NetBeans, I'd prefer to just check the
188          * 'borderPainted' property. I'd recommend to the NetBeans developers,
189          * to switch this property on and off, instead of changing the border.
190          */

191     }
192
193 }
Popular Tags