Class TreeBuilder<C,​L>

  • Type Parameters:
    C - the type used to identify containers
    L - the type used to identify leaves

    public abstract class TreeBuilder<C,​L>
    extends java.lang.Object
    A TreeBuilder aids in the dynamic construction of a TreeModel. 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 class  TreeBuilder.Container<T>
      The class used to represent container nodes in the tree.
      static class  TreeBuilder.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.
    • 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 in
        leaf - the leaf to add; if null ensures 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 a DefaultTreeModel.
        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 File and this method returns container.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