Package ca.cgjennings.ui
Class TreeBuilder<C,L>
- java.lang.Object
-
- ca.cgjennings.ui.TreeBuilder<C,L>
-
- Type Parameters:
C- the type used to identify containersL- the type used to identify leaves
public abstract class TreeBuilder<C,L> extends java.lang.ObjectATreeBuilderaids in the dynamic construction of aTreeModel. It operates under the assumption that the tree is constructed from two kinds of nodes: containers and leaves. For example, if building a tree from a directory structure, directories would be containers and other files would be leaves.- Since:
- 3.0
- Author:
- Chris Jennings
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTreeBuilder.Container<T>The class used to represent container nodes in the tree.static classTreeBuilder.Leaf<T>The class used to represent leaf nodes in the tree.
-
Constructor Summary
Constructors Constructor Description TreeBuilder(C root)Creates a tree builder that will build on a root node for the specified container.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidadd(C parent, L leaf)Adds a new node to the tree as a child of the specified parent container.protected TreeBuilder.Container<C>getContainerNode(C container)Returns the node that represents the specified container.protected abstract CgetParentContainer(C container)Returns the container that is parent container for the specified container.TreeBuilder.Container<C>getRootNode()Returns the node for the root of the tree.
-
-
-
Constructor Detail
-
TreeBuilder
public TreeBuilder(C root)
Creates a tree builder that will build on a root node for the specified container.- Parameters:
root- the container that is the root of the tree
-
-
Method Detail
-
add
public void add(C parent, L leaf)
Adds a new node to the tree as a child of the specified parent container.- Parameters:
parent- the container to place the node inleaf- the leaf to add; ifnullensures that a node for the parent exists
-
getRootNode
public TreeBuilder.Container<C> getRootNode()
Returns the node for the root of the tree. Once all of the desired nodes have been added, call this method to get a root suitable for use with aDefaultTreeModel.- Returns:
- the tree root
-
getContainerNode
protected TreeBuilder.Container<C> getContainerNode(C container)
Returns the node that represents the specified container. If no such node has been previously requested, a node is created and stored to satisfy future requests.- Parameters:
container- the container to create a node for- Returns:
- the container node
-
getParentContainer
protected abstract C getParentContainer(C container)
Returns the container that is parent container for the specified container. This is called when a new container node must be generated, in order to determine which parent node to add the new container to.Note: To function correctly, the implementation must ensure that for any valid input container, recursively calling this method will eventually return the root container. For example, if the container type is
Fileand this method returnscontainer.getParent(), then any directory added to the tree would have to be a direct or indirect child of the file used to create the root.- Parameters:
container- the container to determine a parent for- Returns:
- the container that represents the parent of the specified container
-
-