Copyright © 2005 Scientific Applications. All Rights Reserved.

com.sciapp.treetable
Class ComparableTreeTableModel

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

public abstract class ComparableTreeTableModel
extends MutableTreeTableModel

ComparableTreeTableModel provides for a mutable treetable model whose tree structure is dictated by a dynamic set of rules. These rules are specified by TreeTableComparators. To add a new node, you can use the addNode method. The add(Object data) method can also be used, for which a leaf node will be created automatically with createLeafNode.

WARNING:The insertNodeInto should never be used unless you know the correct index to place the new node.

See Also:
Serialized Form

Field Summary
protected  boolean addChildForGroup
          boolean flag that indicates whether we want a child node added for every new group created, when adding nodes via the addNode method.
protected  ArrayList comparators
          the list of row comparators.
protected  Hashtable defaultComparatorsByColumnClass
          A table of objects that compare two object with each other, indexed by class
 
Fields inherited from class com.sciapp.treetable.MutableTreeTableModel
classes, columns
 
Fields inherited from class com.sciapp.treetable.AbstractTreeTableModel
listenerList, root
 
Fields inherited from interface com.sciapp.treetable.TreeTableModel
CHANGE_COLUMN_SOURCE
 
Constructor Summary
ComparableTreeTableModel(DefaultMutableTreeNode root)
          Creates a ComparableTreeTableModel object.
ComparableTreeTableModel(String[] columns, Class[] classes, DefaultMutableTreeNode root)
          Creates a ComparableTreeTableModel object.
ComparableTreeTableModel(String[] columns, DefaultMutableTreeNode root)
          Creates a ComparableTreeTableModel object.
 
Method Summary
 void add(List data)
          Convenience method to add a list of nodes to the model.
 void add(Object nodeData)
          Convenience method to add a node to the model whose 'column data' is identified by nodeData.
 void addNode(DefaultMutableTreeNode node)
          Adds a node to the model.
 void addRowComparator(TreeTableComparator newComparator)
          Adds a new row comparator at the end of the list of comparators.
 void clearComparators()
          Removes all row comparators and rebuilds the tree.
protected  void comparatorsChanged()
          The comparators have changed in some way and the model re-creates the whole tree structure.
protected  void createDefaultComparators()
          Creates default comparators for objects, strings, date and boolean values.
protected abstract  DefaultMutableTreeNode createLeafNode(Object data)
          Creates and returns a new instance of a leaf node whose 'column data' is identified by data.
protected abstract  DefaultMutableTreeNode createNonLeafNode(DefaultMutableTreeNode child)
          Creates and returns a new instance of a group (non-leaf) node whose child is identified by child.
 Object findTreeNode(Object comparison, int level)
          Returns the tree node that is found to be equal to comparison at the specified level.
 boolean getAddChildForGroup()
          Determines whether we want a child node added for every new group created, when adding nodes via the addNode method.
 Comparator getDefaultComparator(Class columnClass)
          Returns the comparator to use for objects of the specified columnClass.
 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.
 void insertRowComparator(TreeTableComparator newComparator, int index)
          Inserts a row comparator at index in the list of row comparators of this model.
 void loadComparators(ObjectInputStream in)
          Restores the TreeTableComparators and assigns them on the model.
 void refreshComparators()
          Indicates that the underlying comparators have changed and the model should re-create the whole tree structure.
 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.
protected abstract  DefaultMutableTreeNode replicateLeafNode(DefaultMutableTreeNode child)
          Creates and returns a new instance of the leaf node identified by child.
 void saveComparators(ObjectOutputStream out)
          Saves the TreeTableComparators that are currently installed on the model.
 void setAddChildForGroup(boolean addChildForGroup)
          Determines whether we want a child node added for every new group created, when adding nodes via the addNode method.
 void setDefaultComparator(Class columnClass, Comparator comparator)
          Sets the default comparator for columnClass.
 TreeTableComparator setRowComparator(TreeTableComparator newComparator, int index)
          Sets a treetable comparator at index.
 
