Copyright © 2005 Scientific Applications. All Rights Reserved.

com.sciapp.treetable
Class DynamicTreeTableModel

java.lang.Object
  extended by com.sciapp.treetable.AbstractTreeTableModel
      extended by com.sciapp.treetable.TreeModelMap
          extended by com.sciapp.treetable.DynamicTreeTableModel
All Implemented Interfaces:
ReorderListener, ReorderModel, TreeTableModel, Serializable, EventListener, TableModelListener, TreeModelListener, TreeModel

public class DynamicTreeTableModel
extends TreeModelMap
implements TableModelListener, ReorderModel, ReorderListener

DynamicTreeTableModel wraps around a ListTableModel instance in order to provide a tree-like view of the table's data.

Rows are grouped together if they are found to be equal by the row comparators that are defined. Row comparators can be added/removed via the addRowComparator and removeRowComparator methods respectively. In the simplest of cases, a DefaultTreeTableComparator may be used, which compares rows by using a single column. However, more complex schemes can be employed, such as comparing using more than one columns.

A DynamicTreeTableModel builds a tree structure every time the data is changed. This tree comprises of nodes represented by TreeTableRow objects that are divided in:

  1. DataRow objects: nodes that are associated with actual data of the underlying ListTableModel. These nodes cannot have children and the values that are shown on the table depend on the object they are referring to.
  2. AggregateRow objects: nodes that are not associated with the data of the underlying ListTableModel, but that provide information about several rows of the table. These are further classifed into:

The values returned by aggregate rows are defined by Aggregator objects (see Aggregator).

See Also:
Serialized Form

Field Summary
protected  ArrayList comparators
          the list of row comparators.
protected  Aggregator defaultAggregator
          the default aggregator
protected  Hashtable defaultComparatorsByColumnClass
          A table of objects that compare two object with each other, indexed by class
protected  Footer footer
          the footer object
protected  ListTableModel tableModel
           
 
Fields inherited from class com.sciapp.treetable.TreeModelMap
treeModel
 
Fields inherited from class com.sciapp.treetable.AbstractTreeTableModel
listenerList, root
 
Fields inherited from interface com.sciapp.treetable.TreeTableModel
CHANGE_COLUMN_SOURCE
 
Constructor Summary
DynamicTreeTableModel(ListTableModel tableModel)
          DynamicTreeTableModel constructor comment.
 
Method Summary
 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 addRow(Object row)
          Adds a row to the end of the model.
 void addRowComparator(TreeTableComparator newComparator)
          Adds a new row comparator at the end of the list of comparators.
 void addRows(List addedRows)
          Adds a list of rows to the end of the model.
protected  void buildFooter()
          Creates and adds FooterRows to the tree.
protected  void buildTree()
          Creates the tree based on the data supplied by the underlying ListTableModel.
 void clear()
          Clears the model of any data.
 void clearComparators()
          Removes all row comparators and rebuilds the tree.
protected  Aggregator createDefaultAggregator()
          Creates and returns the aggregator that will be used by default.
protected  void createDefaultComparators()
          Creates default comparators for objects, strings, date and boolean values.
