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 |
| |
36 |
| |
37 |
| |
38 |
| package com.gargoylesoftware.base.gui; |
39 |
| |
40 |
| import com.gargoylesoftware.base.util.DetailedIllegalArgumentException; |
41 |
| import com.gargoylesoftware.base.util.DetailedNullPointerException; |
42 |
| import java.io.Serializable; |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| public class TableLayoutConstraints implements Serializable { |
57 |
| |
58 |
| |
59 |
| private static final long serialVersionUID = 4323958798646824406L; |
60 |
| |
61 |
| private boolean isImmutable_ = false; |
62 |
| |
63 |
| |
64 |
| private boolean obeyMaximumSize_ = false; |
65 |
| |
66 |
| private boolean obeyMinimumSize_ = true; |
67 |
| |
68 |
| private int row_; |
69 |
| private int column_; |
70 |
| private int rowSpan_; |
71 |
| private int columnSpan_; |
72 |
| private int verticalAlignment_; |
73 |
| private boolean verticalStretch_; |
74 |
| private int horizontalAlignment_; |
75 |
| private boolean horizontalStretch_; |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
58
| public TableLayoutConstraints( final int row,
|
83 |
| final int column ) { |
84 |
| |
85 |
58
| setRow(row);
|
86 |
56
| setColumn(column);
|
87 |
| |
88 |
54
| setRowSpan(1);
|
89 |
54
| setColumnSpan(1);
|
90 |
54
| setVerticalAlignment( TableLayout.CENTER );
|
91 |
54
| setHorizontalAlignment( TableLayout.LEFT );
|
92 |
54
| setVerticalStretch( false );
|
93 |
54
| setHorizontalStretch( false );
|
94 |
| } |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
0
| public final String toString() {
|
101 |
0
| return getClass().getName()
|
102 |
| + "] row_=[" + row_ |
103 |
| + "] rowSpan_=[" + rowSpan_ |
104 |
| + "] column_=[" + column_ |
105 |
| + "] columnSpan_=[" + columnSpan_ |
106 |
| + "] verticalAlignment_=[" + verticalAlignment_ |
107 |
| + "] verticalStretch_=["+ verticalStretch_ |
108 |
| + "] horizontalAlignment_=[" + horizontalAlignment_ |
109 |
| + "] horizontalStretch_=["+ horizontalStretch_ |
110 |
| + "] isImmutable_=["+ isImmutable_ |
111 |
| + "] obeyMinimumSize_=["+ obeyMinimumSize_ |
112 |
| + "] obeyMaximumSize_=["+ obeyMaximumSize_ |
113 |
| + "]"; |
114 |
| |
115 |
| } |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
40
| public final void setImmutable() {
|
121 |
40
| isImmutable_ = true;
|
122 |
| } |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
0
| public final boolean isImmutable() {
|
129 |
0
| return isImmutable_;
|
130 |
| } |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
508
| private void ensureMutable() throws IllegalArgumentException {
|
138 |
508
| if( isImmutable_ == true ) {
|
139 |
0
| throw new IllegalArgumentException("This object is now immutable and cannot be modified");
|
140 |
| } |
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
65
| public final void setRow( final int row ) {
|
148 |
65
| ensureMutable();
|
149 |
65
| if( row < 0 ) {
|
150 |
2
| throw new DetailedIllegalArgumentException("row", new Integer(row), "Is less than zero");
|
151 |
| } |
152 |
63
| row_ = row;
|
153 |
| } |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
501
| public final int getRow() {
|
160 |
501
| return row_;
|
161 |
| } |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
63
| public final void setColumn( final int column ) {
|
168 |
63
| ensureMutable();
|
169 |
63
| if( column < 0 ) {
|
170 |
2
| throw new DetailedIllegalArgumentException("column", new Integer(column), "Is less than zero");
|
171 |
| } |
172 |
61
| column_ = column;
|
173 |
| } |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
433
| public final int getColumn() {
|
180 |
433
| return column_;
|
181 |
| } |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
59
| public final void setRowSpan( final int span ) {
|
189 |
59
| ensureMutable();
|
190 |
59
| if( span < 1 ) {
|
191 |
2
| throw new DetailedIllegalArgumentException("span", span, "Is less than one");
|
192 |
| } |
193 |
57
| rowSpan_ = span;
|
194 |
| } |
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
201
| public final int getRowSpan() {
|
201 |
201
| return rowSpan_;
|
202 |
| } |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
62
| public final void setColumnSpan( final int span ) {
|
210 |
62
| ensureMutable();
|
211 |
62
| if( span < 1 ) {
|
212 |
2
| throw new DetailedIllegalArgumentException("span", new Integer(span), "Is less than one");
|
213 |
| } |
214 |
60
| columnSpan_ = span;
|
215 |
| } |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
209
| public final int getColumnSpan() {
|
222 |
209
| return columnSpan_;
|
223 |
| } |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
56
| public final void setVerticalAlignment( final int alignment ) {
|
236 |
56
| ensureMutable();
|
237 |
56
| switch( alignment ) {
|
238 |
1
| case TableLayout.TOP:
|
239 |
0
| case TableLayout.BOTTOM:
|
240 |
54
| case TableLayout.CENTER:
|
241 |
| |
242 |
55
| break;
|
243 |
| |
244 |
1
| default:
|
245 |
1
| throw new DetailedIllegalArgumentException("alignment", alignment);
|
246 |
| } |
247 |
| |
248 |
55
| verticalAlignment_ = alignment;
|
249 |
| } |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
37
| public final int getVerticalAlignment() {
|
256 |
37
| return verticalAlignment_;
|
257 |
| } |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
56
| public final void setHorizontalAlignment( final int alignment ) {
|
270 |
56
| ensureMutable();
|
271 |
| |
272 |
56
| switch( alignment ) {
|
273 |
55
| case TableLayout.LEFT:
|
274 |
0
| case TableLayout.RIGHT:
|
275 |
0
| case TableLayout.CENTER:
|
276 |
| |
277 |
55
| break;
|
278 |
| |
279 |
1
| default:
|
280 |
1
| throw new DetailedIllegalArgumentException("alignment", alignment);
|
281 |
| } |
282 |
| |
283 |
55
| horizontalAlignment_ = alignment;
|
284 |
| } |
285 |
| |
286 |
| |
287 |
| |
288 |
| |
289 |
| |
290 |
37
| public final int getHorizontalAlignment() {
|
291 |
37
| return horizontalAlignment_;
|
292 |
| } |
293 |
| |
294 |
| |
295 |
| |
296 |
| |
297 |
| |
298 |
69
| public final void setVerticalStretch( final boolean stretch ) {
|
299 |
69
| ensureMutable();
|
300 |
69
| verticalStretch_ = stretch;
|
301 |
| } |
302 |
| |
303 |
| |
304 |
| |
305 |
| |
306 |
| |
307 |
39
| public final boolean getVerticalStretch() {
|
308 |
39
| return verticalStretch_;
|
309 |
| } |
310 |
| |
311 |
| |
312 |
| |
313 |
| |
314 |
| |
315 |
73
| public final void setHorizontalStretch( final boolean stretch ) {
|
316 |
73
| ensureMutable();
|
317 |
73
| horizontalStretch_ = stretch;
|
318 |
| } |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
39
| public final boolean getHorizontalStretch() {
|
325 |
39
| return horizontalStretch_;
|
326 |
| } |
327 |
| |
328 |
| |
329 |
| |
330 |
| |
331 |
| |
332 |
| |
333 |
| |
334 |
5
| public final void setObeyMaximumSize( final boolean obey ) {
|
335 |
5
| ensureMutable();
|
336 |
5
| obeyMaximumSize_ = obey;
|
337 |
| } |
338 |
| |
339 |
| |
340 |
| |
341 |
| |
342 |
| |
343 |
118
| public final boolean getObeyMaximumSize() {
|
344 |
118
| return obeyMaximumSize_;
|
345 |
| } |
346 |
| |
347 |
| |
348 |
| |
349 |
| |
350 |
| |
351 |
0
| public final void setObeyMinimumSize( final boolean obey ) {
|
352 |
0
| ensureMutable();
|
353 |
0
| obeyMinimumSize_ = obey;
|
354 |
| } |
355 |
| |
356 |
| |
357 |
| |
358 |
| |
359 |
| |
360 |
82
| public final boolean getObeyMinimumSize() {
|
361 |
82
| return obeyMinimumSize_;
|
362 |
| } |
363 |
| |
364 |
| |
365 |
| |
366 |
| |
367 |
| |
368 |
| |
369 |
| |
370 |
10
| public static TableLayoutConstraints makeConstraints( final String constraintString ) {
|
371 |
10
| assertNotNull("constraintString", constraintString);
|
372 |
9
| if( constraintString.length() == 0 ) {
|
373 |
1
| throw new DetailedIllegalArgumentException("constraintString", constraintString, "May not be empty");
|
374 |
| } |
375 |
| |
376 |
8
| final int commaIndex = constraintString.indexOf(',');
|
377 |
8
| if( commaIndex == -1 ) {
|
378 |
0
| throw new DetailedIllegalArgumentException("constraintString", constraintString, "Missing a comma");
|
379 |
| } |
380 |
| |
381 |
8
| final TableLayoutConstraints constraints = new TableLayoutConstraints(0,0);
|
382 |
8
| parseConstraintString( constraints, constraintString.substring(0, commaIndex), true );
|
383 |
7
| parseConstraintString( constraints, constraintString.substring(commaIndex+1), false );
|
384 |
| |
385 |
7
| return constraints;
|
386 |
| } |
387 |
| |
388 |
| |
389 |
| |
390 |
| |
391 |
| |
392 |
| |
393 |
| |
394 |
15
| private static void parseConstraintString( final TableLayoutConstraints constraints,
|
395 |
| final String constraintString, |
396 |
| final boolean isRow ) { |
397 |
| |
398 |
15
| final int stringLength = constraintString.length();
|
399 |
| |
400 |
15
| if( stringLength == 0 ) {
|
401 |
1
| throw new DetailedIllegalArgumentException("constraintString", constraintString, "May not be empty");
|
402 |
| } |
403 |
| |
404 |
14
| int index = 0;
|
405 |
14
| while( index < stringLength && Character.isDigit( constraintString.charAt(index) ) ) {
|
406 |
15
| index++;
|
407 |
| } |
408 |
| |
409 |
14
| final int number = Integer.parseInt( constraintString.substring(0,index) );
|
410 |
14
| if( isRow ) {
|
411 |
7
| constraints.setRow(number);
|
412 |
| } |
413 |
| else { |
414 |
7
| constraints.setColumn(number);
|
415 |
| } |
416 |
| |
417 |
14
| char nextChar;
|
418 |
| |
419 |
14
| for( ;index < stringLength; index++ ) {
|
420 |
5
| nextChar = constraintString.charAt(index);
|
421 |
| |
422 |
5
| switch(nextChar) {
|
423 |
2
| case 's':
|
424 |
2
| if( isRow ) {
|
425 |
1
| constraints.setVerticalStretch(true);
|
426 |
| } |
427 |
| else { |
428 |
1
| constraints.setHorizontalStretch(true);
|
429 |
| } |
430 |
2
| break;
|
431 |
| |
432 |
3
| case '+':
|
433 |
3
| index++;
|
434 |
3
| final int startIndex = index;
|
435 |
3
| while( index < stringLength && Character.isDigit( constraintString.charAt(index) ) ) {
|
436 |
3
| index++;
|
437 |
| } |
438 |
| |
439 |
3
| final int span = Integer.parseInt( constraintString.substring(startIndex,index) );
|
440 |
3
| if( isRow ) {
|
441 |
1
| constraints.setRowSpan(span);
|
442 |
| } |
443 |
| else { |
444 |
2
| constraints.setColumnSpan(span);
|
445 |
| } |
446 |
3
| index--;
|
447 |
3
| break;
|
448 |
| |
449 |
0
| default:
|
450 |
0
| throw new IllegalArgumentException("Unexpected command ["+nextChar+"]");
|
451 |
| } |
452 |
| } |
453 |
| } |
454 |
| |
455 |
| |
456 |
| |
457 |
| |
458 |
| |
459 |
| |
460 |
| |
461 |
| |
462 |
| |
463 |
10
| protected static final void assertNotNull( final String fieldName, final Object fieldValue )
|
464 |
| throws DetailedNullPointerException { |
465 |
| |
466 |
10
| if( fieldValue == null ) {
|
467 |
1
| throw new DetailedNullPointerException(fieldName);
|
468 |
| } |
469 |
| } |
470 |
| } |
471 |
| |