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.TreeModelA TreeModel for storingDataNodes. 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 voidaddTreeModelListener(javax.swing.event.TreeModelListener listener)voidappendNode(DataNode newChild, DataNode parent)Appends a new child node to the children of a node which exists in this tree model.booleancontainsNode(DataNode node)Indicates whether this model contains a given data node.java.lang.ObjectgetChild(java.lang.Object parentDataNode, int index)Returns a given child of a node in the tree.intgetChildCount(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.intgetIndexOfChild(java.lang.Object parentDataNode, java.lang.Object childDataNode)Returns the index of a given child if it is a direct child of another.TreeModelNodegetModelNode(DataNode dataNode)Returns the TreeModelNode which acts as the container for a given data node.intgetNodeCount()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.ObjectgetRoot()Returns the root of the tree.voidinsertNode(DataNode newChild, DataNode parent, int ipos)Inserts a new node into the child list of a node which exists in this tree model.booleanisLeaf(java.lang.Object dataNode)Indicates whether the node is a leaf, that is cannot have any children.TreeModelNodemakeModelNode(DataNode dataNode, TreeModelNode parent)Creates a new TreeModelNode for use in this TreeModel.voidrefreshNode(DataNode dataNode)Effectively re-initialises a node.voidremoveNode(DataNode dataNode)Removes a node from the tree.voidremoveTreeModelListener(javax.swing.event.TreeModelListener listener)voidrepaintNode(DataNode dataNode)Refreshes the representation of the node iteself.voidreplaceNode(DataNode oldDataNode, DataNode newDataNode)Replaces a given data node with a new one.voidsetRoot(DataNode rootDataNode)Sets the root node of the tree.voidstopExpansion(DataNode dataNode)Stops a node from expanding.voidvalueForPathChanged(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:
getRootin 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:
getChildCountin interfacejavax.swing.tree.TreeModel- Parameters:
parentDataNode-DataNodeobject 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:
isLeafin interfacejavax.swing.tree.TreeModel- Parameters:
dataNode-DataNodenode 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
TreeModelinterface.
-
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:
valueForPathChangedin interfacejavax.swing.tree.TreeModel- Parameters:
path- path to the altered node (all the objects in the path must be DataNodes)newDataNode- theDataNodewhich is now found at path
-
addTreeModelListener
public void addTreeModelListener(javax.swing.event.TreeModelListener listener)
- Specified by:
addTreeModelListenerin interfacejavax.swing.tree.TreeModel
-
removeTreeModelListener
public void removeTreeModelListener(javax.swing.event.TreeModelListener listener)
- Specified by:
removeTreeModelListenerin 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)
-
-