001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.util;
019:
020: import java.util.StringTokenizer;
021:
022: public class TableLayoutConstraints implements TableLayoutConstants {
023:
024: /** Cell in which the upper left corner of the component lays */
025: public int col1, row1;
026:
027: /** Cell in which the lower right corner of the component lays */
028: public int col2, row2;
029:
030: /** Horizontal justification if component occupies just one cell */
031: public int hAlign;
032:
033: /** Verical justification if component occupies just one cell */
034: public int vAlign;
035:
036: /**
037: * Constructs an TableLayoutConstraints with the default settings. This
038: * constructor is equivalent to TableLayoutConstraints(0, 0, 0, 0, FULL,
039: * FULL).
040: */
041:
042: public TableLayoutConstraints() {
043: col1 = row1 = col2 = col2 = 0;
044: hAlign = vAlign = FULL;
045: }
046:
047: /**
048: * Constructs an TableLayoutConstraints from a string.
049: *
050: * @param constraints
051: * indicates TableLayoutConstraints's position and justification
052: * as a string in the form "row, column" or "row, column,
053: * horizontal justification, vertical justification" or "row 1,
054: * column 1, row 2, column 2". It is also acceptable to delimit
055: * the paramters with spaces instead of commas.
056: */
057:
058: public TableLayoutConstraints(String constraints) {
059: // Parse constraints using spaces or commas
060: StringTokenizer st = new StringTokenizer(constraints, ", ");
061: GenericTokenizer gt = new GenericTokenizer(st,
062: GenericTokenizer.IntegerDescriptor);
063:
064: // Use default values for any parameter not specified or specified
065: // incorrectly. The default parameters place the component in a single
066: // cell at column 0, row 0. The component is fully justified.
067: col1 = 0;
068: row1 = 0;
069: col2 = 0;
070: row2 = 0;
071: hAlign = FULL;
072: vAlign = FULL;
073:
074: for (int tokCnt = 0; gt.hasNext(); tokCnt++) {
075: Object token = gt.next();
076: if (token instanceof Integer) {
077: int value = ((Integer) token).intValue();
078: switch (tokCnt) {
079: case 0:
080: // Get the first column (assume component is in only one
081: // column)
082: col1 = value;
083: col2 = col1;
084: break;
085: case 1:
086: // Get the first row (assume component is in only one row)
087: row1 = value;
088: row2 = row1;
089: break;
090: case 2:
091: // Get the second column
092: col2 = value;
093: break;
094: case 3:
095: // Get the second row
096: row2 = value;
097: break;
098: }
099: } else {
100: // assume it is a string then (nothing more has been specified!)
101: String string = (String) token;
102: if (tokCnt % 2 == 0) {
103: // if it is even, it must be specification for horizontally
104: // justification
105: // Check if token means horizontally justification the
106: // component
107: if (string.equalsIgnoreCase("L")) {
108: hAlign = LEFT;
109: } else if (string.equalsIgnoreCase("C")) {
110: hAlign = CENTER;
111: } else if (string.equalsIgnoreCase("F")) {
112: hAlign = FULL;
113: } else if (string.equalsIgnoreCase("R")) {
114: hAlign = RIGHT;
115: }
116: } else {
117: // if it is odd, it must be specification for vertical
118: // justification
119: if (string.equalsIgnoreCase("T")) {
120: vAlign = TOP;
121: } else if (string.equalsIgnoreCase("C")) {
122: vAlign = CENTER;
123: } else if (string.equalsIgnoreCase("F")) {
124: vAlign = FULL;
125: } else if (string.equalsIgnoreCase("B")) {
126: vAlign = BOTTOM;
127: }
128: }
129: }
130: }
131:
132: // Make sure row2 >= row1
133: if (row2 < row1)
134: row2 = row1;
135:
136: // Make sure col2 >= col1
137: if (col2 < col1)
138: col2 = col1;
139: }
140:
141: /**
142: * Constructs an TableLayoutConstraints a set of constraints.
143: *
144: * @param col1
145: * column where upper-left cornor of the component is placed
146: * @param row1
147: * row where upper-left cornor of the component is placed
148: * @param col2
149: * column where lower-right cornor of the component is placed
150: * @param row2
151: * row where lower-right cornor of the component is placed
152: * @param hAlign
153: * horizontal justification of a component in a single cell
154: * @param vAlign
155: * vertical justification of a component in a single cell
156: */
157:
158: public TableLayoutConstraints(int col1, int row1, int col2,
159: int row2, int hAlign, int vAlign) {
160: this .col1 = col1;
161: this .row1 = row1;
162: this .col2 = col2;
163: this .row2 = row2;
164:
165: if ((hAlign < MIN_ALIGN) || (hAlign > MAX_ALIGN))
166: this .hAlign = FULL;
167: else
168: this .hAlign = hAlign;
169:
170: if ((vAlign < MIN_ALIGN) || (vAlign > MAX_ALIGN))
171: this .vAlign = FULL;
172: else
173: this .vAlign = vAlign;
174: }
175:
176: /**
177: * Gets a string representation of this TableLayoutConstraints.
178: *
179: * @return a string in the form "row 1, column 1, row 2, column 2" or "row,
180: * column, horizontal justification, vertical justification"
181: */
182:
183: public String toString() {
184: StringBuffer buffer = new StringBuffer();
185:
186: buffer.append(row1);
187: buffer.append(", ");
188: buffer.append(col1);
189: buffer.append(", ");
190:
191: if ((row1 == row2) && (col1 == col2)) {
192: final char h[] = { 'L', 'C', 'F', 'R' };
193: final char v[] = { 'T', 'C', 'F', 'B' };
194:
195: buffer.append(h[hAlign]);
196: buffer.append(", ");
197: buffer.append(v[vAlign]);
198: } else {
199: buffer.append(row2);
200: buffer.append(", ");
201: buffer.append(col2);
202: }
203:
204: return buffer.toString();
205: }
206:
207: }
|