Package com.sun.demo

Class ElementTreePanel.ElementTreeModel

  • All Implemented Interfaces:
    java.io.Serializable, javax.swing.tree.TreeModel
    Enclosing class:
    ElementTreePanel

    public static class ElementTreePanel.ElementTreeModel
    extends javax.swing.tree.DefaultTreeModel
    ElementTreeModel is an implementation of TreeModel to handle displaying the Elements from a Document. AbstractDocument.AbstractElement is the default implementation used by the swing text package to implement Element, and it implements TreeNode. This makes it trivial to create a DefaultTreeModel rooted at a particular Element from the Document. Unfortunately each Document can have more than one root Element. Implying that to display all the root elements as a child of another root a fake node has be created. This class creates a fake node as the root with the children being the root elements of the Document (getRootElements).

    This subclasses DefaultTreeModel. The majority of the TreeModel methods have been subclassed, primarily to special case the root.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected javax.swing.text.Element[] rootElements  
      • Fields inherited from class javax.swing.tree.DefaultTreeModel

        asksAllowsChildren, listenerList, root
    • Constructor Summary

      Constructors 
      Constructor Description
      ElementTreeModel​(javax.swing.text.Document document)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object getChild​(java.lang.Object parent, int index)
      Returns the child of parent at index index in the parent's child array.
      int getChildCount​(java.lang.Object parent)
      Returns the number of children of parent.
      int getIndexOfChild​(java.lang.Object parent, java.lang.Object child)
      Returns the index of child in parent.
      protected javax.swing.tree.TreeNode[] getPathToRoot​(javax.swing.tree.TreeNode aNode, int depth)
      Returns the path to a particluar node.
      boolean isLeaf​(java.lang.Object node)
      Returns true if node is a leaf.
      void nodeChanged​(javax.swing.tree.TreeNode node)
      Invoke this method after you've changed how node is to be represented in the tree.
      • Methods inherited from class javax.swing.tree.DefaultTreeModel

        addTreeModelListener, asksAllowsChildren, fireTreeNodesChanged, fireTreeNodesInserted, fireTreeNodesRemoved, fireTreeStructureChanged, getListeners, getPathToRoot, getRoot, getTreeModelListeners, insertNodeInto, nodesChanged, nodeStructureChanged, nodesWereInserted, nodesWereRemoved, reload, reload, removeNodeFromParent, removeTreeModelListener, setAsksAllowsChildren, setRoot, valueForPathChanged
      • Methods inherited from class java.lang.Object

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

      • rootElements

        protected javax.swing.text.Element[] rootElements
    • Constructor Detail

      • ElementTreeModel

        public ElementTreeModel​(javax.swing.text.Document document)
    • Method Detail

      • getChild

        public java.lang.Object getChild​(java.lang.Object parent,
                                         int index)
        Returns the child of parent at index index in the parent's child array. parent must be a node previously obtained from this data source. This should not return null if index is a valid index for parent (that is index >= 0 && index < getChildCount(parent)).
        Specified by:
        getChild in interface javax.swing.tree.TreeModel
        Overrides:
        getChild in class javax.swing.tree.DefaultTreeModel
        Parameters:
        parent - a node in the tree, obtained from this data source
        Returns:
        the child of parent at index index
      • getChildCount

        public int getChildCount​(java.lang.Object parent)
        Returns the number of children of parent. Returns 0 if the node is a leaf or if it has no children. parent must be a node previously obtained from this data source.
        Specified by:
        getChildCount in interface javax.swing.tree.TreeModel
        Overrides:
        getChildCount in class javax.swing.tree.DefaultTreeModel
        Parameters:
        parent - a node in the tree, obtained from this data source
        Returns:
        the number of children of the node parent
      • isLeaf

        public boolean isLeaf​(java.lang.Object node)
        Returns true if node is a leaf. It is possible for this method to return false even if node has no children. A directory in a filesystem, for example, may contain no files; the node representing the directory is not a leaf, but it also has no children.
        Specified by:
        isLeaf in interface javax.swing.tree.TreeModel
        Overrides:
        isLeaf in class javax.swing.tree.DefaultTreeModel
        Parameters:
        node - a node in the tree, obtained from this data source
        Returns:
        true if node is a leaf
      • getIndexOfChild

        public int getIndexOfChild​(java.lang.Object parent,
                                   java.lang.Object child)
        Returns the index of child in parent.
        Specified by:
        getIndexOfChild in interface javax.swing.tree.TreeModel
        Overrides:
        getIndexOfChild in class javax.swing.tree.DefaultTreeModel
      • nodeChanged

        public void nodeChanged​(javax.swing.tree.TreeNode node)
        Invoke this method after you've changed how node is to be represented in the tree.
        Overrides:
        nodeChanged in class javax.swing.tree.DefaultTreeModel
      • getPathToRoot

        protected javax.swing.tree.TreeNode[] getPathToRoot​(javax.swing.tree.TreeNode aNode,
                                                            int depth)
        Returns the path to a particluar node. This is recursive.
        Overrides:
        getPathToRoot in class javax.swing.tree.DefaultTreeModel