001: /*
002: * Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.forms.factories;
032:
033: import java.awt.Component;
034: import java.awt.Graphics;
035: import java.awt.Insets;
036: import java.util.StringTokenizer;
037:
038: import javax.swing.border.Border;
039:
040: import com.jgoodies.forms.layout.ConstantSize;
041: import com.jgoodies.forms.layout.Sizes;
042: import com.jgoodies.forms.util.LayoutStyle;
043:
044: /**
045: * Provides constants and factory methods for <code>Border</code>s that use
046: * instances of {@link ConstantSize} to define the margins.
047: * <p>
048: *
049: * <strong>Examples:</strong><br>
050: *
051: * <pre>
052: * Borders.DLU2_BORDER
053: * Borders.createEmptyBorder(Sizes.DLUY4, Sizes.DLUX2, Sizes.DLUY4, Sizes.DLUX2);
054: * Borders.createEmptyBorder("4dlu, 2dlu, 4dlu, 2dlu");
055: * </pre>
056: *
057: * @author Karsten Lentzsch
058: * @version $Revision: 1.2 $
059: *
060: * @see Border
061: * @see Sizes
062: */
063: public final class Borders {
064:
065: // Constant Borders *****************************************************
066:
067: /**
068: * A prepared and reusable EmptyBorder without gaps.
069: */
070: public static final Border EMPTY_BORDER = new javax.swing.border.EmptyBorder(
071: 0, 0, 0, 0);
072:
073: /**
074: * A prepared and reusable Border with 2dlu on all sides.
075: */
076: public static final Border DLU2_BORDER = createEmptyBorder(
077: Sizes.DLUY2, Sizes.DLUX2, Sizes.DLUY2, Sizes.DLUX2);
078:
079: /**
080: * A prepared and reusable Border with 4dlu on all sides.
081: */
082: public static final Border DLU4_BORDER = createEmptyBorder(
083: Sizes.DLUY4, Sizes.DLUX4, Sizes.DLUY4, Sizes.DLUX4);
084:
085: /**
086: * A prepared and reusable Border with 7dlu on all sides.
087: */
088: public static final Border DLU7_BORDER = createEmptyBorder(
089: Sizes.DLUY7, Sizes.DLUX7, Sizes.DLUY7, Sizes.DLUX7);
090:
091: /**
092: * A prepared Border with 14dlu on all sides.
093: */
094: public static final Border DLU14_BORDER = createEmptyBorder(
095: Sizes.DLUY14, Sizes.DLUX14, Sizes.DLUY14, Sizes.DLUX14);
096:
097: /**
098: * A standardized Border that describes the gap between a component and a
099: * button bar in its bottom.
100: */
101: public static final Border BUTTON_BAR_GAP_BORDER = createEmptyBorder(
102: LayoutStyle.getCurrent().getButtonBarPad(), Sizes.dluX(0),
103: Sizes.dluY(0), Sizes.dluX(0));
104:
105: /**
106: * A standardized Border that describes the border around a dialog content
107: * that has no tabs.
108: *
109: * @see #TABBED_DIALOG_BORDER
110: */
111: public static final Border DIALOG_BORDER = createEmptyBorder(
112: LayoutStyle.getCurrent().getDialogMarginY(), LayoutStyle
113: .getCurrent().getDialogMarginX(), LayoutStyle
114: .getCurrent().getDialogMarginY(), LayoutStyle
115: .getCurrent().getDialogMarginX());
116:
117: /**
118: * A standardized Border that describes the border around a dialog content
119: * that uses tabs.
120: *
121: * @see #DIALOG_BORDER
122: */
123: public static final Border TABBED_DIALOG_BORDER = createEmptyBorder(
124: LayoutStyle.getCurrent().getTabbedDialogMarginY(),
125: LayoutStyle.getCurrent().getTabbedDialogMarginX(),
126: LayoutStyle.getCurrent().getTabbedDialogMarginY(),
127: LayoutStyle.getCurrent().getTabbedDialogMarginX());
128:
129: // Factory Methods ******************************************************
130:
131: /**
132: * Creates and returns an <code>EmptyBorder</code> with the specified
133: * gaps.
134: *
135: * @param top
136: * the top gap
137: * @param left
138: * the left-hand side gap
139: * @param bottom
140: * the bottom gap
141: * @param right
142: * the right-hand side gap
143: * @return an <code>EmptyBorder</code> with the specified gaps
144: *
145: * @see #createEmptyBorder(String)
146: */
147: public static Border createEmptyBorder(ConstantSize top,
148: ConstantSize left, ConstantSize bottom, ConstantSize right) {
149: return new EmptyBorder(top, left, bottom, right);
150: }
151:
152: /**
153: * Creates and returns a <code>Border</code> using sizes as specified by
154: * the given string. This string is a comma-separated encoding of 4
155: * <code>ConstantSize</code>s.
156: *
157: * @param encodedSizes
158: * top, left, bottom, right gap encoded as String
159: * @return an <code>EmptyBorder</code> with the specified gaps
160: *
161: * @see #createEmptyBorder(ConstantSize, ConstantSize, ConstantSize,
162: * ConstantSize)
163: */
164: public static Border createEmptyBorder(String encodedSizes) {
165: StringTokenizer tokenizer = new StringTokenizer(encodedSizes,
166: ", ");
167: int tokenCount = tokenizer.countTokens();
168: if (tokenCount != 4) {
169: throw new IllegalArgumentException(
170: "The border requires 4 sizes, but '" + encodedSizes
171: + "' has " + tokenCount + ".");
172: }
173: ConstantSize top = Sizes.constant(tokenizer.nextToken(), false);
174: ConstantSize left = Sizes.constant(tokenizer.nextToken(), true);
175: ConstantSize bottom = Sizes.constant(tokenizer.nextToken(),
176: false);
177: ConstantSize right = Sizes
178: .constant(tokenizer.nextToken(), true);
179: return createEmptyBorder(top, left, bottom, right);
180: }
181:
182: /**
183: * An empty border that uses 4 instances of {@link ConstantSize} to define
184: * the gaps on all sides.
185: */
186: public static final class EmptyBorder implements Border {
187:
188: private final ConstantSize top;
189: private final ConstantSize left;
190: private final ConstantSize bottom;
191: private final ConstantSize right;
192:
193: private EmptyBorder(ConstantSize top, ConstantSize left,
194: ConstantSize bottom, ConstantSize right) {
195: this .top = top;
196: this .left = left;
197: this .bottom = bottom;
198: this .right = right;
199: }
200:
201: /**
202: * Returns this border's top size.
203: *
204: * @return this border's top size
205: */
206: public ConstantSize top() {
207: return top;
208: }
209:
210: /**
211: * Returns this border's left size.
212: *
213: * @return this border's left size
214: */
215: public ConstantSize left() {
216: return left;
217: }
218:
219: /**
220: * Returns this border's bottom size.
221: *
222: * @return this border's bottom size
223: */
224: public ConstantSize bottom() {
225: return bottom;
226: }
227:
228: /**
229: * Returns this border's right size.
230: *
231: * @return this border's right size
232: */
233: public ConstantSize right() {
234: return right;
235: }
236:
237: /**
238: * Paints the border for the specified component with the specified
239: * position and size.
240: *
241: * @param c
242: * the component for which this border is being painted
243: * @param g
244: * the paint graphics
245: * @param x
246: * the x position of the painted border
247: * @param y
248: * the y position of the painted border
249: * @param width
250: * the width of the painted border
251: * @param height
252: * the height of the painted border
253: */
254: public void paintBorder(Component c, Graphics g, int x, int y,
255: int width, int height) {
256: // An empty border doesn't paint.
257: }
258:
259: /**
260: * Returns the insets of the border.
261: *
262: * @param c
263: * the component for which this border insets value applies
264: * @return the border's Insets
265: */
266: public Insets getBorderInsets(Component c) {
267: return new Insets(top.getPixelSize(c),
268: left.getPixelSize(c), bottom.getPixelSize(c), right
269: .getPixelSize(c));
270: }
271:
272: /**
273: * Returns whether or not the border is opaque. If the border is opaque,
274: * it is responsible for filling in it's own background when painting.
275: *
276: * @return false - because the empty border is not opaque
277: */
278: public boolean isBorderOpaque() {
279: return false;
280: }
281:
282: }
283:
284: }
|