42 |
import java.awt.ComponentOrientation; |
import java.awt.ComponentOrientation; |
43 |
import java.awt.Container; |
import java.awt.Container; |
44 |
import java.awt.Dimension; |
import java.awt.Dimension; |
|
import java.awt.GridLayout; |
|
45 |
import java.awt.LayoutManager2; |
import java.awt.LayoutManager2; |
46 |
import java.io.Serializable; |
import java.io.Serializable; |
47 |
|
|
|
|
|
48 |
/** |
/** |
49 |
* A layout for swing components. |
* A layout for swing components. |
|
* This implementation delegates its methods to |
|
|
* java.awt.GridLayout to do its work. |
|
50 |
* |
* |
51 |
* @author Ronald Veldema (rveldema@cs.vu.nl) |
* @author Ronald Veldema (rveldema@cs.vu.nl) |
52 |
*/ |
*/ |
83 |
private Container container; |
private Container container; |
84 |
|
|
85 |
/* |
/* |
|
* Internal layout. |
|
|
*/ |
|
|
private GridLayout grid; |
|
|
|
|
|
/* |
|
86 |
* Current type of component layouting. Defaults to X_AXIS. |
* Current type of component layouting. Defaults to X_AXIS. |
87 |
*/ |
*/ |
88 |
private int way = X_AXIS; |
private int way = X_AXIS; |
99 |
{ |
{ |
100 |
int width = 0; |
int width = 0; |
101 |
int height = 0; |
int height = 0; |
|
ComponentOrientation orientation = container.getComponentOrientation(); |
|
|
|
|
102 |
this.container = container; |
this.container = container; |
103 |
this.way = way; |
this.way = way; |
|
|
|
|
switch (way) |
|
|
{ |
|
|
case X_AXIS: |
|
|
width = 1; |
|
|
break; |
|
|
case Y_AXIS: |
|
|
height = 1; |
|
|
break; |
|
|
case LINE_AXIS: |
|
|
if (orientation.isHorizontal()) |
|
|
height = 1; |
|
|
else |
|
|
width = 1; |
|
|
break; |
|
|
case PAGE_AXIS: |
|
|
if (!orientation.isHorizontal()) |
|
|
height = 1; |
|
|
else |
|
|
width = 1; |
|
|
break; |
|
|
default: |
|
|
throw new AWTError("Invalid value for way"); |
|
|
} |
|
|
|
|
|
grid = new GridLayout(width, height); |
|
104 |
} |
} |
105 |
|
|
106 |
/** |
/** |
107 |
* Adds a component to the layout. |
* Adds a component to the layout. Not used in BoxLayout. |
108 |
* |
* |
109 |
* @param name The name of the component to add. |
* @param name The name of the component to add. |
110 |
* @param component the component to add to the layout. |
* @param component the component to add to the layout. |
111 |
*/ |
*/ |
112 |
public void addLayoutComponent(String name, Component component) |
public void addLayoutComponent(String name, Component component) |
113 |
{ |
{ |
|
if (way == X_AXIS |
|
|
|| (way == LINE_AXIS |
|
|
&& component.getComponentOrientation().isHorizontal()) |
|
|
|| (way == PAGE_AXIS |
|
|
&& !component.getComponentOrientation().isHorizontal())) |
|
|
grid.setColumns(grid.getColumns() + 1); |
|
|
else |
|
|
grid.setRows(grid.getRows() + 1); |
|
114 |
} |
} |
115 |
|
|
116 |
/** |
/** |
117 |
* Removes a component from the layout. |
* Removes a component from the layout. Not used in BoxLayout. |
118 |
* |
* |
119 |
* @param component The component to remove from the layout. |
* @param component The component to remove from the layout. |
120 |
*/ |
*/ |
121 |
public void removeLayoutComponent(Component component) |
public void removeLayoutComponent(Component component) |
122 |
{ |
{ |
123 |
grid.removeLayoutComponent(component); |
} |
124 |
|
|
125 |
if (way == X_AXIS |
private boolean isHorizontalIn(Container parent) |
126 |
|| (way == LINE_AXIS |
{ |
127 |
&& component.getComponentOrientation().isHorizontal()) |
ComponentOrientation orientation = parent.getComponentOrientation(); |
128 |
|| (way == PAGE_AXIS |
return this.way == X_AXIS |
129 |
&& !component.getComponentOrientation().isHorizontal())) |
|| (this.way == LINE_AXIS |
130 |
grid.setColumns(grid.getColumns() - 1); |
&& orientation.isHorizontal()) |
131 |
else |
|| (this.way == PAGE_AXIS |
132 |
grid.setRows(grid.getRows() - 1); |
&& (!orientation.isHorizontal())); |
133 |
} |
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
/** |
/** |
138 |
* Returns the preferred size of the layout. |
* Returns the preferred size of the layout. |
139 |
* |
* |
145 |
{ |
{ |
146 |
if (parent != container) |
if (parent != container) |
147 |
throw new AWTError("invalid parent"); |
throw new AWTError("invalid parent"); |
148 |
|
|
149 |
|
int x = 0; |
150 |
|
int y = 0; |
151 |
|
|
152 |
|
Component[] children = parent.getComponents(); |
153 |
|
|
154 |
|
if (isHorizontalIn(parent)) |
155 |
|
{ |
156 |
|
// sum up preferred widths of components, find maximum of preferred |
157 |
|
// heights |
158 |
|
for (int index = 0; index < children.length; index++) |
159 |
|
{ |
160 |
|
Component comp = children[index]; |
161 |
|
Dimension sz = comp.getPreferredSize(); |
162 |
|
x += sz.width; |
163 |
|
y = Math.max(y, sz.height); |
164 |
|
} |
165 |
|
} |
166 |
|
else |
167 |
|
{ |
168 |
|
// sum up preferred heights of components, find maximum of |
169 |
|
// preferred widths |
170 |
|
for (int index = 0; index < children.length; index++) |
171 |
|
{ |
172 |
|
Component comp = children[index]; |
173 |
|
Dimension sz = comp.getPreferredSize(); |
174 |
|
y += sz.height; |
175 |
|
x = Math.max(x, sz.width); |
176 |
|
} |
177 |
|
} |
178 |
|
|
179 |
return grid.preferredLayoutSize(parent); |
return new Dimension(x, y); |
180 |
} |
} |
181 |
|
|
182 |
/** |
/** |
190 |
{ |
{ |
191 |
if (parent != container) |
if (parent != container) |
192 |
throw new AWTError("invalid parent"); |
throw new AWTError("invalid parent"); |
193 |
|
|
194 |
|
int x = 0; |
195 |
|
int y = 0; |
196 |
|
|
197 |
|
Component[] children = parent.getComponents(); |
198 |
|
|
199 |
|
if (isHorizontalIn(parent)) |
200 |
|
{ |
201 |
|
// sum up preferred widths of components, find maximum of preferred |
202 |
|
// heights |
203 |
|
for (int index = 0; index < children.length; index++) |
204 |
|
{ |
205 |
|
Component comp = children[index]; |
206 |
|
Dimension sz = comp.getMinimumSize(); |
207 |
|
x += sz.width; |
208 |
|
y = Math.max(y, sz.height); |
209 |
|
} |
210 |
|
} |
211 |
|
else |
212 |
|
{ |
213 |
|
// sum up preferred heights of components, find maximum of |
214 |
|
// preferred widths |
215 |
|
for (int index = 0; index < children.length; index++) |
216 |
|
{ |
217 |
|
Component comp = children[index]; |
218 |
|
Dimension sz = comp.getMinimumSize(); |
219 |
|
y += sz.height; |
220 |
|
x = Math.max(x, sz.width); |
221 |
|
} |
222 |
|
} |
223 |
|
|
224 |
return grid.minimumLayoutSize(parent); |
return new Dimension(x, y); |
225 |
} |
} |
226 |
|
|
227 |
/** |
/** |
233 |
{ |
{ |
234 |
if (parent != container) |
if (parent != container) |
235 |
throw new AWTError("invalid parent"); |
throw new AWTError("invalid parent"); |
|
|
|
|
grid.layoutContainer(parent); |
|
|
} |
|
236 |
|
|
237 |
|
Dimension size = parent.getSize(); |
238 |
|
|
239 |
|
Component[] children = parent.getComponents(); |
240 |
|
|
241 |
|
if (isHorizontalIn(parent)) |
242 |
|
{ |
243 |
|
int x = 0; |
244 |
|
for (int index = 0; index < children.length; index++) |
245 |
|
{ |
246 |
|
Component comp = children[index]; |
247 |
|
Dimension sz = comp.getPreferredSize(); |
248 |
|
int width = sz.width; |
249 |
|
int height = sz.height; |
250 |
|
int cy = 0; |
251 |
|
if (height > size.height) |
252 |
|
{ |
253 |
|
height = size.height; |
254 |
|
} |
255 |
|
else |
256 |
|
{ |
257 |
|
cy = (int) ((size.height - height) * comp.getAlignmentY()); |
258 |
|
} |
259 |
|
|
260 |
|
comp.setSize(width, height); |
261 |
|
comp.setLocation(x, cy); |
262 |
|
x = x + width; |
263 |
|
} |
264 |
|
} |
265 |
|
else |
266 |
|
{ |
267 |
|
int y = 0; |
268 |
|
for (int index = 0; index < children.length; index++) |
269 |
|
{ |
270 |
|
Component comp = children[index]; |
271 |
|
Dimension sz = comp.getPreferredSize(); |
272 |
|
int width = sz.width; |
273 |
|
int height = sz.height; |
274 |
|
int cx = 0; |
275 |
|
if (width > size.width) |
276 |
|
{ |
277 |
|
width = size.width; |
278 |
|
} |
279 |
|
else |
280 |
|
{ |
281 |
|
cx = (int) ((size.width - width) * comp.getAlignmentX()); |
282 |
|
} |
283 |
|
|
284 |
|
comp.setSize(width, height); |
285 |
|
comp.setLocation(cx, y); |
286 |
|
y = y + height; |
287 |
|
} |
288 |
|
} |
289 |
|
} |
290 |
|
|
291 |
/** |
/** |
292 |
* Adds a component to the layout. |
* Adds a component to the layout. Not used in BoxLayout |
293 |
* |
* |
294 |
* @param child The component to add to the layout. |
* @param child The component to add to the layout. |
295 |
* @param constraints The constraints for the component in the layout. |
* @param constraints The constraints for the component in the layout. |
296 |
*/ |
*/ |
297 |
public void addLayoutComponent(Component child, Object constraints) |
public void addLayoutComponent(Component child, Object constraints) |
298 |
{ |
{ |
|
addLayoutComponent("", child); |
|
299 |
} |
} |
300 |
|
|
301 |
/** |
/** |
351 |
{ |
{ |
352 |
if (parent != container) |
if (parent != container) |
353 |
throw new AWTError("invalid parent"); |
throw new AWTError("invalid parent"); |
354 |
|
|
355 |
return preferredLayoutSize(parent); |
int x = 0; |
356 |
|
int y = 0; |
357 |
|
|
358 |
|
Component[] children = parent.getComponents(); |
359 |
|
|
360 |
|
if (isHorizontalIn(parent)) |
361 |
|
{ |
362 |
|
|
363 |
|
// sum up preferred widths of components, find maximum of preferred |
364 |
|
// heights |
365 |
|
for (int index = 0; index < children.length; index++) |
366 |
|
{ |
367 |
|
Component comp = children[index]; |
368 |
|
Dimension sz = comp.getMaximumSize(); |
369 |
|
x += sz.width; |
370 |
|
y = Math.max(y, sz.height); |
371 |
|
} |
372 |
|
} |
373 |
|
else |
374 |
|
{ |
375 |
|
// sum up preferred heights of components, find maximum of |
376 |
|
// preferred widths |
377 |
|
for (int index = 0; index < children.length; index++) |
378 |
|
{ |
379 |
|
Component comp = children[index]; |
380 |
|
Dimension sz = comp.getMaximumSize(); |
381 |
|
y += sz.height; |
382 |
|
x = Math.max(x, sz.width); |
383 |
|
} |
384 |
|
} |
385 |
|
return new Dimension(x, y); |
386 |
} |
} |
387 |
} |
} |