1 /* SpringLayout.java --
2 Copyright (C) 2004, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.Component;
42 import java.awt.Container;
43 import java.awt.Dimension;
44 import java.awt.LayoutManager2;
45 import java.util.HashMap;
49 * A very flexible layout manager. Components are laid out by defining the
50 * relationships between them. The relationships are expressed as
51 * {@link Spring}s. You can attach a Spring for each edge of a component and
52 * link it to an edge of a different component. For example, you can say,
53 * the northern edge of component A should be attached to the southern edge
54 * of component B, and the space between them should be something between
55 * x and y pixels, and preferably z pixels.
56 * <p>While quite simple, this layout manager can be used to emulate most other
57 * layout managers, and can also be used to solve some layout problems, which
58 * would be hard to solve with other layout managers.</p>
60 * @author Roman Kennke (roman@ontographics.com)
62 public class SpringLayout implements LayoutManager2
65 /** The right edge of a component. */
66 public static final String EAST = "East";
68 /** The top edge of a component. */
69 public static final String NORTH = "North";
71 /** The bottom edge of a component. */
72 public static final String SOUTH = "South";
74 /** The left edge of a component. */
75 public static final String WEST = "West";
77 /** maps components to their constraints. */
78 private Map constraintsMap;
81 * The constraints that define the relationships between components.
82 * Each Constraints object can hold 4 Springs: one for each edge of the
83 * component. Additionally it can hold Springs for the components width
84 * and the components height. Since the height and width constraints are
85 * dependend on the other constraints, a component can be over-constraint.
86 * In this case (like when all of NORTH, SOUTH and HEIGHT are constraint),
87 * the values are adjusted, so that the mathematics still hold true.
89 * @author Roman Kennke (roman@ontographics.com)
91 public static class Constraints
94 // The constraints for each edge, and width and height.
95 /** The Spring for the left edge. */
98 /** The Spring for the upper edge. */
101 /** The Spring for the height. */
102 private Spring height;
104 /** The Spring for the width. */
105 private Spring width;
107 /** The Spring for the right edge. */
110 /** The Spring for the bottom edge. */
111 private Spring south;
114 In each axis the user can set three values, i.e. x, width, east, if all
115 three are set, then there's no room for manoeuvre so in those cases the
116 third will be described by the below spring which is calculated in terms
123 * Creates a new Constraints object.
124 * There is no constraint set.
128 x = y = height = width = east = south = v = h = null;
132 * Creates a new Constraints object.
134 * @param x the constraint for the left edge of the component.
135 * @param y the constraint for the upper edge of the component.
137 public Constraints(Spring x, Spring y)
141 width = height = east = south = v = h = null;
145 * Creates a new Constraints object.
147 * @param x the constraint for the left edge of the component.
148 * @param y the constraint for the upper edge of the component.
149 * @param width the constraint for the width of the component.
150 * @param height the constraint for the height of the component.
152 public Constraints(Spring x, Spring y, Spring width, Spring height)
157 this.height = height;
158 east = south = v = h = null;
162 * Create a new Constraints object which tracks the indicated
163 * component. The x and y positions for this Constraints object
164 * are constant Springs created with the component's location at
165 * the time this constructor is called. The width and height
166 * of this Constraints are Springs created using
167 * {@link Spring#width(Component)} and {@link Spring#height(Component)},
169 * @param component the component to track
172 public Constraints(Component component)
174 this(Spring.constant(component.getX()),
175 Spring.constant(component.getY()),
176 Spring.width(component),
177 Spring.height(component));
181 * Returns the constraint for the edge with the <code>edgeName</code>.
182 * This is expected to be one of
183 * {@link #EAST}, {@link #WEST}, {@link #NORTH} or {@link #SOUTH}.
185 * @param edgeName the name of the edge.
186 * @return the constraint for the specified edge.
188 public Spring getConstraint(String edgeName)
190 Spring retVal = null;
191 if (edgeName.equals(SpringLayout.NORTH))
193 else if (edgeName.equals(SpringLayout.WEST))
195 else if (edgeName.equals(SpringLayout.SOUTH))
197 else if (edgeName.equals(SpringLayout.EAST))
203 * Returns the constraint for the height of the component.
205 * @return the height constraint.
207 public Spring getHeight()
211 else if ((v == null) && (y != null) && (south != null))
212 v = Spring.sum(south, Spring.minus(y));
217 * Returns the constraint for the width of the component.
219 * @return the width constraint.
221 public Spring getWidth()
225 else if ((h == null) && (x != null) && (east != null))
226 h = Spring.sum(east, Spring.minus(x));
231 * Returns the constraint for the left edge of the component.
233 * @return the left-edge constraint (== WEST).
239 else if ((h == null) && (width != null) && (east != null))
240 h = Spring.sum(east, Spring.minus(width));
245 * Returns the constraint for the upper edge of the component.
247 * @return the upper-edge constraint (== NORTH).
253 else if ((v == null) && (height != null) && (south != null))
254 v = Spring.sum(south, Spring.minus(height));
259 * Returns the constraint for the lower edge of the component.
261 * @return the lower-edge constraint (== SOUTH).
263 public Spring getSouth()
267 else if ((v == null) && (height != null) && (y != null))
268 v = Spring.sum(y, height);
273 * Returns the constraint for the right edge of the component.
275 * @return the right-edge constraint (== EAST).
277 public Spring getEast()
281 else if ((h == null) && (width != null) && (x != null))
282 h = Spring.sum(x, width);
287 * Sets a constraint for the specified edge. If this leads to an
288 * over-constrained situation, the constraints get adjusted, so that
289 * the mathematics still hold true.
291 * @param edgeName the name of the edge, one of {@link #EAST},
292 * {@link #WEST}, {@link #NORTH} or {@link #SOUTH}.
293 * @param s the constraint to be set.
295 public void setConstraint(String edgeName, Spring s)
298 if (edgeName.equals(SpringLayout.WEST))
300 else if (edgeName.equals(SpringLayout.NORTH))
302 else if (edgeName.equals(SpringLayout.EAST))
304 else if (edgeName.equals(SpringLayout.SOUTH))
310 * Sets the height-constraint.
312 * @param s the constraint to be set.
314 public void setHeight(Spring s)
318 if ((south != null) && (y != null) && (height != null))
323 * Sets the width-constraint.
325 * @param s the constraint to be set.
327 public void setWidth(Spring s)
331 if ((east != null) && (x != null) && (width != null))
336 * Sets the WEST-constraint.
338 * @param s the constraint to be set.
340 public void setX(Spring s)
344 if ((width != null) && (east != null) && (x != null))
349 * Sets the NORTH-constraint.
351 * @param s the constraint to be set.
353 public void setY(Spring s)
357 if ((height != null) && (south != null) && (y != null))
362 * Sets the SOUTH-constraint.
364 * @param s the constraint to be set.
366 public void setSouth(Spring s)
370 if ((height != null) && (south != null) && (y != null))
375 * Sets the EAST-constraint.
377 * @param s the constraint to be set.
379 public void setEast(Spring s)
383 if ((width != null) && (east != null) && (x != null))
387 public void dropCalcResult()
390 x.setValue(Spring.UNSET);
392 y.setValue(Spring.UNSET);
394 width.setValue(Spring.UNSET);
396 height.setValue(Spring.UNSET);
398 east.setValue(Spring.UNSET);
400 south.setValue(Spring.UNSET);
402 h.setValue(Spring.UNSET);
404 v.setValue(Spring.UNSET);
409 * Creates a new SpringLayout.
411 public SpringLayout()
413 constraintsMap = new HashMap();
417 * Adds a layout component and a constraint object to this layout.
418 * This method is usually only called by a {@link java.awt.Container}s add
421 * @param component the component to be added.
422 * @param constraint the constraint to be set.
424 public void addLayoutComponent(Component component, Object constraint)
426 constraintsMap.put(component, constraint);
430 * Adds a layout component and a constraint object to this layout.
431 * This method is usually only called by a {@link java.awt.Container}s add
432 * method. This method does nothing, since SpringLayout does not manage
433 * String-indexed components.
435 * @param name the name.
436 * @param c the component to be added.
438 public void addLayoutComponent(String name, Component c)
444 * The trick to SpringLayout is that the network of Springs needs to
445 * completely created before the positioning results are generated.
447 * Using the springs directly during network creation will set their values
448 * before the network is completed, Using Deferred Springs during creation of
449 * the network allows all the edges to be connected together and the network
450 * to be created without resolving the Springs until their results need to be
451 * known, at which point the network is complete and the spring addition and
452 * and substitution calculations will work on a complete and valid network.
454 * @author Caolan McNamara (caolanm@redhat.com)
456 private static class DeferredSpring extends Spring
458 private SpringLayout sl;
459 private String edgeName;
462 public String toString()
464 return "DeferredSpring of edge" + edgeName + " of " + "something";
467 public DeferredSpring(SpringLayout s, String edge, Component component)
474 private Spring resolveSpring()
476 return sl.getConstraints(c).getConstraint(edgeName);
479 public int getMaximumValue()
481 return resolveSpring().getMaximumValue();
484 public int getMinimumValue()
486 return resolveSpring().getMinimumValue();
489 public int getPreferredValue()
491 return resolveSpring().getPreferredValue();
494 public int getValue()
496 int nRet = resolveSpring().getValue();
497 if (nRet == Spring.UNSET)
498 nRet = getPreferredValue();
502 public void setValue(int size)
504 resolveSpring().setValue(size);
508 private static abstract class DeferredDimension extends Spring
512 public DeferredDimension()
514 value = Spring.UNSET;
517 public void setValue(int val)
522 public int getValue()
524 if (value == Spring.UNSET)
525 return getPreferredValue();
530 private static class DeferredWidth extends DeferredDimension
535 public DeferredWidth(Component component)
540 public String toString()
542 return "DeferredWidth of " + "something";
545 //clip max to a value we can do meaningful calculation with
546 public int getMaximumValue()
548 int widget_width = c.getMaximumSize().width;
549 return Math.min(Short.MAX_VALUE, widget_width);
552 public int getMinimumValue()
554 return c.getMinimumSize().width;
557 public int getPreferredValue()
559 return c.getPreferredSize().width;
563 private static class DeferredHeight extends DeferredDimension
567 public String toString()
569 return "DeferredHeight of " + "something";
572 public DeferredHeight(Component component)
577 //clip max to a value we can do meaningful calculations with it
578 public int getMaximumValue()
580 int widget_height = c.getMaximumSize().height;
581 return Math.min(Short.MAX_VALUE, widget_height);
584 public int getMinimumValue()
586 return c.getMinimumSize().height;
589 public int getPreferredValue()
591 return c.getPreferredSize().height;
596 * Returns the constraint of the edge named by <code>edgeName</code>.
598 * @param c the component from which to get the constraint.
599 * @param edgeName the name of the edge, one of {@link #EAST},
600 * {@link #WEST}, {@link #NORTH} or {@link #SOUTH}.
601 * @return the constraint of the edge <code>edgeName</code> of the
604 public Spring getConstraint(String edgeName, Component c)
606 return new DeferredSpring(this, edgeName, c);
610 * Returns the {@link Constraints} object associated with the specified
613 * @param c the component for which to determine the constraint.
614 * @return the {@link Constraints} object associated with the specified
617 public SpringLayout.Constraints getConstraints(Component c)
619 Constraints constraints = (Constraints) constraintsMap.get(c);
621 if (constraints == null)
623 constraints = new Constraints();
625 constraints.setWidth(new DeferredWidth(c));
626 constraints.setHeight(new DeferredHeight(c));
627 constraints.setX(Spring.constant(0));
628 constraints.setY(Spring.constant(0));
630 constraintsMap.put(c, constraints);
637 * Returns the X alignment of the Container <code>p</code>.
640 * the {@link java.awt.Container} for which to determine the X
644 public float getLayoutAlignmentX(Container p)
650 * Returns the Y alignment of the Container <code>p</code>.
652 * @param p the {@link java.awt.Container} for which to determine the Y
656 public float getLayoutAlignmentY(Container p)
662 * Recalculate a possibly cached layout.
664 public void invalidateLayout(Container p)
666 // nothing to do here yet
669 private Constraints initContainer(Container p)
671 Constraints c = getConstraints(p);
673 c.setX(Spring.constant(0));
674 c.setY(Spring.constant(0));
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));
686 * Lays out the container <code>p</code>.
688 * @param p the container to be laid out.
690 public void layoutContainer(Container p)
692 java.awt.Insets insets = p.getInsets();
694 Component[] components = p.getComponents();
696 Constraints cs = initContainer(p);
699 for (int index = 0 ; index < components.length; index++)
701 Component c = components[index];
702 getConstraints(c).dropCalcResult();
705 int offsetX = p.getInsets().left;
706 int offsetY = p.getInsets().right;
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);
713 for (int index = 0; index < components.length; index++)
715 Component c = components[index];
717 Constraints constraints = getConstraints(c);
719 int x = constraints.getX().getValue();
720 int y = constraints.getY().getValue();
721 int width = constraints.getWidth().getValue();
722 int height = constraints.getHeight().getValue();
724 c.setBounds(x + offsetX, y + offsetY, width, height);
729 * Calculates the maximum size of the layed out container. This
730 * respects the maximum sizes of all contained components.
732 * @param p the container to be laid out.
733 * @return the maximum size of the container.
735 public Dimension maximumLayoutSize(Container p)
737 java.awt.Insets insets = p.getInsets();
739 Constraints cs = initContainer(p);
741 int maxX = cs.getWidth().getMaximumValue() + insets.left + insets.right;
742 int maxY = cs.getHeight().getMaximumValue() + insets.top + insets.bottom;
744 return new Dimension(maxX, maxY);
749 * Calculates the minimum size of the layed out container. This
750 * respects the minimum sizes of all contained components.
752 * @param p the container to be laid out.
753 * @return the minimum size of the container.
755 public Dimension minimumLayoutSize(Container p)
757 java.awt.Insets insets = p.getInsets();
759 Constraints cs = initContainer(p);
761 int maxX = cs.getWidth().getMinimumValue() + insets.left + insets.right;
762 int maxY = cs.getHeight().getMinimumValue() + insets.top + insets.bottom;
764 return new Dimension(maxX, maxY);
768 * Calculates the preferred size of the layed out container. This
769 * respects the preferred sizes of all contained components.
771 * @param p the container to be laid out.
772 * @return the preferred size of the container.
774 public Dimension preferredLayoutSize(Container p)
776 java.awt.Insets insets = p.getInsets();
778 Constraints cs = initContainer(p);
780 int maxX = cs.getWidth().getPreferredValue() + insets.left + insets.right;
781 int maxY = cs.getHeight().getPreferredValue() + insets.top + insets.bottom;
783 return new Dimension(maxX, maxY);
787 * Attaches the edge <code>e1</code> of component <code>c1</code> to
788 * the edge <code>e2</code> of component <code>c2</code> width the
789 * fixed strut <code>pad</code>.
791 * @param e1 the edge of component 1.
792 * @param c1 the component 1.
793 * @param pad the space between the components in pixels.
794 * @param e2 the edge of component 2.
795 * @param c2 the component 2.
797 public void putConstraint(String e1, Component c1, int pad, String e2,
800 putConstraint(e1, c1, Spring.constant(pad), e2, c2);
804 * Attaches the edge <code>e1</code> of component <code>c1</code> to
805 * the edge <code>e2</code> of component <code>c2</code> width the
806 * {@link Spring} <code>s</code>.
808 * @param e1 the edge of component 1.
809 * @param c1 the component 1.
810 * @param s the space between the components as a {@link Spring} object.
811 * @param e2 the edge of component 2.
812 * @param c2 the component 2.
814 public void putConstraint(String e1, Component c1, Spring s, String e2,
817 Constraints constraints1 = getConstraints(c1);
819 Spring otherEdge = getConstraint(e2, c2);
820 constraints1.setConstraint(e1, Spring.sum(s, otherEdge));
825 * Removes a layout component.
826 * @param c the layout component to remove.
828 public void removeLayoutComponent(Component c)