1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48:
62: public class SpringLayout implements LayoutManager2
63: {
64:
65:
66: public static final String EAST = "East";
67:
68:
69: public static final String NORTH = "North";
70:
71:
72: public static final String SOUTH = "South";
73:
74:
75: public static final String WEST = "West";
76:
77:
78: private Map constraintsMap;
79:
80:
91: public static class Constraints
92: {
93:
94:
95:
96: private Spring x;
97:
98:
99: private Spring y;
100:
101:
102: private Spring height;
103:
104:
105: private Spring width;
106:
107:
108: private Spring east;
109:
110:
111: private Spring south;
112:
113:
117: public Constraints()
118: {
119: x = y = height = width = east = south = null;
120: }
121:
122:
128: public Constraints(Spring x, Spring y)
129: {
130: this.x = x;
131: this.y = y;
132: width = height = east = south = null;
133: }
134:
135:
143: public Constraints(Spring x, Spring y, Spring width, Spring height)
144: {
145: this.x = x;
146: this.y = y;
147: this.width = width;
148: this.height = height;
149: east = south = null;
150: }
151:
152:
160: public Spring getConstraint(String edgeName)
161: {
162: Spring retVal = null;
163: if (edgeName.equals(SpringLayout.NORTH))
164: retVal = y;
165: else if (edgeName.equals(SpringLayout.WEST))
166: retVal = x;
167: else if (edgeName.equals(SpringLayout.SOUTH))
168: {
169: retVal = south;
170: if ((retVal == null) && (y != null) && (height != null))
171: retVal = Spring.sum(y, height);
172: }
173: else if (edgeName.equals(SpringLayout.EAST))
174: {
175: retVal = east;
176: if ((retVal == null) && (x != null) && (width != null))
177: retVal = Spring.sum(x, width);
178: }
179:
180: return retVal;
181: }
182:
183:
188: public Spring getHeight()
189: {
190: Spring retVal = height;
191: if ((retVal == null) && (y != null) && (south != null))
192: {
193: retVal = Spring.sum(south, Spring.minus(y));
194: }
195: return retVal;
196: }
197:
198:
203: public Spring getWidth()
204: {
205: Spring retVal = width;
206: if ((retVal == null) && (x != null) && (east != null))
207: {
208: retVal = Spring.sum(east, Spring.minus(x));
209: }
210: return retVal;
211: }
212:
213:
218: public Spring getX()
219: {
220: Spring retVal = x;
221: if ((retVal == null) && (width != null) && (east != null))
222: {
223: retVal = Spring.sum(east, Spring.minus(width));
224: }
225: return retVal;
226: }
227:
228:
233: public Spring getY()
234: {
235: Spring retVal = y;
236: if ((retVal == null) && (height != null) && (south != null))
237: {
238: retVal = Spring.sum(south, Spring.minus(height));
239: }
240: return retVal;
241: }
242:
243:
252: public void setConstraint(String edgeName, Spring s)
253: {
254:
255: if (edgeName.equals(SpringLayout.WEST))
256: {
257: x = s;
258: if ((width != null) && (east != null))
259: width = Spring.sum(east, Spring.minus(x));
260: }
261: else if (edgeName.equals(SpringLayout.NORTH))
262: {
263: y = s;
264: if ((height != null) && (south != null))
265: height = Spring.sum(south, Spring.minus(y));
266: }
267: else if (edgeName.equals(SpringLayout.EAST))
268: {
269: east = s;
270: if ((x != null) && (width != null))
271: x = Spring.sum(east, Spring.minus(width));
272: }
273: else if (edgeName.equals(SpringLayout.SOUTH))
274: {
275: south = s;
276: if ((height != null) && (y != null))
277: y = Spring.sum(south, Spring.minus(height));
278: }
279:
280: }
281:
282:
287: public void setHeight(Spring s)
288: {
289: height = s;
290: if ((south != null) && (y != null))
291: south = Spring.sum(y, height);
292:
293: }
294:
295:
300: public void setWidth(Spring s)
301: {
302: width = s;
303: if ((east != null) && (x != null))
304: east = Spring.sum(x, width);
305:
306: }
307:
308:
313: public void setX(Spring s)
314: {
315: x = s;
316: if ((width != null) && (east != null))
317: width = Spring.sum(east, Spring.minus(x));
318:
319: }
320:
321:
326: public void setY(Spring s)
327: {
328: y = s;
329: if ((height != null) && (south != null))
330: height = Spring.sum(south, Spring.minus(y));
331:
332: }
333: }
334:
335:
338: public SpringLayout()
339: {
340:
341: constraintsMap = new HashMap();
342: }
343:
344:
352: public void addLayoutComponent(Component component, Object constraint)
353: {
354: constraintsMap.put(component, constraint);
355: }
356:
357:
358:
367: public void addLayoutComponent(String name, Component c)
368: {
369:
370: }
371:
372:
381: public Spring getConstraint(String edgeName, Component c)
382: {
383: Constraints constraints = getConstraints(c);
384: return constraints.getConstraint(edgeName);
385: }
386:
387:
395: public SpringLayout.Constraints getConstraints(Component c)
396: {
397: Constraints constraints = (Constraints) constraintsMap.get(c);
398:
399: if (constraints == null)
400: {
401: Container parent = c.getParent();
402: constraints = new Constraints();
403:
404: if (parent != null)
405: {
406: constraints.setX(Spring.constant(parent.getInsets().left));
407: constraints.setY(Spring.constant(parent.getInsets().top));
408: }
409: else
410: {
411: constraints.setX(Spring.constant(0));
412: constraints.setY(Spring.constant(0));
413: }
414: }
415: constraints.setWidth(Spring.constant(c.getMinimumSize().width,
416: c.getPreferredSize().width,
417: c.getMaximumSize().width));
418: constraints.setHeight(Spring.constant(c.getMinimumSize().height,
419: c.getPreferredSize().height,
420: c.getMaximumSize().height));
421: constraintsMap.put(c, constraints);
422:
423: return constraints;
424: }
425:
426:
434: public float getLayoutAlignmentX(Container p)
435: {
436: return 0.0F;
437: }
438:
439:
446: public float getLayoutAlignmentY(Container p)
447: {
448: return 0.0F;
449: }
450:
451:
454: public void invalidateLayout(Container p)
455: {
456:
457: }
458:
459:
464: public void layoutContainer(Container p)
465: {
466:
467: addLayoutComponent(p, new Constraints(Spring.constant(0),
468: Spring.constant(0)));
469:
470: int offsetX = p.getInsets().left;
471: int offsetY = p.getInsets().right;
472:
473: Component[] components = p.getComponents();
474: for (int index = 0; index < components.length; index++)
475: {
476: Component c = components[index];
477:
478: Constraints constraints = getConstraints(c);
479: int x = constraints.getX().getValue();
480: int y = constraints.getY().getValue();
481: int width = constraints.getWidth().getValue();
482: int height = constraints.getHeight().getValue();
483:
484: c.setLocation(x + offsetX, y + offsetY);
485: c.setSize(width, height);
486: }
487:
488: }
489:
490:
497: public Dimension maximumLayoutSize(Container p)
498: {
499: int maxX = 0;
500: int maxY = 0;
501:
502: int offsetX = p.getInsets().left;
503: int offsetY = p.getInsets().right;
504:
505: Component[] components = p.getComponents();
506: for (int index = 0; index < components.length; index++)
507: {
508: Component c = components[index];
509: Constraints constraints = getConstraints(c);
510: int x = constraints.getX().getMaximumValue();
511: int y = constraints.getY().getMaximumValue();
512: int width = constraints.getWidth().getMaximumValue();
513: int height = constraints.getHeight().getMaximumValue();
514:
515: int rightEdge = offsetX + x + width;
516: if (rightEdge > maxX)
517: maxX = rightEdge;
518: int bottomEdge = offsetY + y + height;
519: if (bottomEdge > maxY)
520: maxY = bottomEdge;
521: }
522:
523: return new Dimension(maxX, maxY);
524: }
525:
526:
527:
534: public Dimension minimumLayoutSize(Container p)
535: {
536: int maxX = 0;
537: int maxY = 0;
538:
539: int offsetX = p.getInsets().left;
540: int offsetY = p.getInsets().right;
541:
542: Component[] components = p.getComponents();
543: for (int index = 0; index < components.length; index++)
544: {
545: Component c = components[index];
546: Constraints constraints = getConstraints(c);
547: int x = constraints.getX().getMinimumValue();
548: int y = constraints.getY().getMinimumValue();
549: int width = constraints.getWidth().getMinimumValue();
550: int height = constraints.getHeight().getMinimumValue();
551:
552: int rightEdge = offsetX + x + width;
553: if (rightEdge > maxX)
554: maxX = rightEdge;
555: int bottomEdge = offsetY + y + height;
556: if (bottomEdge > maxY)
557: maxY = bottomEdge;
558: }
559:
560: return new Dimension(maxX, maxY);
561: }
562:
563:
570: public Dimension preferredLayoutSize(Container p)
571: {
572: int maxX = 0;
573: int maxY = 0;
574:
575: int offsetX = p.getInsets().left;
576: int offsetY = p.getInsets().right;
577:
578: Component[] components = p.getComponents();
579: for (int index = 0; index < components.length; index++)
580: {
581: Component c = components[index];
582: Constraints constraints = getConstraints(c);
583: int x = constraints.getX().getPreferredValue();
584: int y = constraints.getY().getPreferredValue();
585: int width = constraints.getWidth().getPreferredValue();
586: int height = constraints.getHeight().getPreferredValue();
587:
588: int rightEdge = offsetX + x + width;
589: if (rightEdge > maxX)
590: maxX = rightEdge;
591: int bottomEdge = offsetY + y + height;
592: if (bottomEdge > maxY)
593: maxY = bottomEdge;
594: }
595: return new Dimension(maxX, maxY);
596: }
597:
598:
609: public void putConstraint(String e1, Component c1, int pad, String e2,
610: Component c2)
611: {
612: Constraints constraints1 = getConstraints(c1);
613: Constraints constraints2 = getConstraints(c2);
614:
615: Spring strut = Spring.constant(pad);
616: Spring otherEdge = constraints2.getConstraint(e2);
617: constraints1.setConstraint(e1, Spring.sum(strut, otherEdge));
618: }
619:
620:
631: public void putConstraint(String e1, Component c1, Spring s, String e2,
632: Component c2)
633: {
634: Constraints constraints1 = getConstraints(c1);
635: Constraints constraints2 = getConstraints(c2);
636:
637: Spring otherEdge = constraints2.getConstraint(e2);
638: constraints1.setConstraint(e1, Spring.sum(s, otherEdge));
639:
640: }
641:
642:
646: public void removeLayoutComponent(Component c)
647: {
648:
649: }
650: }