EchoPoint
2.1.0rc5

echopointng.tree
Class DefaultTreeModel

java.lang.Object
  extended by echopointng.tree.DefaultTreeModel
All Implemented Interfaces:
TreeModel, Serializable

public class DefaultTreeModel
extends Object
implements TreeModel, Serializable

A simple tree data model that uses TreeNodes.

See Also:
Serialized Form

Field Summary
protected  boolean asksAllowsChildren
          Determines how the isLeaf method figures out if a node is a leaf node.
protected  EventListenerList listenerList
          Listeners.
protected  HashMap nodeIds
          Map of Node identifiers keyed by the Nodes themselves
protected  TreeNode root
          Root of the tree.
 
Constructor Summary
DefaultTreeModel(TreeNode root)
          Creates a tree in which any node can have children.
DefaultTreeModel(TreeNode root, boolean asksAllowsChildren)
          Creates a tree specifying whether any node can have children, or whether only certain nodes can have children.
 
Method Summary
 void addTreeModelListener(TreeModelListener l)
          Adds a TreeModelListener to the model
 boolean asksAllowsChildren()
          Tells how leaf nodes are determined.
protected  void fireTreeNodesChanged(Object source, Object[] path, int[] childIndices, Object[] children)
          Notify all listeners that have registered interest for notification on this event type.
protected  void fireTreeNodesInserted(Object source, Object[] path, int[] childIndices, Object[] children)
          Notify all listeners that have registered interest for notification on this event type.
protected  void fireTreeNodesRemoved(Object source, Object[] path, int[] childIndices, Object[] children)
          Notify all listeners that have registered interest for notification on this event type.
protected  void fireTreeStructureChanged(Object source, Object[] path, int[] childIndices, Object[] children)
          Notify all listeners that have registered interest for notification on this event type.
 Object getChild(Object parent, int index)
          Returns the child of parent at index index in the parent's child array.
 int getChildCount(Object parent)
          Returns the number of children of parent .
 int getIndexOfChild(Object parent, Object child)
          Returns the index of child in parent.
 Object getNodeById(String id)
           
 String getNodeId(Object node)
          Returns an identifier for a node.
 Object getParent(Object child)
          Returns the parent of child .
 Object[] getPathToRoot(Object node)
          Builds the parents of node up to and including the root node, where the original node is the last element in the returned array.
protected  TreeNode[] getPathToRoot(TreeNode aNode, int depth)
          Builds the parents of node up to and including the root node, where the original node is the last element in the returned array.
 Object getRoot()
          Returns the root of the tree.
 void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index)
          Invoked this to insert newChild at location index in parents children.
 boolean isLeaf(Object node)
          Returns whether the specified node is a leaf node.
 void nodeChanged(TreeNode node)
          Invoke this method after you've changed how node is to be represented in the tree.
 void nodesChanged(TreeNode node, int[] childIndices)
          Invoke this method after you've changed how the children identified by childIndicies are to be represented in the tree.
 void nodeStructureChanged(TreeNode node)
          Invoke this method if you've totally changed the children of node and its childrens children...
 void nodesWereInserted(TreeNode node, int[] childIndices)
          Invoke this method after you've inserted some TreeNodes into node.
 void nodesWereRemoved(TreeNode node, int[] childIndices, Object[] removedChildren)
          Invoke this method after you've removed some TreeNodes from node.
 void reload()
          Invoke this method if you've modified the TreeNodes upon which this model depends.
 void reload(TreeNode node)
          Invoke this method if you've modified the TreeNodes upon which this model depends.
 void removeNodeFromParent(MutableTreeNode node)
          Message this to remove node from its parent.
 void removeTreeModelListener(TreeModelListener l)
          Removes a TreeModelListener from the model
 void setAsksAllowsChildren(boolean newValue)
          Sets whether or not to test leafness by asking getAllowsChildren() or isLeaf() to the TreeNodes.
 void setRoot(TreeNode root)
          Sets the root to root.
 void valueForPathChanged(TreePath path, Object newValue)
          This sets the user object of the TreeNode identified by path and posts a node changed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

root

protected TreeNode root
Root of the tree.


listenerList

protected EventListenerList listenerList
Listeners.


asksAllowsChildren

protected boolean asksAllowsChildren
Determines how the isLeaf method figures out if a node is a leaf node. If true, a node is a leaf node if it does not allow children. (If it allows children, it is not a leaf node, even if no children are present.) That lets you distinguish between folder nodes and file nodes in a file system, for example.

If this value is false, then any node which has no children is a leaf node, and any node may acquire children.


nodeIds

protected HashMap nodeIds
Map of Node identifiers keyed by the Nodes themselves

Constructor Detail

DefaultTreeModel

public DefaultTreeModel(TreeNode root)
Creates a tree in which any node can have children.

Parameters:
root - a TreeNode object that is the root of the tree

DefaultTreeModel

public DefaultTreeModel(TreeNode root,
                        boolean asksAllowsChildren)
Creates a tree specifying whether any node can have children, or whether only certain nodes can have children.

Parameters:
root - a TreeNode object that is the root of the tree
asksAllowsChildren - a boolean, false if any node can have children, true if each node is asked to see if it can have children
Method Detail

addTreeModelListener

public void addTreeModelListener(TreeModelListener l)
Adds a TreeModelListener to the model

Specified by:
addTreeModelListener in interface TreeModel
Parameters:
l - the listener to add

asksAllowsChildren

public boolean asksAllowsChildren()
Tells how leaf nodes are determined.

Returns:
true if only nodes which do not allow children are leaf nodes, false if nodes which have no children (even if allowed) are leaf nodes

fireTreeNodesChanged

