File: TableLayoutConstraints.java

package info (click to toggle)
libtablelayout-java 20090826-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 328 kB
  • ctags: 249
  • sloc: java: 1,161; xml: 25; sh: 14; makefile: 5
file content (394 lines) | stat: -rw-r--r-- 13,189 bytes parent folder | download | duplicates (4)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * ====================================================================
 *
 * The Clearthought Software License, Version 2.0
 *
 * Copyright (c) 2001 Daniel Barbalace.  All rights reserved.
 *
 * Project maintained at https://tablelayout.dev.java.net
 *
 * I. Terms for redistribution of original source and binaries
 *
 * Redistribution and use of unmodified source and/or binaries are
 * permitted provided that the following condition is met:
 *
 * 1. Redistributions of original source code must retain the above
 *    copyright notice and license.  You are not required to redistribute
 *    the original source; you may choose to redistribute only the
 *    binaries.
 *
 * Basically, if you distribute unmodified source, you meet
 * automatically comply with the license with no additional effort on
 * your part.
 *
 * II. Terms for distribution of derived works via subclassing and/or
 *     composition.
 *
 * You may generate derived works by means of subclassing and/or
 * composition (e.g., the Adaptor Pattern), provided that the following
 * conditions are met:
 *
 * 1. Redistributions of original source code must retain the above
 *    copyright notice and license.  You are not required to redistribute
 *    the original source; you may choose to redistribute only the
 *    binaries.
 *
 * 2. The original software is not altered.
 *
 * 3. Derived works are not contained in the info.clearthought
 *    namespace/package or any subpackage of info.clearthought.
 *
 * 4. Derived works do not use the class or interface names from the
 *    info.clearthought... packages
 *
 * For example, you may define a class with the following full name:
 *    org.nameOfMyOrganization.layouts.RowMajorTableLayout
 *
 * However, you may not define a class with the either of the
 * following full names:
 *    info.clearthought.layout.RowMajorTableLayout
 *    org.nameOfMyOrganization.layouts.TableLayout
 *
 * III. Terms for redistribution of source modified via patch files.
 *
 * You may generate derived works by means of patch files provided
 * that the following conditions are met:
 *
 * 1. Redistributions of original source code must retain the above
 *    copyright notice and license.  You are not required to
 *    redistribute the original source; you may choose to redistribute
 *    only the binaries resulting from the patch files.
 *
 * 2. The original source files are not altered.  All alteration is
 *    done in patch files.
 *
 * 3. Derived works are not contained in the info.clearthought
 *    namespace/package or any subpackage of info.clearthought.  This
 *    means that your patch files must change the namespace/package
 *    for the derived work.  See section II for examples.
 *
 * 4. Derived works do not use the class or interface names from the
 *    info.clearthought... packages.  This means your patch files
 *    must change the names of the interfaces and classes they alter.
 *    See section II for examples.
 *
 * 5. Derived works must include the following disclaimer.
 *    "This work is derived from Clearthought's TableLayout,
 *     https://tablelayout.dev.java.net, by means of patch files
 *     rather than subclassing or composition.  Therefore, this work
 *     might not contain the latest fixes and features of TableLayout."
 *
 * IV. Terms for repackaging, transcoding, and compiling of binaries.
 *
 * You may do any of the following with the binaries of the
 * original software.
 *
 * 1. You may move binaries (.class files) from the original .jar file
 *    to your own .jar file.
 *
 * 2. You may move binaries from the original .jar file to other
 *    resource containing files, including but not limited to .zip,
 *    .gz, .tar, .dll, .exe files.
 *
 * 3. You may backend compile the binaries to any platform, including
 *    but not limited to Win32, Win64, MAC OS, Linux, Palm OS, any
 *    handheld or embedded platform.
 *
 * 4. You may transcribe the binaries to other virtual machine byte
 *    code protocols, including but not limited to .NET.
 *
 * V. License Disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, AFFILATED BUSINESSES,
 * OR ANYONE ELSE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 */

