001: /*
002: * Copyright (c) 2002-2007 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.<p>
047: *
048: * <strong>Examples:</strong><br>
049: * <pre>
050: * Borders.DLU2_BORDER
051: * Borders.createEmptyBorder(Sizes.DLUY4, Sizes.DLUX2, Sizes.DLUY4, Sizes.DLUX2);
052: * Borders.createEmptyBorder("4dlu, 2dlu, 4dlu, 2dlu");
053: * </pre>
054: *
055: * @author Karsten Lentzsch
056: * @version $Revision: 1.2 $
057: *
058: * @see Border
059: * @see Sizes
060: */
061: public final class Borders {
062:
063: private Borders() {
064: // Overrides default constructor; prevents instantiation.
065: }
066:
067: // Constant Borders *****************************************************
068:
069: /**
070: * A prepared and reusable EmptyBorder without gaps.
071: */
072: public static final Border EMPTY_BORDER = new javax.swing.border.EmptyBorder(
073: 0, 0, 0, 0);
074:
075: /**
076: * A prepared and reusable Border with 2dlu on all sides.
077: */
078: public static final Border DLU2_BORDER = createEmptyBorder(
079: Sizes.DLUY2, Sizes.DLUX2, Sizes.DLUY2, Sizes.DLUX2);
080:
081: /**
082: * A prepared and reusable Border with 4dlu on all sides.
083: */
084: public static final Border DLU4_BORDER = createEmptyBorder(
085: Sizes.DLUY4, Sizes.DLUX4, Sizes.DLUY4, Sizes.DLUX4);
086:
087: /**
088: * A prepared and reusable Border with 7dlu on all sides.
089: */
090: public static final Border DLU7_BORDER = createEmptyBorder(
091: Sizes.DLUY7, Sizes.DLUX7, Sizes.DLUY7, Sizes.DLUX7);
092:
093: /**
094: * A prepared Border with 14dlu on all sides.
095: */
096: public static final Border DLU14_BORDER = createEmptyBorder(
097: Sizes.DLUY14, Sizes.DLUX14, Sizes.DLUY14, Sizes.DLUX14);
098:
099: /**
100: * A standardized Border that describes the gap between a component
101: * and a button bar in its bottom.
102: */
103: public static final Border BUTTON_BAR_GAP_BORDER = createEmptyBorder(
104: LayoutStyle.getCurrent().getButtonBarPad(), Sizes.dluX(0),
105: Sizes.dluY(0), Sizes.dluX(0));
106:
107: /**
108: * A standardized Border that describes the border around
109: * a dialog content that has no tabs.
110: *
111: * @see #TABBED_DIALOG_BORDER
112: */
113: public static final Border DIALOG_BORDER = createEmptyBorder(
114: LayoutStyle.getCurrent().getDialogMarginY(), LayoutStyle
115: .getCurrent().getDialogMarginX(), LayoutStyle
116: .getCurrent().getDialogMarginY(), LayoutStyle
117: .getCurrent().getDialogMarginX());
118:
119: /**
120: * A standardized Border that describes the border around
121: * a dialog content that uses tabs.
122: *
123: * @see #DIALOG_BORDER
124: */
125: public static final Border TABBED_DIALOG_BORDER = createEmptyBorder(
126: LayoutStyle.getCurrent().getTabbedDialogMarginY(),
127: LayoutStyle.getCurrent().getTabbedDialogMarginX(),
128: LayoutStyle.getCurrent().getTabbedDialogMarginY(),
129: LayoutStyle.getCurrent().getTabbedDialogMarginX());
130:
131: // Factory Methods ******************************************************
132:
133: /**
134: * Creates and returns an <code>EmptyBorder</code> with the specified
135: * gaps.
136: *
137: * @param top the top gap
138: * @param left the left-hand side gap
139: * @param bottom the bottom gap
140: * @param right the right-hand side gap
141: * @return an <code>EmptyBorder</code> with the specified gaps
142: *
143: * @see #createEmptyBorder(String)
144: */
145: public static Border createEmptyBorder(ConstantSize top,
146: ConstantSize left, ConstantSize bottom, ConstantSize right) {
147: return new EmptyBorder(top, left, bottom, right);
148: }
149:
150: /**
151: * Creates and returns a <code>Border</code> using sizes as specified by
152: * the given string. This string is a comma-separated encoding of
153: * 4 <code>ConstantSize</code>s.
154: *
155: * @param encodedSizes top, left, bottom, right gap encoded as String
156: * @return an <code>EmptyBorder</code> with the specified gaps
157: *
158: * @see #createEmptyBorder(ConstantSize, ConstantSize, ConstantSize, ConstantSize)
159: */
160: public static Border createEmptyBorder(String encodedSizes) {
161: StringTokenizer tokenizer = new StringTokenizer(encodedSizes,
162: ", ");
163: int tokenCount = tokenizer.countTokens();
164: if (tokenCount != 4) {
165: throw new IllegalArgumentException(
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(),
172: false);
173: ConstantSize right = Sizes
174: .constant(tokenizer.nextToken(), true);
175: return createEmptyBorder(top, left, bottom, right);
176: }
177:
178: /**
179: * An empty border that uses 4 instances of {@link ConstantSize}
180: * to define the gaps on all sides.
181: */
182: public static final class EmptyBorder implements Border {
183:
184: private final ConstantSize top;
185: private final ConstantSize left;
186: private final ConstantSize bottom;
187: private final ConstantSize right;
188:
189: private EmptyBorder(ConstantSize top, ConstantSize left,
190: ConstantSize bottom, ConstantSize right) {
191: this .top = top;
192: this .left = left;
193: this .bottom = bottom;
194: this .right = right;
195: }
196:
197: /**
198: * Returns this border's top size.
199: *
200: * @return this border's top size
201: */
202: public ConstantSize top() {
203: return top;
204: }
205:
206: /**
207: * Returns this border's left size.
208: *
209: * @return this border's left size
210: */
211: public ConstantSize left() {
212: return left;
213: }
214:
215: /**
216: * Returns this border's bottom size.
217: *
218: * @return this border's bottom size
219: */
220: public ConstantSize bottom() {
221: return bottom;
222: }
223:
224: /**
225: * Returns this border's right size.
226: *
227: * @return this border's right size
228: */
229: public ConstantSize right() {
230: return right;
231: }
232:
233: /**
234: * Paints the border for the specified component with the specified
235: * position and size.
236: *
237: * @param c the component for which this border is being painted
238: * @param g the paint graphics
239: * @param x the x position of the painted border
240: * @param y the y position of the painted border
241: * @param width the width of the painted border
242: * @param height the height of the painted border
243: */
244: public void paintBorder(Component c, Graphics g, int x, int y,
245: int width, int height) {
246: // An empty border doesn't paint.
247: }
248:
249: /**
250: * Returns the insets of the border.
251: *
252: * @param c the component for which this border insets value applies
253: * @return the border's Insets
254: */
255: public Insets getBorderInsets(Component c) {
256: return new Insets(top.getPixelSize(c),
257: left.getPixelSize(c), bottom.getPixelSize(c), right
258: .getPixelSize(c));
259: }
260:
261: /**
262: * Returns whether or not the border is opaque. If the border
263: * is opaque, it is responsible for filling in it's own
264: * background when painting.
265: *
266: * @return false - because the empty border is not opaque
267: */
268: public boolean isBorderOpaque() {
269: return false;
270: }
271:
272: }
273:
274: }
|