protected  Footer createDefaultFooter()
          Creates and returns the default footer.
 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.
 Aggregator getAggregator(int columnIndex)
          Returns the aggregator for columnIndex.
 Object getCellValue(Object row, int index)
          Returns the Object found at index of row passed as a parameter.
 Class getColumnClass(int c)
          Returns the class of all the cells in the column.
 int getColumnCount()
          Returns the number of columns.
 String getColumnName(int columnIndex)
          Returns the name of the column at columnIndex as it will be displayed by the table
 int getDataRow(TreeTableRow node)
          Returns the index that corresponds to the object of the ListTableModel that holds the actual tabular data, for the node node.
 int[] getDataRows(TreeTableRow node)
          Returns the indexes that correspond to the objects of the ListTableModel that holds the actual tabular data, that are children of the TreeTableRow for the node node.
 Aggregator getDefaultAggregator()
          Returns the default aggregator that will be used to compute values for the aggregate rows of the model.
 Comparator getDefaultComparator(Class columnClass)
          Returns the comparator to use for objects of the specified columnClass.
 Footer getFooter()
          Returns the footer associated with this TreeTableModel instance.
 ListTableModel getModel()
          Returns the underlying ListTableModel model
 int[] getModelIndexesUnderRow(TreeTableRow row, boolean sorted)
          Returns the indexes that correspond to the objects of the ListTableModel that holds the actual tabular data, that are children of the TreeTableRow row.
 TreeTableComparator getRowComparator(int index)
          Returns the row comparator that will be used to determine row similarity at index.
 TreeTableComparator[] getRowComparators()
          Returns the list of row comparators as an array.
 int getRowComparatorSize()
          Returns the size of the currently defined row comparators of this treetable.
 Object getValueAt(Object n, int columnIndex)
          Returns the value to be displayed for node, at column index column.
 void insertRowComparator(TreeTableComparator newComparator, int index)
          Inserts a row comparator at index in the list of row comparators of this model.
 boolean isAggregate(Object node)
          Determines if node is an aggregate row.
 boolean isCellEditable(Object node, int columnIndex)
          Returns true if node at columnIndex is editable.
 boolean isFooter(Object node)
          Determines if node is a footer.
 boolean isHeader(Object node)
          Determines if node is a header.
 void loadComparators(ObjectInputStream in)
          Restores the TreeTableComparators and assigns them on the model.
 void refresh()
          Forces a re-creation of the entire tree.
 void removeReorderListener(ReorderListener l)
          Removes a listener from the list that is notified each time a reorder event is generated.
 void removeRow(TreeTableRow row)
          Removes row from the model.
 TreeTableComparator removeRowComparator(int index)
          Removes the row comparator that is located at index.
 boolean removeRowComparator(TreeTableComparator comparator)
          Removes a comparator from the list of comparators of this TreeTableModel.
 void removeRows(TreeTableRow[] rows)
          Removes a list of rows identified by rows from the model.
 void rowsReordered(ReorderEvent e)
          Called whenever the rows of a JTable have been restructured.
 void saveComparators(ObjectOutputStream out)
          Saves the TreeTableComparators that are currently installed on the model.
 void setDefaultAggregator(Aggregator aggregator)
          Sets the default aggregator that will be used to compute values for the aggregate rows of the model.
 void setDefaultComparator(Class columnClass, Comparator comparator)
          Sets the default comparator for columnClass.
 void setFooter(Footer footer)
          Sets a footer instance to be used as the model's default footer.
 void setModel(ListTableModel newModel)
          Sets the underlying ListTableModel to newModel.
 TreeTableComparator setRowComparator(TreeTableComparator newComparator, int index)
          Sets a treetable comparator at index.
 void setValueAt(Object aValue, Object node, int columnIndex)
          Sets the value for node at columnIndex to aValue.
 void tableChanged(TableModelEvent e)
          This fine grain notification tells listeners the exact range of cells, rows, or columns that changed.
 
Methods inherited from class com.sciapp.treetable.TreeModelMap
getChild, getChildCount, getIndexOfChild, getPathToRoot, getPathToRoot, getRoot, getTreeModel, isLeaf, nodeChanged, nodesChanged, nodeStructureChanged, setTreeModel, treeNodesChanged, treeNodesInserted, treeNodesRemoved, treeStructureChanged, valueForPathChanged
 
Methods inherited from class com.sciapp.treetable.AbstractTreeTableModel
addTreeModelListener, fireTreeNodesChanged, fireTreeNodesInserted, fireTreeNodesRemoved, fireTreeStructureChanged, fireTreeTableStructureChanged, fireTreeTableStructureChanged, removeTreeModelListener, setRoot
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tableModel

protected ListTableModel tableModel

footer

protected transient Footer footer
the footer object


comparators

protected ArrayList comparators
the list of row comparators.


defaultAggregator

protected transient Aggregator defaultAggregator
the default aggregator


defaultComparatorsByColumnClass

protected transient Hashtable defaultComparatorsByColumnClass
A table of objects that compare two object with each other, indexed by class

Constructor Detail

DynamicTreeTableModel

public DynamicTreeTableModel(ListTableModel tableModel)
DynamicTreeTableModel constructor comment.

Parameters:
tableModel - com.sciapp.table.ListTableModel
Method Detail

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

addRow

public void addRow(Object row)
Adds a row to the end of the model.

Parameters:
row - the row being added

addRowComparator

public void addRowComparator(TreeTableComparator newComparator)
Adds a new row comparator at the end of the list of comparators.

Parameters:
newComparator - the new row comparator to add

addRows

public void addRows(List addedRows)
Adds a list of rows to the end of the model.

