com.sciapp.tree
Class TreeTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.sciapp.table.ListTableMap
          extended by com.sciapp.tree.TreeTableModel
All Implemented Interfaces:
ReorderListener, ListTableModel, ReorderModel, Serializable, EventListener, TableModelListener, TableModel

public class TreeTableModel
extends ListTableMap

TreeTableModel is the tablemodel that the TreeTable class uses. TreeTableModel 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 TreeTableModel 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).


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  HeaderRow root
          the root object of the tree structure
protected  JTree tree
          the tree structure created every time the data changes
protected  DefaultTreeModel treeModel
          the model of the tree
 
Fields inherited from class com.sciapp.table.ListTableMap
tableModel
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
TreeTableModel(ListTableModel tableModel)
          Constructs a TreeTableModel with tableModel as the underlying tablemodel holding the data.
 
Method Summary
 void addRowComparator(TreeTableComparator newComparator)
          Adds a new row comparator at the end of the list of comparators.
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 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.
protected  void fireSuperTableDataChanged()
          Notifies all listeners that all cell values in the table's rows may have changed.
 void fireTableChanged(TableModelEvent e)
          Forwards the given notification event to all TableModelListeners that registered themselves as listeners for this table model.
 Aggregator getAggregator(int rowIndex, int columnIndex)
          Returns the aggregator at rowIndex and columnIndex.
 int getDataRow(int rowIndex)
          Returns the index that corresponds to the object of the ListTableModel that holds the actual tabular data, that is found at row rowIndex.
 int[] getDataRows(int rowIndex)
          Returns the indexes that correspond to the objects of the ListTableModel that holds the actual tabular data, that are children of the TreeTableRow found at row rowIndex.
 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.
 int getLevel(int rowIndex)
          Returns the level of the TreeTableRow object at rowIndex below the root node of the tree structure of the model.
 int[] getModelIndexesUnderRow(int 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 found at row row.
 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.
 int getRowCount()
          Returns tableModel.getRowCount
 List getRows()
          Returns tableModel.getRows Returns a list containing the TreeTableRows of the model.
 TreeTableRow getTreeRow(int rowIndex)
          Returns the TreeTableRow found at row rowIndex of the TreeTable.
 Object getValueAt(int rowIndex, int columnIndex)
          Returns the value for the cell at columnIndex and rowIndex.
 void insertRowComparator(TreeTableComparator newComparator, int index)
          Inserts a row comparator at index in the list of row comparators of this model.
 boolean isAggregate(int rowIndex)
          Determines if the row at rowIndex is an aggregate row.
 boolean isCellEditable(int rowIndex, int columnIndex)
          Returns false if the row at rowIndex is an aggregate row, otherwise returns the value of its super method.
 boolean isFooter(int rowIndex)
          Determines if the row at rowIndex is a footer.
 boolean isHeader(int rowIndex)
          Determines if the row at rowIndex is a header.
 void refresh()
          Forces a re-creation of the entire tree.
 void removeRow(int row)
          Removes a row from the data 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(int[] rows)
          Removes a few rows from the data model
 void rowsReordered(ReorderEvent e)
          Called whenever the rows of a JTable have been restructured.
 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.
 TreeTableComparator setRowComparator(TreeTableComparator newComparator, int index)
          Sets a TreeTable comparator at index.
 void setValueAt(Object aValue, int rowIndex, int columnIndex)
          Sets the value in the cell at columnIndex and rowIndex to aValue.
 
Methods inherited from class com.sciapp.table.ListTableMap
addReorderListener, addRow, addRows, clear, fireRowsReordered, getCellValue, getColumnClass, getColumnCount, getColumnName, getInnerModel, getModel, removeReorderListener, setModel, tableChanged
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.sciapp.table.ListTableModel
fireTableCellUpdated, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, removeTableModelListener
 

Field Detail

footer

protected transient Footer footer
the footer object


tree

protected JTree tree
the tree structure created every time the data changes


treeModel

protected DefaultTreeModel treeModel
the model of the tree


root

protected HeaderRow root
the root object of the tree structure


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

TreeTableModel

public TreeTableModel(ListTableModel tableModel)
Constructs a TreeTableModel with tableModel as the underlying tablemodel holding the data.

Method Detail

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

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().


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.tree.Footer).

Returns:
a new instance of the default footer

fireSuperTableDataChanged

protected void fireSuperTableDataChanged()
Notifies all listeners that all cell values in the table's rows may have changed. One of the listeners is normally the table itself.


fireTableChanged

