TreeTableComparators

Top  Previous  Next

 

TreeTableComparators are used in order to dynamically group the rows of a TreeTable component. TreeTableComparator implements the java.util.Comparator interface, thus the method:

 

public int compare(Object o1, Object o2);

 

, should be implemented.

 

The supplied objects to the compare method above are the row data objects of the underlying ListTableModel.

 

DynamicTreeTableModel 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, DefaultTreeTableComparator, compares row data based on a single column. The comparator to use for the column is retrieved with DynamicTreeTableModel's method:

 

public Comparator getDefaultComparator(Class columnClass);

 

Also, DynamicTreeTableModel 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);

 

It is up to the developer to use the installed 'class' comparators with their own TreeTableComparator implementation.

 

Example 1: Create a TreeTableComparator that compares data based on the first column

 

//ttm is the TreeTableModel

 

class FirstColumnTreeTableComparator implements TreeTableComparator {

       DynamicTreeTableModel model;

       FirstColumnTreeTableComparator(DynamicTreeTableModel model) {

               this.model = model;

       }

       public int compare(Object o1, Object o2) {

               //get the cell value for column 0

               Object val1 = model.getCellValue(o1, 0);

               Object val2 = model.getCellValue(o2, 0);

 

               //get a comparator from the treetable model using the object's class

               Comparator c = model.getDefaultComparator(val1.getClass());

 

               //we can use this comparator to make the comparison

               int comparison = c.compare(val1, val2);

 

               //return

               return comparison;

       }

       public boolean isGroupedByColumn(int column) {

               return column == 0;

       }

};

 

Example 2: Create a TreeTableComparator that compares data based on the first and the second column

 

//ttm is the TreeTableModel

 

class MultipleColumnTreeTableComparator implements TreeTableComparator {

       DynamicTreeTableModel model;

       FirstColumnTreeTableComparator(DynamicTreeTableModel model) {

               this.model = model;

       }

       public int compare(Object o1, Object o2) {

               //get the cell value for column 0

               Object val1 = model.getCellValue(o1, 0);

               Object val2 = model.getCellValue(o2, 0);

 

               //get the cell value for column 1

               Object val3 = model.getCellValue(o1, 1);

               Object val4 = model.getCellValue(o2, 1);

 

               //get a comparator for the first column from the treetable model using the object's class

               Comparator c1 = model.getDefaultComparator(val1.getClass());

 

               //get a comparator for the second column from the treetable model using the object's class

               Comparator c2 = model.getDefaultComparator(val3.getClass());

 

               //we can use these comparators to make the comparison

               int comparison1 = c1.compare(val1, val2);

               int comparsion2 = c2.compare(val3, val4);

 

               //return

               return comparison1 == 0 ? comparison2 : comparison1;

       }

       public boolean isGroupedByColumn(int column) {

               return column == 0 && column == 1;

       }

};

 

Example 3: Create a TreeTableComparator that compares data based on the objects supplied. We assume that the supplied objects implement the Comparable interface.

 

//ttm is the TreeTableModel

 

class ComparableTreeTableComparator implements TreeTableComparator {

       public int compare(Object o1, Object o2) {

               Comparable c1 = (Comparable) o1;

               return c1.compareTo(o2);

       }

       public boolean isGroupedByColumn(int column) {

               return false;

       }

};