KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Insets JavaDoc;
36
37 import javax.swing.AbstractButton JavaDoc;
38 import javax.swing.ButtonModel JavaDoc;
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JComboBox JavaDoc;
41 import javax.swing.border.AbstractBorder JavaDoc;
42 import javax.swing.border.Border JavaDoc;
43 import javax.swing.border.CompoundBorder JavaDoc;
44 import javax.swing.plaf.BorderUIResource JavaDoc;
45 import javax.swing.plaf.UIResource JavaDoc;
46 import javax.swing.plaf.basic.BasicBorders JavaDoc;
47 import javax.swing.plaf.metal.MetalBorders JavaDoc;
48 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
49 import javax.swing.text.JTextComponent JavaDoc;
50
51 import com.jgoodies.looks.LookUtils;
52
53
54 /**
55  * This class consists of a set of <code>Border</code>s used
56  * by the JGoodies Plastic XP Look and Feel UI delegates.
57  *
58  * @author Karsten Lentzsch
59  * @version $Revision: 1.2 $
60  */

61
62 final class PlasticXPBorders {
63
64
65     // Accessing and Creating Borders ***************************************
66

67     private static Border JavaDoc buttonBorder;
68     private static Border JavaDoc comboBoxArrowButtonBorder;
69     private static Border JavaDoc comboBoxEditorBorder;
70     private static Border JavaDoc scrollPaneBorder;
71     private static Border JavaDoc textFieldBorder;
72     private static Border JavaDoc toggleButtonBorder;
73     
74
75     /*
76      * Returns a border instance for a <code>JButton</code>.
77      */

78     static Border JavaDoc getButtonBorder() {
79         if (buttonBorder == null) {
80             buttonBorder = new BorderUIResource.CompoundBorderUIResource JavaDoc(
81                     new XPButtonBorder(),
82                     new BasicBorders.MarginBorder JavaDoc());
83         }
84         return buttonBorder;
85     }
86
87     /*
88      * Returns a border instance for a <code>JComboBox</code>'s arrow button.
89      */

90     static Border JavaDoc getComboBoxArrowButtonBorder() {
91         if (comboBoxArrowButtonBorder == null) {
92             comboBoxArrowButtonBorder = new CompoundBorder JavaDoc( // No UIResource
93
new XPComboBoxArrowButtonBorder(),
94                                     new BasicBorders.MarginBorder JavaDoc());
95         }
96         return comboBoxArrowButtonBorder;
97     }
98
99     /*
100      * Returns a border instance for a <code>JComboBox</code>'s editor.
101      */

102     static Border JavaDoc getComboBoxEditorBorder() {
103         if (comboBoxEditorBorder == null) {
104             comboBoxEditorBorder = new CompoundBorder JavaDoc( // No UIResource
105
new XPComboBoxEditorBorder(),
106                                     new BasicBorders.MarginBorder JavaDoc());
107         }
108         return comboBoxEditorBorder;
109     }
110
111     /*
112      * Returns a border instance for a <code>JScrollPane</code>.
113      */

114     static Border JavaDoc getScrollPaneBorder() {
115         if (scrollPaneBorder == null) {
116             scrollPaneBorder = new XPScrollPaneBorder();
117         }
118         return scrollPaneBorder;
119     }
120
121     /*
122      * Returns a border instance for a <code>JTextField</code>.
123      */

124     static Border JavaDoc getTextFieldBorder() {
125         if (textFieldBorder == null) {
126             textFieldBorder = new BorderUIResource.CompoundBorderUIResource JavaDoc(
127                                     new XPTextFieldBorder(),
128                                     new BasicBorders.MarginBorder JavaDoc());
129         }
130         return textFieldBorder;
131     }
132
133     /*
134      * Returns a border instance for a <code>JToggleButton</code>.
135      */

136     static Border JavaDoc getToggleButtonBorder() {
137         if (toggleButtonBorder == null) {
138             toggleButtonBorder = new BorderUIResource.CompoundBorderUIResource JavaDoc(
139                     new XPButtonBorder(),
140                     new BasicBorders.MarginBorder JavaDoc());
141         }
142         return toggleButtonBorder;
143     }
144
145     /*
146      * A border for buttons.
147      */

148     private static class XPButtonBorder extends AbstractBorder JavaDoc implements UIResource JavaDoc {
149
150         protected static final Insets JavaDoc INSETS = LookUtils.IS_LOW_RESOLUTION
151             ? new Insets JavaDoc(3, 2, 3, 2)
152             : new Insets JavaDoc(2, 2, 2, 2);
153
154         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int w, int h) {
155             AbstractButton JavaDoc button = (AbstractButton JavaDoc) c;
156             ButtonModel JavaDoc model = button.getModel();
157
158             if (!model.isEnabled()) {
159                 PlasticXPUtils.drawDisabledButtonBorder(g, x, y, w, h);
160                 return;
161             }
162             
163             boolean isPressed = model.isPressed() && model.isArmed();
164             boolean isDefault = button instanceof JButton JavaDoc
165                                      && ((JButton JavaDoc) button).isDefaultButton();
166             boolean isFocused = button.isFocusPainted() && button.hasFocus();
167
168             if (isPressed)
169                 PlasticXPUtils.drawPressedButtonBorder(g, x, y, w, h);
170             else if (isFocused)
171                 PlasticXPUtils.drawFocusedButtonBorder(g, x, y, w, h);
172             else if (isDefault)
173                 PlasticXPUtils.drawDefaultButtonBorder(g, x, y, w, h);
174             else
175                 PlasticXPUtils.drawPlainButtonBorder(g, x, y, w, h);
176         }
177
178         public Insets JavaDoc getBorderInsets(Component JavaDoc c) { return INSETS; }
179
180         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc newInsets) {
181             newInsets.top = INSETS.top;
182             newInsets.left = INSETS.left;
183             newInsets.bottom = INSETS.bottom;
184             newInsets.right = INSETS.right;
185             return newInsets;
186         }
187     }
188
189
190     /*
191      * A border for combo box arrow buttons.
192      */

