Class DataNodeTreeModel
- All Implemented Interfaces:
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.
- Author:
- Mark Taylor
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new DataNodeTreeModel with a default root node.DataNodeTreeModel
(DataNode rootDataNode) Constructs a new DataNodeTreeModel with a given root node. -
Method Summary
Modifier and TypeMethodDescriptionvoid
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.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.getModelNode
(DataNode dataNode) Returns theTreeModelNode
which acts as the container for a given data node.int
Returns the number of nodes known in this model.DataNode[]
getPathToRoot
(DataNode dataNode) Returns an array representing the position of the givenDataNode
in the tree.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
Indicates whether the node is a leaf, that is cannot have any children.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
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.
-
Constructor Details
-
DataNodeTreeModel
public DataNodeTreeModel()Constructs a new DataNodeTreeModel with a default root node. -
DataNodeTreeModel
Constructs a new DataNodeTreeModel with a given root node.- Parameters:
rootDataNode
- the root node
-
-
Method Details
-
getRoot
Returns the root of the tree. -
getChild
Returns a given child of a node in the tree. -
getChildCount
Returns the number of children a node currently has.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.
- Specified by:
getChildCount
in interfaceTreeModel
- Parameters:
parentDataNode
-DataNode
object in the tree to be queried- Returns:
- number of children
-
getCurrentChildren
Returns an array of the children currently owned by a given node. UnlikegetChildCount(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.- Parameters:
dataNode
- the node whose children are being enquired about- Returns:
- an array of
dataNode
's children
-
isLeaf
Indicates whether the node is a leaf, that is cannot have any children. Non-leaf nodes may still be childless though. -
getIndexOfChild
Returns the index of a given child if it is a direct child of another.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 theTreeModel
interface.- Specified by:
getIndexOfChild
in interfaceTreeModel
- Parameters:
parentDataNode
-DataNode
which is the parent objectchildDataNode
-DataNode
which is a child ofparentDataNode
- Returns:
- the child number of
childDataNode
withinparentDataNode
or -1 if it's not a child
-
valueForPathChanged
Messaged when the user has altered the value for an item.- Specified by:
valueForPathChanged
in interfaceTreeModel
- Parameters:
path
- path to the altered node (all the objects in the path must beDataNode
s)newDataNode
- theDataNode
which is now found atpath
-
addTreeModelListener
- Specified by:
addTreeModelListener
in interfaceTreeModel
-
removeTreeModelListener
- Specified by:
removeTreeModelListener
in interfaceTreeModel
-
getNodeCount
public int getNodeCount()Returns the number of nodes known in this model.- Returns:
- node count
-
containsNode
Indicates whether this model contains a given data node.- Returns:
true
iff this model containsnode
-
getPathToRoot
Returns an array representing the position of the givenDataNode
in the tree. The root is the first element in the returned array, anddataNode
is the last. The length of the returned array gives the node's depth in the tree. IfdataNode
does not exist in this model,null
is returned.- Parameters:
dataNode
- the node to find the path of- Returns:
- the path from the root to
dataNode
-
insertNode
Inserts a new node into the child list of a node which exists in this tree model. The model's listeners are notified. This method may be called from any thread, not just the event dispatch thread.- Parameters:
newChild
- the new data node to insert intoparent
's list of childrenparent
- the parent node in whose childrennewChild
should be insertedipos
- the position at which the insertion should take place
-
appendNode
Appends a new child node to the children of a node which exists in this tree model. The model's listeners are notified. This method may be called from any thread, not just the event dispatch thread.- Parameters:
newChild
- the new data node to add at the end ofparent
's list of childrenparent
- the parent node to whose childrennewChild
should be appended
-
removeNode
Removes a node from the tree. The model's listeners are notified. This method may be called from any thread, not just the event dispatch thread.- Parameters:
dataNode
- the node to remove
-
replaceNode
Replaces a given data node with a new one. This only works for non-root nodes. To replace the root node, usesetRoot(uk.ac.starlink.datanode.nodes.DataNode)
.- Parameters:
oldDataNode
- the node to be replacednewDataNode
- the node to replace it with
-
setRoot
Sets the root node of the tree.- Parameters:
rootDataNode
- the new root node
-
refreshNode
Effectively re-initialises a node. Its children are removed from the tree, and any subsequent attempts to read the children (triggered by getChildCount) will cause them to be re-acquired from the DataNode.- Parameters:
dataNode
- the node to refresh
-
repaintNode
Refreshes the representation of the node iteself. It is redrawn, but nothing is done about its children.- Parameters:
dataNode
- the node to repaint
-
stopExpansion
Stops a node from expanding. If the given node is currently undergoing expansion, calling this will stop it, leaving in place any children which have already been added to it. Processing work associated with the expansion should be stopped, though this may not happen immediately. If the given node is not undergoing expansion this method will have no effect.- Parameters:
dataNode
- the node whose expansion is to be stopped
-
getModelNode
Returns theTreeModelNode
which acts as the container for a given data node.- Parameters:
dataNode
- the data node whose model node is required- Returns:
dataNode
's container model node
-
makeModelNode
Creates a new TreeModelNode for use in this TreeModel. Note that this method should be used rather than the TreeModelNode constructor, since this model needs to be able to keep track of the nodes that have been created.- Parameters:
dataNode
- the DataNode managed by this TreeModelNodeparent
- the parent of this node (null for the root)
-