addedRows contains objects representing the actual rows being added. e.g. java.util.Vector, for DefaultTableModel.

Parameters:
addedRows - the rows being added.

buildFooter

protected void buildFooter()
Creates and adds FooterRows to the tree. If the footer instance is null, this method returns immediately.


buildTree

protected void buildTree()
Creates the tree based on the data supplied by the underlying ListTableModel. This method does not create the footers. This is done with buildFooter().


clear

public void clear()
Clears the model of any data.


clearComparators

public void clearComparators()
Removes all row comparators and rebuilds the tree.


createDefaultAggregator

protected Aggregator createDefaultAggregator()
Creates and returns the aggregator that will be used by default. This implementation returns a DefaultCellAggregator.

Returns:
a new instance of the default aggregator

createDefaultComparators

protected void createDefaultComparators()
Creates default comparators for objects, strings, date and boolean values. These comparators can be used by TreeTableComparators, such as DefaultTreeTableComparator in order to group the rows of the TreeTable.


createDefaultFooter

protected Footer createDefaultFooter()
Creates and returns the default footer. This method returns null by default. You need to override to supply your own footer, or you can use setFooter(com.sciapp.treetable.Footer).

Returns:
a new instance of the default footer

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

getAggregator

public Aggregator getAggregator(int columnIndex)
Returns the aggregator for columnIndex. This method returns the default aggregator by default. You will need to override it if you desire a more complex behaviour.

Parameters:
columnIndex - the index of the column
Returns:
the aggregator at columnIndex

getCellValue

public Object getCellValue(Object row,
                           int index)
Returns the Object found at index of row passed as a parameter. This is an actual call to the underlying list tablemodel.

Parameters:
row - the row data
index - an index
Returns:
the object found at column index of the row data row

getColumnClass

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

Specified by:
getColumnClass in interface TreeTableModel
Parameters:
c - 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 columnIndex)
Returns the name of the column at columnIndex as it will be displayed by the table

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

getDataRow

public int getDataRow(TreeTableRow node)
Returns the index that corresponds to the object of the ListTableModel that holds the actual tabular data, for the node node. If this node is an aggregate row, this method returns -1.

Parameters:
node - the node whose model index we want returned.
Returns:
a model index

getDataRows

public int[] getDataRows(TreeTableRow node)
Returns the indexes that correspond to the objects of the ListTableModel that holds the actual tabular data, that are children of the TreeTableRow for the node node. If this node is not an aggregate row, then an array of one integer is returned, which contains the modelIndex of that node.

Parameters:
node - the node under which all model indexes are returned.
Returns:
an integer array of model indexes

getDefaultAggregator

public Aggregator getDefaultAggregator()
Returns the default aggregator that will be used to compute values for the aggregate rows of the model.

Returns:
the default aggregator

getDefaultComparator

public Comparator getDefaultComparator(Class columnClass)
Returns the comparator to use for objects of the specified columnClass. If there is no entry for this columnClass, this method returns the entry for the most specific superclass. TreeTableModel installs entries for Object, String, Date and Boolean, all of which can be modified or replaced.

Parameters:
columnClass - the class for which we want to find a comparator
Returns:
the comparator for this columnClass

getFooter

public Footer getFooter()
Returns the footer associated with this TreeTableModel instance.

Returns:
the footer of the TreeTableModel

getModel

public ListTableModel getModel()
Returns the underlying ListTableModel model

Returns:
the underlying ListTableModel model

getModelIndexesUnderRow

public int[] getModelIndexesUnderRow(TreeTableRow row,
                                     boolean sorted)
Returns the indexes that correspond to the objects of the ListTableModel that holds the actual tabular data, that are children of the TreeTableRow row. If row is not an aggregate row, then an array of one integer is returned, which contains the modelIndex of that row.

Parameters:
row - the node under which all model indexes are returned.
sorted - if this is true, the integer array is sorted, otherwise the array contains the model indexes as they appear on the table.
Returns:
an array of model indexes

getRowComparator

public TreeTableComparator getRowComparator(int index)
Returns the row comparator that will be used to determine row similarity at index.

Returns:
the row comparator at index

getRowComparators

public TreeTableComparator[] getRowComparators()
Returns the list of row comparators as an array.

Returns:
an array of row comparators

getRowComparatorSize

public int getRowComparatorSize()
Returns the size of the currently defined row comparators of this treetable.

Returns:
the size of the list of row comparators

