View Javadoc

1   // %1116523449328:hoplugins.commons.ui.info.clearthought.layout%
2   /*
3    * ====================================================================
4    *
5    * The Clearthought Software License, Version 1.0
6    *
7    * Copyright (c) 2001 Daniel Barbalace.  All rights reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. The original software may not be altered.  However, the classes
17   *    provided may be subclasses as long as the subclasses are not
18   *    packaged in the info.clearthought package or any subpackage of
19   *    info.clearthought.
20   *
21   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
22   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24   * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, AFFILATED BUSINESSES,
25   * OR ANYONE ELSE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32   * SUCH DAMAGE.
33   * ====================================================================
34   */
35  package hoplugins.commons.ui.info.clearthought.layout;
36  
37  import java.util.NoSuchElementException;
38  import java.util.StringTokenizer;
39  
40  /***
41   * TableLayoutConstraints binds components to their constraints.
42   *
43   * @author Daniel E. Barbalace
44   * @version 1.2 3/15/04
45   */
46  public class TableLayoutConstraints implements TableLayoutConstants {
47      /*** Cell in which the upper left corner of the component lays */
48      public int col1;
49  
50      /*** Cell in which the lower right corner of the component lays */
51      public int col2;
52  
53      /*** Horizontal justification if component occupies just one cell */
54      public int hAlign;
55  
56      /*** Cell in which the upper left corner of the component lays */
57      public int row1;
58  
59      /*** Cell in which the lower right corner of the component lays */
60      public int row2;
61  
62      /*** Verical justification if component occupies just one cell */
63      public int vAlign;
64  
65      /***
66       * Constructs an TableLayoutConstraints with the default settings.  This constructor is
67       * equivalent to TableLayoutConstraints(0, 0, 0, 0, FULL, FULL).
68       */
69      public TableLayoutConstraints() {
70          col1 = row1 = col2 = col2 = 0;
71          hAlign = vAlign = FULL;
72      }
73  
74      /***
75       * Constructs an TableLayoutConstraints from a string.
76       *
77       * @param constraints indicates TableLayoutConstraints's position and justification as a string
78       *        in the form "row, column" or "row, column, horizontal justification, vertical
79       *        justification" or "row 1, column 1, row 2, column 2". It is also acceptable to
80       *        delimit the paramters with spaces instead of commas.
81       *
82       * @throws IllegalArgumentException -
83       */
84      public TableLayoutConstraints(String constraints) {
85          // Use default values for any parameter not specified or specified
86          // incorrectly.  The default parameters place the component in a single
87          // cell at column 0, row 0.  The component is fully justified.
88          col1 = 0;
89          row1 = 0;
90          col2 = 0;
91          row2 = 0;
92          hAlign = FULL;
93          vAlign = FULL;
94  
95          // Parse constraints using spaces or commas
96          StringTokenizer st = new StringTokenizer(constraints, ", "); //$NON-NLS-1$
97          int numToken = st.countTokens();
98  
99          try {
100             // Check constraints
101             if ((numToken != 2) && (numToken != 4) && (numToken != 6)) {
102                 throw new RuntimeException();
103             }
104 
105             // Get the first column (assume component is in only one column)
106             String tokenA = st.nextToken();
107 
108             col1 = new Integer(tokenA).intValue();
109             col2 = col1;
110 
111             // Get the first row (assume component is in only one row)
112             String tokenB = st.nextToken();
113 
114             row1 = new Integer(tokenB).intValue();
115             row2 = row1;
116 
117             // Get next two tokens
118             tokenA = st.nextToken();
119             tokenB = st.nextToken();
120 
121             try {
122                 // Attempt to use tokens A and B as col2 and row2
123                 col2 = new Integer(tokenA).intValue();
124                 row2 = new Integer(tokenB).intValue();
125 
126                 // Get next two tokens
127                 tokenA = st.nextToken();
128                 tokenB = st.nextToken();
129             }
130             catch (NumberFormatException error) {
131                 col2 = col1;
132                 row2 = row1;
133             }
134 
135             // Check if token means horizontally justification the component
136             if ((tokenA.equalsIgnoreCase("L"))
137                 || (tokenA.equalsIgnoreCase("LEFT"))) { //$NON-NLS-1$ //$NON-NLS-2$
138                 hAlign = LEFT;
139             }
140             else if ((tokenA.equalsIgnoreCase("C")) //$NON-NLS-1$
141                 || (tokenA.equalsIgnoreCase("CENTER"))) { //$NON-NLS-1$
142                 hAlign = CENTER;
143             }
144             else if ((tokenA.equalsIgnoreCase("F")) //$NON-NLS-1$
145                 || (tokenA.equalsIgnoreCase("FULL"))) { //$NON-NLS-1$
146                 hAlign = FULL;
147             }
148             else if ((tokenA.equalsIgnoreCase("R")) //$NON-NLS-1$
149                 || (tokenA.equalsIgnoreCase("RIGHT"))) { //$NON-NLS-1$
150                 hAlign = RIGHT;
151             }
152             else if ((tokenA.equalsIgnoreCase("LD")) //$NON-NLS-1$
153                 || (tokenA.equalsIgnoreCase("LEADING"))) { //$NON-NLS-1$
154                 hAlign = LEADING;
155             }
156             else if ((tokenA.equalsIgnoreCase("TL")) //$NON-NLS-1$
157                 || (tokenA.equalsIgnoreCase("TRAILING"))) { //$NON-NLS-1$
158                 hAlign = TRAILING;
159             }
160             else {
161                 throw new RuntimeException();
162             }
163 
164             // Check if token means horizontally justification the component
165             if ((tokenB.equalsIgnoreCase("T"))
166                 || (tokenB.equalsIgnoreCase("TOP"))) { //$NON-NLS-1$ //$NON-NLS-2$
167                 vAlign = TOP;
168             }
169             else if ((tokenB.equalsIgnoreCase("C")) //$NON-NLS-1$
170                 || (tokenB.equalsIgnoreCase("CENTER"))) { //$NON-NLS-1$
171                 vAlign = CENTER;
172             }
173             else if ((tokenB.equalsIgnoreCase("F")) //$NON-NLS-1$
174                 || (tokenB.equalsIgnoreCase("FULL"))) { //$NON-NLS-1$
175                 vAlign = FULL;
176             }
177             else if ((tokenB.equalsIgnoreCase("B")) //$NON-NLS-1$
178                 || (tokenB.equalsIgnoreCase("BOTTOM"))) { //$NON-NLS-1$
179                 vAlign = BOTTOM;
180             }
181             else {
182                 throw new RuntimeException();
183             }
184         }
185         catch (NoSuchElementException error) {
186         }
187         catch (RuntimeException error) {
188             throw new IllegalArgumentException(
189                 "Expected constraints in one of the following formats:\n" //$NON-NLS-1$
190                 + "  col1, row1\n  col1, row1, col2, row2\n" //$NON-NLS-1$
191                 + "  col1, row1, hAlign, vAlign\n" //$NON-NLS-1$
192                 + "  col1, row1, col2, row2, hAlign, vAlign\n" //$NON-NLS-1$
193                 + "Constraints provided '" + constraints + "'"); //$NON-NLS-1$ //$NON-NLS-2$
194         }
195 
196         // Make sure row2 >= row1
197         if (row2 < row1) {
198             row2 = row1;
199         }
200 
201         // Make sure col2 >= col1
202         if (col2 < col1) {
203             col2 = col1;
204         }
205     }
206 
207     /***
208      * Constructs an TableLayoutConstraints a set of constraints.
209      *
210      * @param col1 column where upper-left cornor of the component is placed
211      * @param row1 row where upper-left cornor of the component is placed
212      * @param col2 column where lower-right cornor of the component is placed
213      * @param row2 row where lower-right cornor of the component is placed
214      * @param hAlign horizontal justification of a component in a single cell
215      * @param vAlign vertical justification of a component in a single cell
216      */
217     public TableLayoutConstraints(int col1, int row1, int col2, int row2,
218         int hAlign, int vAlign) {
219         this.col1 = col1;
220         this.row1 = row1;
221         this.col2 = col2;
222         this.row2 = row2;
223 
224         if ((hAlign == LEFT) || (hAlign == RIGHT) || (hAlign == CENTER)
225             || (hAlign == FULL) || (hAlign == TRAILING)) {
226             this.hAlign = hAlign;
227         }
228         else {
229             this.hAlign = FULL;
230         }
231 
232         if ((vAlign == LEFT) || (vAlign == RIGHT) || (vAlign == CENTER)) {
233             this.vAlign = vAlign;
234         }
235         else {
236             this.vAlign = FULL;
237         }
238     }
239 
240     /***
241      * Gets a string representation of this TableLayoutConstraints.
242      *
243      * @return a string in the form "row 1, column 1, row 2, column 2, horizontal justification,
244      *         vertical justification"
245      */
246     public String toString() {
247         StringBuffer buffer = new StringBuffer();
248 
249         buffer.append(col1);
250         buffer.append(", "); //$NON-NLS-1$
251         buffer.append(row1);
252         buffer.append(", "); //$NON-NLS-1$
253 
254         buffer.append(col2);
255         buffer.append(", "); //$NON-NLS-1$
256         buffer.append(row2);
257         buffer.append(", "); //$NON-NLS-1$
258 
259         final String[] h = {
260                 "left", "center", "full", "right", "leading", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
261                 "trailing"
262             }; //$NON-NLS-1$
263         final String[] v = { "top", "center", "full", "bottom" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
264 
265         buffer.append(h[hAlign]);
266         buffer.append(", "); //$NON-NLS-1$
267         buffer.append(v[vAlign]);
268 
269         return buffer.toString();
270     }
271 }