1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49:
56: public class BoxLayout implements LayoutManager2, Serializable
57: {
58:
59:
62: public static final int X_AXIS = 0;
63:
64:
67: public static final int Y_AXIS = 1;
68:
69:
72: public static final int LINE_AXIS = 2;
73:
74:
77: public static final int PAGE_AXIS = 3;
78:
79:
82: private static final long serialVersionUID = -2474455742719112368L;
83:
84:
87: private Container container;
88:
89:
92: private int way = X_AXIS;
93:
94:
97: private SizeRequirements[] xChildren;
98:
99:
102: private SizeRequirements[] yChildren;
103:
104:
107: private SizeRequirements xTotal;
108:
109:
112: private SizeRequirements yTotal;
113:
114:
117: private int[] offsetsX;
118:
119:
122: private int[] offsetsY;
123:
124:
127: private int[] spansX;
128:
129:
132: private int[] spansY;
133:
134:
142: public BoxLayout(Container container, int way)
143: {
144: if (way != X_AXIS && way != Y_AXIS && way != LINE_AXIS && way != PAGE_AXIS)
145: throw new AWTError("Invalid axis");
146:
147: int width = 0;
148: int height = 0;
149: this.container = container;
150: this.way = way;
151: }
152:
153:
159: public void addLayoutComponent(String name, Component component)
160: {
161:
162: }
163:
164:
169: public void removeLayoutComponent(Component component)
170: {
171:
172: }
173:
174: private boolean isHorizontalIn(Container parent)
175: {
176: ComponentOrientation orientation = parent.getComponentOrientation();
177: return this.way == X_AXIS
178: || (this.way == LINE_AXIS
179: && orientation.isHorizontal())
180: || (this.way == PAGE_AXIS
181: && (!orientation.isHorizontal()));
182: }
183:
184:
185:
186:
193: public Dimension preferredLayoutSize(Container parent)
194: {
195: synchronized (container.getTreeLock())
196: {
197: if (container != parent)
198: throw new AWTError("BoxLayout can't be shared");
199:
200: checkTotalRequirements();
201: Insets i = container.getInsets();
202: return new Dimension(xTotal.preferred + i.left + i.right,
203: yTotal.preferred + i.top + i.bottom);
204: }
205: }
206:
207:
214: public Dimension minimumLayoutSize(Container parent)
215: {
216: synchronized (container.getTreeLock())
217: {
218: if (container != parent)
219: throw new AWTError("BoxLayout can't be shared");
220:
221: checkTotalRequirements();
222: Insets i = container.getInsets();
223: return new Dimension(xTotal.minimum + i.left + i.right,
224: yTotal.minimum + i.top + i.bottom);
225: }
226: }
227:
228:
233: public void layoutContainer(Container parent)
234: {
235: synchronized (container.getTreeLock())
236: {
237: if (container != parent)
238: throw new AWTError("BoxLayout can't be shared");
239:
240: checkLayout();
241: Component[] children = container.getComponents();
242: Insets in = container.getInsets();
243: for (int i = 0; i < children.length; i++)
244: children[i].setBounds(offsetsX[i] + in.left, offsetsY[i] + in.top,
245: spansX[i], spansY[i]);
246: }
247: }
248:
249:
255: public void addLayoutComponent(Component child, Object constraints)
256: {
257:
258: }
259:
260:
267: public float getLayoutAlignmentX(Container parent)
268: {
269: synchronized (container.getTreeLock())
270: {
271: if (container != parent)
272: throw new AWTError("BoxLayout can't be shared");
273:
274: checkTotalRequirements();
275: return xTotal.alignment;
276: }
277: }
278:
279:
286: public float getLayoutAlignmentY(Container parent)
287: {
288: synchronized (container.getTreeLock())
289: {
290: if (container != parent)
291: throw new AWTError("BoxLayout can't be shared");
292:
293: checkTotalRequirements();
294: return yTotal.alignment;
295: }
296: }
297:
298:
303: public void invalidateLayout(Container parent)
304: {
305: if (container != parent)
306: throw new AWTError("BoxLayout can't be shared");
307:
308: synchronized (container.getTreeLock())
309: {
310: xChildren = null;
311: yChildren = null;
312: xTotal = null;
313: yTotal = null;
314: offsetsX = null;
315: offsetsY = null;
316: spansX = null;
317: spansY = null;
318: }
319: }
320:
321:
329: public Dimension maximumLayoutSize(Container parent)
330: {
331: synchronized (container.getTreeLock())
332: {
333: if (container != parent)
334: throw new AWTError("BoxLayout can't be shared");
335:
336: checkTotalRequirements();
337: Insets i = container.getInsets();
338: return new Dimension(xTotal.maximum + i.left + i.right,
339: yTotal.maximum + i.top + i.bottom);
340: }
341: }
342:
343:
348: private void checkTotalRequirements()
349: {
350: if (xTotal == null || yTotal == null)
351: {
352: checkRequirements();
353: if (isHorizontalIn(container))
354: {
355: xTotal = SizeRequirements.getTiledSizeRequirements(xChildren);
356: yTotal = SizeRequirements.getAlignedSizeRequirements(yChildren);
357: }
358: else
359: {
360: xTotal = SizeRequirements.getAlignedSizeRequirements(xChildren);
361: yTotal = SizeRequirements.getTiledSizeRequirements(yChildren);
362: }
363: }
364: }
365:
366:
371: private void checkRequirements()
372: {
373: if (xChildren == null || yChildren == null)
374: {
375: Component[] children = container.getComponents();
376: xChildren = new SizeRequirements[children.length];
377: yChildren = new SizeRequirements[children.length];
378: for (int i = 0; i < children.length; i++)
379: {
380: if (! children[i].isVisible())
381: {
382: xChildren[i] = new SizeRequirements();
383: yChildren[i] = new SizeRequirements();
384: }
385: else
386: {
387: xChildren[i] =
388: new SizeRequirements(children[i].getMinimumSize().width,
389: children[i].getPreferredSize().width,
390: children[i].getMaximumSize().width,
391: children[i].getAlignmentX());
392: yChildren[i] =
393: new SizeRequirements(children[i].getMinimumSize().height,
394: children[i].getPreferredSize().height,
395: children[i].getMaximumSize().height,
396: children[i].getAlignmentY());
397: }
398: }
399: }
400: }
401:
402:
407: private void checkLayout()
408: {
409: if (offsetsX == null || offsetsY == null || spansX == null
410: || spansY == null)
411: {
412: checkRequirements();
413: checkTotalRequirements();
414: int len = container.getComponents().length;
415: offsetsX = new int[len];
416: offsetsY = new int[len];
417: spansX = new int[len];
418: spansY = new int[len];
419:
420: Insets in = container.getInsets();
421: int width = container.getWidth() - in.left - in.right;
422: int height = container.getHeight() - in.top -in.bottom;
423:
424: if (isHorizontalIn(container))
425: {
426: SizeRequirements.calculateTiledPositions(width,
427: xTotal, xChildren,
428: offsetsX, spansX);
429: SizeRequirements.calculateAlignedPositions(height,
430: yTotal, yChildren,
431: offsetsY, spansY);
432: }
433: else
434: {
435: SizeRequirements.calculateAlignedPositions(width,
436: xTotal, xChildren,
437: offsetsX, spansX);
438: SizeRequirements.calculateTiledPositions(height,
439: yTotal, yChildren,
440: offsetsY, spansY);
441: }
442: }
443: }
444: }