com.brunchboy.util.swing.relativelayout
Class DependencyManager

java.lang.Object
  |
  +--com.brunchboy.util.swing.relativelayout.DependencyManager

public class DependencyManager
extends Object

Keeps track of all dependencies that have been declared or derived in setting up the component constraints in the RelativeLayout, and can sort them in the proper order for resolution when it's time to lay the components out.

Version:
$Id: DependencyManager.java,v 1.2 2002/08/19 01:38:43 jim Exp $
Author:
James Elliott, jim@brunchboy.com

Field Summary
static String ROOT_NAME
          The name of the special Component corresponding to the container in which the RelativeLayout is managing components.
static String VERSION
          Provides access to the CVS version of this class.
 
Constructor Summary
DependencyManager()
           
 
Method Summary
 void add(Attribute dependent, Attribute anchor)
          Records a dependency between attributes.
 List sort()
          Returns the list of dependent attributes, sorted in the order in which they must be resolved.
 void validate()
          Validates the set of dependencies by verifying that there are no cycles and that every dependency can be resolved.
 
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

ROOT_NAME

public static final String ROOT_NAME
The name of the special Component corresponding to the container in which the RelativeLayout is managing components. Dependencies associated with this component are allowed to be roots in the dependency tree because its dimensions are known.

See Also:
Constant Field Values
Constructor Detail

DependencyManager

public DependencyManager()
Method Detail

add

public void add(Attribute dependent,
                Attribute anchor)
         throws IllegalStateException
Records a dependency between attributes.

Parameters:
dependent - the attribute which depends on another.
anchor - the attribute whose value controls the dependent.
Throws:
IllegalStateException - if there is already a dependency between these attributes.

validate

public void validate()
Validates the set of dependencies by verifying that there are no cycles and that every dependency can be resolved. Normal return indicates the dependency tree is valid. As a side effect, sets up the reference counts in the nodes, and builds the list of root nodes.

Throws:
IllegalStateException - if a cycle or unresolvable dependency is detected.

sort

public List sort()
Returns the list of dependent attributes, sorted in the order in which they must be resolved.

If the sort has already been performed, its cached result is returned. Otherwise, ensures that the graph is in a valid state (which, as a side effect, sets up the reference counts needed by the sort). Then recursively scans through resolved nodes, decrementing child reference counts. When a child's reference count reaches zero, that child is resolved; it is added to the List and its descendants are searched.

Returns:
the list of Attributes that need to be resolved, in the order that they should be resolved.
Throws:
IllegalStateException - if there is a cycle or otherwise invalid dependency tree.