getValueAt

public Object getValueAt(Object n,
                         int columnIndex)
Returns the value to be displayed for node, at column index column.

Specified by:
getValueAt in interface TreeTableModel
Parameters:
n - the node to query
columnIndex - the column index
Returns:
the value

insertRowComparator

public void insertRowComparator(TreeTableComparator newComparator,
                                int index)
Inserts a row comparator at index in the list of row comparators of this model.

Parameters:
newComparator - the new row comparator to insert
index - the index at which we want the comparator inserted

isAggregate

public boolean isAggregate(Object node)
Determines if node is an aggregate row.

Specified by:
isAggregate in interface TreeTableModel
Overrides:
isAggregate in class AbstractTreeTableModel
Parameters:
node - the node to consider
Returns:
true if node is an aggregate row, false otherwise

isCellEditable

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

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

isFooter

public boolean isFooter(Object node)
Determines if node is a footer.

Specified by:
isFooter in interface TreeTableModel
Overrides:
isFooter in class AbstractTreeTableModel
Parameters:
node - the row to consider
Returns:
true if node is a footer, false otherwise

isHeader

public boolean isHeader(Object node)
Determines if node is a header.

Specified by:
isHeader in interface TreeTableModel
Overrides:
isHeader in class AbstractTreeTableModel
Parameters:
node - the row to query
Returns:
true if node is a header, false otherwise

loadComparators

public void loadComparators(ObjectInputStream in)
Restores the TreeTableComparators and assigns them on the model. The comparators were serialized and saved to an ObjectOutputStream with the saveComparators method.

Parameters:
in - the object input stream to read from in order to construct the comparators.

refresh

public void refresh()
Forces a re-creation of the entire tree. This method will also rebuild footers and headers.


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

removeRow

public void removeRow(TreeTableRow row)
Removes row from the model.

Parameters:
row - the node to remove.

removeRowComparator

public TreeTableComparator removeRowComparator(int index)
Removes the row comparator that is located at index.

Parameters:
index - the location of the comparator we want removed
Returns:
the row comparator that was removed

removeRowComparator

public boolean removeRowComparator(TreeTableComparator comparator)
Removes a comparator from the list of comparators of this TreeTableModel.

Parameters:
comparator - the comparator to remove
Returns:
true if the comparator was found and removed from the list

removeRows

public void removeRows(TreeTableRow[] rows)
Removes a list of rows identified by rows from the model.

Parameters:
rows - the nodes to remove.

rowsReordered

public void rowsReordered(ReorderEvent e)
Called whenever the rows of a JTable have been restructured.

Specified by:
rowsReordered in interface ReorderListener
Parameters:
e - the ReorderEvent object

saveComparators

public void saveComparators(ObjectOutputStream out)
Saves the TreeTableComparators that are currently installed on the model. A comparator must be serializable, otherwise a NotSerializableException is thrown. The comparators can be restored with the loadComparators method.

Parameters:
out - the object output stream to write to when saving the comparators state.

setDefaultAggregator

public void setDefaultAggregator(Aggregator aggregator)
Sets the default aggregator that will be used to compute values for the aggregate rows of the model.

Parameters:
aggregator - the aggregator to assign as the default

setDefaultComparator

public void setDefaultComparator(Class columnClass,
                                 Comparator comparator)
Sets the default comparator for columnClass.

Parameters:
columnClass - the Class for which a comparator we want to set
comparator - the comparator to set

setFooter

public void setFooter(Footer footer)
Sets a footer instance to be used as the model's default footer.

Parameters:
footer - the footer object to assign to this TreeTableModel

setModel

public void setModel(ListTableModel newModel)
Sets the underlying ListTableModel to newModel.

Parameters:
newModel - the new ListTableModel to set

setRowComparator

public TreeTableComparator setRowComparator(TreeTableComparator newComparator,
                                            int index)
Sets a treetable comparator at index.

Parameters:
newComparator - the new comparator to assign
index - set the new comparator at index
Returns:
the old comparator assigned at index

setValueAt

public void setValueAt(Object aValue,
                       Object node,
                       int columnIndex)
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
columnIndex - the column whose value is to be changed

tableChanged

public void tableChanged(TableModelEvent e)
This fine grain notification tells listeners the exact range of cells, rows, or columns that changed.

Specified by:
tableChanged in interface TableModelListener

Copyright © 2005 Scientific Applications. All Rights Reserved.