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.layout;
032:
033: import java.util.StringTokenizer;
034:
035: /**
036: * Specifies columns in {@link FormLayout} by their default orientation, start
037: * size and resizing behavior.
038: * <p>
039: *
040: * <strong>Examples:</strong><br>
041: * The following examples specify a column with FILL alignment, a size of
042: * 10 dlu that won't grow.
043: *
044: * <pre>
045: * new ColumnSpec(Sizes.dluX(10));
046: * new ColumnSpec(ColumnSpec.FILL, Sizes.dluX(10), 0.0);
047: * new ColumnSpec(ColumnSpec.FILL, Sizes.dluX(10), ColumnSpec.NO_GROW);
048: * new ColumnSpec("10dlu");
049: * new ColumnSpec("10dlu:0");
050: * new ColumnSpec("fill:10dlu:0");
051: * </pre>
052: *
053: * <p>
054: *
055: * The {@link com.jgoodies.forms.factories.FormFactory} provides predefined
056: * frequently used <code>ColumnSpec</code> instances.
057: *
058: * @author Karsten Lentzsch
059: * @version $Revision: 1.2 $
060: *
061: * @see com.jgoodies.forms.factories.FormFactory
062: */
063:
064: public final class ColumnSpec extends FormSpec {
065:
066: // Horizontal Orientations *********************************************
067:
068: /**
069: * By default put components in the left.
070: */
071: public static final DefaultAlignment LEFT = FormSpec.LEFT_ALIGN;
072:
073: /**
074: * By default put the components in the center.
075: */
076: public static final DefaultAlignment CENTER = FormSpec.CENTER_ALIGN;
077:
078: /**
079: * By default put components in the middle.
080: */
081: public static final DefaultAlignment MIDDLE = CENTER;
082:
083: /**
084: * By default put components in the right.
085: */
086: public static final DefaultAlignment RIGHT = FormSpec.RIGHT_ALIGN;
087:
088: /**
089: * By default fill the component into the column.
090: */
091: public static final DefaultAlignment FILL = FormSpec.FILL_ALIGN;
092:
093: /**
094: * Unless overridden the default alignment for a column is FILL.
095: */
096: public static final DefaultAlignment DEFAULT = FILL;
097:
098: // Instance Creation ****************************************************
099:
100: /**
101: * Constructs a <code>ColumnSpec</code> for the given default alignment,
102: * size and resize weight.
103: * <p>
104: * The resize weight must be a non-negative double; you can use
105: * <code>NO_GROW</code> as a convenience value for no resize.
106: *
107: * @param defaultAlignment
108: * the spec's default alignment
109: * @param size
110: * constant, component size or bounded size
111: * @param resizeWeight
112: * the spec resize weight
113: * @throws IllegalArgumentException
114: * if the resize weight is negative
115: */
116: public ColumnSpec(DefaultAlignment defaultAlignment, Size size,
117: double resizeWeight) {
118: super (defaultAlignment, size, resizeWeight);
119: }
120:
121: /**
122: * Constructs a <code>ColumnSpec</code> for the given size using the
123: * default alignment, and no resizing.
124: *
125: * @param size
126: * constant size, component size, or bounded size
127: * @throws IllegalArgumentException
128: * if the pixel size is invalid or the resize weight is negative
129: */
130: public ColumnSpec(Size size) {
131: super (DEFAULT, size, NO_GROW);
132: }
133:
134: /**
135: * Constructs a <code>ColumnSpec</code> from the specified encoded
136: * description. The description will be parsed to set initial values.
137: *
138: * @param encodedDescription
139: * the encoded description
140: */
141: public ColumnSpec(String encodedDescription) {
142: super (DEFAULT, encodedDescription);
143: }
144:
145: // Implementing Abstract Behavior ***************************************
146:
147: /**
148: * Returns if this is a horizontal specification (vs. vertical). Used to
149: * distinct between horizontal and vertical dialog units, which have
150: * different conversion factors.
151: *
152: * @return always true (for horizontal)
153: */
154: protected final boolean isHorizontal() {
155: return true;
156: }
157:
158: // Parsing and Decoding of Column Descriptions **************************
159:
160: /**
161: * Parses and splits encoded column specifications and returns an array of
162: * <code>ColumnSpec</code> objects.
163: *
164: * @param encodedColumnSpecs
165: * comma separated encoded column specifications
166: * @return an array of decoded column specifications
167: * @throws NullPointerException
168: * if the encoded column specifications string is
169: * <code>null</code>
170: *
171: * @see ColumnSpec#ColumnSpec(String)
172: */
173: public static ColumnSpec[] decodeSpecs(String encodedColumnSpecs) {
174: if (encodedColumnSpecs == null)
175: throw new NullPointerException(
176: "The column description must not be null.");
177:
178: StringTokenizer tokenizer = new StringTokenizer(
179: encodedColumnSpecs, ", ");
180: int columnCount = tokenizer.countTokens();
181: ColumnSpec[] columnSpecs = new ColumnSpec[columnCount];
182: for (int i = 0; i < columnCount; i++) {
183: columnSpecs[i] = new ColumnSpec(tokenizer.nextToken());
184: }
185: return columnSpecs;
186: }
187:
188: }
|