.useful Home Page

org.dotuseful.ui.tree
Class AutomatedTreeNode

java.lang.Object
  extended byjavax.swing.tree.DefaultMutableTreeNode
      extended byorg.dotuseful.ui.tree.AutomatedTreeNode
All Implemented Interfaces:
java.lang.Cloneable, java.util.EventListener, javax.swing.tree.MutableTreeNode, java.io.Serializable, javax.swing.event.TreeModelListener, javax.swing.tree.TreeNode
Direct Known Subclasses:
SortedTreeNode

public class AutomatedTreeNode
extends javax.swing.tree.DefaultMutableTreeNode
implements javax.swing.event.TreeModelListener

AutomatedTreeNode extends DefaultMutableTreeNode adding support for automatic notification of node changes. AutomatedTreeNodes are used with AutomatedTreeModel. In this model each node considered as a little tree which can fire TreeModel events about its changes. Each parent node registers itself as a listener of its child nodes events and transfers events to its own listeners (which is its parent) up to the root node. You can use AutomatedTreeModel which automatically handles events and fires them as usual TreeModel.

A sample of code that uses DefaultTreeModel and DefaultTreeNodes

setUserObject( event.getObject() );
//getting a tree model from somewhere
DefaultTreeModel model = ( DefaultTreeModel ) titleTree.getModel();
model.nodeChanged( node );

A sample of code that uses AutomatedTreeModel and AutomatedTreeNodes

setUserObject( event.getObject() );
//Everything else automated

This is not a thread safe class. If you intend to use an AutomatedTreeNode (or a tree of TreeNodes) in more than one thread, you need to do your own synchronizing. A good convention to adopt is synchronizing on the root node of a tree.

Author:
dkrukovsky
See Also:
Serialized Form

Field Summary
protected  javax.swing.event.EventListenerList listenerList
          Listeners.
 
Fields inherited from class javax.swing.tree.DefaultMutableTreeNode
allowsChildren, children, EMPTY_ENUMERATION, parent, userObject
 
Constructor Summary
AutomatedTreeNode()
          Creates an AutomatedTreeNode that has no parent and no children, but which allows children.
AutomatedTreeNode(java.lang.Object userObject)
          Creates an AutomatedTreeNode node with no parent, no children, but which allows children, and initializes it with the specified user object.
AutomatedTreeNode(java.lang.Object userObject, boolean allowsChildren)
          Creates an AutomatedTreeNode with no parent, no children, initialized with the specified user object, and that allows children only if specified.
 
Method Summary
 void addTreeModelListener(javax.swing.event.TreeModelListener l)
          Adds a listener for the TreeModelEvent posted after the node changes.
