matthew.awt
Class StrutLayout

java.lang.Object
  |
  +--matthew.awt.StrutLayout

public class StrutLayout
extends java.lang.Object
implements java.awt.LayoutManager2

StrutLayout is an AWT layout manager that lays out components by logically connecting them with struts. Each StrutLayout has a root component which any number of child components may connect to. Each child component may have also have child components, and so on.

Each component in a StrutLayout may also have internal springs associated with it. These springs can be used to make the component expand horizontally and/or vertically to take up any extra space. The expansion takes into account any child components, ensuring they are not pushed off the edge of the layout area.

Additionally, each component may be a member of a size group which allows sets of components to maintain the same horizontal and/or vertical dimensions as the largest component in the group.

NOTE: although AWT layout managers are in theory able to layout more than container simultaneously, StrutLayout will get very confused if you try to use it like this. For now, you need to use one instance of StrutLayout per container.

See Also:
StrutLayout.StrutConstraint, StrutLayout.VectorConstraint, setSprings(java.awt.Component, int), createSizeGroup()

Inner Class Summary
protected static class StrutLayout.ComponentInfo
          Stores constraint information for a component.
 class StrutLayout.SizeGroup
          Represents a group of components who have their width and/or height tied to the size of the group.
protected static class StrutLayout.SizeGroupInfo
          Stores size group information.
protected static class StrutLayout.SpringInfo
          Stores spring constraint information.
static class StrutLayout.Strut
          Represents a strut going from a parent component to a child (see StrutLayout.StrutConstraint for a description of struts).
protected static class StrutLayout.StrutConnection
          Represents a strut from a parent to a child.
static class StrutLayout.StrutConstraint
          Represents a strut constraint placed on the location of a component.
static class StrutLayout.VectorConstraint
          Represents a strut with arbritrary direction and length.
 
Field Summary
protected  int alignment
           
static int BOTTOM_LEFT
          Represents the bottom left corner of a rectangle.
static int BOTTOM_RIGHT
          Represents the bottom right corner of a rectangle.
static int CENTER
          Represents the center of a rectangle.
protected  java.util.Hashtable componentInfoHash
           
protected static int defaultStrutLength
           
static int EAST
          Represents the direction towards the right of the screen.
protected  boolean invalid
           
protected static java.awt.Dimension maximumLayoutSize
           
static int MID_BOTTOM
          Represents the middle of the bottom side of a rectangle.
static int MID_LEFT
          Represents the middle of the left side of a rectangle.
static int MID_RIGHT
          Represents the middle of the right side of a rectangle.
static int MID_TOP
          Represents the middle of the top side of a rectangle.
static int NORTH
          Represents the direction towards the top of the screen.
static int NORTH_EAST
          Represents the direction towards the top right of the screen.
static int NORTH_WEST
          Represents the direction towards top left of the screen.
protected  java.awt.Dimension preferredLayoutSize
           
protected  StrutLayout.ComponentInfo rootComponentInfo
           
static int SIZE_BOTH
          Specifies a member of a size group whose width and height are tied to the group's size.
static int SIZE_HEIGHT
          Specifies a member of a size group whose height is tied to the group's height.
static int SIZE_NONE
          Specifies a member of a size group who participates in setting the group's size but which does not change size itself.
static int SIZE_WIDTH
          Specifies a member of a size group whose width is tied to the group's width.
static int SOUTH
          Represents the direction towards the bottom of the screen.
static int SOUTH_EAST
          Represents the direction towards the bottom right of the screen.
static int SOUTH_WEST
          Represents the direction towards the bottom left of the screen.
static int SPRING_BOTH
          Specifies a spring that expands both horizontally and vertically.
static int SPRING_HORIZ
          Specifies a spring that expands horizontally.
static int SPRING_NONE
          Specifies a non-existent spring.
static int SPRING_VERT
          Specifies a spring that expands vertically.
static int TOP_LEFT
          Represents the top left corner of a rectangle.
static int TOP_RIGHT
          Represents the top right corner of a rectangle.
static int WEST
          Represents the direction towards the left of the screen.
 
