ComparableTreeTableModel

Top  Previous  Next

 

ComparableTreeTableModel is a MutableTreeTableModel subclass whose tree structure is dictated by a dynamic set of rules. These rules are specified by TreeTableComparators.

 

ComparableTreeTableModel manages a collection of TreeTableComparators. The collection can be manipulated with the methods:

 

public void addRowComparator(TreeTableComparator newComparator);

public void insertRowComparator(TreeTableComparator newComparator, int index);

public TreeTableComparator removeRowComparator(int index);

public boolean removeRowComparator(TreeTableComparator comparator);

public TreeTableComparator setRowComparator(TreeTableComparator newComparator, int index);

public TreeTableComparator[] getRowComparators();

public TreeTableComparator getRowComparator(int index);

 

A TreeTableComparator implementation, TreeNodeComparator, compares two tree nodes between them based on a single column. This differs from DefaultTreeTableComparator, used by DynamicTreeTableModel, which compares the row objects of a tree node. The comparator to use for the column is retrieved with ComparableTreeTableModel's method:

 

public Comparator getDefaultComparator(Class columnClass);

 

Also, ComparableTreeTableModel installs comparators for all the common classes with the method:

 

protected void createDefaultComparators();

 

You can assign the comparator to use for a column with:

 

public void setDefaultComparator(Class columnClass, Comparator comparator);

 

ComparableTreeTableModel has methods for adding nodes to the tree, taking into account the installed comparators.

 

To add a new node, you can use the method:

 

public void addNode(DefaultMutableTreeNode node);

 

The methods:

 

public void add(List data);

public void add(Object nodeData);

 

can also be used for adding nodes, for which one or more leaf nodes will be created automatically with:

 

protected abstract DefaultMutableTreeNode createLeafNode(Object data);

 

Non-leaf (group) nodes will be created automatically with:

 

protected abstract DefaultMutableTreeNode createNonLeafNode(DefaultMutableTreeNode child);

 

Finally, for re-creating the tree, when one or more comparators have changed, the

 

protected abstract DefaultMutableTreeNode createNonLeafNode(DefaultMutableTreeNode child);

 

method needs to be implemented.

 

WARNING: In ComparableTreeTableModel's context, the method:

 

public void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index);

 

should never be used to add a node, unless the correct index to place the new node is known.

 

Next, we discuss two subclasses of MutableTreeTableModel, DefaultMutableTreeTableModel and ObjectTreeTableModel.