File: SingleFiledLayout.java

package info (click to toggle)
libtablelayout-java 20070421-2
  • links: PTS, VCS
  • area: non-free
  • in suites: lenny
  • size: 156 kB
  • ctags: 125
  • sloc: java: 1,155; xml: 25; sh: 12; makefile: 5
file content (416 lines) | stat: -rw-r--r-- 11,741 bytes parent folder | download
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * ====================================================================
 *
 * The Clearthought Software License, Version 1.0
 *
 * Copyright (c) 2001 Daniel Barbalace.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. The original software may not be altered.  However, the classes
 *    provided may be subclasses as long as the subclasses are not
 *    packaged in the info.clearthought package or any subpackage of
 *    info.clearthought.
 *
 * 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 info.clearthought.layout;



import java.awt.*;



/**
 * SingleFiledLayout lays out components singled filed.  This layout manager is
 * like FlowLayout except that all components are placed in a single row or
 * column.
 *
 * @version 1.1 April 4, 2002
 * @author  Daniel E. Barbalace
 */

public class SingleFiledLayout implements
    java.awt.LayoutManager,
    java.io.Serializable
{



/** Align components in a column */
public static final int COLUMN = 0;

/** Align components in a row */
public static final int ROW = 1;

/** Left justify components */
public static final int LEFT = 0;

/** Top justify components */
public static final int TOP = 0;

/** Center components */
public static final int CENTER = 1;

/** Full justify components */
public static final int FULL = 2;

/** Bottom justify components */
public static final int BOTTOM = 3;

/** Right justify components */
public static final int RIGHT = 4;

/** Default gap -- derived classes may override */
public static int DEFAULT_GAP = 5;



/** ROW or COLUMN -- should the components be aligned in a row or column */
protected int orientation;

/** LEFT, TOP, CENTER, FULL, BOTTOM, RIGHT -- how should components of different
    sizes be aligned */
protected int justification;

/** Space between components in pixels */
protected int gap;



/**
 * Constructs an instance of SingleFiledLayout that will align components in a
 * column using the default gap and LEFT justification.
 */

public SingleFiledLayout ()
{
    this (COLUMN, LEFT, DEFAULT_GAP);
}


/**
 * Constructs an instance of SingleFiledLayout using the default gap and LEFT
 * or TOP justification.
 *
 * @param orientation    ROW or COLUMN -- should the components be aligned in
 *                       a row or column
 */

public SingleFiledLayout (int orientation)
{
    this (orientation, LEFT, DEFAULT_GAP);
}



/**
 * Constructs an instance of SingleFiledLayout.
 *
 * @param orientation      ROW or COLUMN -- should the components be aligned in
 *                         a row or column
 * @param justification    LEFT, TOP, CENTER, FULL, BOTTOM, RIGHT -- how should
 *                         components of different sizes be aligned
 * @param gap              space between components in pixels
 */

public SingleFiledLayout (int orientation, int justification, int gap)
{
    // Validate parameters
    if (orientation != ROW)
        orientation = COLUMN;

    if ((justification != CENTER) && (justification != FULL) &&
        (justification != RIGHT))
        justification = LEFT;

    if (gap < 0)
        gap = 0;

    // Copy parameters
    this.orientation = orientation;
    this.justification = justification;
    this.gap = gap;
}



//******************************************************************************
//** java.awt.event.LayoutManager methods                                    ***
//******************************************************************************



/**
 * To lay out the specified container using this layout.  This method
 * repositions the components in the specified target container.
 *
 * <p>User code should not have to call this method directly.</p>
 *
 * @param container    container being served by this layout manager
 */

public void layoutContainer (Container container)
{
    // Use preferred size to get maximum width or height
    Dimension size = container.getSize();

    // Get the container's insets
    Insets inset = container.getInsets();

    // Start at top left of container
    int x = inset.left;
    int y = inset.top;

    // Get the components inside the container
    Component component[] = container.getComponents();

    // Components arranged in a column
    if (orientation == COLUMN)
        // Add each component
        for (int counter = 0; counter < component.length; counter++)
        {
            // Use preferred size of component
            Dimension d = component[counter].getPreferredSize();

            // Align component
            switch (justification)
            {
                case LEFT :
                    x = inset.left;
                break;

                case CENTER :
                    x = ((size.width - d.width) >> 1) +
                        inset.left - inset.right;
                break;

                case FULL :
                    x = inset.left;
                    d.width = size.width - inset.left - inset.right;
                break;

                case RIGHT :
                    x = size.width - d.width - inset.right;
                break;
            }

            // Set size and location
            component[counter].setBounds (x, y, d.width, d.height);

            // Increment y
            y += d.height + gap;
        }
    // Components arranged in a row
    else
        // Add each component
        for (int counter = 0; counter < component.length; counter++)
        {
            // Use preferred size of component
            Dimension d = component[counter].getPreferredSize();

            // Align component
            switch (justification)
            {
                case TOP :
                    y = inset.top;
                break;

                case CENTER :
                    y = ((size.height - d.height) >> 1) +
                        inset.top - inset.bottom;
                break;

                case FULL :
                    y = inset.top;
                    d.height = size.height - inset.top - inset.bottom;
                break;

                case BOTTOM :
                    y = size.height - d.height - inset.bottom;
                break;
            }

            // Set size and location
            component[counter].setBounds (x, y, d.width, d.height);

            // Increment x
            x += d.width + gap;
        }
}



/**
 * Determines the preferred size of the container argument using this layout.
 * The preferred size is the smallest size that, if used for the container's
 * size, will ensure that no component is truncated when the component is it's
 * preferred size.
 *
 * @param container    container being served by this layout manager
 *
 * @return a dimension indicating the container's preferred size
 */

public Dimension preferredLayoutSize (Container container)
{
    int totalWidth = 0;  // Width of all components
    int totalHeight = 0; // Height of all components

    // Get the components inside the container
    Component component[] = container.getComponents();

    // Components arranged in a column
    if (orientation == COLUMN)
    {
        // Add each component
        for (int counter = 0; counter < component.length; counter++)
        {
            Dimension d = component[counter].getPreferredSize();

            if (totalWidth < d.width)
                totalWidth = d.width;

            totalHeight += d.height + gap;
        }

        // Subtract extra gap
        totalHeight -= gap;
    }
    // Components arranged in a row
    else
    {
        // Add each component
        for (int counter = 0; counter < component.length; counter++)
        {
            Dimension d = component[counter].getPreferredSize();

            totalWidth += d.width + gap;

            if (totalHeight < d.height)
                totalHeight = d.height;
        }

        // Subtract extra gap
        totalWidth -= gap;
    }

    // Add insets to preferred size
    Insets inset = container.getInsets();
    totalWidth += inset.left + inset.right;
    totalHeight += inset.top + inset.bottom;

    return new Dimension(totalWidth, totalHeight);
}



/**
 * Determines the minimum size of the container argument using this layout.
 * The minimum size is the smallest size that, if used for the container's
 * size, will ensure that no component is truncated.  The minimum size is the
 * preferred size.
 *
 * @param container    container being served by this layout manager
 *
 * @return a dimension indicating the container's minimum size
 */

public Dimension minimumLayoutSize (Container container)
{
    int totalWidth = 0;  // Width of all components
    int totalHeight = 0; // Height of all components

    // Get the components inside the container
    Component component[] = container.getComponents();

    // Components arranged in a column
    if (orientation == COLUMN)
    {
        // Add each component
        for (int counter = 0; counter < component.length; counter++)
        {
            Dimension d = component[counter].getMinimumSize();

            if (totalWidth < d.width)
                totalWidth = d.width;

            totalHeight += d.height + gap;
        }

        // Subtract extra gap
        totalHeight -= gap;
    }
    // Components arranged in a row
    else
    {
        // Add each component
        for (int counter = 0; counter < component.length; counter++)
        {
            Dimension d = component[counter].getMinimumSize();

            totalWidth += d.width + gap;

            if (totalHeight < d.height)
                totalHeight = d.height;
        }

        // Subtract extra gap
        totalWidth =- gap;
    }

    // Add insets to preferred size
    Insets inset = container.getInsets();
    totalWidth += inset.left + inset.right;
    totalHeight += inset.top + inset.bottom;

    return new Dimension(totalWidth, totalHeight);
}



/**
 * Adds the specified component with the specified name to the layout.
 *
 * @param name         dummy parameter
 * @param component    component to add
 */

public void addLayoutComponent (String name, Component component)
{
}



/**
 * Removes the specified component with the specified name from the layout.
 *
 * @param component    component being removed
 */

public void removeLayoutComponent (Component component)
{
}



}