public class DataNodeTreeModel extends Object implements TreeModel
DataNode
s.
Each Object in the TreeModel is a DataNode.
Node expansion is handled carefully: children are added one child at a time
with suitable TreeModelEvents fired after each one rather than
providing the whole list of children at once, since this could
be a time-consuming operation and might cause the user interface
to lock up for longer than was acceptable.
Associated with each DataNode
in the tree is a
TreeModelNode
, which handles some of the structure and is
in fact used internally by this tree model to store the tree structure.
This can be obtained using the getModelNode(uk.ac.starlink.datanode.nodes.DataNode)
method
and manipulated directly for more direct control over the tree structure
than is possibly by manipulating DataNode
s.
Constructor and Description |
---|
DataNodeTreeModel()
Constructs a new DataNodeTreeModel with a default root node.
|
DataNodeTreeModel(DataNode rootDataNode)
Constructs a new DataNodeTreeModel with a given root node.
|
Modifier and Type | Method and Description |
---|---|
void |
addTreeModelListener(TreeModelListener listener) |
void |
appendNode(DataNode newChild,
DataNode parent)
Appends a new child node to the children of a node which
exists in this tree model.
|
boolean |
containsNode(DataNode node)
Indicates whether this model contains a given data node.
|
Object |
getChild(Object parentDataNode,
int index)
Returns a given child of a node in the tree.
|
int |
getChildCount(Object parentDataNode)
Returns the number of children a node currently has.
|
DataNode[] |
getCurrentChildren(DataNode dataNode)
Returns an array of the children currently owned by a given node.
|
int |
getIndexOfChild(Object parentDataNode,
Object childDataNode)
Returns the index of a given child if it is a direct child of another.
|
TreeModelNode |
getModelNode(DataNode dataNode)
Returns the
TreeModelNode which acts as the container
for a given data node. |
int |
getNodeCount()
Returns the number of nodes known in this model.
|
DataNode[] |
getPathToRoot(DataNode dataNode)
Returns an array representing the position of the given
DataNode in the tree. |
Object |
getRoot()
Returns the root of the tree.
|
void |
insertNode(DataNode newChild,
DataNode parent,
int ipos)
Inserts a new node into the child list of a node which exists
in this tree model.
|
boolean |
isLeaf(Object dataNode)
Indicates whether the node is a leaf, that is cannot have any
children.
|
TreeModelNode |
makeModelNode(DataNode dataNode,
TreeModelNode parent)
Creates a new TreeModelNode for use in this TreeModel.
|
void |
refreshNode(DataNode dataNode)
Effectively re-initialises a node.
|
void |
removeNode(DataNode dataNode)
Removes a node from the tree.
|
void |
removeTreeModelListener(TreeModelListener listener) |
void |
repaintNode(DataNode dataNode)
Refreshes the representation of the node iteself.
|
void |
replaceNode(DataNode oldDataNode,
DataNode newDataNode)
Replaces a given data node with a new one.
|
void |
setRoot(DataNode rootDataNode)
Sets the root node of the tree.
|
void |
stopExpansion(DataNode dataNode)
Stops a node from expanding.
|
void |
valueForPathChanged(TreePath path,
Object newDataNode)
Messaged when the user has altered the value for an item.
|
public DataNodeTreeModel()
public DataNodeTreeModel(DataNode rootDataNode)
rootDataNode
- the root nodepublic Object getRoot()
public Object getChild(Object parentDataNode, int index)
public int getChildCount(Object parentDataNode)
The first time that this method is called on a node it is
used as a trigger to begin node expansion. This may be a
a time-consuming business, so this method will return a
number of children which can be determined without delay
(this may be zero), and will initiate the process of retrieving
all the other children. This is done in a separate thread,
and a suitable TreeModelEvent
is fired each time a
child arrives. The expansion process will continue until
all the children have been found. No notification is currently
made when the set of children is complete.
Subsequent calls return the number of children which are currently present, but again do not offer a guarantee that more might not be about to arrive.
getChildCount
in interface TreeModel
parentDataNode
- DataNode
object in the tree to be
queriedpublic DataNode[] getCurrentChildren(DataNode dataNode)
getChildCount(java.lang.Object)
, this does not trigger expansion
of a not-currently-expanded node, it just gives a snapshot of
the current state of the model.dataNode
- the node whose children are being enquired aboutdataNode
's childrenpublic boolean isLeaf(Object dataNode)
public int getIndexOfChild(Object parentDataNode, Object childDataNode)
Note this returns -1 (without error) if childDataNode
does not appear in the tree at all; this appears to be required
on occasion by Sun's JTree implementation, though that's not
documented in the TreeModel
interface.
getIndexOfChild
in interface TreeModel
parentDataNode
- DataNode
which is the parent objectchildDataNode
- DataNode
which is a child of
parentDataNode
childDataNode
within
parentDataNode
or -1 if it's not a childpublic void valueForPathChanged(TreePath path, Object newDataNode)
valueForPathChanged
in interface TreeModel
path
- path to the altered node (all the objects in the
path must be DataNode
s)newDataNode
- the DataNode
which is now found at
path
public void addTreeModelListener(TreeModelListener listener)
addTreeModelListener
in interface TreeModel
public void removeTreeModelListener(TreeModelListener listener)
removeTreeModelListener
in interface TreeModel
public int getNodeCount()
public boolean containsNode(DataNode node)
true
iff this model contains node
public DataNode[] getPathToRoot(DataNode dataNode)
DataNode
in the tree.
The root is the first element in the returned
array, and dataNode
is the last. The length of
the returned array gives the node's depth in the tree.
If dataNode
does not exist in this model, null
is returned.dataNode
- the node to find the path ofdataNode
public void insertNode(DataNode newChild, DataNode parent, int ipos)
newChild
- the new data node to insert into
parent
's list of childrenparent
- the parent node in whose children newChild
should be insertedipos
- the position at which the insertion should take placepublic void appendNode(DataNode newChild, DataNode parent)
newChild
- the new data node to add at the end of
parent
's list of childrenparent
- the parent node to whose children newChild
should be appendedpublic void removeNode(DataNode dataNode)
dataNode
- the node to removepublic void replaceNode(DataNode oldDataNode, DataNode newDataNode)
setRoot(uk.ac.starlink.datanode.nodes.DataNode)
.oldDataNode
- the node to be replacednewDataNode
- the node to replace it withpublic void setRoot(DataNode rootDataNode)
rootDataNode
- the new root nodepublic void refreshNode(DataNode dataNode)
dataNode
- the node to refreshpublic void repaintNode(DataNode dataNode)
dataNode
- the node to repaintpublic void stopExpansion(DataNode dataNode)
dataNode
- the node whose expansion is to be stoppedpublic TreeModelNode getModelNode(DataNode dataNode)
TreeModelNode
which acts as the container
for a given data node.dataNode
- the data node whose model node is requireddataNode
's container model nodepublic TreeModelNode makeModelNode(DataNode dataNode, TreeModelNode parent)
dataNode
- the DataNode managed by this TreeModelNodeparent
- the parent of this node (null for the root)Copyright © 2025 Central Laboratory of the Research Councils. All Rights Reserved.