Package com.sun.demo

Class ElementTreePanel.ElementTreeModel

java.lang.Object
javax.swing.tree.DefaultTreeModel
com.sun.demo.ElementTreePanel.ElementTreeModel
All Implemented Interfaces:
Serializable, TreeModel
Enclosing class:
ElementTreePanel

public static class ElementTreePanel.ElementTreeModel extends 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:
  • Field Details

    • rootElements

      protected Element[] rootElements
  • Constructor Details

    • ElementTreeModel

      public ElementTreeModel(Document document)
  • Method Details

    • getChild

      public Object getChild(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 TreeModel
      Overrides:
      getChild in class 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(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 TreeModel
      Overrides:
      getChildCount in class 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(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 TreeModel
      Overrides:
      isLeaf in class DefaultTreeModel
      Parameters:
      node - a node in the tree, obtained from this data source
      Returns:
      true if node is a leaf
    • getIndexOfChild

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

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

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