1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
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          
86          
87          
88          col1 = 0;
89          row1 = 0;
90          col2 = 0;
91          row2 = 0;
92          hAlign = FULL;
93          vAlign = FULL;
94  
95          
96          StringTokenizer st = new StringTokenizer(constraints, ", "); 
97          int numToken = st.countTokens();
98  
99          try {
100             
101             if ((numToken != 2) && (numToken != 4) && (numToken != 6)) {
102                 throw new RuntimeException();
103             }
104 
105             
106             String tokenA = st.nextToken();
107 
108             col1 = new Integer(tokenA).intValue();
109             col2 = col1;
110 
111             
112             String tokenB = st.nextToken();
113 
114             row1 = new Integer(tokenB).intValue();
115             row2 = row1;
116 
117             
118             tokenA = st.nextToken();
119             tokenB = st.nextToken();
120 
121             try {
122                 
123                 col2 = new Integer(tokenA).intValue();
124                 row2 = new Integer(tokenB).intValue();
125 
126                 
127                 tokenA = st.nextToken();
128                 tokenB = st.nextToken();
129             }
130             catch (NumberFormatException error) {
131                 col2 = col1;
132                 row2 = row1;
133             }
134 
135             
136             if ((tokenA.equalsIgnoreCase("L"))
137                 || (tokenA.equalsIgnoreCase("LEFT"))) { 
138                 hAlign = LEFT;
139             }
140             else if ((tokenA.equalsIgnoreCase("C")) 
141                 || (tokenA.equalsIgnoreCase("CENTER"))) { 
142                 hAlign = CENTER;
143             }
144             else if ((tokenA.equalsIgnoreCase("F")) 
145                 || (tokenA.equalsIgnoreCase("FULL"))) { 
146                 hAlign = FULL;
147             }
148             else if ((tokenA.equalsIgnoreCase("R")) 
149                 || (tokenA.equalsIgnoreCase("RIGHT"))) { 
150                 hAlign = RIGHT;
151             }
152             else if ((tokenA.equalsIgnoreCase("LD")) 
153                 || (tokenA.equalsIgnoreCase("LEADING"))) { 
154                 hAlign = LEADING;
155             }
156             else if ((tokenA.equalsIgnoreCase("TL")) 
157                 || (tokenA.equalsIgnoreCase("TRAILING"))) { 
158                 hAlign = TRAILING;
159             }
160             else {
161                 throw new RuntimeException();
162             }
163 
164             
165             if ((tokenB.equalsIgnoreCase("T"))
166                 || (tokenB.equalsIgnoreCase("TOP"))) { 
167                 vAlign = TOP;
168             }
169             else if ((tokenB.equalsIgnoreCase("C")) 
170                 || (tokenB.equalsIgnoreCase("CENTER"))) { 
171                 vAlign = CENTER;
172             }
173             else if ((tokenB.equalsIgnoreCase("F")) 
174                 || (tokenB.equalsIgnoreCase("FULL"))) { 
175                 vAlign = FULL;
176             }
177             else if ((tokenB.equalsIgnoreCase("B")) 
178                 || (tokenB.equalsIgnoreCase("BOTTOM"))) { 
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" 
190                 + "  col1, row1\n  col1, row1, col2, row2\n" 
191                 + "  col1, row1, hAlign, vAlign\n" 
192                 + "  col1, row1, col2, row2, hAlign, vAlign\n" 
193                 + "Constraints provided '" + constraints + "'"); 
194         }
195 
196         
197         if (row2 < row1) {
198             row2 = row1;
199         }
200 
201         
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(", "); 
251         buffer.append(row1);
252         buffer.append(", "); 
253 
254         buffer.append(col2);
255         buffer.append(", "); 
256         buffer.append(row2);
257         buffer.append(", "); 
258 
259         final String[] h = {
260                 "left", "center", "full", "right", "leading", 
261                 "trailing"
262             }; 
263         final String[] v = { "top", "center", "full", "bottom" }; 
264 
265         buffer.append(h[hAlign]);
266         buffer.append(", "); 
267         buffer.append(v[vAlign]);
268 
269         return buffer.toString();
270     }
271 }