Constructor Summary
StrutLayout()
           
 
Method Summary
 void addLayoutComponent(java.awt.Component component, java.lang.Object constraintObject)
          Adds a component to the layout, possibly using a strut as the constraint.
 void addLayoutComponent(java.lang.String name, java.awt.Component component)
          Does nothing.
static void addStruts(java.awt.Container container, StrutLayout.Strut[] struts)
          Adds a number of struts to the layout at once.
protected  void applySizeGroupings()
          Apply any size group constraints.
protected  void assignPreferredSizes()
          Sizes components to their preferred size.
protected  java.awt.Rectangle assignRelativePositions()
          Assigns component positions starting from root, adjusting for the case where child components extend above or to the left of the root.
protected  java.awt.Rectangle assignRelativePositions(StrutLayout.ComponentInfo root)
          Recursively assign components relative positions based on their strut constraints starting from root.
protected  java.awt.Point calculateAlignmentOffset(java.awt.Dimension layoutArea, java.awt.Rectangle boundingBox)
          Returns an offset that will align boundingBox within the given layoutArea according to the current alignment.
 StrutLayout.SizeGroup createSizeGroup()
          Creates a new group of components whose sizes are tied together.
protected  void doAddRootComponent(java.awt.Component rootComponent)
          Does the work of adding a root component to the layout.
protected  void doAddStrutComponent(java.awt.Component parent, java.awt.Component child, int fromConnector, int toConnector, int hdelta, int vdelta)
          Does the work of adding a component with a strut constraint to the layout.
protected  boolean evaluateSprings(java.awt.Dimension layoutArea)
          Evaluate spring settings, expanding components where possible.
 int getAlignment()
          Returns the current alignment the layout area within the container (see setAlignment() for details).
static int getDefaultStrutLength()
          Returns the current default length for struts.
protected  java.awt.Point getDeltaForDirection(int direction)
          Computes a delta vector that codes for a direction.
 float getLayoutAlignmentX(java.awt.Container parent)
           
 float getLayoutAlignmentY(java.awt.Container parent)
           
protected static java.awt.Point getOffsetForConnector(java.awt.Rectangle shape, int connector)
          Returns a vector (as a Point) that can be added to a component's location to get the location of the specified connector.
 StrutLayout.SizeGroup getSizeGroup(java.awt.Component component)
          Returns the size group that a component is a member of.
 void invalidateLayout(java.awt.Container parent)
           
 void layoutContainer(java.awt.Container parent)
           
 java.awt.Dimension maximumLayoutSize(java.awt.Container parent)
           
 java.awt.Dimension minimumLayoutSize(java.awt.Container parent)
           
 java.awt.Dimension preferredLayoutSize(java.awt.Container parent)
           
protected  void recalculateLayout(java.awt.Container container)
          Completely recalculates the layout information.
protected  void removeComponentInfo(StrutLayout.ComponentInfo componentInfo)
          Remove the ComponentInfo for a component and all its children.
 void removeLayoutComponent(java.awt.Component component)
          Removes a component from a layout.
protected  void resetInfo()
          Resets the SizeGroup maxHeight and maxWidth variables and the SpringInfo horizExtent and vertExtent variables to their initial value of 0.
 void setAlignment(int alignment)
          Sets the alignment of the layout area within the container (the default is CENTER).
static void setDefaultStrutLength(int length)
          Sets the current default length for struts.
 void setPreferredSize(java.awt.Component component, java.awt.Dimension preferredSize)
          Fixes the preferred size for a component regardless of what its getPreferredSize () method returns.
 void setSprings(java.awt.Component component, int springs)
          Sets internal springs on a component which act to expand the component horizontally and/or vertically to fill empty space.
protected  void translateComponents(int xdelta, int ydelta)
          Translate all component locations by xdelta, ydelta.
protected  void updateSpringInfo(StrutLayout.ComponentInfo componentInfo, int fromConnector, java.awt.Rectangle childBounds)
          Updates the horizontal and vertical extent settings for a component.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TOP_LEFT