193     private static class XPComboBoxArrowButtonBorder extends AbstractBorder JavaDoc implements UIResource JavaDoc {
194
195         protected static final Insets JavaDoc INSETS = new Insets JavaDoc(1, 1, 1, 1);
196
197         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int w, int h) {
198             PlasticComboBoxButton button = (PlasticComboBoxButton) c;
199             JComboBox JavaDoc comboBox = button.getComboBox();
200             ButtonModel JavaDoc model = button.getModel();
201
202             if (!model.isEnabled()) {
203                 PlasticXPUtils.drawDisabledButtonBorder(g, x, y, w, h);
204             } else {
205                 boolean isPressed = model.isPressed() && model.isArmed();
206                 boolean isFocused = comboBox.hasFocus();
207                 if (isPressed)
208                     PlasticXPUtils.drawPressedButtonBorder(g, x, y, w, h);
209                 else if (isFocused)
210                     PlasticXPUtils.drawFocusedButtonBorder(g, x, y, w, h);
211                 else
212                     PlasticXPUtils.drawPlainButtonBorder(g, x, y, w, h);
213             }
214             if (comboBox.isEditable()) {
215                 // Paint two pixel on the arrow button's left hand side.
216
g.setColor(model.isEnabled()
217                                 ? PlasticLookAndFeel.getControlDarkShadow()
218                                 : MetalLookAndFeel.getControlShadow());
219                 g.fillRect(x, y, 1, 1);
220                 g.fillRect(x, y + h-1, 1, 1);
221             }
222         }
223
224         public Insets JavaDoc getBorderInsets(Component JavaDoc c) { return INSETS; }
225     }
226
227
228     /*
229      * A border for combo box editors.
230      */

231     private static class XPComboBoxEditorBorder extends AbstractBorder JavaDoc {
232
233         private static final Insets JavaDoc INSETS = new Insets JavaDoc(1, 1, 1, 0);
234
235         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int w, int h) {
236             g.setColor(c.isEnabled()
237                            ? PlasticLookAndFeel.getControlDarkShadow()
238                            : MetalLookAndFeel.getControlShadow());
239             PlasticXPUtils.drawRect(g, x, y, w+1, h-1);
240         }
241
242         public Insets JavaDoc getBorderInsets(Component JavaDoc c) { return INSETS; }
243     }
244
245
246     /*
247      * A border for text fields.
248      */

249     private static class XPTextFieldBorder extends AbstractBorder JavaDoc {
250
251         private static final Insets JavaDoc INSETS = new Insets JavaDoc(1, 1, 1, 1);
252
253         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int w, int h) {
254             
255             boolean enabled = ((c instanceof JTextComponent JavaDoc)
256                                && (c.isEnabled() && ((JTextComponent JavaDoc) c).isEditable()))
257                                ||
258                                c.isEnabled();
259         
260             g.setColor(enabled
261                             ? PlasticLookAndFeel.getControlDarkShadow()
262                             : MetalLookAndFeel.getControlShadow());
263             PlasticXPUtils.drawRect(g, x, y, w-1, h-1);
264         }
265  
266         public Insets JavaDoc getBorderInsets(Component JavaDoc c) { return INSETS; }
267
268         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc newInsets) {
269             newInsets.top = INSETS.top;
270             newInsets.left = INSETS.left;
271             newInsets.bottom = INSETS.bottom;
272             newInsets.right = INSETS.right;
273             return newInsets;
274         }
275     }
276
277     /**
278      * Unlike Metal we paint a simple rectangle.
279      * Being a subclass of MetalBorders.ScrollPaneBorder ensures that
280      * the ScrollPaneUI will update the ScrollbarsFreeStanding property.
281      */

282     private static class XPScrollPaneBorder extends MetalBorders.ScrollPaneBorder JavaDoc {
283
284         private static final Insets JavaDoc INSETS = new Insets JavaDoc(1, 1, 1, 1);
285
286         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int w, int h) {
287             g.setColor(c.isEnabled()
288                     ? PlasticLookAndFeel.getControlDarkShadow()
289                     : MetalLookAndFeel.getControlShadow());
290             PlasticXPUtils.drawRect(g, x, y, w-1, h-1);
291         }
292         
293         public Insets JavaDoc getBorderInsets(Component JavaDoc c) { return INSETS; }
294
295         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc newInsets) {
296             newInsets.top = INSETS.top;
297             newInsets.left = INSETS.left;
298             newInsets.bottom = INSETS.bottom;
299             newInsets.right = INSETS.right;
300             return newInsets;
301         }
302     }
303
304     
305 }
Popular Tags