Methods Summary |
---|
public void | addTreeModelListener(TreeModelListener l)Adds a listener for the TreeModelEvent posted after the tree changes.
listenerList.add(TreeModelListener.class, l);
|
public boolean | asksAllowsChildren()Tells how leaf nodes are determined.
return asksAllowsChildren;
|
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. The event instance
is lazily created using the parameters passed into
the fire method.
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
TreeModelEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==TreeModelListener.class) {
// Lazily create the event:
if (e == null)
e = new TreeModelEvent(source, path,
childIndices, children);
((TreeModelListener)listeners[i+1]).treeNodesChanged(e);
}
}
|
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. The event instance
is lazily created using the parameters passed into
the fire method.
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
TreeModelEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==TreeModelListener.class) {
// Lazily create the event:
if (e == null)
e = new TreeModelEvent(source, path,
childIndices, children);
((TreeModelListener)listeners[i+1]).treeNodesInserted(e);
}
}
|
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. The event instance
is lazily created using the parameters passed into
the fire method.
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
TreeModelEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==TreeModelListener.class) {
// Lazily create the event:
if (e == null)
e = new TreeModelEvent(source, path,
childIndices, children);
((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);
}
}
|
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. The event instance
is lazily created using the parameters passed into
the fire method.
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
TreeModelEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==TreeModelListener.class) {
// Lazily create the event:
if (e == null)
e = new TreeModelEvent(source, path,
childIndices, children);
((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
}
}
|
private void | fireTreeStructureChanged(java.lang.Object source, TreePath path)
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
TreeModelEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==TreeModelListener.class) {
// Lazily create the event:
if (e == null)
e = new TreeModelEvent(source, path);
((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
}
}
|
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)).
return ((TreeNode)parent).getChildAt(index);
|
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.
return ((TreeNode)parent).getChildCount();
|
public int | getIndexOfChild(java.lang.Object parent, java.lang.Object child)Returns the index of child in parent.
If either the parent or child is null , returns -1.
if(parent == null || child == null)
return -1;
return ((TreeNode)parent).getIndex((TreeNode)child);
|
public T[] | getListeners(java.lang.Class listenerType)Returns an array of all the objects currently registered
as FooListener s
upon this model.
FooListener s are registered using the
addFooListener method.
You can specify the listenerType argument
with a class literal,
such as
FooListener.class .
For example, you can query a
DefaultTreeModel m
for its tree model listeners with the following code:
TreeModelListener[] tmls = (TreeModelListener[])(m.getListeners(TreeModelListener.class));
If no such listeners exist, this method returns an empty array.
return listenerList.getListeners(listenerType);
|
public TreeNode[] | getPathToRoot(TreeNode aNode)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.
return getPathToRoot(aNode, 0);
|
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.
TreeNode[] retNodes;
// This method recurses, traversing towards the root in order
// size the array. On the way back, it fills in the nodes,
// starting from the root and working back to the original node.
/* Check for null, in case someone passed in a null node, or
they passed in an element that isn't rooted at root. */
if(aNode == null) {
if(depth == 0)
return null;
else
retNodes = new TreeNode[depth];
}
else {
depth++;
if(aNode == root)
retNodes = new TreeNode[depth];
else
retNodes = getPathToRoot(aNode.getParent(), depth);
retNodes[retNodes.length - depth] = aNode;
}
return retNodes;
|
public java.lang.Object | getRoot()Returns the root of the tree. Returns null only if the tree has
no nodes.
return root;
|
public TreeModelListener[] | getTreeModelListeners()Returns an array of all the tree model listeners
registered on this model.
return (TreeModelListener[])listenerList.getListeners(
TreeModelListener.class);
|
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.
parent.insert(newChild, index);
int[] newIndexs = new int[1];
newIndexs[0] = index;
nodesWereInserted(parent, newIndexs);
|
public boolean | isLeaf(java.lang.Object node)Returns whether the specified node is a leaf node.
The way the test is performed depends on the
askAllowsChildren setting.
if(asksAllowsChildren)
return !((TreeNode)node).getAllowsChildren();
return ((TreeNode)node).isLeaf();
|
public void | nodeChanged(TreeNode node)Invoke this method after you've changed how node is to be
represented in the tree.
if(listenerList != null && node != null) {
TreeNode parent = node.getParent();
if(parent != null) {
int anIndex = parent.getIndex(node);
if(anIndex != -1) {
int[] cIndexs = new int[1];
cIndexs[0] = anIndex;
nodesChanged(parent, cIndexs);
}
}
else if (node == getRoot()) {
nodesChanged(node, null);
}
}
|
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.
if(node != null) {
fireTreeStructureChanged(this, getPathToRoot(node), null, null);
}
|
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.
if(node != null) {
if (childIndices != null) {
int cCount = childIndices.length;
if(cCount > 0) {
Object[] cChildren = new Object[cCount];
for(int counter = 0; counter < cCount; counter++)
cChildren[counter] = node.getChildAt
(childIndices[counter]);
fireTreeNodesChanged(this, getPathToRoot(node),
childIndices, cChildren);
}
}
else if (node == getRoot()) {
fireTreeNodesChanged(this, getPathToRoot(node), null, null);
}
}
|
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.
if(listenerList != null && node != null && childIndices != null
&& childIndices.length > 0) {
int cCount = childIndices.length;
Object[] newChildren = new Object[cCount];
for(int counter = 0; counter < cCount; counter++)
newChildren[counter] = node.getChildAt(childIndices[counter]);
fireTreeNodesInserted(this, getPathToRoot(node), childIndices,
newChildren);
}
|
public void | nodesWereRemoved(TreeNode node, int[] childIndices, java.lang.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.
if(node != null && childIndices != null) {
fireTreeNodesRemoved(this, getPathToRoot(node), childIndices,
removedChildren);
}
|
private void | readObject(java.io.ObjectInputStream s)
s.defaultReadObject();
Vector values = (Vector)s.readObject();
int indexCounter = 0;
int maxCounter = values.size();
if(indexCounter < maxCounter && values.elementAt(indexCounter).
equals("root")) {
root = (TreeNode)values.elementAt(++indexCounter);
indexCounter++;
}
|
public void | reload()Invoke this method if you've modified the {@code TreeNode}s upon which
this model depends. The model will notify all of its listeners that the
model has changed.
reload(root);
|
public void | reload(TreeNode node)Invoke this method if you've modified the {@code TreeNode}s upon which
this model depends. The model will notify all of its listeners that the
model has changed below the given node.
if(node != null) {
fireTreeStructureChanged(this, getPathToRoot(node), null, null);
}
|
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.
MutableTreeNode parent = (MutableTreeNode)node.getParent();
if(parent == null)
throw new IllegalArgumentException("node does not have a parent.");
int[] childIndex = new int[1];
Object[] removedArray = new Object[1];
childIndex[0] = parent.getIndex(node);
parent.remove(childIndex[0]);
removedArray[0] = node;
nodesWereRemoved(parent, childIndex, removedArray);
|
public void | removeTreeModelListener(TreeModelListener l)Removes a listener previously added with addTreeModelListener().
listenerList.remove(TreeModelListener.class, l);
|
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.
asksAllowsChildren = newValue;
|
public void | setRoot(TreeNode root)Sets the root to root . A null root implies
the tree is to display nothing, and is legal.
Object oldRoot = this.root;
this.root = root;
if (root == null && oldRoot != null) {
fireTreeStructureChanged(this, null);
}
else {
nodeStructureChanged(root);
}
|
public void | valueForPathChanged(TreePath path, java.lang.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.
MutableTreeNode aNode = (MutableTreeNode)path.getLastPathComponent();
aNode.setUserObject(newValue);
nodeChanged(aNode);
|
private void | writeObject(java.io.ObjectOutputStream s)
Vector values = new Vector();
s.defaultWriteObject();
// Save the root, if its Serializable.
if(root != null && root instanceof Serializable) {
values.addElement("root");
values.addElement(root);
}
s.writeObject(values);
|