KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > factories > Borders


1 /*
2  * Copyright (c) 2003 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.forms.factories;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Insets JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37
38 import javax.swing.border.Border JavaDoc;
39
40 import com.jgoodies.forms.layout.ConstantSize;
41 import com.jgoodies.forms.layout.Sizes;
42
43 /**
44  * Provides constants and factory methods for <code>Border</code>s that use
45  * instances of {@link ConstantSize} to define the margins.
46  * <p>
47  * <b>Examples:</b><br>
48  * <pre>
49  * Borders.DLU2_BORDER
50  * Borders.createEmptyBorder(Sizes.DLUY4, Sizes.DLUX2, Sizes.DLUY4, Sizes.DLUX2);
51  * Borders.createEmptyBorder("4dlu, 2dlu, 4dlu, 2dlu");
52  * </pre>
53  * <p>
54  * TODO: Honor the current LayoutStyle
55  *
56  * @author Karsten Lentzsch
57  * @version $Revision: 1.4 $
58  * @see Border
59  * @see Sizes
60  */

61 public final class Borders {
62
63
64     // Constant Borders *****************************************************
65

66     /**
67      * A prepared and reusable EmptyBorder without gaps.
68      */

69     public static final Border JavaDoc EMPTY_BORDER =
70         new javax.swing.border.EmptyBorder JavaDoc(0, 0, 0, 0);
71         
72     /**
73      * A prepared and reusable Border with 2dlu on all sides.
74      */

75     public static final Border JavaDoc DLU2_BORDER =
76         createEmptyBorder(Sizes.DLUY2,
77                           Sizes.DLUX2,
78                           Sizes.DLUY2,
79                           Sizes.DLUX2);
80                         
81     /**
82      * A prepared and reusable Border with 4dlu on all sides.
83      */

84     public static final Border JavaDoc DLU4_BORDER =
85         createEmptyBorder(Sizes.DLUY4,
86                           Sizes.DLUX4,
87                           Sizes.DLUY4,
88                           Sizes.DLUX4);
89                         
90     /**
91      * A prepared and reusable Border with 7dlu on all sides.
92      */

93     public static final Border JavaDoc DLU7_BORDER =
94         createEmptyBorder(Sizes.DLUY7,
95                           Sizes.DLUX7,
96                           Sizes.DLUY7,
97                           Sizes.DLUX7);
98                         
99     /**
100      * A prepared Border with 14dlu on all sides.
101      */

102     public static final Border JavaDoc DLU14_BORDER =
103         createEmptyBorder(Sizes.DLUY14,
104                           Sizes.DLUX14,
105                           Sizes.DLUY14,
106                           Sizes.DLUX14);
107                         
108     /**
109      * A standardized Border that describes the gap between a dialog
110      * content and a bottom button bar.
111      * A future version of this constant shall honor the
112      * {@link com.jgoodies.forms.util.LayoutStyle}.
113      */

114     public static final Border JavaDoc BUTTON_BAR_GAP_BORDER =
115         createEmptyBorder(Sizes.DLUY6, Sizes.ZERO, Sizes.ZERO, Sizes.ZERO);
116                         
117     /**
118      * A standardized Border that describes the border around
119      * a dialog content that has no tabs.
120      * A future version of this constant shall honor the
121      * {@link com.jgoodies.forms.util.LayoutStyle}.
122      */

123     public static final Border JavaDoc DIALOG_BORDER =
124         DLU7_BORDER;
125         
126     /**
127      * A standardized Border that describes the border around
128      * a dialog content that uses tabs.
129      * A future version of this constant shall honor the
130      * {@link com.jgoodies.forms.util.LayoutStyle}.
131      */

132     public static final Border JavaDoc TABBED_DIALOG_BORDER =
133         DLU4_BORDER;
134     
135     
136     // Factory Methods ******************************************************
137

138     /**
139      * Creates and answers an <code>EmptyBorder</code> with the specified
140      * gaps.
141      *
142      * @param top the top gap
143      * @param left the left-hand side gap
144      * @param bottom the bottom gap
145      * @param right the right-hand side gap
146      * @return an <code>EmptyBorder</code> with the specified gaps
147      */

148     public static Border JavaDoc createEmptyBorder(ConstantSize top, ConstantSize left,
149                                             ConstantSize bottom, ConstantSize right) {
150         return new EmptyBorder(top, left, bottom, right);
151     }
152     
153     /**
154      * Creates and answers a <code>Border</code> using sizes as specified by
155      * the given string. This string is a comma-separated encoding of
156      * 4 <code>ConstantSize</code>s.
157      *
158      * @param encodedSizes top, left, bottom, right gap encoded as String
159      * @return an <code>EmptyBorder</code> with the specified gaps
160      */

161     public static Border JavaDoc createEmptyBorder(String JavaDoc encodedSizes) {
162         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(encodedSizes, ", ");
163         int tokenCount = tokenizer.countTokens();
164         if (tokenCount != 4) {
165             throw new IllegalArgumentException JavaDoc(
166                 "The border requires 4 sizes, but '" + encodedSizes +
167                 "' has " + tokenCount + ".");
168         }
169         ConstantSize top = Sizes.constant(tokenizer.nextToken(), false);
170         ConstantSize left = Sizes.constant(tokenizer.nextToken(), true);
171         ConstantSize bottom = Sizes.constant(tokenizer.nextToken(), false);
172         ConstantSize right = Sizes.constant(tokenizer.nextToken(), true);
173         return createEmptyBorder(top, left, bottom, right);
174     }
175
176     /**
177      * An empty border that uses 4 instances of {@link ConstantSize}
178      * to define the gaps on all sides.
179      */

180     public static final class EmptyBorder implements Border JavaDoc {
181         
182         private final ConstantSize top;
183         private final ConstantSize left;
184         private final ConstantSize bottom;
185         private final ConstantSize right;
186         
187         EmptyBorder(ConstantSize top,
188                                 ConstantSize left,
189                                 ConstantSize bottom,
190                                 ConstantSize right) {
191             this.top = top;
192             this.left = left;
193             this.bottom = bottom;
194             this.right = right;
195         }
196
197         /**
198          * Paints the border for the specified component with the specified
199          * position and size.
200          *
201          * @param c the component for which this border is being painted
202          * @param g the paint graphics
203          * @param x the x position of the painted border
204          * @param y the y position of the painted border
205          * @param width the width of the painted border
206          * @param height the height of the painted border
207          */

208         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g,
209                                 int x, int y, int width, int height) {
210             // An empty border doesn't paint.
211
}
212     
213         /**
214          * Returns the insets of the border.
215          * @param c the component for which this border insets value applies
216          */

217         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
218             return new Insets JavaDoc(top.getPixelSize(c),
219                                left.getPixelSize(c),
220                                bottom.getPixelSize(c),
221                                right.getPixelSize(c));
222         }
223     
224         /**
225          * Returns whether or not the border is opaque. If the border
226          * is opaque, it is responsible for filling in it's own
227          * background when painting.
228          */

229         public boolean isBorderOpaque() {
230             return false;
231         }
232
233     }
234
235
236 }
237
Popular Tags