001: /*
002: * June 2006: This is the original file except of this paragraph and
003: * for consistency, the original package name info.clearthought
004: * was renamed.
005: */
006: /*
007: * ====================================================================
008: *
009: * The Clearthought Software License, Version 1.0
010: *
011: * Copyright (c) 2001 Daniel Barbalace. All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. The original software may not be altered. However, the classes
021: * provided may be subclasses as long as the subclasses are not
022: * packaged in the info.clearthought package or any subpackage of
023: * info.clearthought.
024: *
025: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
026: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
027: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
028: * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, AFFILATED BUSINESSES,
029: * OR ANYONE ELSE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
030: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
031: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
032: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
033: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
034: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
035: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
036: * SUCH DAMAGE.
037: * ====================================================================
038: */
039:
040: package org.gui4j.core.swing;
041:
042: import java.io.Serializable;
043: import java.util.NoSuchElementException;
044: import java.util.StringTokenizer;
045:
046: /**
047: * TableLayoutConstraints binds components to their constraints.
048: *
049: * @version 1.1 4/4/02
050: * @author Daniel E. Barbalace
051: */
052:
053: public class TableLayoutConstraints implements TableLayoutConstants,
054: Serializable {
055:
056: /** Cell in which the upper left corner of the component lays */
057: public int col1, row1;
058:
059: /** Cell in which the lower right corner of the component lays */
060: public int col2, row2;
061:
062: /** Horizontal justification if component occupies just one cell */
063: public int hAlign;
064:
065: /** Verical justification if component occupies just one cell */
066: public int vAlign;
067:
068: /**
069: * Constructs an TableLayoutConstraints with the default settings. This
070: * constructor is equivalent to TableLayoutConstraints(0, 0, 0, 0, FULL, FULL).
071: */
072:
073: public TableLayoutConstraints() {
074: col1 = row1 = col2 = col2 = 0;
075: hAlign = vAlign = FULL;
076: }
077:
078: /**
079: * Constructs an TableLayoutConstraints from a string.
080: *
081: * @param constraints indicates TableLayoutConstraints's position and justification
082: * as a string in the form "row, column" or
083: * "row, column, horizontal justification, vertical
084: * justification" or "row 1, column 1, row 2, column 2".
085: * It is also acceptable to delimit the paramters with
086: * spaces instead of commas.
087: */
088:
089: public TableLayoutConstraints(String constraints) {
090: // Use default values for any parameter not specified or specified
091: // incorrectly. The default parameters place the component in a single
092: // cell at column 0, row 0. The component is fully justified.
093: col1 = 0;
094: row1 = 0;
095: col2 = 0;
096: row2 = 0;
097: hAlign = FULL;
098: vAlign = FULL;
099:
100: // Parse constraints using spaces or commas
101: StringTokenizer st = new StringTokenizer(constraints, ", ");
102: int numToken = st.countTokens();
103:
104: try {
105: // Check constraints
106: if ((numToken != 2) && (numToken != 4) && (numToken != 6))
107: throw new RuntimeException();
108:
109: // Get the first column (assume component is in only one column)
110: String tokenA = st.nextToken();
111: col1 = new Integer(tokenA).intValue();
112: col2 = col1;
113:
114: // Get the first row (assume component is in only one row)
115: String tokenB = st.nextToken();
116: row1 = new Integer(tokenB).intValue();
117: row2 = row1;
118:
119: // Get next two tokens
120: tokenA = st.nextToken();
121: tokenB = st.nextToken();
122:
123: try {
124: // Attempt to use tokens A and B as col2 and row2
125: col2 = new Integer(tokenA).intValue();
126: row2 = new Integer(tokenB).intValue();
127:
128: // Get next two tokens
129: tokenA = st.nextToken();
130: tokenB = st.nextToken();
131: } catch (NumberFormatException error) {
132: col2 = col1;
133: row2 = row1;
134: }
135:
136: // Check if token means horizontally justification the component
137: if ((tokenA.equalsIgnoreCase("L"))
138: || (tokenA.equalsIgnoreCase("LEFT")))
139: hAlign = LEFT;
140: else if ((tokenA.equalsIgnoreCase("C"))
141: || (tokenA.equalsIgnoreCase("CENTER")))
142: hAlign = CENTER;
143: else if ((tokenA.equalsIgnoreCase("F"))
144: || (tokenA.equalsIgnoreCase("FULL")))
145: hAlign = FULL;
146: else if ((tokenA.equalsIgnoreCase("R"))
147: || (tokenA.equalsIgnoreCase("RIGHT")))
148: hAlign = RIGHT;
149: else
150: throw new RuntimeException();
151:
152: // Check if token means horizontally justification the component
153: if ((tokenB.equalsIgnoreCase("T"))
154: || (tokenB.equalsIgnoreCase("TOP")))
155: vAlign = TOP;
156: else if ((tokenB.equalsIgnoreCase("C"))
157: || (tokenB.equalsIgnoreCase("CENTER")))
158: vAlign = CENTER;
159: else if ((tokenB.equalsIgnoreCase("F"))
160: || (tokenB.equalsIgnoreCase("FULL")))
161: vAlign = FULL;
162: else if ((tokenB.equalsIgnoreCase("B"))
163: || (tokenB.equalsIgnoreCase("BOTTOM")))
164: vAlign = BOTTOM;
165: else
166: throw new RuntimeException();
167: } catch (NoSuchElementException error) {
168: } catch (RuntimeException error) {
169: throw new IllegalArgumentException(
170: "Expected constraints in one of the following formats:\n"
171: + " col1, row1\n col1, row1, col2, row2\n"
172: + " col1, row1, hAlign, vAlign\n"
173: + " col1, row1, col2, row2, hAlign, vAlign\n"
174: + "Constraints provided '" + constraints
175: + "'");
176: }
177:
178: // Make sure row2 >= row1
179: if (row2 < row1)
180: row2 = row1;
181:
182: // Make sure col2 >= col1
183: if (col2 < col1)
184: col2 = col1;
185: }
186:
187: /**
188: * Constructs an TableLayoutConstraints a set of constraints.
189: *
190: * @param col1 column where upper-left cornor of the component is placed
191: * @param row1 row where upper-left cornor of the component is placed
192: * @param col2 column where lower-right cornor of the component is placed
193: * @param row2 row where lower-right cornor of the component is placed
194: * @param hAlign horizontal justification of a component in a single cell
195: * @param vAlign vertical justification of a component in a single cell
196: */
197:
198: public TableLayoutConstraints(int col1, int row1, int col2,
199: int row2, int hAlign, int vAlign) {
200: this .col1 = col1;
201: this .row1 = row1;
202: this .col2 = col2;
203: this .row2 = row2;
204:
205: if ((hAlign < MIN_ALIGN) || (hAlign > MAX_ALIGN))
206: this .hAlign = FULL;
207: else
208: this .hAlign = hAlign;
209:
210: if ((vAlign < MIN_ALIGN) || (vAlign > MAX_ALIGN))
211: this .vAlign = FULL;
212: else
213: this .vAlign = vAlign;
214: }
215:
216: /**
217: * Gets a string representation of this TableLayoutConstraints.
218: *
219: * @return a string in the form "row 1, column 1, row 2, column 2, horizontal
220: * justification, vertical justification"
221: */
222:
223: public String toString() {
224: StringBuffer buffer = new StringBuffer();
225:
226: buffer.append(col1);
227: buffer.append(", ");
228: buffer.append(row1);
229: buffer.append(", ");
230:
231: buffer.append(col2);
232: buffer.append(", ");
233: buffer.append(row2);
234: buffer.append(", ");
235:
236: final String h[] = { "left", "center", "full", "right" };
237: final String v[] = { "top", "center", "full", "bottom" };
238:
239: buffer.append(h[hAlign]);
240: buffer.append(", ");
241: buffer.append(v[vAlign]);
242:
243: return buffer.toString();
244: }
245:
246: }
|