rich client 2.0

Polish up your JFace-Viewer! Sorting a table


18. January 2006
Tom Seidel @ 17:12

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.

table_sorter_initalize.png
The inital view with sorting

table_sorter_reverse.png
The ascending sorting after a click on the ID-Column header.

The Sorter:Initializing the Sorter with a default column

JAVA:
  1. public class CollationSorter extends ViewerSorter {
  2.  
  3. private Map sortMap = new HashMap();
  4.  
  5. /**
  6. * Creates an instance of the sorter
  7. * @param tc0 the default sorter-column.
  8. */
  9. public CollationSorter(TableColumn defaultColumn) {
  10. setCurrentColumn(defaultColumn);
  11. }
  12.  
  13. /**
  14. * Pushs the current sortorder in a map which key is the
  15. * table-column.
  16. * @param column
  17. */
  18. public void pushSortCriteria(TableColumn column) {
  19. if (this.sortMap.get(column) == null) {
  20. this.sortMap.put(column,new Boolean(true));
  21. }
  22. else {
  23. boolean newSort = !((Boolean)this.sortMap.get(column)).booleanValue();
  24. this.sortMap.put(column,new Boolean(newSort));
  25. }
  26. }
  27.  
  28. /**
  29. * Asks for the current sort-order and inverts the sort-order
  30. * @param column the requested column
  31. * @return true if the sortIndex is descending, else false.
  32. */
  33. public boolean isDescending(TableColumn column) {
  34. boolean returnValue = true;
  35. if (this.sortMap.get(column) != null) {
  36. returnValue = ((Boolean)this.sortMap.get(column)).booleanValue();
  37. } else {
  38. pushSortCriteria(column);
  39. }
  40. return returnValue;
  41. }
  42.  
  43. private TableColumn currentColumn = null;
  44.  
  45. /* (non-Javadoc)
  46. * @see org.eclipse.jface.viewers.ViewerSorter#getCollator()
  47. */
  48. public int compare(Viewer viewer, Object obj1, Object obj2) {
  49. int rc = -1;
  50. // get the data
  51. AbstractBaseElement data1 = (AbstractBaseElement) obj1;
  52. AbstractBaseElement data2 = (AbstractBaseElement) obj2;
  53.  
  54. CollationKey key1 = null;
  55. CollationKey key2 = null;
  56.  
  57. if (this.currentColumn == ((TableViewer)viewer).getTable().getColumn(0)) {
  58. key1 = getCollator().getCollationKey(data1.getId());
  59. key2 = getCollator().getCollationKey(data2.getId());
  60.  
  61. }
  62. else if (this.currentColumn == ((TableViewer)viewer).getTable().getColumn(1)){
  63. key1 = getCollator().getCollationKey(data1.getName());
  64. key2 = getCollator().getCollationKey(data2.getName());
  65. }
  66. else if (this.currentColumn == ((TableViewer)viewer).getTable().getColumn(2)){
  67. key1 = getCollator().getCollationKey(data1.getDescription());
  68. key2 = getCollator().getCollationKey(data2.getDescription());
  69. }
  70. // replace null-strings with empty-strings
  71. if (key1 == null)
  72. key1 = getCollator().getCollationKey(""); //$NON-NLS-1$
  73.  
  74. if (key2 == null)
  75. key2 = getCollator().getCollationKey(""); //$NON-NLS-1$
  76.  
  77. if (isDescending(this.currentColumn)) {
  78. rc = key1.compareTo(key2);
  79. }
  80. else {
  81. rc = key2.compareTo(key1);
  82. }
  83. return rc;
  84. }
  85.  
  86. /**
  87. * Sets the sort column.
  88. * @param currentColumn The currentColumn to set.
  89. */
  90. public void setCurrentColumn(TableColumn currentColumn) {
  91. this.currentColumn  = currentColumn;
  92. pushSortCriteria(currentColumn);
  93. }
  94. }

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)

2 Comments »

  1. […] 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

  2. 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

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress