-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColumnLayout.java
More file actions
151 lines (133 loc) · 3.86 KB
/
ColumnLayout.java
File metadata and controls
151 lines (133 loc) · 3.86 KB
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
import java.awt.*;
/**
* The ColumnLayout class lays out items vertically and centered
* so that the left hand side of each component lines up.
*
* @version March 1997
* @author John D. Ramsdell
*/
public class ColumnLayout implements LayoutManager, java.io.Serializable {
int hgap; // Horizontal gap
int vgap; // Vertical gap
/**
* Constructs a new ColumnLayout.
* @param hgap horizontal gap
* @param vgap vertical gap
*/
public ColumnLayout(int hgap, int vgap) {
this.hgap = hgap;
this.vgap = vgap;
}
/**
* Constructs a new ColumnLayout
* with default values for the
* horizontal and vertical gap.
*/
public ColumnLayout() {
this(5, 5);
}
/**
* Adds the specified component to the layout. Not used by this class.
* @param name the name of the component
* @param comp the the component to be added
*/
public void addLayoutComponent(String name, Component comp) {
}
/**
* Removes the specified component from the layout. Not used by
* this class.
* @param comp the component to remove
*/
public void removeLayoutComponent(Component comp) {
}
/**
* Returns the preferred dimensions for this layout given the components
* in the specified target container.
* @param target the component which needs to be laid out
* @see Container
* @see #minimumLayoutSize
*/
public Dimension preferredLayoutSize(Container target) {
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount();
for (int i = 0 ; i < nmembers ; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
dim.width = Math.max(dim.width, d.width);
if (i > 0) {
dim.height += vgap;
}
dim.height += d.height;
}
}
return addEdges(target, dim);
}
private Dimension addEdges(Container target, Dimension dim) {
Insets insets = target.getInsets();
dim.width += insets.left + insets.right + hgap*2;
dim.height += insets.top + insets.bottom + vgap*2;
return dim;
}
/**
* Returns the minimum dimensions needed to layout the components
* contained in the specified target container.
* @param target the component which needs to be laid out
* @see #preferredLayoutSize
*/
public Dimension minimumLayoutSize(Container target) {
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount();
for (int i = 0 ; i < nmembers ; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getMinimumSize();
dim.width = Math.max(dim.width, d.width);
if (i > 0) {
dim.height += vgap;
}
dim.height += d.height;
}
}
return addEdges(target, dim);
}
/**
* Lays out the container.
* @param target the specified component being laid out.
* @see Container
*/
public void layoutContainer(Container target) {
Insets insets = target.getInsets();
int maxwidth = target.getSize().width
- (insets.left + insets.right + hgap*2);
int x = insets.left + hgap;
int y = insets.top;
int nmembers = target.getComponentCount();
int preferred_width = 0;
for (int i = 0; i < nmembers; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
preferred_width = Math.max(preferred_width, d.width);
}
}
if (preferred_width < maxwidth)
x += (maxwidth - preferred_width) / 2;
for (int i = 0; i < nmembers; i++) {
Component m = target.getComponent(i);
y += vgap;
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
m.setSize(d.width, d.height);
m.setLocation(x, y);
y += d.height;
}
}
}
/**
* Returns the String representation of this ColumnLayout's values.
*/
public String toString() {
return getClass().getName() + "[ hgap=" + hgap + ",vgap=" + vgap + "]";
}
}