summaryrefslogtreecommitdiff
path: root/java/gjt/ColumnLayout.java
blob: bc51b44e456032c34f2f025c441493305f8a8236 (plain)
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
package gjt;

import java.awt.*;

/**
 * ColumnLayout lays out components in a column.  At 
 * construction time, both horizontal orientation and vertical 
 * orientation may be specified, along with the gap to use 
 * between components.<p>
 *
 * Horizontal orientation must be one of the following:
 * <dl>
 * <dd> LEFT 
 * <dd> CENTER  
 * <dd> RIGHT
 * </dl>
 *
 * Vertical orientation must be one of the following:
 * <dl>
 * <dd> TOP
 * <dd> CENTER  
 * <dd> BOTTOM
 * </dl>
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     Orientation
 * @see     RowLayout
 */
public class ColumnLayout implements LayoutManager {
    static private int _defaultGap = 5;

    private int         gap;
    private Orientation horizontalOrientation;
    private Orientation verticalOrientation;

    public ColumnLayout() {
        this(Orientation.CENTER, 
             Orientation.CENTER, _defaultGap);
    }
    public ColumnLayout(int gap) {
        this(Orientation.CENTER, Orientation.CENTER, gap);
    }
    public ColumnLayout(Orientation horizontalOrient, 
                        Orientation verticalOrient) {
        this(horizontalOrient, verticalOrient, _defaultGap);
    }
    public ColumnLayout(Orientation horizontalOrient, 
                        Orientation verticalOrient, int gap) {
        Assert.notFalse(gap >= 0);
        Assert.notFalse(
            horizontalOrient == Orientation.LEFT   ||
            horizontalOrient == Orientation.CENTER ||
            horizontalOrient == Orientation.RIGHT);
        Assert.notFalse(
            verticalOrient   == Orientation.TOP    ||
            verticalOrient   == Orientation.CENTER ||
            verticalOrient   == Orientation.BOTTOM);

        this.gap                   = gap;
        this.verticalOrientation   = verticalOrient;
        this.horizontalOrientation = horizontalOrient;
    }

    public void addLayoutComponent(String name, 
                                   Component comp) {
    }
    public void removeLayoutComponent(Component comp) { 
    }

    public Dimension preferredLayoutSize(Container target) {
        Insets    insets      = target.insets();
        Dimension dim         = new Dimension(0,0);
        int       ncomponents = target.countComponents();
        Component comp;
        Dimension d;

        for (int i = 0 ; i < ncomponents ; i++) {
            comp = target.getComponent(i);

            if(comp.isVisible()) {
                d = comp.preferredSize();
                if(i > 0) 
                    dim.height += gap;

                dim.height += d.height;
                dim.width   = Math.max(d.width, dim.width);
            }
        }
        dim.width  += insets.left + insets.right;
        dim.height += insets.top  + insets.bottom;
        return dim;
    }
    public Dimension minimumLayoutSize(Container target) {
        Insets    insets      = target.insets();
        Dimension dim         = new Dimension(0,0);
        int       ncomponents = target.countComponents();
        Component comp;
        Dimension d;

        for (int i = 0 ; i < ncomponents ; i++) {
            comp = target.getComponent(i);

            if(comp.isVisible()) {
                d = comp.minimumSize();

                dim.width  = Math.max(d.width, dim.width);
                dim.height += d.height;

                if(i > 0) dim.height += gap;
            }
        }
        dim.width  += insets.left + insets.right;
        dim.height += insets.top  + insets.bottom;

        return dim;
    }
    public void layoutContainer(Container target) {
        Insets    insets        = target.insets();
        int       top           = insets.top;
        int       left          = 0;
        int       ncomponents   = target.countComponents();
        Dimension preferredSize = target.preferredSize();
        Dimension targetSize    = target.size();
        Component comp;
        Dimension ps;

        if(verticalOrientation == Orientation.CENTER)
            top += (targetSize.height/2) - 
                   (preferredSize.height/2);
        else if(verticalOrientation == Orientation.BOTTOM)
            top = targetSize.height - preferredSize.height + 
                  insets.top;

        for (int i = 0 ; i < ncomponents ; i++) {
            comp = target.getComponent(i);
            left = insets.left;

            if(comp.isVisible()) {
                ps = comp.preferredSize();

                if(horizontalOrientation == Orientation.CENTER)
                    left = (targetSize.width/2) - (ps.width/2);
                else if(
                  horizontalOrientation == Orientation.RIGHT) {
                    left = targetSize.width - ps.width - 
                           insets.right;
                }
                comp.reshape(left,top,ps.width,ps.height);
                top += ps.height + gap;
            }
        }
    }
}