javax.swing.tree

Class DefaultTreeModel

public class DefaultTreeModel extends Object implements Serializable, TreeModel

DefaultTreeModel
Field Summary
protected booleanasksAllowsChildren
asksAllowsChildren
protected EventListenerListlistenerList
listenerList
protected TreeNoderoot
root
Constructor Summary
DefaultTreeModel(TreeNode root)
Constructor DefaultTreeModel where any node can have children.
DefaultTreeModel(TreeNode aRoot, boolean asksAllowsChildren)
Create the DefaultTreeModel that may check if the nodes can have children or not.
Method Summary
voidaddTreeModelListener(TreeModelListener listener)
Registers a listere to the model.
booleanasksAllowsChildren()
asksAllowsChildren
protected voidfireTreeNodesChanged(Object source, Object[] path, int[] childIndices, Object[] children)
Notifies all listeners that have registered interest for notification on this event type.
protected voidfireTreeNodesInserted(Object source, Object[] path, int[] childIndices, Object[] children)
fireTreeNodesInserted
protected voidfireTreeNodesRemoved(Object source, Object[] path, int[] childIndices, Object[] children)
fireTreeNodesRemoved
protected voidfireTreeStructureChanged(Object source, Object[] path, int[] childIndices, Object[] children)
fireTreeStructureChanged
ObjectgetChild(Object node, int idx)
getChild
intgetChildCount(Object node)
getChildCount
intgetIndexOfChild(Object parent, Object child)
getIndexOfChild
<T extends EventListener> T[]getListeners(Class<T> listenerType)
Returns the registered listeners of a given type.
TreeNode[]getPathToRoot(TreeNode 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 node, 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.
ObjectgetRoot()
getRoot
TreeModelListener[]getTreeModelListeners()
Returns all registered TreeModelListener listeners.
voidinsertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index)
Invoked this to insert newChild at location index in parents children.
booleanisLeaf(Object node)
Returns if the specified node is a leaf or not.
voidnodeChanged(TreeNode node)
Invoke this method after you've changed how node is to be represented in the tree.
voidnodesChanged(TreeNode node, int[] childIndices)
Invoke this method after you've changed how the children identified by childIndices are to be represented in the tree.
voidnodesWereInserted(TreeNode parent, 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.
voidnodesWereRemoved(TreeNode parent, 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.
voidnodeStructureChanged(TreeNode node)
Invoke this method if you've totally changed the children of node and its childrens children.
voidreload()

Invoke this method if you've modified the TreeNodes upon which this model depends.

voidreload(TreeNode node)
Invoke this method if you've modified the TreeNodes upon which this model depends.
voidremoveNodeFromParent(MutableTreeNode node)
Message this to remove node from its parent.
voidremoveTreeModelListener(TreeModelListener listener)
Removes a listener from the model.
voidsetAsksAllowsChildren(boolean value)
setAsksAllowsChildren
voidsetRoot(TreeNode root)
setRoot
voidvalueForPathChanged(TreePath path, Object newValue)
Messaged when the user has altered the value for the item identified by path to newValue.

Field Detail

asksAllowsChildren

protected boolean asksAllowsChildren
asksAllowsChildren

listenerList

protected EventListenerList listenerList
listenerList

root

protected TreeNode root
root

Constructor Detail

DefaultTreeModel

public DefaultTreeModel(TreeNode root)
Constructor DefaultTreeModel where any node can have children.

Parameters: root the tree root.

DefaultTreeModel

public DefaultTreeModel(TreeNode aRoot, boolean asksAllowsChildren)
Create the DefaultTreeModel that may check if the nodes can have children or not.

Parameters: aRoot the tree root. asksAllowsChildren if true, each node is asked if it can have children. If false, the model does not care about this, supposing, that any node can have children.

Method Detail

addTreeModelListener

public void addTreeModelListener(TreeModelListener listener)
Registers a listere to the model.

Parameters: listener the listener to add

asksAllowsChildren

public boolean asksAllowsChildren()
asksAllowsChildren

Returns: boolean

fireTreeNodesChanged

protected void fireTreeNodesChanged(Object source, Object[] path, int[] childIndices, Object[] children)
Notifies 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.

Parameters: source the node being changed path the path to the root node childIndices the indices of the changed elements children the changed elements

fireTreeNodesInserted

protected void fireTreeNodesInserted(Object source, Object[] path, int[] childIndices, Object[] children)
fireTreeNodesInserted

Parameters: source the node where new nodes got inserted path the path to the root node childIndices the indices of the new elements children the new elements

fireTreeNodesRemoved

protected void fireTreeNodesRemoved(Object source, Object[] path, int[] childIndices, Object[] children)
fireTreeNodesRemoved

Parameters: source the node where nodes got removed- path the path to the root node childIndices the indices of the removed elements children the removed elements

fireTreeStructureChanged

protected void fireTreeStructureChanged(Object source, Object[] path, int[] childIndices, Object[] children)
fireTreeStructureChanged

Parameters: source the node where the model has changed path the path to the root node childIndices the indices of the affected elements children the affected elements

getChild

public Object getChild(Object node, int idx)
getChild

Parameters: node TODO idx TODO

Returns: Object

getChildCount

public int getChildCount(Object node)
getChildCount

Parameters: node TODO

Returns: int

getIndexOfChild

public int getIndexOfChild(Object parent, Object child)
getIndexOfChild

Parameters: parent TODO child TODO

Returns: int

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns the registered listeners of a given type.

Parameters: listenerType the listener type to return

Returns: an array of listeners

Since: 1.3

getPathToRoot

public TreeNode[] getPathToRoot(TreeNode 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

Returns: TreeNode[] - the path from node to the root

getPathToRoot

protected TreeNode[] getPathToRoot(TreeNode node, 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: node - 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

getRoot

public Object getRoot()
getRoot

Returns: Object

getTreeModelListeners

public TreeModelListener[] getTreeModelListeners()
Returns all registered TreeModelListener listeners.

Returns: an array of listeners.

Since: 1.4

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.

Parameters: newChild is the node to add to the parent's children parent is the parent of the newChild index is the index of the newChild

isLeaf

public boolean isLeaf(Object node)
Returns if the specified node is a leaf or not. When {@link #asksAllowsChildren} is true, then this checks if the TreeNode allows children, otherwise it returns the TreeNode's leaf property.

Parameters: node the node to check

Returns: boolean true if the node is a leaf node, false otherwise

Throws: ClassCastException if the specified node is not a TreeNode instance

See Also: getAllowsChildren isLeaf

nodeChanged

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

Parameters: node that was changed

nodesChanged

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

Parameters: node that is the parent of the children that changed in a tree. childIndices are the child nodes that changed.

nodesWereInserted

public void nodesWereInserted(TreeNode parent, 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.

Parameters: parent that had a child added to childIndices of the children added

nodesWereRemoved

public void nodesWereRemoved(TreeNode parent, 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.

Parameters: parent that had a child added to childIndices of the children added removedChildren are all the children removed from parent.

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.

Parameters: node that had its children and grandchildren changed.

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. It will fire the events, necessary to update the layout caches and repaint the tree. The tree will not be properly refreshed if you call the JTree.repaint instead.

This method will refresh the information about whole tree from the root. If only part of the tree should be refreshed, it is more effective to call {@link #reload(TreeNode)}.

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. It will fire the events, necessary to update the layout caches and repaint the tree. The tree will not be properly refreshed if you call the JTree.repaint instead.

Parameters: node - the tree node, from which the tree nodes have changed (inclusive). If you do not know this node, call {@link #reload()} instead.

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.

Parameters: node to be removed

removeTreeModelListener

public void removeTreeModelListener(TreeModelListener listener)
Removes a listener from the model.

Parameters: listener the listener to remove

setAsksAllowsChildren

public void setAsksAllowsChildren(boolean value)
setAsksAllowsChildren

Parameters: value TODO

setRoot

public void setRoot(TreeNode root)
setRoot

Parameters: root the root node.

valueForPathChanged

public void valueForPathChanged(TreePath path, Object newValue)
Messaged when the user has altered the value for the item identified by path to newValue. If newValue signifies a truly new value the model should post a treeNodesChanged event. 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.

Parameters: path - path to the node that the user has altered newValue - the new value from the TreeCellEditor