Class HashTreeModel<T>

  • All Implemented Interfaces:
    javax.swing.tree.TreeModel

    public class HashTreeModel<T>
    extends java.lang.Object
    implements javax.swing.tree.TreeModel
    A HashTreeModel is an implementation of the Swing TreeModel backed by a HashMap. Any objects can be added as nodes or the class can be parameterized so that only nodes belonging to some type may be added (noting that TreeModel methods will still use Object). An object may only be inserted into the tree once as enforced by Object.equals(java.lang.Object) as this causes problems with the JTree.
    Author:
    Jay Detwiler
    • Constructor Summary

      Constructors 
      Constructor Description
      HashTreeModel​(T root)
      Create the HashTreeModel with the given object as root.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addChild​(T parent, T child)
      Add a node to a parent.
      void addTreeModelListener​(javax.swing.event.TreeModelListener l)  
      T getChild​(java.lang.Object parent, int index)  
      int getChildCount​(java.lang.Object parent)  
      int getIndexOfChild​(java.lang.Object parent, java.lang.Object child)  
      T getRoot()
      Returns the root object.
      boolean isLeaf​(java.lang.Object node)  
      boolean isMember​(java.lang.Object node)
      Return true if the given node is a member of the tree.
      void removeChildren​(T node)  
      void removeNode​(T node)
      Remove a node from the tree.
      void removeTreeModelListener​(javax.swing.event.TreeModelListener l)  
      void valueForPathChanged​(javax.swing.tree.TreePath path, java.lang.Object newValue)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HashTreeModel

        public HashTreeModel​(T root)
        Create the HashTreeModel with the given object as root.
        Parameters:
        root - the root object
    • Method Detail

      • getRoot

        public T getRoot()
        Returns the root object.
        Specified by:
        getRoot in interface javax.swing.tree.TreeModel
        Returns:
        the root object.
      • addChild

        public void addChild​(T parent,
                             T child)
        Add a node to a parent. To be valid, the parent must be a member and the child must not be a member of the tree.
        Parameters:
        parent - the node in the tree to add a new node to
        child - a node to add to the tree
        Throws:
        java.lang.IllegalArgumentException - if the parent is not a member or the child already is a member of the tree
        java.lang.NullPointerException - if either argument is null
      • removeNode

        public void removeNode​(T node)
        Remove a node from the tree. This removes the node and any children. The node must be a member of the tree and not the root.
        Parameters:
        node - a node to remove
        Throws:
        java.lang.NullPointerException - if the node is null
        java.lang.IllegalArgumentException - if the node is not a member or if the node is the root of the tree
      • removeChildren

        public void removeChildren​(T node)
      • isMember

        public boolean isMember​(java.lang.Object node)
        Return true if the given node is a member of the tree.
        Parameters:
        node - a node to test
        Returns:
        true if the given node is a member of the tree.
      • getChild

        public T getChild​(java.lang.Object parent,
                          int index)
        Specified by:
        getChild in interface javax.swing.tree.TreeModel
      • getChildCount

        public int getChildCount​(java.lang.Object parent)
        Specified by:
        getChildCount in interface javax.swing.tree.TreeModel
      • isLeaf

        public boolean isLeaf​(java.lang.Object node)
        Specified by:
        isLeaf in interface javax.swing.tree.TreeModel
      • valueForPathChanged

        public void valueForPathChanged​(javax.swing.tree.TreePath path,
                                        java.lang.Object newValue)
        Specified by:
        valueForPathChanged in interface javax.swing.tree.TreeModel
      • getIndexOfChild

        public int getIndexOfChild​(java.lang.Object parent,
                                   java.lang.Object child)
        Specified by:
        getIndexOfChild in interface javax.swing.tree.TreeModel
      • addTreeModelListener

        public void addTreeModelListener​(javax.swing.event.TreeModelListener l)
        Specified by:
        addTreeModelListener in interface javax.swing.tree.TreeModel
      • removeTreeModelListener

        public void removeTreeModelListener​(javax.swing.event.TreeModelListener l)
        Specified by:
        removeTreeModelListener in interface javax.swing.tree.TreeModel