Class DataNodeTreeModel
- java.lang.Object
-
- uk.ac.starlink.datanode.tree.DataNodeTreeModel
-
- All Implemented Interfaces:
javax.swing.tree.TreeModel
public class DataNodeTreeModel extends java.lang.Object implements javax.swing.tree.TreeModel
A TreeModel for storingDataNode
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 thegetModelNode(uk.ac.starlink.datanode.nodes.DataNode)
method and manipulated directly for more direct control over the tree structure than is possibly by manipulating DataNodes.- Author:
- Mark Taylor
-
-
Constructor Summary
Constructors Constructor Description DataNodeTreeModel()
Constructs a new DataNodeTreeModel with a default root node.DataNodeTreeModel(DataNode rootDataNode)
Constructs a new DataNodeTreeModel with a given root node.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addTreeModelListener(javax.swing.event.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.java.lang.Object
getChild(java.lang.Object parentDataNode, int index)
Returns a given child of a node in the tree.int
getChildCount(java.lang.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(java.lang.Object parentDataNode, java.lang.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.java.lang.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(java.lang.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(javax.swing.event.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(javax.swing.tree.TreePath path, java.lang.Object newDataNode)
Messaged when the user has altered the value for an item.
-
-
-
Constructor Detail
-
DataNodeTreeModel
public DataNodeTreeModel()
Constructs a new DataNodeTreeModel with a default root node.
-
DataNodeTreeModel
public DataNodeTreeModel(DataNode rootDataNode)
Constructs a new DataNodeTreeModel with a given root node.- Parameters:
rootDataNode
- the root node
-
-
Method Detail
-
getRoot
public java.lang.Object getRoot()
Returns the root of the tree.- Specified by:
getRoot
in interfacejavax.swing.tree.TreeModel
- Returns:
- tree root
DataNode
-
getChild
public java.lang.Object getChild(java.lang.Object parentDataNode, int index)
Returns a given child of a node in the tree.
-
getChildCount
public int getChildCount(java.lang.Object parentDataNode)
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 interfacejavax.swing.tree.TreeModel
- Parameters:
parentDataNode
-DataNode
object in the tree to be queried- Returns:
- number of children
-
getCurrentChildren
public DataNode[] getCurrentChildren(DataNode dataNode)
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
public boolean isLeaf(java.lang.Object dataNode)
Indicates whether the node is a leaf, that is cannot have any children. Non-leaf nodes may still be childless though.- Specified by:
isLeaf
in interfacejavax.swing.tree.TreeModel
- Parameters:
dataNode
-DataNode
node to query- Returns:
- true iff dataNode cannot support children
-
getIndexOfChild
public int getIndexOfChild(java.lang.Object parentDataNode, java.lang.Object childDataNode)
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 the
TreeModel
interface.
-
valueForPathChanged
public void valueForPathChanged(javax.swing.tree.TreePath path, java.lang.Object newDataNode)
Messaged when the user has altered the value for an item.- Specified by:
valueForPathChanged
in interfacejavax.swing.tree.TreeModel
- Parameters:
path
- path to the altered node (all the objects in the path must be DataNodes)newDataNode
- theDataNode
which is now found at path
-
addTreeModelListener
public void addTreeModelListener(javax.swing.event.TreeModelListener listener)
- Specified by:
addTreeModelListener
in interfacejavax.swing.tree.TreeModel
-
removeTreeModelListener
public void removeTreeModelListener(javax.swing.event.TreeModelListener listener)
- Specified by:
removeTreeModelListener
in interfacejavax.swing.tree.TreeModel
-
getNodeCount
public int getNodeCount()
Returns the number of nodes known in this model.- Returns:
- node count
-
containsNode
public boolean containsNode(DataNode node)
Indicates whether this model contains a given data node.- Returns:
- true iff this model contains node
-
getPathToRoot
public DataNode[] getPathToRoot(DataNode dataNode)
Returns an array representing the position of the given 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.- Parameters:
dataNode
- the node to find the path of- Returns:
- the path from the root to dataNode
-
insertNode
public 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. 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 into parent's list of childrenparent
- the parent node in whose children newChild should be insertedipos
- the position at which the insertion should take place
-
appendNode
public void appendNode(DataNode newChild, DataNode parent)
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 of parent's list of childrenparent
- the parent node to whose children newChild should be appended
-
removeNode
public void removeNode(DataNode dataNode)
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
public void replaceNode(DataNode oldDataNode, DataNode newDataNode)
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
public void setRoot(DataNode rootDataNode)
Sets the root node of the tree.- Parameters:
rootDataNode
- the new root node
-
refreshNode
public void refreshNode(DataNode dataNode)
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
public void repaintNode(DataNode dataNode)
Refreshes the representation of the node iteself. It is redrawn, but nothing is done about its children.- Parameters:
dataNode
- the node to repaint
-
stopExpansion
public void stopExpansion(DataNode dataNode)
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
public TreeModelNode getModelNode(DataNode dataNode)
Returns the TreeModelNode 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
public TreeModelNode makeModelNode(DataNode dataNode, TreeModelNode parent)
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)
-
-