public void fireTableChanged(TableModelEvent e)
Forwards the given notification event to all TableModelListeners that registered themselves as listeners for this table model.

Specified by:
fireTableChanged in interface ListTableModel
Overrides:
fireTableChanged in class AbstractTableModel
Parameters:
e - the event to be forwarded
See Also:
AbstractTableModel.addTableModelListener(javax.swing.event.TableModelListener), TableModelEvent, EventListenerList

getAggregator

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

Parameters:
rowIndex - the index of the row
columnIndex - the index of the column
Returns:
the aggregator at (rowIndex, columnIndex)

getDataRow

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

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

getDataRows

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

Parameters:
rowIndex - the index of the node under which all model indexes are returned.
Returns:
an 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

getLevel

public int getLevel(int rowIndex)
Returns the level of the TreeTableRow object at rowIndex below the root node of the tree structure of the model. If the TreeTableRow object is not a header row, this method returns 0. i.e. the first header row is at level 1, the next header below that is at level 2, etc.

Parameters:
rowIndex - the row index at which the TreeTableRow is located
Returns:
the level of the TreeTableRow at rowIndex

getModelIndexesUnderRow

public int[] getModelIndexesUnderRow(int 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 found at row row. If the TreeTable object at row is not an aggregate row, then an array of one integer is returned, which contains the modelIndex of that row.

Parameters:
row - the index of 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

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

getRowCount

public int getRowCount()
Returns tableModel.getRowCount

Specified by:
getRowCount in interface TableModel
Overrides:
getRowCount in class ListTableMap
Returns:
the number of rows in the model
See Also:
TableModel.getColumnCount()

getRows

public List getRows()
Returns tableModel.getRows Returns a list containing the TreeTableRows of the model.

Specified by:
getRows in interface ListTableModel
Overrides:
getRows in class ListTableMap
Returns:
the rows of the datamodel as a java.util.List

getTreeRow

public TreeTableRow getTreeRow(int rowIndex)
Returns the TreeTableRow found at row rowIndex of the TreeTable.

Parameters:
rowIndex - the row of the table
Returns:
the TreeTableRow object at rowIndex

getValueAt

public Object getValueAt(int rowIndex,
                         int columnIndex)
Returns the value for the cell at columnIndex and rowIndex. If the row at rowIndex is an aggregate row, the aggregator at (rowIndex, columnIndex) is retrieved and used to return the aggregate value. Otherwise, the value of the object at the specified cell is returned as normal.

Specified by:
getValueAt in interface TableModel
Overrides:
getValueAt in class ListTableMap
Parameters:
rowIndex - the row whose value is to be queried
columnIndex - the column whose value is to be queried
Returns:
the value Object at the specified cell

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(int rowIndex)
Determines if the row at rowIndex is an aggregate row.

Parameters:
rowIndex - the row to query
Returns:
true if the row is an aggregate row, false otherwise

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int columnIndex)
Returns false if the row at rowIndex is an aggregate row, otherwise returns the value of its super method.

Specified by:
isCellEditable in interface TableModel
Overrides:
isCellEditable in class ListTableMap
Parameters:
rowIndex - the row being queried
columnIndex - the column being queried
Returns:
true if the cell is editable, false otherwise
See Also:
TableModel.setValueAt(java.lang.Object, int, int)

isFooter

public boolean isFooter(int rowIndex)
Determines if the row at rowIndex is a footer.

Parameters:
rowIndex - the row to query
Returns:
true if the row is a footer, false otherwise

isHeader

public boolean isHeader(int rowIndex)
Determines if the row at rowIndex is a header.

Parameters:
rowIndex - the row to query
Returns:
true if the row is a header, false otherwise

refresh

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


removeRow

public void removeRow(int row)
Removes a row from the data model

Specified by:
removeRow in interface ListTableModel
Overrides:
removeRow in class ListTableMap
Parameters:
row - the index of the row being removed

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(int[] rows)
Removes a few rows from the data model

Specified by:
removeRows in interface ListTableModel
Overrides:
removeRows in class ListTableMap
Parameters:
rows - an integer array that contains indexes of the rows being deleted

rowsReordered

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

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

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

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,
                       int rowIndex,
                       int columnIndex)
Sets the value in the cell at columnIndex and rowIndex to aValue.

Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class ListTableMap
Parameters:
aValue - value to assign to cell
rowIndex - row of cell
columnIndex - column of cell
See Also:
TableModel.getValueAt(int, int), TableModel.isCellEditable(int, int)