Copyright © 2005 Scientific Applications. All Rights Reserved.

com.sciapp.treetable
Class MutableTreeTableModel

java.lang.Object
  extended by com.sciapp.treetable.AbstractTreeTableModel
      extended by com.sciapp.treetable.MutableTreeTableModel
All Implemented Interfaces:
ReorderModel, TreeTableModel, Serializable, TreeModel
Direct Known Subclasses:
ComparableTreeTableModel, DirectoryTreeTableModel

public abstract class MutableTreeTableModel
extends AbstractTreeTableModel
implements ReorderModel

MutableTreeTableModel provides for a TreeTableModel whose nodes can be added or removed dynamically. You can use insertNodeInto to add a node and removeNodeFromParent to remove a node.

See Also:
Serialized Form

Field Summary
protected  Class[] classes
          the column classes
protected  String[] columns
          the column names
 
Fields inherited from class com.sciapp.treetable.AbstractTreeTableModel
listenerList, root
 
Fields inherited from interface com.sciapp.treetable.TreeTableModel
CHANGE_COLUMN_SOURCE
 
Constructor Summary
MutableTreeTableModel(String[] columns, Class[] classes, TreeNode root)
          Constructs a MutableTreeTableModel.
MutableTreeTableModel(String[] columns, TreeNode root)
          Constructs a MutableTreeTableModel.
MutableTreeTableModel(TreeNode root)
          Constructs a MutableTreeTableModel.
 
Method Summary
 void addColumn(String columnName)
          Adds a column to the model.
 void addColumn(String columnName, Class columnClass)
          Adds a column to the model.
 void addReorderListener(ReorderListener l)
          Adds a ReorderListener that is notified each time a change to the data model occurs so that the table updates the selected rows accordingly.
 void clear()
          Clears the model of any data.
 void fireRowsMapped(int[] changedIndices)
          Notifies listeners that the rows of the table have been reordered according to the changedIndices map.
 void fireRowsReordered(ReorderEvent event)
          Notifies listeners that the rows of the table have been reordered in some way.
 Object getChild(Object parent, int index)
          Returns the child of parent at index index in the parent's child array.
 int getChildCount(Object parent)
          Returns the number of children of parent.
 Class getColumnClass(int column)
          Returns the class of all the cells in the column.
 int getColumnCount()
          Returns the number of columns.
 String getColumnName(int column)
          Returns the name of the column at columnIndex as it will be displayed by the table
 int getIndexOfChild(Object parent, Object child)
          Returns the index of child in parent.
 TreeNode[] getPathToRoot(TreeNode aNode)
          Builds the parents of node up to and including the root node, where the original node is the last element in the returned array.
protected  TreeNode[] getPathToRoot(TreeNode aNode, int depth)
          Builds the parents of node up to and including the root node, where the original node is the last element in the returned array.
 void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index)
          Inserts a node at a specific location index in its parents children.
 boolean isCellEditable(Object node, int column)
          Returns true if node at columnIndex is editable.
 boolean isLeaf(Object node)
          Returns true if node is a leaf.
 void moveNode(MutableTreeNode fromNode, int startIndex, int endIndex, int toIndex)
          Moves one or more children nodes of fromNode starting at startIndex to endIndex to the toIndex.
 void nodeChanged(TreeNode node)
          This method should be invoked when the representation of node in the tree has changed.
 void nodesChanged(TreeNode node, int[] childIndices)
          This method should be invoked when the representation of the children identified by childIndices in the tree has changed.
 void nodeStructureChanged(TreeNode node)
          This method should be invoked when the children of node and its children children have completely changed.
 void nodesWereInserted(TreeNode node, int[] childIndices)
          Invoke this method after you've inserted some TreeNodes into node.
 void nodesWereRemoved(TreeNode node, int[] childIndices, Object[] removedChildren)
          Invoke this method after you've removed some TreeNodes from node.
 void removeNodeFromParent(MutableTreeNode node)
          Removes a node from its parent.
 void removeReorderListener(ReorderListener l)
          Removes a listener from the list that is notified each time a reorder event is generated.
 void setColumnClasses(Class[] newColumnClasses)
          Assigns the column classes.
 void setColumnNames(String[] newNames)
          Assigns the column names.
 void setValueAt(Object aValue, Object node, int column)
          Sets the value for node at columnIndex to aValue.
 void valueForPathChanged(TreePath path, Object newValue)
          Messaged when the user has altered the value for the item identified by path to newValue.
 