package org.debian.tablelayout;



import java.util.*;



/**
 * TableLayoutConstraints binds components to their constraints.
 *
 * @version 2.0 March 14, 2004
 * @author  Daniel E. Barbalace
 */

public class TableLayoutConstraints implements TableLayoutConstants
{



/** Cell in which the upper left corner of the component lays */
public int col1, row1;

/** Cell in which the lower right corner of the component lays */
public int col2, row2;

/** Horizontal justification if component occupies just one cell */
public int hAlign;

/** Vertical justification if component occupies just one cell */
public int vAlign;



/**
 * Constructs an TableLayoutConstraints with the default settings.  This
 * constructor is equivalent to TableLayoutConstraints(0, 0, 0, 0, FULL, FULL).
 */

public TableLayoutConstraints ()
{
    col1 = row1 = col1 = col2 = 0;
    hAlign = vAlign = FULL;
}



/**
 * Constructs an TableLayoutConstraints a set of constraints.
 *
 * @param col      column where the component is placed
 * @param row      row where the component is placed
 */

public TableLayoutConstraints (int col, int row)
{
	this(col, row, col, row, FULL, FULL);
}



/**
 * Constructs an TableLayoutConstraints a set of constraints.
 *
 * @param col1      column where upper-left corner of the component is placed
 * @param row1      row where upper-left corner of the component is placed
 * @param col2      column where lower-right corner of the component is placed
 * @param row2      row where lower-right corner of the component is placed
 */

public TableLayoutConstraints (int col1, int row1, int col2, int row2)
{
	this(col1, row1, col2, row2, FULL, FULL);
}



/**
 * Constructs an TableLayoutConstraints a set of constraints.
 *
 * @param col1      column where upper-left corner of the component is placed
 * @param row1      row where upper-left corner of the component is placed
 * @param col2      column where lower-right corner of the component is placed
 * @param row2      row where lower-right corner of the component is placed
 * @param hAlign    horizontal justification of a component in a single cell
 * @param vAlign    vertical justification of a component in a single cell
 */

public TableLayoutConstraints
    (int col1, int row1, int col2, int row2, int hAlign, int vAlign)
{
    this.col1 = col1;
    this.row1 = row1;
    this.col2 = col2;
    this.row2 = row2;

    if ((hAlign == LEFT) ||
        (hAlign == RIGHT) ||
        (hAlign == CENTER) ||
        (hAlign == FULL) ||
        (hAlign == LEADING) ||
        (hAlign == TRAILING))
    {
        this.hAlign = hAlign;
    }
    else
        this.hAlign = FULL;

	if ((vAlign == LEFT) ||
		(vAlign == RIGHT) ||
		(vAlign == CENTER))
	{
		this.vAlign = vAlign;
	}
    else
        this.vAlign = FULL;
}



/**
 * Constructs an TableLayoutConstraints from a string.
 *
 * @param constraints    indicates TableLayoutConstraints's position and
 *                       justification as a string in the form "column, row" or
 *                       "column, row, horizontal justification, vertical
 *                       justification" or "column 1, row 1, column 2, row 2"
 *                       or "column 1, row 1, column 2, row 2, horizontal
 *                       justification, vertical justification".
 *                       It is also acceptable to delimit the parameters with
 *                       spaces instead of commas.
 */

public TableLayoutConstraints (String constraints)
{
    // Use default values for any parameter not specified or specified
    // incorrectly.  The default parameters place the component in a single
    // cell at column 0, row 0.  The component is fully justified.
    col1 = 0;
    row1 = 0;
    col2 = 0;
    row2 = 0;
    hAlign = FULL;
    vAlign = FULL;

    // Parse constraints using spaces or commas
    StringTokenizer st = new StringTokenizer(constraints, ", ");
    int numToken = st.countTokens();

    try
    {
        // Check constraints
        if ((numToken != 2) && (numToken != 4) && (numToken != 6))
            throw new RuntimeException();

        // Get the first column (assume component is in only one column)
        String tokenA = st.nextToken();
        col1 = new Integer(tokenA).intValue();
        col2 = col1;

        // Get the first row (assume component is in only one row)
        String tokenB = st.nextToken();
        row1 = new Integer(tokenB).intValue();
        row2 = row1;

        // Get next two tokens
        tokenA = st.nextToken();
        tokenB = st.nextToken();

        try
        {
            // Attempt to use tokens A and B as col2 and row2
            col2 = new Integer(tokenA).intValue();
            row2 = new Integer(tokenB).intValue();

            // Get next two tokens
            tokenA = st.nextToken();
            tokenB = st.nextToken();
        }
        catch (NumberFormatException error)
        {
            col2 = col1;
            row2 = row1;
        }

        // Check if token means horizontally justification the component
        if ((tokenA.equalsIgnoreCase("L")) || (tokenA.equalsIgnoreCase("LEFT")))
            hAlign = LEFT;
        else if ((tokenA.equalsIgnoreCase("C")) ||
                 (tokenA.equalsIgnoreCase("CENTER")))
            hAlign = CENTER;
        else if ((tokenA.equalsIgnoreCase("F")) ||
                 (tokenA.equalsIgnoreCase("FULL")))
            hAlign = FULL;
        else if ((tokenA.equalsIgnoreCase("R")) ||
                 (tokenA.equalsIgnoreCase("RIGHT")))
            hAlign = RIGHT;
        else if ((tokenA.equalsIgnoreCase("LD")) ||
                 (tokenA.equalsIgnoreCase("LEADING")))
            hAlign = LEADING;
        else if ((tokenA.equalsIgnoreCase("TL")) ||
                 (tokenA.equalsIgnoreCase("TRAILING")))
            hAlign = TRAILING;
        else
            throw new RuntimeException();

        // Check if token means horizontally justification the component
        if ((tokenB.equalsIgnoreCase("T")) || (tokenB.equalsIgnoreCase("TOP")))
            vAlign = TOP;
        else if ((tokenB.equalsIgnoreCase("C")) ||
                 (tokenB.equalsIgnoreCase("CENTER")))
            vAlign = CENTER;
        else if ((tokenB.equalsIgnoreCase("F")) ||
                 (tokenB.equalsIgnoreCase("FULL")))
            vAlign = FULL;
        else if ((tokenB.equalsIgnoreCase("B")) ||
                 (tokenB.equalsIgnoreCase("BOTTOM")))
            vAlign = BOTTOM;
        else
            throw new RuntimeException();
    }
    catch (NoSuchElementException error) {}
    catch (RuntimeException error)
    {
        throw new IllegalArgumentException
            ("Expected constraints in one of the following formats:\n" +
             "  col1, row1\n  col1, row1, col2, row2\n" +
             "  col1, row1, hAlign, vAlign\n" +
             "  col1, row1, col2, row2, hAlign, vAlign\n" +
             "Constraints provided '" + constraints + "'");
    }

    // Make sure row2 >= row1
    if (row2 < row1)
        row2 = row1;

    // Make sure col2 >= col1
    if (col2 < col1)
        col2 = col1;
}



/**
 * Gets a string representation of this TableLayoutConstraints.
 *
 * @return a string in the form "row 1, column 1, row 2, column 2, horizontal
 *         justification, vertical justification"
 */

public String toString ()
{
    StringBuffer buffer = new StringBuffer();

    buffer.append (col1);
    buffer.append (", ");
    buffer.append (row1);
    buffer.append (", ");

    buffer.append (col2);
    buffer.append (", ");
    buffer.append (row2);
    buffer.append (", ");

    final String h[] = {"left", "center", "full", "right", "leading",
                        "trailing"};
    final String v[] = {"top", "center", "full", "bottom"};

    buffer.append (h[hAlign]);
    buffer.append (", ");
    buffer.append (v[vAlign]);

    return buffer.toString();
}



}