# Class DataNodeTreeModel

java.lang.Object
uk.ac.starlink.datanode.tree.DataNodeTreeModel

All Implemented Interfaces:
  `TreeModel`

public class DataNodeTreeModel extends Object implements TreeModel
A TreeModel for storing `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(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

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

Modifier and Type
Method
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.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### 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 Details

### getRoot

public Object getRoot()
Returns the root of the tree.

Specified by:
  `getRoot` in interface `TreeModel`
Returns:
  tree root `DataNode`

### getChild

public Object getChild(Object parentDataNode, int index)
Returns a given child of a node in the tree.

Specified by:
  `getChild` in interface `TreeModel`
Parameters:
  `parentDataNode` - parent `DataNode`
  `index` - index of the child of `parentDataNode` required
Returns:
  child `DataNode`, the `index`'th child of `parentDataNode`

### getChildCount

public int getChildCount(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 interface `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. Unlike `getChildCount(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(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 interface `TreeModel`
Parameters:
  `dataNode` - `DataNode` node to query
Returns:
  true iff `dataNode` cannot support children

### getIndexOfChild

public int getIndexOfChild(Object parentDataNode, 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.

Specified by:
  `getIndexOfChild` in interface `TreeModel`
Parameters:
  `parentDataNode` - `DataNode` which is the parent object
  `childDataNode` - `DataNode` which is a child of `parentDataNode`
Returns:
  the child number of `childDataNode` within `parentDataNode` or -1 if it's not a child

### valueForPathChanged

public void valueForPathChanged(TreePath path, Object newDataNode)
Messaged when the user has altered the value for an item.

Specified by:
  `valueForPathChanged` in interface `TreeModel`
Parameters:
  `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`

### addTreeModelListener

public void addTreeModelListener(TreeModelListener listener)

Specified by:
  `addTreeModelListener` in interface `TreeModel`

### removeTreeModelListener

public void removeTreeModelListener(TreeModelListener listener)

Specified by:
  `removeTreeModelListener` in interface `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 children
  `parent` - the parent node in whose children `newChild` should be inserted
  `ipos` - 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 children
  `parent` - 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, use `setRoot(DataNode)`.

Parameters:
  `oldDataNode` - the node to be replaced
  `newDataNode` - 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 TreeModelNode
  `parent` - the parent of this node (null for the root)