Methods inherited from class com.sciapp.treetable.MutableTreeTableModel
addColumn, addColumn, addReorderListener, clear, fireRowsMapped, fireRowsReordered, getChild, getChildCount, getColumnClass, getColumnCount, getColumnName, getIndexOfChild, getPathToRoot, getPathToRoot, insertNodeInto, isCellEditable, isLeaf, moveNode, nodeChanged, nodesChanged, nodeStructureChanged, nodesWereInserted, nodesWereRemoved, removeNodeFromParent, removeReorderListener, setColumnClasses, setColumnNames, setValueAt, valueForPathChanged
 
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

comparators

protected ArrayList comparators
the list of row comparators.


defaultComparatorsByColumnClass

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


addChildForGroup

protected boolean addChildForGroup
boolean flag that indicates whether we want a child node added for every new group created, when adding nodes via the addNode method.

Constructor Detail

ComparableTreeTableModel

public ComparableTreeTableModel(String[] columns,
                                Class[] classes,
                                DefaultMutableTreeNode root)
Creates a ComparableTreeTableModel object.


ComparableTreeTableModel

public ComparableTreeTableModel(String[] columns,
                                DefaultMutableTreeNode root)
Creates a ComparableTreeTableModel object.


ComparableTreeTableModel

public ComparableTreeTableModel(DefaultMutableTreeNode root)
Creates a ComparableTreeTableModel object.

Method Detail

add

public void add(Object nodeData)
Convenience method to add a node to the model whose 'column data' is identified by nodeData. This will effectively call createLeafNode on nodeData.

Parameters:
nodeData - the 'column data' of the node to add.

add

public void add(List data)
Convenience method to add a list of nodes to the model. The node's column data are stored in the list data, which is passed as a parameter. Each element in the list identifies a new node to add.

Parameters:
data - the list of 'column data' to add as nodes to the model.

addNode

public void addNode(DefaultMutableTreeNode node)
Adds a node to the model.

Parameters:
node - the node to add.

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

clearComparators

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


comparatorsChanged

protected void comparatorsChanged()
The comparators have changed in some way and the model re-creates the whole tree structure.


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.


createLeafNode

protected abstract DefaultMutableTreeNode createLeafNode(Object data)
Creates and returns a new instance of a leaf node whose 'column data' is identified by data. data in this framework is either DefaultMutableTreeNode's user object attribute or the aggregate values of a TreeTableRow.

Parameters:
data - the node's 'column data'.
Returns:
the newly created leaf node.

createNonLeafNode

protected abstract DefaultMutableTreeNode createNonLeafNode(DefaultMutableTreeNode child)
Creates and returns a new instance of a group (non-leaf) node whose child is identified by child.

Parameters:
child - this node's child (or one of its children)
Returns:
the newly created group (non-leaf) node.

findTreeNode

public Object findTreeNode(Object comparison,
                           int level)
Returns the tree node that is found to be equal to comparison at the specified level.

If comparators are in use, this method will use binary search algorithm to scan through the tree. Therefore, it is imperative that the tree has not been modified independently by the programmer, otherwise, results are bound to be inconsistent.

Parameters:
comparison - the node to compare to when scanning through the tree
level - the node's tree level
Returns:
the node in the tree that matches comparison

getAddChildForGroup

public boolean getAddChildForGroup()
Determines whether we want a child node added for every new group created, when adding nodes via the addNode method.

Returns:
true if a child will be added for every newly created group node, false otherwise.

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

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

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

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.

refreshComparators

public void refreshComparators()
Indicates that the underlying comparators have changed and the model should re-create the whole tree structure. This will effectively call comparatorsChanged.


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

replicateLeafNode

protected abstract DefaultMutableTreeNode replicateLeafNode(DefaultMutableTreeNode child)
Creates and returns a new instance of the leaf node identified by child. This method is used when the underlying comparators have changed, so that the tree can be restructured.

Parameters:
child - the leaf we want to create a replica of.
Returns:
the newly created leaf node.

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.

setAddChildForGroup

public void setAddChildForGroup(boolean addChildForGroup)
Determines whether we want a child node added for every new group created, when adding nodes via the addNode method.

Parameters:
addChildForGroup - boolean flag that is true if a child will be added to the model for every newly created group node, false otherwise.

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

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

Copyright © 2005 Scientific Applications. All Rights Reserved.