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:
119: private Spring v;
120: private Spring h;
121:
122:
126: public Constraints()
127: {
128: x = y = height = width = east = south = v = h = null;
129: }
130:
131:
137: public Constraints(Spring x, Spring y)
138: {
139: this.x = x;
140: this.y = y;
141: width = height = east = south = v = h = null;
142: }
143:
144:
152: public Constraints(Spring x, Spring y, Spring width, Spring height)
153: {
154: this.x = x;
155: this.y = y;
156: this.width = width;
157: this.height = height;
158: east = south = v = h = null;
159: }
160:
161:
172: public Constraints(Component component)
173: {
174: this(Spring.constant(component.getX()),
175: Spring.constant(component.getY()),
176: Spring.width(component),
177: Spring.height(component));
178: }
179:
180:
188: public Spring getConstraint(String edgeName)
189: {
190: Spring retVal = null;
191: if (edgeName.equals(SpringLayout.NORTH))
192: retVal = getY();
193: else if (edgeName.equals(SpringLayout.WEST))
194: retVal = getX();
195: else if (edgeName.equals(SpringLayout.SOUTH))
196: retVal = getSouth();
197: else if (edgeName.equals(SpringLayout.EAST))
198: retVal = getEast();
199: return retVal;
200: }
201:
202:
207: public Spring getHeight()
208: {
209: if (height != null)
210: return height;
211: else if ((v == null) && (y != null) && (south != null))
212: v = Spring.sum(south, Spring.minus(y));
213: return v;
214: }
215:
216:
221: public Spring getWidth()
222: {
223: if (width != null)
224: return width;
225: else if ((h == null) && (x != null) && (east != null))
226: h = Spring.sum(east, Spring.minus(x));
227: return h;
228: }
229:
230:
235: public Spring getX()
236: {
237: if (x != null)
238: return x;
239: else if ((h == null) && (width != null) && (east != null))
240: h = Spring.sum(east, Spring.minus(width));
241: return h;
242: }
243:
244:
249: public Spring getY()
250: {
251: if (y != null)
252: return y;
253: else if ((v == null) && (height != null) && (south != null))
254: v = Spring.sum(south, Spring.minus(height));
255: return v;
256: }
257:
258:
263: public Spring getSouth()
264: {
265: if (south != null)
266: return south;
267: else if ((v == null) && (height != null) && (y != null))
268: v = Spring.sum(y, height);
269: return v;
270: }
271:
272:
277: public Spring getEast()
278: {
279: if (east != null)
280: return east;
281: else if ((h == null) && (width != null) && (x != null))
282: h = Spring.sum(x, width);
283: return h;
284: }
285:
286:
295: public void setConstraint(String edgeName, Spring s)
296: {
297:
298: if (edgeName.equals(SpringLayout.WEST))
299: setX(s);
300: else if (edgeName.equals(SpringLayout.NORTH))
301: setY(s);
302: else if (edgeName.equals(SpringLayout.EAST))
303: setEast(s);
304: else if (edgeName.equals(SpringLayout.SOUTH))
305: setSouth(s);
306:
307: }
308:
309:
314: public void setHeight(Spring s)
315: {
316: height = s;
317: v = null;
318: if ((south != null) && (y != null) && (height != null))
319: south = null;
320: }
321:
322:
327: public void setWidth(Spring s)
328: {
329: width = s;
330: h = null;
331: if ((east != null) && (x != null) && (width != null))
332: east = null;
333: }
334:
335:
340: public void setX(Spring s)
341: {
342: x = s;
343: h = null;
344: if ((width != null) && (east != null) && (x != null))
345: width = null;
346: }
347:
348:
353: public void setY(Spring s)
354: {
355: y = s;
356: v = null;
357: if ((height != null) && (south != null) && (y != null))
358: height = null;
359: }
360:
361:
366: public void setSouth(Spring s)
367: {
368: south = s;
369: v = null;
370: if ((height != null) && (south != null) && (y != null))
371: y = null;
372: }
373:
374:
379: public void setEast(Spring s)
380: {
381: east = s;
382: h = null;
383: if ((width != null) && (east != null) && (x != null))
384: x = null;
385: }
386:
387: public void dropCalcResult()
388: {
389: if (x != null)
390: x.setValue(Spring.UNSET);
391: if (y != null)
392: y.setValue(Spring.UNSET);
393: if (width != null)
394: width.setValue(Spring.UNSET);
395: if (height != null)
396: height.setValue(Spring.UNSET);
397: if (east != null)
398: east.setValue(Spring.UNSET);
399: if (south != null)
400: south.setValue(Spring.UNSET);
401: if (h != null)
402: h.setValue(Spring.UNSET);
403: if (v != null)
404: v.setValue(Spring.UNSET);
405: }
406: }
407:
408:
411: public SpringLayout()
412: {
413: constraintsMap = new HashMap();
414: }
415:
416:
424: public void addLayoutComponent(Component component, Object constraint)
425: {
426: constraintsMap.put(component, constraint);
427: }
428:
429:
438: public void addLayoutComponent(String name, Component c)
439: {
440:
441: }
442:
443:
456: private static class DeferredSpring extends Spring
457: {
458: private SpringLayout sl;
459: private String edgeName;
460: private Component c;
461:
462: public String toString()
463: {
464: return "DeferredSpring of edge" + edgeName + " of " + "something";
465: }
466:
467: public DeferredSpring(SpringLayout s, String edge, Component component)
468: {
469: sl = s;
470: edgeName = edge;
471: c = component;
472: }
473:
474: private Spring resolveSpring()
475: {
476: return sl.getConstraints(c).getConstraint(edgeName);
477: }
478:
479: public int getMaximumValue()
480: {
481: return resolveSpring().getMaximumValue();
482: }
483:
484: public int getMinimumValue()
485: {
486: return resolveSpring().getMinimumValue();
487: }
488:
489: public int getPreferredValue()
490: {
491: return resolveSpring().getPreferredValue();
492: }
493:
494: public int getValue()
495: {
496: int nRet = resolveSpring().getValue();
497: if (nRet == Spring.UNSET)
498: nRet = getPreferredValue();
499: return nRet;
500: }
501:
502: public void setValue(int size)
503: {
504: resolveSpring().setValue(size);
505: }
506: }
507:
508: private abstract static class DeferredDimension extends Spring
509: {
510: private int value;
511:
512: public DeferredDimension()
513: {
514: value = Spring.UNSET;
515: }
516:
517: public void setValue(int val)
518: {
519: value = val;
520: }
521:
522: public int getValue()
523: {
524: if (value == Spring.UNSET)
525: return getPreferredValue();
526: return value;
527: }
528: }
529:
530: private static class DeferredWidth extends DeferredDimension
531: {
532: private Component c;
533:
534:
535: public DeferredWidth(Component component)
536: {
537: c = component;
538: }
539:
540: public String toString()
541: {
542: return "DeferredWidth of " + "something";
543: }
544:
545:
546: public int getMaximumValue()
547: {
548: int widget_width = c.getMaximumSize().width;
549: return Math.min(Short.MAX_VALUE, widget_width);
550: }
551:
552: public int getMinimumValue()
553: {
554: return c.getMinimumSize().width;
555: }
556:
557: public int getPreferredValue()
558: {
559: return c.getPreferredSize().width;
560: }
561: }
562:
563: private static class DeferredHeight extends DeferredDimension
564: {
565: private Component c;
566:
567: public String toString()
568: {
569: return "DeferredHeight of " + "something";
570: }
571:
572: public DeferredHeight(Component component)
573: {
574: c = component;
575: }
576:
577:
578: public int getMaximumValue()
579: {
580: int widget_height = c.getMaximumSize().height;
581: return Math.min(Short.MAX_VALUE, widget_height);
582: }
583:
584: public int getMinimumValue()
585: {
586: return c.getMinimumSize().height;
587: }
588:
589: public int getPreferredValue()
590: {
591: return c.getPreferredSize().height;
592: }
593: }
594:
595:
604: public Spring getConstraint(String edgeName, Component c)
605: {
606: return new DeferredSpring(this, edgeName, c);
607: }
608:
609:
617: public SpringLayout.Constraints getConstraints(Component c)
618: {
619: Constraints constraints = (Constraints) constraintsMap.get(c);
620:
621: if (constraints == null)
622: {
623: constraints = new Constraints();
624:
625: constraints.setWidth(new DeferredWidth(c));
626: constraints.setHeight(new DeferredHeight(c));
627: constraints.setX(Spring.constant(0));
628: constraints.setY(Spring.constant(0));
629:
630: constraintsMap.put(c, constraints);
631: }
632:
633: return constraints;
634: }
635:
636:
644: public float getLayoutAlignmentX(Container p)
645: {
646: return 0.0F;
647: }
648:
649:
656: public float getLayoutAlignmentY(Container p)
657: {
658: return 0.0F;
659: }
660:
661:
664: public void invalidateLayout(Container p)
665: {
666:
667: }
668:
669: private Constraints initContainer(Container p)
670: {
671: Constraints c = getConstraints(p);
672:
673: c.setX(Spring.constant(0));
674: c.setY(Spring.constant(0));
675: c.setWidth(null);
676: c.setHeight(null);
677: if (c.getEast() == null)
678: c.setEast(Spring.constant(0, 0, Integer.MAX_VALUE));
679: if (c.getSouth() == null)
680: c.setSouth(Spring.constant(0, 0, Integer.MAX_VALUE));
681:
682: return c;
683: }
684:
685:
690: public void layoutContainer(Container p)
691: {
692: java.awt.Insets insets = p.getInsets();
693:
694: Component[] components = p.getComponents();
695:
696: Constraints cs = initContainer(p);
697: cs.dropCalcResult();
698:
699: for (int index = 0 ; index < components.length; index++)
700: {
701: Component c = components[index];
702: getConstraints(c).dropCalcResult();
703: }
704:
705: int offsetX = p.getInsets().left;
706: int offsetY = p.getInsets().right;
707:
708: cs.getX().setValue(0);
709: cs.getY().setValue(0);
710: cs.getWidth().setValue(p.getWidth() - offsetX - insets.right);
711: cs.getHeight().setValue(p.getHeight() - offsetY - insets.bottom);
712:
713: for (int index = 0; index < components.length; index++)
714: {
715: Component c = components[index];
716:
717: Constraints constraints = getConstraints(c);
718:
719: int x = constraints.getX().getValue();
720: int y = constraints.getY().getValue();
721: int width = constraints.getWidth().getValue();
722: int height = constraints.getHeight().getValue();
723:
724: c.setBounds(x + offsetX, y + offsetY, width, height);
725: }
726: }
727:
728:
735: public Dimension maximumLayoutSize(Container p)
736: {
737: java.awt.Insets insets = p.getInsets();
738:
739: Constraints cs = initContainer(p);
740:
741: int maxX = cs.getWidth().getMaximumValue() + insets.left + insets.right;
742: int maxY = cs.getHeight().getMaximumValue() + insets.top + insets.bottom;
743:
744: return new Dimension(maxX, maxY);
745: }
746:
747:
748:
755: public Dimension minimumLayoutSize(Container p)
756: {
757: java.awt.Insets insets = p.getInsets();
758:
759: Constraints cs = initContainer(p);
760:
761: int maxX = cs.getWidth().getMinimumValue() + insets.left + insets.right;
762: int maxY = cs.getHeight().getMinimumValue() + insets.top + insets.bottom;
763:
764: return new Dimension(maxX, maxY);
765: }
766:
767:
774: public Dimension preferredLayoutSize(Container p)
775: {
776: java.awt.Insets insets = p.getInsets();
777:
778: Constraints cs = initContainer(p);
779:
780: int maxX = cs.getWidth().getPreferredValue() + insets.left + insets.right;
781: int maxY = cs.getHeight().getPreferredValue() + insets.top + insets.bottom;
782:
783: return new Dimension(maxX, maxY);
784: }
785:
786:
797: public void putConstraint(String e1, Component c1, int pad, String e2,
798: Component c2)
799: {
800: putConstraint(e1, c1, Spring.constant(pad), e2, c2);
801: }
802:
803:
814: public void putConstraint(String e1, Component c1, Spring s, String e2,
815: Component c2)
816: {
817: Constraints constraints1 = getConstraints(c1);
818:
819: Spring otherEdge = getConstraint(e2, c2);
820: constraints1.setConstraint(e1, Spring.sum(s, otherEdge));
821:
822: }
823:
824:
828: public void removeLayoutComponent(Component c)
829: {
830:
831: }
832: }