Methods inherited from class com.sciapp.treetable.AbstractTreeTableModel
addTreeModelListener, fireTreeNodesChanged, fireTreeNodesInserted, fireTreeNodesRemoved, fireTreeStructureChanged, fireTreeTableStructureChanged, fireTreeTableStructureChanged, getRoot, isAggregate, isFooter, isHeader, removeTreeModelListener, setRoot
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.sciapp.treetable.TreeTableModel
getValueAt
 

Field Detail

columns

protected String[] columns
the column names


classes

protected Class[] classes
the column classes

Constructor Detail

MutableTreeTableModel

public MutableTreeTableModel(String[] columns,
                             Class[] classes,
                             TreeNode root)
Constructs a MutableTreeTableModel.


MutableTreeTableModel

public MutableTreeTableModel(String[] columns,
                             TreeNode root)
Constructs a MutableTreeTableModel.


MutableTreeTableModel

public MutableTreeTableModel(TreeNode root)
Constructs a MutableTreeTableModel.

Method Detail

addColumn

public void addColumn(String columnName)
Adds a column to the model.

Parameters:
columnName - the name of the column to add.

addColumn

public void addColumn(String columnName,
                      Class columnClass)
Adds a column to the model.

Parameters:
columnName - the name of the column to add.
columnClass - the class of the column to add.

addReorderListener

public void addReorderListener(ReorderListener l)
Adds a ReorderListener that is notified each time a change to the data model occurs so that the table updates the selected rows accordingly.

Specified by:
addReorderListener in interface ReorderModel
Parameters:
l - the ReorderListener

clear

public void clear()
Clears the model of any data. The root must be a MutableTreeNode, otherwise an exception is thrown.


fireRowsMapped

public void fireRowsMapped(int[] changedIndices)
Notifies listeners that the rows of the table have been reordered according to the changedIndices map.

Specified by:
fireRowsMapped in interface ReorderModel
Parameters:
changedIndices - int array indicating how the rows have been reordered.
See Also:
ReorderEvent

fireRowsReordered

public void fireRowsReordered(ReorderEvent event)
Notifies listeners that the rows of the table have been reordered in some way.

Specified by:
fireRowsReordered in interface ReorderModel
Parameters:
event - the reorder event.
See Also:
ReorderEvent

getChild

public Object getChild(Object parent,
                       int index)
Returns the child of parent at index index in the parent's child array. parent must be a node previously obtained from this data source. This should not return null if index is a valid index for parent (that is index >= 0 && index < getChildCount(parent)).

Specified by:
getChild in interface TreeModel
Parameters:
parent - a node in the tree, obtained from this data source
Returns:
the child of parent at index index

getChildCount

public int getChildCount(Object parent)
Returns the number of children of parent. Returns 0 if the node is a leaf or if it has no children. parent must be a node previously obtained from this data source.

Specified by:
getChildCount in interface TreeModel
Parameters:
parent - a node in the tree, obtained from this data source
Returns:
the number of children of the node parent

getColumnClass

public Class getColumnClass(int column)
Returns the class of all the cells in the column.

Specified by:
getColumnClass in interface TreeTableModel
Parameters:
column - the index of the column
Returns:
the class of all the cells in the column.

getColumnCount

public int getColumnCount()
Returns the number of columns.

Specified by:
getColumnCount in interface TreeTableModel
Returns:
the number of columns.

getColumnName

public String getColumnName(int column)
Returns the name of the column at columnIndex as it will be displayed by the table

Specified by:
getColumnName in interface TreeTableModel
Parameters:
column - the index of the column
Returns:
the name of the column

getIndexOfChild

public int getIndexOfChild(Object parent,
                           Object child)
Returns the index of child in parent. If parent is null or child is null, returns -1.

Specified by:
getIndexOfChild in interface TreeModel
Parameters:
parent - a note in the tree, obtained from this data source
child - the node we are interested in
Returns:
the index of the child in the parent, or -1 if either child or parent are null

getPathToRoot

public TreeNode[] getPathToRoot(TreeNode aNode)
Builds the parents of node up to and including the root node, where the original node is the last element in the returned array. The length of the returned array gives the node's depth in the tree.


