http://www.javawiki.org/2006_01_18/tidy-up-your-jface-viewer-sorting-a-table/
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.
The inital view with sorting
The ascending sorting after a click on the ID-Column 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
. Now look at the Sorter:
Initializing the Sorter with a default column
JAVA:
-
-
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) {
-
this.
sortMap.
put(column,
new Boolean(true));
-
}
-
else {
-
boolean newSort = !
((Boolean)this.
sortMap.
get(column
)).
booleanValue();
-
this.
sortMap.
put(column,
new Boolean(newSort
));
-
}
-
}
-
-
/**
-
* 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) {
-
returnValue =
((Boolean)this.
sortMap.
get(column
)).
booleanValue();
-
} else {
-
pushSortCriteria(column);
-
}
-
return returnValue;
-
}
-
-
-
-
/* (non-Javadoc)
-
* @see org.eclipse.jface.viewers.ViewerSorter#getCollator()
-
*/
-
public int compare
(Viewer viewer,
Object obj1,
Object obj2
) {
-
int rc = -1;
-
// get the data
-
AbstractBaseElement data1 = (AbstractBaseElement) obj1;
-
AbstractBaseElement data2 = (AbstractBaseElement) obj2;
-
-
-
-
-
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.
-
*/
-
public void setCurrentColumn
(TableColumn currentColumn
) {
-
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 (Requires Model-Plugin)
Download the JFace TableSorter RCP (source included)