Polish up your JFace-Viewer! Sorting a table
18. January 2006
Today I want to show how to add a sorter to your JFace-Table. The requirement is to sort descending und ascending by clicking on the TableColumn-Header.
JFace already provides sorting-functionality. We just have to provide something like an alogrithm to arrange the items. For that we implemented the CollectionSorter that uses the default Collator from ViewerSorter.

The inital view with sorting

The ascending sorting after a click on the ID-Column header.
The Sorter:Initializing the Sorter with a default column
-
public class CollationSorter extends ViewerSorter {
-
-
-
/**
-
* Creates an instance of the sorter
-
* @param tc0 the default sorter-column.
-
*/
-
setCurrentColumn(defaultColumn);
-
}
-
-
/**
-
* Pushs the current sortorder in a map which key is the
-
* table-column.
-
* @param column
-
*/
-
if (this.sortMap.get(column) == null) {
-
}
-
else {
-
}
-
}
-
-
/**
-
* Asks for the current sort-order and inverts the sort-order
-
* @param column the requested column
-
* @return true if the sortIndex is descending, else false.
-
*/
-
boolean returnValue = true;
-
if (this.sortMap.get(column) != null) {
-
} else {
-
pushSortCriteria(column);
-
}
-
return returnValue;
-
}
-
-
-
/* (non-Javadoc)
-
* @see org.eclipse.jface.viewers.ViewerSorter#getCollator()
-
*/
-
int rc = -1;
-
// get the data
-
AbstractBaseElement data1 = (AbstractBaseElement) obj1;
-
AbstractBaseElement data2 = (AbstractBaseElement) obj2;
-
-
CollationKey key1 = null;
-
CollationKey key2 = null;
-
-
if (this.currentColumn == ((TableViewer)viewer).getTable().getColumn(0)) {
-
key1 = getCollator().getCollationKey(data1.getId());
-
key2 = getCollator().getCollationKey(data2.getId());
-
-
}
-
else if (this.currentColumn == ((TableViewer)viewer).getTable().getColumn(1)){
-
key1 = getCollator().getCollationKey(data1.getName());
-
key2 = getCollator().getCollationKey(data2.getName());
-
}
-
else if (this.currentColumn == ((TableViewer)viewer).getTable().getColumn(2)){
-
key1 = getCollator().getCollationKey(data1.getDescription());
-
key2 = getCollator().getCollationKey(data2.getDescription());
-
}
-
// replace null-strings with empty-strings
-
if (key1 == null)
-
key1 = getCollator().getCollationKey(""); //$NON-NLS-1$
-
-
if (key2 == null)
-
key2 = getCollator().getCollationKey(""); //$NON-NLS-1$
-
-
if (isDescending(this.currentColumn)) {
-
rc = key1.compareTo(key2);
-
}
-
else {
-
rc = key2.compareTo(key1);
-
}
-
return rc;
-
}
-
-
/**
-
* Sets the sort column.
-
* @param currentColumn The currentColumn to set.
-
*/
-
this.currentColumn = currentColumn;
-
pushSortCriteria(currentColumn);
-
}
-
}
This sorter you can use everywhere, you just have to modify the Creation of the Collator-Keys.
Direction Indicator
This feature will be aviable in Eclipse 3.2
Download the JFace TableSorter Plugin (source included - 17kByte) (Requires Model-Plugin)
Download the JFace TableSorter RCP (source included - 5,72 MByte)



[…] Similar to this article we have to implement a sorter that holds ascending/descanding state. In Addition we have to set the indicator. PLAIN TEXT JAVA: […]
Pingback by rich client 2.0 » TableViewer with Eclipse 3.2 - Sorter, Filter and Sort indicator — 31. July 2006 @ 19:59
With this code, we just have default sort order as ascending, not descending although we have the sort key map but it always returns to default sort order. So please modify it! I moved the sort key map to the table composite (JFace) to hold the sort state and it’s OK!
Is there any suggestion?
Comment by Linh — 15. January 2007 @ 04:51