getPathToRoot

protected TreeNode[] getPathToRoot(TreeNode aNode,
                                   int depth)
Builds the parents of node up to and including the root node, where the original node is the last element in the returned array. The length of the returned array gives the node's depth in the tree.


insertNodeInto

public void insertNodeInto(MutableTreeNode newChild,
                           MutableTreeNode parent,
                           int index)
Inserts a node at a specific location index in its parents children. The appropriate event is also created.

Parameters:
newChild - the node to insert
parent - the parent node to remove node from
index - the location in the parent node's childrens

isCellEditable

public boolean isCellEditable(Object node,
                              int column)
Returns true if node at columnIndex is editable.

Specified by:
isCellEditable in interface TreeTableModel
Parameters:
node - the node to consider
column - the column index
Returns:
true if the node at columnIndex is editable, false otherwise

isLeaf

public boolean isLeaf(Object node)
Returns true if node is a leaf. It is possible for this method to return false even if node has no children. A directory in a filesystem, for example, may contain no files; the node representing the directory is not a leaf, but it also has no children.

Specified by:
isLeaf in interface TreeModel
Parameters:
node - a node in the tree, obtained from this data source
Returns:
true if node is a leaf

moveNode

public void moveNode(MutableTreeNode fromNode,
                     int startIndex,
                     int endIndex,
                     int toIndex)
Moves one or more children nodes of fromNode starting at startIndex to endIndex to the toIndex. This method will also send a nodeStructureChanged notification message to all the listeners.

Parameters:
fromNode - the node whose children are to be moved
startIndex - the starting child index to be moved
endIndex - the ending child index to be moved
toIndex - the destination of the children nodes to be moved
Throws:
ArrayIndexOutOfBoundsException - if any of the indices are out of range. Or if endIndex is less than startIndex.

nodeChanged

public void nodeChanged(TreeNode node)
This method should be invoked when the representation of node in the tree has changed.


nodesChanged

public void nodesChanged(TreeNode node,
                         int[] childIndices)
This method should be invoked when the representation of the children identified by childIndices in the tree has changed.


nodeStructureChanged

public void nodeStructureChanged(TreeNode node)
This method should be invoked when the children of node and its children children have completely changed. This will post a treeStructureChanged event.


nodesWereInserted

public void nodesWereInserted(TreeNode node,
                              int[] childIndices)
Invoke this method after you've inserted some TreeNodes into node. childIndices should be the index of the new elements and must be sorted in ascending order.


nodesWereRemoved

public void nodesWereRemoved(TreeNode node,
                             int[] childIndices,
                             Object[] removedChildren)
Invoke this method after you've removed some TreeNodes from node. childIndices should be the index of the removed elements and must be sorted in ascending order. And removedChildren should be the array of the children objects that were removed.


removeNodeFromParent

public void removeNodeFromParent(MutableTreeNode node)
Removes a node from its parent. The appropriate event is also created.

Parameters:
node - the node to remove

removeReorderListener

public void removeReorderListener(ReorderListener l)
Removes a listener from the list that is notified each time a reorder event is generated.

Specified by:
removeReorderListener in interface ReorderModel
Parameters:
l - the RearrangeListener

setColumnClasses

public void setColumnClasses(Class[] newColumnClasses)
Assigns the column classes.

Parameters:
newColumnClasses - the column classes to assign as an array.

setColumnNames

public void setColumnNames(String[] newNames)
Assigns the column names.

Parameters:
newNames - the column names to assign as an array.

setValueAt

public void setValueAt(Object aValue,
                       Object node,
                       int column)
Sets the value for node at columnIndex to aValue.

Specified by:
setValueAt in interface TreeTableModel
Parameters:
aValue - the new value
node - the node whose value is to be changed
column - the column whose value is to be changed

valueForPathChanged

public void valueForPathChanged(TreePath path,
                                Object newValue)
Messaged when the user has altered the value for the item identified by path to newValue. If newValue signifies a truly new value the model should post a treeNodesChanged event.

Specified by:
valueForPathChanged in interface TreeModel
Parameters:
path - path to the node that the user has altered
newValue - the new value from the TreeCellEditor

Copyright © 2005 Scientific Applications. All Rights Reserved.