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 * Creates a new Constraints object.
115 * There is no constraint set.
119 x
= y
= height
= width
= east
= south
= null;
123 * Creates a new Constraints object.
125 * @param x the constraint for the left edge of the component.
126 * @param y the constraint for the upper edge of the component.
128 public Constraints(Spring x
, Spring y
)
132 width
= height
= east
= south
= null;
136 * Creates a new Constraints object.
138 * @param x the constraint for the left edge of the component.
139 * @param y the constraint for the upper edge of the component.
140 * @param width the constraint for the width of the component.
141 * @param height the constraint for the height of the component.
143 public Constraints(Spring x
, Spring y
, Spring width
, Spring height
)
148 this.height
= height
;
153 * Create a new Constraints object which tracks the indicated
154 * component. The x and y positions for this Constraints object
155 * are constant Springs created with the component's location at
156 * the time this constructor is called. The width and height
157 * of this Constraints are Springs created using
158 * {@link Spring#width(Component)} and {@link Spring#height(Component)},
160 * @param component the component to track
163 public Constraints(Component component
)
165 this(Spring
.constant(component
.getX()),
166 Spring
.constant(component
.getY()),
167 Spring
.width(component
),
168 Spring
.height(component
));
172 * Returns the constraint for the edge with the <code>edgeName</code>.
173 * This is expected to be one of
174 * {@link #EAST}, {@link #WEST}, {@link #NORTH} or {@link #SOUTH}.
176 * @param edgeName the name of the edge.
177 * @return the constraint for the specified edge.
179 public Spring
getConstraint(String edgeName
)
181 Spring retVal
= null;
182 if (edgeName
.equals(SpringLayout
.NORTH
))
184 else if (edgeName
.equals(SpringLayout
.WEST
))
186 else if (edgeName
.equals(SpringLayout
.SOUTH
))
189 if ((retVal
== null) && (y
!= null) && (height
!= null))
190 retVal
= Spring
.sum(y
, height
);
192 else if (edgeName
.equals(SpringLayout
.EAST
))
195 if ((retVal
== null) && (x
!= null) && (width
!= null))
196 retVal
= Spring
.sum(x
, width
);
203 * Returns the constraint for the height of the component.
205 * @return the height constraint.
207 public Spring
getHeight()
209 Spring retVal
= height
;
210 if ((retVal
== null) && (y
!= null) && (south
!= null))
212 retVal
= Spring
.sum(south
, Spring
.minus(y
));
218 * Returns the constraint for the width of the component.
220 * @return the width constraint.
222 public Spring
getWidth()
224 Spring retVal
= width
;
225 if ((retVal
== null) && (x
!= null) && (east
!= null))
227 retVal
= Spring
.sum(east
, Spring
.minus(x
));
233 * Returns the constraint for the left edge of the component.
235 * @return the left-edge constraint (== WEST).
240 if ((retVal
== null) && (width
!= null) && (east
!= null))
242 retVal
= Spring
.sum(east
, Spring
.minus(width
));
248 * Returns the constraint for the upper edge of the component.
250 * @return the upper-edge constraint (== NORTH).
255 if ((retVal
== null) && (height
!= null) && (south
!= null))
257 retVal
= Spring
.sum(south
, Spring
.minus(height
));
263 * Sets a constraint for the specified edge. If this leads to an
264 * over-constrained situation, the constraints get adjusted, so that
265 * the mathematics still hold true.
267 * @param edgeName the name of the edge, one of {@link #EAST},
268 * {@link #WEST}, {@link #NORTH} or {@link #SOUTH}.
269 * @param s the constraint to be set.
271 public void setConstraint(String edgeName
, Spring s
)
274 if (edgeName
.equals(SpringLayout
.WEST
))
277 if ((width
!= null) && (east
!= null))
278 width
= Spring
.sum(east
, Spring
.minus(x
));
280 else if (edgeName
.equals(SpringLayout
.NORTH
))
283 if ((height
!= null) && (south
!= null))
284 height
= Spring
.sum(south
, Spring
.minus(y
));
286 else if (edgeName
.equals(SpringLayout
.EAST
))
289 if ((x
!= null) && (width
!= null))
290 x
= Spring
.sum(east
, Spring
.minus(width
));
292 else if (edgeName
.equals(SpringLayout
.SOUTH
))
295 if ((height
!= null) && (y
!= null))
296 y
= Spring
.sum(south
, Spring
.minus(height
));
302 * Sets the height-constraint.
304 * @param s the constraint to be set.
306 public void setHeight(Spring s
)
309 if ((south
!= null) && (y
!= null))
310 south
= Spring
.sum(y
, height
);
315 * Sets the width-constraint.
317 * @param s the constraint to be set.
319 public void setWidth(Spring s
)
322 if ((east
!= null) && (x
!= null))
323 east
= Spring
.sum(x
, width
);
328 * Sets the WEST-constraint.
330 * @param s the constraint to be set.
332 public void setX(Spring s
)
335 if ((width
!= null) && (east
!= null))
336 width
= Spring
.sum(east
, Spring
.minus(x
));
341 * Sets the NORTH-constraint.
343 * @param s the constraint to be set.
345 public void setY(Spring s
)
348 if ((height
!= null) && (south
!= null))
349 height
= Spring
.sum(south
, Spring
.minus(y
));
355 * Creates a new SpringLayout.
357 public SpringLayout()
360 constraintsMap
= new HashMap();
364 * Adds a layout component and a constraint object to this layout.
365 * This method is usually only called by a {@link java.awt.Container}s add
368 * @param component the component to be added.
369 * @param constraint the constraint to be set.
371 public void addLayoutComponent(Component component
, Object constraint
)
373 constraintsMap
.put(component
, constraint
);
378 * Adds a layout component and a constraint object to this layout.
379 * This method is usually only called by a {@link java.awt.Container}s add
380 * method. This method does nothing, since SpringLayout does not manage
381 * String-indexed components.
383 * @param name the name.
384 * @param c the component to be added.
386 public void addLayoutComponent(String name
, Component c
)
392 * Returns the constraint of the edge named by <code>edgeName</code>.
394 * @param c the component from which to get the constraint.
395 * @param edgeName the name of the edge, one of {@link #EAST},
396 * {@link #WEST}, {@link #NORTH} or {@link #SOUTH}.
397 * @return the constraint of the edge <code>edgeName</code> of the
400 public Spring
getConstraint(String edgeName
, Component c
)
402 Constraints constraints
= getConstraints(c
);
403 return constraints
.getConstraint(edgeName
);
407 * Returns the {@link Constraints} object associated with the specified
410 * @param c the component for which to determine the constraint.
411 * @return the {@link Constraints} object associated with the specified
414 public SpringLayout
.Constraints
getConstraints(Component c
)
416 Constraints constraints
= (Constraints
) constraintsMap
.get(c
);
418 if (constraints
== null)
420 Container parent
= c
.getParent();
421 constraints
= new Constraints();
425 constraints
.setX(Spring
.constant(parent
.getInsets().left
));
426 constraints
.setY(Spring
.constant(parent
.getInsets().top
));
430 constraints
.setX(Spring
.constant(0));
431 constraints
.setY(Spring
.constant(0));
434 constraints
.setWidth(Spring
.constant(c
.getMinimumSize().width
,
435 c
.getPreferredSize().width
,
436 c
.getMaximumSize().width
));
437 constraints
.setHeight(Spring
.constant(c
.getMinimumSize().height
,
438 c
.getPreferredSize().height
,
439 c
.getMaximumSize().height
));
440 constraintsMap
.put(c
, constraints
);
446 * Returns the X alignment of the Container <code>p</code>.
449 * the {@link java.awt.Container} for which to determine the X
453 public float getLayoutAlignmentX(Container p
)
459 * Returns the Y alignment of the Container <code>p</code>.
461 * @param p the {@link java.awt.Container} for which to determine the Y
465 public float getLayoutAlignmentY(Container p
)
471 * Recalculate a possibly cached layout.
473 public void invalidateLayout(Container p
)
475 // nothing to do here yet
479 * Lays out the container <code>p</code>.
481 * @param p the container to be laid out.
483 public void layoutContainer(Container p
)
486 addLayoutComponent(p
, new Constraints(Spring
.constant(0),
487 Spring
.constant(0)));
489 int offsetX
= p
.getInsets().left
;
490 int offsetY
= p
.getInsets().right
;
492 Component
[] components
= p
.getComponents();
493 for (int index
= 0; index
< components
.length
; index
++)
495 Component c
= components
[index
];
497 Constraints constraints
= getConstraints(c
);
498 int x
= constraints
.getX().getValue();
499 int y
= constraints
.getY().getValue();
500 int width
= constraints
.getWidth().getValue();
501 int height
= constraints
.getHeight().getValue();
503 c
.setLocation(x
+ offsetX
, y
+ offsetY
);
504 c
.setSize(width
, height
);
510 * Calculates the maximum size of the layed out container. This
511 * respects the maximum sizes of all contained components.
513 * @param p the container to be laid out.
514 * @return the maximum size of the container.
516 public Dimension
maximumLayoutSize(Container p
)
521 int offsetX
= p
.getInsets().left
;
522 int offsetY
= p
.getInsets().right
;
524 Component
[] components
= p
.getComponents();
525 for (int index
= 0; index
< components
.length
; index
++)
527 Component c
= components
[index
];
528 Constraints constraints
= getConstraints(c
);
529 int x
= constraints
.getX().getMaximumValue();
530 int y
= constraints
.getY().getMaximumValue();
531 int width
= constraints
.getWidth().getMaximumValue();
532 int height
= constraints
.getHeight().getMaximumValue();
534 int rightEdge
= offsetX
+ x
+ width
;
535 if (rightEdge
> maxX
)
537 int bottomEdge
= offsetY
+ y
+ height
;
538 if (bottomEdge
> maxY
)
542 return new Dimension(maxX
, maxY
);
547 * Calculates the minimum size of the layed out container. This
548 * respects the minimum sizes of all contained components.
550 * @param p the container to be laid out.
551 * @return the minimum size of the container.
553 public Dimension
minimumLayoutSize(Container p
)
558 int offsetX
= p
.getInsets().left
;
559 int offsetY
= p
.getInsets().right
;
561 Component
[] components
= p
.getComponents();
562 for (int index
= 0; index
< components
.length
; index
++)
564 Component c
= components
[index
];
565 Constraints constraints
= getConstraints(c
);
566 int x
= constraints
.getX().getMinimumValue();
567 int y
= constraints
.getY().getMinimumValue();
568 int width
= constraints
.getWidth().getMinimumValue();
569 int height
= constraints
.getHeight().getMinimumValue();
571 int rightEdge
= offsetX
+ x
+ width
;
572 if (rightEdge
> maxX
)
574 int bottomEdge
= offsetY
+ y
+ height
;
575 if (bottomEdge
> maxY
)
579 return new Dimension(maxX
, maxY
);
583 * Calculates the preferred size of the layed out container. This
584 * respects the preferred sizes of all contained components.
586 * @param p the container to be laid out.
587 * @return the preferred size of the container.
589 public Dimension
preferredLayoutSize(Container p
)
594 int offsetX
= p
.getInsets().left
;
595 int offsetY
= p
.getInsets().right
;
597 Component
[] components
= p
.getComponents();
598 for (int index
= 0; index
< components
.length
; index
++)
600 Component c
= components
[index
];
601 Constraints constraints
= getConstraints(c
);
602 int x
= constraints
.getX().getPreferredValue();
603 int y
= constraints
.getY().getPreferredValue();
604 int width
= constraints
.getWidth().getPreferredValue();
605 int height
= constraints
.getHeight().getPreferredValue();
607 int rightEdge
= offsetX
+ x
+ width
;
608 if (rightEdge
> maxX
)
610 int bottomEdge
= offsetY
+ y
+ height
;
611 if (bottomEdge
> maxY
)
614 return new Dimension(maxX
, maxY
);
618 * Attaches the edge <code>e1</code> of component <code>c1</code> to
619 * the edge <code>e2</code> of component <code>c2</code> width the
620 * fixed strut <code>pad</code>.
622 * @param e1 the edge of component 1.
623 * @param c1 the component 1.
624 * @param pad the space between the components in pixels.
625 * @param e2 the edge of component 2.
626 * @param c2 the component 2.
628 public void putConstraint(String e1
, Component c1
, int pad
, String e2
,
631 Constraints constraints1
= getConstraints(c1
);
632 Constraints constraints2
= getConstraints(c2
);
634 Spring strut
= Spring
.constant(pad
);
635 Spring otherEdge
= constraints2
.getConstraint(e2
);
636 constraints1
.setConstraint(e1
, Spring
.sum(strut
, otherEdge
));
640 * Attaches the edge <code>e1</code> of component <code>c1</code> to
641 * the edge <code>e2</code> of component <code>c2</code> width the
642 * {@link Spring} <code>s</code>.
644 * @param e1 the edge of component 1.
645 * @param c1 the component 1.
646 * @param s the space between the components as a {@link Spring} object.
647 * @param e2 the edge of component 2.
648 * @param c2 the component 2.
650 public void putConstraint(String e1
, Component c1
, Spring s
, String e2
,
653 Constraints constraints1
= getConstraints(c1
);
654 Constraints constraints2
= getConstraints(c2
);
656 Spring otherEdge
= constraints2
.getConstraint(e2
);
657 constraints1
.setConstraint(e1
, Spring
.sum(s
, otherEdge
));
662 * Removes a layout component.
663 * @param c the layout component to remove.
665 public void removeLayoutComponent(Component c
)