protected void fireTreeNodesChanged(Object source,
                                    Object[] path,
                                    int[] childIndices,
                                    Object[] children)
Notify all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.


fireTreeNodesInserted

protected void fireTreeNodesInserted(Object source,
                                     Object[] path,
                                     int[] childIndices,
                                     Object[] children)
Notify all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.


fireTreeNodesRemoved

protected void fireTreeNodesRemoved(Object source,
                                    Object[] path,
                                    int[] childIndices,
                                    Object[] children)
Notify all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.


fireTreeStructureChanged

protected void fireTreeStructureChanged(Object source,
                                        Object[] path,
                                        int[] childIndices,
                                        Object[] children)
Notify all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.


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
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
Parameters:
parent - a node in the tree, obtained from this data source
Returns:
the number of children of the node parent

getIndexOfChild

public int getIndexOfChild(Object parent,
                           Object child)
Returns the index of child in parent.

Specified by:
getIndexOfChild in interface TreeModel

getParent

public Object getParent(Object child)
Returns the parent of child . Returns null if the node has no parent or the tree model does not support traversing from a child to a parent node.

Parameters:
child - a node in the tree
Returns:
the parent of the node child

getPathToRoot

protected TreeNode[] getPathToRoot(TreeNode aNode,
                                   int depth)
Builds the parents of node up to and including the root node, where the original node is the last element in the returned array. The length of the returned array gives the node's depth in the tree.

Parameters:
aNode - the TreeNode to get the path for
depth - an int giving the number of steps already taken towards the root (on recursive calls), used to size the returned array
Returns:
an array of TreeNodes giving the path from the root to the specified node

getPathToRoot

public Object[] getPathToRoot(Object node)
Builds the parents of node up to and including the root node, where the original node is the last element in the returned array. The length of the returned array gives the node's depth in the tree.

Parameters:
node - the TreeNode to get the path for

getRoot

public Object getRoot()
Returns the root of the tree. Returns null only if the tree has no nodes.

Specified by:
getRoot in interface TreeModel
Returns:
the root of the tree

insertNodeInto

public void insertNodeInto(MutableTreeNode newChild,
                           MutableTreeNode parent,
                           int index)
Invoked this to insert newChild at location index in parents children. This will then message nodesWereInserted to create the appropriate event. This is the preferred way to add children as it will create the appropriate event.


isLeaf

public boolean isLeaf(Object node)
Returns whether the specified node is a leaf node. The way the test is performed depends on the askAllowsChildren setting.

Specified by:
isLeaf in interface TreeModel
Parameters:
node - a node in the tree, obtained from this data source
Returns:
true if node is a leaf

nodeChanged

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


nodesChanged

public void nodesChanged(TreeNode node,
                         int[] childIndices)
Invoke this method after you've changed how the children identified by childIndicies are to be represented in the tree.


nodeStructureChanged

public void nodeStructureChanged(TreeNode node)
Invoke this method if you've totally changed the children of node and its childrens children... This will post a treeStructureChanged event.


nodesWereInserted

public void nodesWereInserted(TreeNode node,
                              int[] childIndices)
Invoke this method after you've inserted some TreeNodes into node. childIndices should be the index of the new elements and must be sorted in ascending order.


nodesWereRemoved

public void nodesWereRemoved(TreeNode node,
                             int[] childIndices,
                             Object[] removedChildren)
Invoke this method after you've removed some TreeNodes from node. childIndices should be the index of the removed elements and must be sorted in ascending order. And removedChildren should be the array of the children objects that were removed.


reload

public void reload()
Invoke this method if you've modified the TreeNodes upon which this model depends. The model will notify all of its listeners that the model has changed.


reload

public void reload(TreeNode node)
Invoke this method if you've modified the TreeNodes upon which this model depends. The model will notify all of its listeners that the model has changed below the node.


removeNodeFromParent

public void removeNodeFromParent(MutableTreeNode node)
Message this to remove node from its parent. This will message nodesWereRemoved to create the appropriate event. This is the preferred way to remove a node as it handles the event creation for you.


removeTreeModelListener

public void removeTreeModelListener(TreeModelListener l)
Removes a TreeModelListener from the model

Specified by:
removeTreeModelListener in interface TreeModel

setAsksAllowsChildren

public void setAsksAllowsChildren(boolean newValue)
Sets whether or not to test leafness by asking getAllowsChildren() or isLeaf() to the TreeNodes. If newvalue is true, getAllowsChildren() is messaged, otherwise isLeaf() is messaged.


setRoot

public void setRoot(TreeNode root)
Sets the root to root. This will throw an IllegalArgumentException if root is null.


valueForPathChanged

public void valueForPathChanged(TreePath path,
                                Object newValue)
This sets the user object of the TreeNode identified by path and posts a node changed. If you use custom user objects in the TreeModel you're going to need to subclass this and set the user object of the changed node to something meaningful.

Specified by:
valueForPathChanged in interface TreeModel
Parameters:
path - path to the node that the user has altered.
newValue - the new value from the Tree.

getNodeId

public String getNodeId(Object node)
Description copied from interface: TreeModel
Returns an identifier for a node. The identifier should be assigned to the Node when added to the model and thereafter it should not change.

Specified by:
getNodeId in interface TreeModel
Returns:
String - a unique identifier for a specific node
See Also:
TreeModel.getNodeId(java.lang.Object)

getNodeById

public Object getNodeById(String id)
Specified by:
getNodeById in interface TreeModel
Returns:
a node Object based on its unique identifier (obtained using getNodeId)
See Also:
TreeModel.getNodeById(java.lang.String)

EchoPoint
2.1.0rc5