2
* Copyright (c) 2002-2008 JGoodies Karsten Lentzsch. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
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.
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.
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.
31
package com.jgoodies.forms.factories;
33
import java.awt.Component;
34
import java.awt.Insets;
36
import javax.swing.border.AbstractBorder;
37
import javax.swing.border.Border;
39
import com.jgoodies.forms.layout.ConstantSize;
40
import com.jgoodies.forms.layout.Sizes;
41
import com.jgoodies.forms.util.LayoutStyle;
44
* Provides constants and factory methods for <code>Border</code>s that use
45
* instances of {@link ConstantSize} to define the margins.<p>
47
* <strong>Examples:</strong><br>
50
* Borders.createEmptyBorder(Sizes.DLUY4, Sizes.DLUX2, Sizes.DLUY4, Sizes.DLUX2);
51
* Borders.createEmptyBorder("4dlu, 2dlu, 4dlu, 2dlu");
54
* @author Karsten Lentzsch
55
* @version $Revision: 1.10 $
60
public final class Borders {
63
// Overrides default constructor; prevents instantiation.
67
// Constant Borders *****************************************************
70
* A prepared and reusable EmptyBorder without gaps.
72
public static final Border EMPTY_BORDER =
73
new javax.swing.border.EmptyBorder(0, 0, 0, 0);
77
* A prepared and reusable Border with 2dlu on all sides.
79
public static final Border DLU2_BORDER =
80
createEmptyBorder(Sizes.DLUY2,
87
* A prepared and reusable Border with 4dlu on all sides.
89
public static final Border DLU4_BORDER =
90
createEmptyBorder(Sizes.DLUY4,
97
* A prepared and reusable Border with 7dlu on all sides.
99
public static final Border DLU7_BORDER =
100
createEmptyBorder(Sizes.DLUY7,
107
* A prepared Border with 14dlu on all sides.
109
public static final Border DLU14_BORDER =
110
createEmptyBorder(Sizes.DLUY14,
117
* A prepared Border with 21dlu on all sides.
121
public static final Border DLU21_BORDER =
122
createEmptyBorder(Sizes.DLUY21,
129
* A standardized Border that describes the gap between a component
130
* and a button bar in its bottom.
132
public static final Border BUTTON_BAR_GAP_BORDER =
134
LayoutStyle.getCurrent().getButtonBarPad(),
141
* A standardized Border that describes the border around
142
* a dialog content that has no tabs.
144
* @see #TABBED_DIALOG_BORDER
146
public static final Border DIALOG_BORDER =
148
LayoutStyle.getCurrent().getDialogMarginY(),
149
LayoutStyle.getCurrent().getDialogMarginX(),
150
LayoutStyle.getCurrent().getDialogMarginY(),
151
LayoutStyle.getCurrent().getDialogMarginX()
156
* A standardized Border that describes the border around
157
* a dialog content that uses tabs.
159
* @see #DIALOG_BORDER
161
public static final Border TABBED_DIALOG_BORDER =
163
LayoutStyle.getCurrent().getTabbedDialogMarginY(),
164
LayoutStyle.getCurrent().getTabbedDialogMarginX(),
165
LayoutStyle.getCurrent().getTabbedDialogMarginY(),
166
LayoutStyle.getCurrent().getTabbedDialogMarginX()
170
// Factory Methods ******************************************************
173
* Creates and returns an <code>EmptyBorder</code> with the specified
176
* @param top the top gap
177
* @param left the left-hand side gap
178
* @param bottom the bottom gap
179
* @param right the right-hand side gap
180
* @return an <code>EmptyBorder</code> with the specified gaps
182
* @throws NullPointerException if top, left, bottom, or right is {@code null}
184
* @see #createEmptyBorder(String)
186
public static Border createEmptyBorder(ConstantSize top, ConstantSize left,
187
ConstantSize bottom, ConstantSize right) {
188
return new EmptyBorder(top, left, bottom, right);
192
* Creates and returns a <code>Border</code> using sizes as specified by
193
* the given string. This string is a comma-separated encoding of
194
* 4 <code>ConstantSize</code>s.
196
* @param encodedSizes top, left, bottom, right gap encoded as String
197
* @return an <code>EmptyBorder</code> with the specified gaps
199
* @see #createEmptyBorder(ConstantSize, ConstantSize, ConstantSize, ConstantSize)
201
public static Border createEmptyBorder(String encodedSizes) {
202
String[] token = encodedSizes.split("\\s*,\\s*");
203
int tokenCount = token.length;
204
if (token.length != 4) {
205
throw new IllegalArgumentException(
206
"The border requires 4 sizes, but \"" + encodedSizes +
207
"\" has " + tokenCount + ".");
209
ConstantSize top = Sizes.constant(token[0], false);
210
ConstantSize left = Sizes.constant(token[1], true);
211
ConstantSize bottom = Sizes.constant(token[2], false);
212
ConstantSize right = Sizes.constant(token[3], true);
213
return createEmptyBorder(top, left, bottom, right);
218
* An empty border that uses 4 instances of {@link ConstantSize}
219
* to define the top, left, bottom and right gap.
221
public static final class EmptyBorder extends AbstractBorder {
223
private final ConstantSize top;
224
private final ConstantSize left;
225
private final ConstantSize bottom;
226
private final ConstantSize right;
232
ConstantSize right) {
236
|| (right == null)) {
237
throw new NullPointerException("The top, left, bottom, and right must not be null.");
241
this.bottom = bottom;
246
* Returns the insets of the border.
248
* @param c the component for which this border insets value applies
249
* @param insets the insets to be reinitialized
250
* @return the <code>insets</code> object
252
public Insets getBorderInsets(Component c, Insets insets) {
253
insets.top = top.getPixelSize(c);
254
insets.left = left.getPixelSize(c);
255
insets.bottom = bottom.getPixelSize(c);
256
insets.right = right.getPixelSize(c);
261
* Returns the insets of the border.
263
* @param c the component for which this border insets value applies
264
* @return the border's Insets
266
public Insets getBorderInsets(Component c) {
267
return getBorderInsets(c, new Insets(0, 0, 0, 0));
271
* Returns this border's top size.
273
* @return this border's top size
275
public ConstantSize top() {
280
* Returns this border's left size.
282
* @return this border's left size
284
public ConstantSize left() {
289
* Returns this border's bottom size.
291
* @return this border's bottom size
293
public ConstantSize bottom() {
298
* Returns this border's right size.
300
* @return this border's right size
302
public ConstantSize right() {