public static final int TOP_LEFT
Represents the top left corner of a rectangle.

MID_TOP

public static final int MID_TOP
Represents the middle of the top side of a rectangle.

TOP_RIGHT

public static final int TOP_RIGHT
Represents the top right corner of a rectangle.

MID_RIGHT

public static final int MID_RIGHT
Represents the middle of the right side of a rectangle.

BOTTOM_RIGHT

public static final int BOTTOM_RIGHT
Represents the bottom right corner of a rectangle.

MID_BOTTOM

public static final int MID_BOTTOM
Represents the middle of the bottom side of a rectangle.

BOTTOM_LEFT

public static final int BOTTOM_LEFT
Represents the bottom left corner of a rectangle.

MID_LEFT

public static final int MID_LEFT
Represents the middle of the left side of a rectangle.

CENTER

public static final int CENTER
Represents the center of a rectangle.

NORTH

public static final int NORTH
Represents the direction towards the top of the screen.

SOUTH

public static final int SOUTH
Represents the direction towards the bottom of the screen.

EAST

public static final int EAST
Represents the direction towards the right of the screen.

WEST

public static final int WEST
Represents the direction towards the left of the screen.

NORTH_EAST

public static final int NORTH_EAST
Represents the direction towards the top right of the screen.

SOUTH_EAST

public static final int SOUTH_EAST
Represents the direction towards the bottom right of the screen.

SOUTH_WEST

public static final int SOUTH_WEST
Represents the direction towards the bottom left of the screen.

NORTH_WEST

public static final int NORTH_WEST
Represents the direction towards top left of the screen.

SPRING_NONE

public static final int SPRING_NONE
Specifies a non-existent spring.

SPRING_HORIZ

public static final int SPRING_HORIZ
Specifies a spring that expands horizontally.

SPRING_VERT

public static final int SPRING_VERT
Specifies a spring that expands vertically.

SPRING_BOTH

public static final int SPRING_BOTH
Specifies a spring that expands both horizontally and vertically.

SIZE_WIDTH

public static final int SIZE_WIDTH
Specifies a member of a size group whose width is tied to the group's width.

SIZE_HEIGHT

public static final int SIZE_HEIGHT
Specifies a member of a size group whose height is tied to the group's height.

SIZE_BOTH

public static final int SIZE_BOTH
Specifies a member of a size group whose width and height are tied to the group's size.

SIZE_NONE

public static final int SIZE_NONE
Specifies a member of a size group who participates in setting the group's size but which does not change size itself.

rootComponentInfo

protected StrutLayout.ComponentInfo rootComponentInfo

componentInfoHash

protected java.util.Hashtable componentInfoHash

invalid

protected boolean invalid

preferredLayoutSize

protected java.awt.Dimension preferredLayoutSize

alignment

protected int alignment

defaultStrutLength

protected static int defaultStrutLength

maximumLayoutSize

protected static final java.awt.Dimension maximumLayoutSize
Constructor Detail

StrutLayout

public StrutLayout()
Method Detail

setAlignment

public void setAlignment(int alignment)
Sets the alignment of the layout area within the container (the default is CENTER). This only has effect when the components managed by the layout do not use all the space in the container.
Parameters:
alignment - The alignment of the layout area within the container. One of TOP_LEFT, MID_TOP, TOP_RIGHT, MID_RIGHT, BOTTOM_RIGHT, MID_BOTTOM, BOTTOM_LEFT, MID_LEFT or CENTER (the default).
See Also:
getAlignment()

getAlignment

public int getAlignment()
Returns the current alignment the layout area within the container (see setAlignment() for details).
Returns:
The current alignment of components within the layout area.
See Also:
setAlignment(int)

setSprings

public void setSprings(java.awt.Component component,
                       int springs)
Sets internal springs on a component which act to expand the component horizontally and/or vertically to fill empty space. Child components are pushed as far down/right as they can go without pushing them off the container.

NOTE: while child components are protected from being covered by their sprung parent components, this does not prevent others not connected to the parent from being potentially obscured. It is up to you to ensure the layout rules do not allow this.

