com.brunchboy.util.swing.relativelayout
Class RelativeLayout

java.lang.Object
  |
  +--com.brunchboy.util.swing.relativelayout.RelativeLayout
All Implemented Interfaces:
LayoutManager, LayoutManager2

public class RelativeLayout
extends Object
implements LayoutManager2

A layout manager that uses a dependency analyzer to support an arbitrary (but consistent and complete) set of relative constraints between its components.

Version:
$Id: RelativeLayout.java,v 1.3 2002/08/15 00:34:38 jim Exp $
Author:
James Elliott, jim@brunchboy.com

Field Summary
static String VERSION
          Provides access to the CVS version of this class.
 
Constructor Summary
RelativeLayout()
           
 
Method Summary
 void addConstraint(String componentName, AttributeType type, Constraint constraint)
          Programmatically add a constraint for a component attribute.
 void addLayoutComponent(Component component, Object constraints)
          Add a component to be laid out, with specified constraints.
 void addLayoutComponent(String name, Component component)
          Add a component to be laid out.
 float getLayoutAlignmentX(Container target)
          Returns the alignment along the x axis.
 float getLayoutAlignmentY(Container target)
          Returns the alignment along the y axis.
 void invalidateLayout(Container target)
          Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
 void layoutContainer(Container parent)
          Lays out the specified container, according to the constraints that have been established.
 Dimension maximumLayoutSize(Container target)
          Calculates the maximum size dimensions for the specified container, given the components it contains.
 Dimension minimumLayoutSize(Container parent)
          Calculates the minimum size dimensions for the specified container, given the components it contains.
 Dimension preferredLayoutSize(Container parent)
          Calculates the preferred size dimensions for the specified container, given the components it contains.
 void removeLayoutComponent(Component component)
          Removes the specified component from the layout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

public static final String VERSION
Provides access to the CVS version of this class.

See Also:
Constant Field Values
Constructor Detail

RelativeLayout

public RelativeLayout()
Method Detail

addLayoutComponent

public void addLayoutComponent(String name,
                               Component component)
Add a component to be laid out. The constraints will be set up separately through addConstraint.

Specified by:
addLayoutComponent in interface LayoutManager
Parameters:
name - the logical name by which the component will be known; this will be used in all constraints that relate to the component. If the component's actual name isn't already equal this value, it will be set by this method.
component - the graphical component whose size and position is to be managed according to our constraint hierarchy.
Throws:
IllegalStateException - if multiple components are added with the same logical name, or if the same component is added more than once.
IllegalArgumentException - if you try to use the name reserved for the container, DependencyManager.ROOT_NAME.
NullPointerException - if name is null.

addLayoutComponent

public void addLayoutComponent(Component component,
                               Object constraints)
Add a component to be laid out, with specified constraints.

Specified by:
addLayoutComponent in interface LayoutManager2
Parameters:
component - the graphical component whose size and position is to be managed according to our constraint hierarchy.
constraints - currently must be a String specifying the logical name of the component (making this method is identical to the version in which the parameters are reversed). A straightforward enhancement would be to also support passing a new class (perhaps named ConstraintList) that spelled out the component's logical name and the constraints associated with it. It turns out that the XML-based configuration is even more convenient, so this hasn't been implemented.
Throws:
IllegalStateException - if multiple components are added with the same logical name, or if the same component is added more than once.
IllegalArgumentException - if the constraints parameter is not a String.
NullPointerException - if name is null.

addConstraint

public void addConstraint(String componentName,
                          AttributeType type,
                          Constraint constraint)
Programmatically add a constraint for a component attribute.

Parameters:
componentName - the logical name of the component for which a constraint is to be recorded.
type - the attribute for which a constraint is to be recorded.
constraint - the information from which the attribute value should be computed (for example an AttributeConstraint or AxisConstraint).
Throws:
IllegalStateException - if this would overconstrain the component.
IllegalArgumentException - if you try to use the name reserved for the container, DependencyManager.ROOT_NAME.
NullPointerException - if any argument is null.

layoutContainer

public void layoutContainer(Container parent)
Lays out the specified container, according to the constraints that have been established.

Specified by:
layoutContainer in interface LayoutManager
Parameters:
parent - the container to be laid out.
Throws:
IllegalStateException - if there is a problem with the dependency tree.

removeLayoutComponent

public void removeLayoutComponent(Component component)
Removes the specified component from the layout. This should be done only with great care, for if there are any other components with constraints that depend on this one, the layout will crash.

Specified by:
removeLayoutComponent in interface LayoutManager
Parameters:
component - the component to be removed.

preferredLayoutSize

public Dimension preferredLayoutSize(Container parent)
Calculates the preferred size dimensions for the specified container, given the components it contains. Only components whose natural sizes are considered in the layout will contribute (components fully constrained to the container size can have no say). Even without such components, this is only an approximation because it can't take into account a layout like one where components are relative to a centered component, and thus get shoved outside the container. To solve that would require something like performing a binary search through the size space (with care to avoid infinite loops when components are explicitly constrained to extend beyond the container's bounds).

Specified by:
preferredLayoutSize in interface LayoutManager
Parameters:
parent - the container to be laid out.
Returns:
an approximation of a size which allows all components to be drawn at their preferred widths.
Throws:
IllegalStateException - if there are any unresolved component attributes.
See Also:
minimumLayoutSize(java.awt.Container)

minimumLayoutSize

public Dimension minimumLayoutSize(Container parent)
Calculates the minimum size dimensions for the specified container, given the components it contains. Only components whose natural sizes are considered in the layout will contribute (components fully constrained to the container size can have no say). Even without such components, this is only an approximation because it can't take into account a layout like one where components are relative to a centered component, and thus get shoved outside the container. To solve that would require something like performing a binary search through the size space (with care to avoid infinite loops when components are explicitly constrained to extend beyond the container's bounds).

Specified by:
minimumLayoutSize in interface LayoutManager
Parameters:
parent - the component to be laid out.
Returns:
an approximation of a size which allows all components to be drawn at their minimum widths.
Throws:
IllegalStateException - if there are any unresolved component attributes.
See Also:
preferredLayoutSize(java.awt.Container)

maximumLayoutSize

public Dimension maximumLayoutSize(Container target)
Calculates the maximum size dimensions for the specified container, given the components it contains. This implementation simply always returns the maximum possible size, since there isn't any other meaningful choice.

Specified by:
maximumLayoutSize in interface LayoutManager2
See Also:
Component.getMaximumSize(), LayoutManager

getLayoutAlignmentY

public float getLayoutAlignmentY(Container target)
Returns the alignment along the y axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.

Specified by:
getLayoutAlignmentY in interface LayoutManager2

getLayoutAlignmentX

public float getLayoutAlignmentX(Container target)
Returns the alignment along the x axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.

Specified by:
getLayoutAlignmentX in interface LayoutManager2

invalidateLayout

public void invalidateLayout(Container target)
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded. Since no per-container information is cached, this does nothing.

Specified by:
invalidateLayout in interface LayoutManager2