protected  void fireTreeNodesChanged(int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesChanged() method.
protected  void fireTreeNodesChanged(java.lang.Object[] path, int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesChanged() method.
protected  void fireTreeNodesChanged(java.lang.Object source, java.lang.Object[] path, int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesChanged() method.
protected  void fireTreeNodesInserted(int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesInserted() method.
protected  void fireTreeNodesInserted(java.lang.Object source, java.lang.Object[] path, int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesInserted() method.
protected  void fireTreeNodesRemoved(int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesRemoved() method.
protected  void fireTreeNodesRemoved(java.lang.Object source, java.lang.Object[] path, int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesRemoved() method.
protected  void fireTreeStructureChanged()
          Notifies all listeners that have registered interest for notification on this event type by firing a treeStructureChanged() method.
protected  void fireTreeStructureChanged(int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type by firing a treeStructureChanged() method.
protected  void fireTreeStructureChanged(java.lang.Object source, java.lang.Object[] path, int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type by firing a treeStructureChanged() method.
 void insert(javax.swing.tree.MutableTreeNode newChild, int childIndex)
          Removes newChild from its present parent (if it has a parent), sets the child's parent to this node, adds the child to this node's child array at index childIndex, fires a nodesWereInserted event, and then adds itself as a TreeModelListener to newChild.
protected  void nodeChanged()
          Invoke this method after the node changed how it is to be represented in the tree.
protected  void nodeStructureChanged()
          Invoke this method if you've totally changed the children of node and its childrens children.
protected  void nodesWereInserted(int[] childIndices)
          This method invoked after you've inserted some AutomatedTreeNodes into node. childIndices should be the index of the new elements and must be sorted in ascending order.
protected  void nodesWereRemoved(int[] childIndices, java.lang.Object[] removedChildren)
          This method invoked after you've removed some AutomatedTreeNodes from node. childIndices should be the index of the removed elements and must be sorted in ascending order.
 void remove(int childIndex)
          Removes the child at the specified index from this node's children and sets that node's parent to null.
 void removeTreeModelListener(javax.swing.event.TreeModelListener l)
          Removes a listener previously added with addTreeModelListener() .
 void setUserObject(java.lang.Object userObject)
          Sets the user object for this node to userObject.
 void treeNodesChanged(javax.swing.event.TreeModelEvent e)
           Invoked after a node (or a set of siblings) has changed in some way.
 void treeNodesInserted(javax.swing.event.TreeModelEvent e)
           Invoked after nodes have been inserted into the tree.
 void treeNodesRemoved(javax.swing.event.TreeModelEvent e)
           Invoked after nodes have been removed from the tree.
 void treeStructureChanged(javax.swing.event.TreeModelEvent e)
           Invoked after the tree has drastically changed structure from a given node down.
 
Methods inherited from class javax.swing.tree.DefaultMutableTreeNode
add, breadthFirstEnumeration, children, clone, depthFirstEnumeration, getAllowsChildren, getChildAfter, getChildAt, getChildBefore, getChildCount, getDepth, getFirstChild, getFirstLeaf, getIndex, getLastChild, getLastLeaf, getLeafCount, getLevel, getNextLeaf, getNextNode, getNextSibling, getParent, getPath, getPathToRoot, getPreviousLeaf, getPreviousNode, getPreviousSibling, getRoot, getSharedAncestor, getSiblingCount, getUserObject, getUserObjectPath, isLeaf, isNodeAncestor, isNodeChild, isNodeDescendant, isNodeRelated, isNodeSibling, isRoot, pathFromAncestorEnumeration, postorderEnumeration, preorderEnumeration, remove, removeAllChildren, removeFromParent, setAllowsChildren, setParent, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

listenerList

protected javax.swing.event.EventListenerList listenerList
Listeners.

Constructor Detail

AutomatedTreeNode

public AutomatedTreeNode()
Creates an AutomatedTreeNode that has no parent and no children, but which allows children.


AutomatedTreeNode

public AutomatedTreeNode(java.lang.Object userObject)
Creates an AutomatedTreeNode node with no parent, no children, but which allows children, and initializes it with the specified user object.

Parameters:
userObject - an Object provided by the user that constitutes the node's data

AutomatedTreeNode

public AutomatedTreeNode(java.lang.Object userObject,
                         boolean allowsChildren)
Creates an AutomatedTreeNode with no parent, no children, initialized with the specified user object, and that allows children only if specified.

Parameters:
userObject - an Object provided by the user that constitutes the node's data
allowsChildren - if true, the node is allowed to have child nodes -- otherwise, it is always a leaf node
Method Detail

insert

public void insert(javax.swing.tree.MutableTreeNode newChild,
                   int childIndex)
Removes newChild from its present parent (if it has a parent), sets the child's parent to this node, adds the child to this node's child array at index childIndex, fires a nodesWereInserted event, and then adds itself as a TreeModelListener to newChild. newChild must not be null and must not be an ancestor of this node.

Specified by:
insert in interface javax.swing.tree.MutableTreeNode
Parameters:
newChild - the MutableTreeNode to insert under this node
childIndex - the index in this node's child array where this node is to be inserted
Throws:
java.lang.ArrayIndexOutOfBoundsException - if childIndex is out of bounds
java.lang.IllegalArgumentException - if newChild is null or is an ancestor of this node
java.lang.IllegalStateException - if this node does not allow children
See Also:
DefaultMutableTreeNode.isNodeDescendant(javax.swing.tree.DefaultMutableTreeNode)

remove

public void remove(int childIndex)
Removes the child at the specified index from this node's children and sets that node's parent to null. The child node to remove must be a MutableTreeNode.

Specified by:
remove in interface javax.swing.tree.MutableTreeNode
Parameters:
childIndex - the index in this node's child array of the child to remove
Throws:
java.lang.ArrayIndexOutOfBoundsException - if childIndex is out of bounds

setUserObject

public void setUserObject(java.lang.Object userObject)
Sets the user object for this node to userObject.

Specified by:
setUserObject in interface javax.swing.tree.MutableTreeNode
Parameters:
userObject - the Object that constitutes this node's user-specified data
See Also:
DefaultMutableTreeNode.toString()

treeNodesChanged

public void treeNodesChanged(javax.swing.event.TreeModelEvent e)

Invoked after a node (or a set of siblings) has changed in some way. The node(s) have not changed locations in the tree or altered their children arrays, but other attributes have changed and may affect presentation. Example: the name of a file has changed, but it is in the same location in the file system.

Specified by:
treeNodesChanged in interface javax.swing.event.TreeModelListener

treeNodesInserted

public void treeNodesInserted(javax.swing.event.TreeModelEvent e)

Invoked after nodes have been inserted into the tree.

Specified by:
treeNodesInserted in interface javax.swing.event.TreeModelListener

treeNodesRemoved

public void treeNodesRemoved(javax.swing.event.TreeModelEvent e)

Invoked after nodes have been removed from the tree. Note that if a subtree is removed from the tree, this method may only be invoked once for the root of the removed subtree, not once for each individual set of siblings removed.

Specified by:
treeNodesRemoved in interface javax.swing.event.TreeModelListener

treeStructureChanged

public void treeStructureChanged(javax.swing.event.TreeModelEvent e)

Invoked after the tree has drastically changed structure from a given node down. If the path returned by e.getPath() is of length one and the first element does not identify the current root node the first element should become the new root of the tree.

Specified by:
treeStructureChanged in interface javax.swing.event.TreeModelListener

nodeChanged

protected void nodeChanged()
Invoke this method after the node changed how it is to be represented in the tree.


nodesWereInserted

protected void nodesWereInserted(int[] childIndices)
This method invoked after you've inserted some AutomatedTreeNodes into node. childIndices should be the index of the new elements and must be sorted in ascending order.


nodesWereRemoved

protected void nodesWereRemoved(int[] childIndices,
                                java.lang.Object[] removedChildren)
This method invoked after you've removed some AutomatedTreeNodes 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.


nodeStructureChanged

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


addTreeModelListener

public void addTreeModelListener(javax.swing.event.TreeModelListener l)
Adds a listener for the TreeModelEvent posted after the node changes.

Parameters:
l - the listener to add
See Also:
removeTreeModelListener(javax.swing.event.TreeModelListener)

removeTreeModelListener

public void removeTreeModelListener(javax.swing.event.TreeModelListener l)
Removes a listener previously added with addTreeModelListener() .

Parameters:
l - the listener to remove
See Also:
addTreeModelListener(javax.swing.event.TreeModelListener)

fireTreeNodesChanged

protected void fireTreeNodesChanged(int[] childIndices,
                                    java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesChanged() method.

Parameters:
childIndices - the indices of the changed elements
children - the changed elements
See Also:
EventListenerList

fireTreeNodesChanged

protected void fireTreeNodesChanged(java.lang.Object[] path,
                                    int[] childIndices,
                                    java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesChanged() method.

Parameters:
path - the path to the root node
childIndices - the indices of the changed elements
children - the changed elements
See Also:
EventListenerList

fireTreeNodesChanged

protected void fireTreeNodesChanged(java.lang.Object source,
                                    java.lang.Object[] path,
                                    int[] childIndices,
                                    java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesChanged() method.

Parameters:
source - the node being changed
path - the path to the root node
childIndices - the indices of the changed elements
children - the changed elements
See Also:
EventListenerList

fireTreeNodesInserted

protected void fireTreeNodesInserted(int[] childIndices,
                                     java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesInserted() method.

Parameters:
childIndices - the indices of the changed elements
children - the changed elements
See Also:
EventListenerList

fireTreeNodesInserted

protected void fireTreeNodesInserted(java.lang.Object source,
                                     java.lang.Object[] path,
                                     int[] childIndices,
                                     java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesInserted() method.

Parameters:
source - the node being changed
path - the path to the root node
childIndices - the indices of the changed elements
children - the changed elements
See Also:
EventListenerList

fireTreeNodesRemoved

protected void fireTreeNodesRemoved(int[] childIndices,
                                    java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesRemoved() method.

Parameters:
childIndices - the indices of the changed elements
children - the changed elements
See Also:
EventListenerList

fireTreeNodesRemoved

protected void fireTreeNodesRemoved(java.lang.Object source,
                                    java.lang.Object[] path,
                                    int[] childIndices,
                                    java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type by firing a treeNodesRemoved() method.

Parameters:
source - the node being changed
path - the path to the root node
childIndices - the indices of the changed elements
children - the changed elements
See Also:
EventListenerList

fireTreeStructureChanged

protected void fireTreeStructureChanged()
Notifies all listeners that have registered interest for notification on this event type by firing a treeStructureChanged() method.

See Also:
EventListenerList

fireTreeStructureChanged

protected void fireTreeStructureChanged(int[] childIndices,
                                        java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type by firing a treeStructureChanged() method.

Parameters:
childIndices - the indices of the changed elements
children - the changed elements
See Also:
EventListenerList

fireTreeStructureChanged

protected void fireTreeStructureChanged(java.lang.Object source,
                                        java.lang.Object[] path,
                                        int[] childIndices,
                                        java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type by firing a treeStructureChanged() method.

Parameters:
source - the node being changed
path - the path to the root node
childIndices - the indices of the changed elements
children - the changed elements
See Also:
EventListenerList

© 2004 .useful community

SourceForge.net-Logo