Example:

strutLayout.setSprings (tablePane, StrutLayout.SPRING_BOTH);
Parameters:
component - The component to add the spring to. The component must already be a part of the layout
springs - The spring(s) to add to the component. One of SPRING_NONE, SPRING_HORIZ, SPRING_VERT or SPRING_BOTH.
See Also:
SPRING_NONE, SPRING_VERT, SPRING_HORIZ, SPRING_BOTH

createSizeGroup

public StrutLayout.SizeGroup createSizeGroup()
Creates a new group of components whose sizes are tied together. See StrutLayout.SizeGroup for a full description.
Returns:
A new size group object.
See Also:
StrutLayout.SizeGroup, getSizeGroup(java.awt.Component)

getSizeGroup

public StrutLayout.SizeGroup getSizeGroup(java.awt.Component component)
Returns the size group that a component is a member of.
Parameters:
component - The component to be queried.
Returns:
The size group the component is a member of or null if the component is not a member of a group.
See Also:
StrutLayout.SizeGroup, createSizeGroup()

getDefaultStrutLength

public static int getDefaultStrutLength()
Returns the current default length for struts. This setting is global to all instances of StrutLayout.
Returns:
Returns the current default length.
See Also:
setDefaultStrutLength(int), StrutLayout.StrutConstraint, StrutLayout.Strut

setDefaultStrutLength

public static void setDefaultStrutLength(int length)
Sets the current default length for struts. This setting is global to all instances of StrutLayout.
See Also:
getDefaultStrutLength(), StrutLayout.StrutConstraint, StrutLayout.Strut

setPreferredSize

public void setPreferredSize(java.awt.Component component,
                             java.awt.Dimension preferredSize)
Fixes the preferred size for a component regardless of what its getPreferredSize () method returns. A component's preferred size can also be fixed in this way by sizing it to a non-zero area (via setSize ()) before adding it to the layout.
Parameters:
component - The component whose preferred size is to be fixed.
preferredSize - The new preferred size of the component. If this is null, then the component's preferred size reverts to the result of its getPreferredSize () method.
See Also:
addLayoutComponent(java.awt.Component, java.lang.Object)

addStruts

public static void addStruts(java.awt.Container container,
                             StrutLayout.Strut[] struts)
Adds a number of struts to the layout at once. This is a convenient shortcut which is equivalent to a series of Component.add (...) calls.
Parameters:
struts - The struts to be added.
See Also:
StrutLayout.Strut, addLayoutComponent(java.awt.Component, java.lang.Object)

addLayoutComponent

public void addLayoutComponent(java.lang.String name,
                               java.awt.Component component)
Does nothing. Use addLayoutComponent (Component, Object) instead.
See Also:
addLayoutComponent(java.awt.Component, java.lang.Object)

removeLayoutComponent

public void removeLayoutComponent(java.awt.Component component)
Removes a component from a layout. This also removes all child components connected by struts.
Parameters:
component - The component to remove.
See Also:
addLayoutComponent(java.awt.Component, java.lang.Object)

addLayoutComponent

public void addLayoutComponent(java.awt.Component component,
                               java.lang.Object constraintObject)
Adds a component to the layout, possibly using a strut as the constraint. Using null as the constraint indicates the component is the root component (of which there may only be one).

NOTE: If the component has a non-zero size when added, this size will be used as its preferred size rather than the result of its getPreferredSize () method.

Specified by:
addLayoutComponent in interface java.awt.LayoutManager2
Parameters:
component - The component to add.
constraintObject - The constraint to use when laying out the component. This may be an instance of either StrutLayout.StrutConstraint or StrutLayout.VectorConstraint or null for the root component.
See Also:
StrutLayout.StrutConstraint, StrutLayout.VectorConstraint, setPreferredSize(java.awt.Component, java.awt.Dimension)

getLayoutAlignmentX

public float getLayoutAlignmentX(java.awt.Container parent)
Specified by:
getLayoutAlignmentX in interface java.awt.LayoutManager2

getLayoutAlignmentY

public float getLayoutAlignmentY(java.awt.Container parent)
Specified by:
getLayoutAlignmentY in interface java.awt.LayoutManager2

maximumLayoutSize

public java.awt.Dimension maximumLayoutSize(java.awt.Container parent)
Specified by:
maximumLayoutSize in interface java.awt.LayoutManager2

minimumLayoutSize

public java.awt.Dimension minimumLayoutSize(java.awt.Container parent)

preferredLayoutSize

public java.awt.Dimension preferredLayoutSize(java.awt.Container parent)

layoutContainer

public void layoutContainer(java.awt.Container parent)

invalidateLayout

public void invalidateLayout(java.awt.Container parent)
Specified by:
invalidateLayout in interface java.awt.LayoutManager2

recalculateLayout

protected void recalculateLayout(java.awt.Container container)
Completely recalculates the layout information.

resetInfo

protected void resetInfo()
Resets the SizeGroup maxHeight and maxWidth variables and the SpringInfo horizExtent and vertExtent variables to their initial value of 0.

assignPreferredSizes

protected void assignPreferredSizes()
Sizes components to their preferred size. Also collects max height and width information for size groups.

assignRelativePositions

protected java.awt.Rectangle assignRelativePositions()
Assigns component positions starting from root, adjusting for the case where child components extend above or to the left of the root.
Returns:
The bounding box for all components.
See Also:
assignRelativePositions(StrutLayout.ComponentInfo)

assignRelativePositions

protected java.awt.Rectangle assignRelativePositions(StrutLayout.ComponentInfo root)
Recursively assign components relative positions based on their strut constraints starting from root.
Parameters:
root - The component to start the layout at.
Returns:
The bounding box containing the root and all its child components.

updateSpringInfo

protected void updateSpringInfo(StrutLayout.ComponentInfo componentInfo,
                                int fromConnector,
                                java.awt.Rectangle childBounds)
Updates the horizontal and vertical extent settings for a component. childBounds is the bounding box of a child tree connected to the fromConnector connection point of a parent component. This routine checks whether the child is affected by horizontal and/or vertical expansion of the parent and, if so, updates the vertical and horizontal child extent settings for the component's springInfo.

evaluateSprings

protected boolean evaluateSprings(java.awt.Dimension layoutArea)
Evaluate spring settings, expanding components where possible.
Parameters:
layoutArea - The max area available for layout.

applySizeGroupings

protected void applySizeGroupings()
Apply any size group constraints.

translateComponents

protected void translateComponents(int xdelta,
                                   int ydelta)
Translate all component locations by xdelta, ydelta.

calculateAlignmentOffset

protected java.awt.Point calculateAlignmentOffset(java.awt.Dimension layoutArea,
                                                  java.awt.Rectangle boundingBox)
Returns an offset that will align boundingBox within the given layoutArea according to the current alignment.

getOffsetForConnector

protected static java.awt.Point getOffsetForConnector(java.awt.Rectangle shape,
                                                      int connector)
Returns a vector (as a Point) that can be added to a component's location to get the location of the specified connector.
Parameters:
shape - The shape to find the connector offset for. Only the shape's width and height are used.
connector - The connector. See the TOP_LEFT, etc constants.
Returns:
The offset vector.

getDeltaForDirection

protected java.awt.Point getDeltaForDirection(int direction)
Computes a delta vector that codes for a direction. For example, getDeltaForDirection (SOUTH_WEST) returns (-1, 1).

doAddStrutComponent

protected void doAddStrutComponent(java.awt.Component parent,
                                   java.awt.Component child,
                                   int fromConnector,
                                   int toConnector,
                                   int hdelta,
                                   int vdelta)
Does the work of adding a component with a strut constraint to the layout.

doAddRootComponent

protected void doAddRootComponent(java.awt.Component rootComponent)
Does the work of adding a root component to the layout.

removeComponentInfo

protected void removeComponentInfo(StrutLayout.ComponentInfo componentInfo)
Remove the ComponentInfo for a component and all its children.
Parameters:
componentInfo - The componentInfo to remove.