rich client 2.0

TableViewer with Eclipse 3.2 - Sorter, Filter and Sort indicator


28. July 2006
Tom Seidel @ 02:06

table_fract.png In Eclipse 3.1 there was always the problem that you couldn't give a sort indicator to a sortable TableViewer. This issue was resolved with Eclipse 3.2. Today I want to show a simple TableViewer with a sorting and indicators, filters and a column reordering. Unfortunately the implementation for the sorters on JFace-Viewers don't support an ascending or desending flag that takes also care for the indicator-representation. We have to implement this for our own.

Defining the view

The first steps remain constant. Implement your view, label- and contenproviders. When you're defining the TableColumn-objects add a Listener to each TableColumn

JAVA:
  1. tc0.addListener(SWT.Selection, sortListener);

Implement the Listener and Sorter-Coupling

Similar to this article we have to implement a sorter that holds ascending/descanding state. In Addition we have to set the indicator.

JAVA:
  1. Listener sortListener = new Listener() {
  2.     public void handleEvent(Event e) {
  3.         // determine new sort column and direction
  4.         TableColumn sortColumn = View.this.viewer.getTable().getSortColumn();
  5.         TableColumn currentColumn = (TableColumn) e.widget;
  6.         int dir = View.this.viewer.getTable().getSortDirection();
  7.         if (sortColumn == currentColumn) {
  8.             dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
  9.         } else {
  10.             View.this.viewer.getTable().setSortColumn(currentColumn);
  11.             dir = SWT.UP;
  12.         }
  13.         // sort the data based on column and direction
  14.         String sortIdentifier = null;
  15.         if (currentColumn == tc0) {
  16.             sortIdentifier = Sorter.MAIL_SORT;
  17.         }
  18.         if (currentColumn == tc1) {
  19.             sortIdentifier = Sorter.IP_SORT;
  20.         }
  21.         if (currentColumn == tc2) {
  22.             sortIdentifier = Sorter.DEPT_SORT;
  23.         }
  24.         if (currentColumn == tc3) {
  25.             sortIdentifier = Sorter.COMP_SORT;
  26.         }
  27.         View.this.viewer.getTable().setSortDirection(dir);
  28.         View.this.viewer.setSorter(new Sorter(sortIdentifier,dir));
  29.     }
  30. };

Our sorter is instanciated with a direction

JAVA:
  1. public Sorter(String column, int dir) {
  2.     super();
  3.     this.column = column;
  4.     this.dir = dir;
  5. }

and the result is inverted if the current sort-direction is SWT.DOWN.

JAVA:
  1. public int compare(Viewer viewer, Object e1, Object e2) {
  2.     int returnValue = 0;
  3.     if (this.column == MAIL_SORT) {
  4.         returnValue = ((ResultItem) e1).getMail().compareTo(((ResultItem) e2).getMail());
  5.     }
  6.     if (this.column == IP_SORT) {
  7.         returnValue = ((ResultItem) e1).getIp().compareTo(((ResultItem) e2).getIp());
  8.     }
  9.     if (this.column == DEPT_SORT) {
  10.         returnValue = ((ResultItem) e1).getDepartment().compareTo(((ResultItem) e2).getDepartment());
  11.     }
  12.     if (this.column == COMP_SORT) {
  13.         returnValue = ((ResultItem) e1).getCompany().compareTo(((ResultItem) e2).getCompany());
  14.     }
  15.     if (this.dir == SWT.DOWN) {
  16.         returnValue = returnValue * -1;
  17.     }
  18.     return returnValue;
  19. }

Downloads

Download the TableViewer-Demo as Plug-In (Source included - 35kByte)
Download the TableViewer-Demo as RCP (Source inluded - 9,3Mbyte)

10 Comments »

  1. Good article, very clear and easy to follow. It helped me a lot.

    Comment by Bruno Negrao — 10. November 2006 @ 23:00

  2. very good~~~

    Comment by Leo — 22. December 2006 @ 09:16

  3. Its nice, but I don’t found the source code.

    Comment by Dominique — 24. December 2006 @ 13:14

  4. The source is in the TableViewer-Demo as Plug-In download

    Comment by Han — 4. September 2007 @ 21:53

  5. Well done, thats what it´s got to be, your articles are always a pleasure. Greeting from munich.

    Comment by Martin — 26. October 2007 @ 17:21

  6. Good work. I was breaking my head with sorting tableviewer in RCP. Thanks for your tips I got it working. But I dont see the source in your attachment. It would be great if you added thesource.

    Comment by Sarya — 2. May 2008 @ 15:52

  7. thnx! helped me a lot..

    Comment by shai ben hur — 15. February 2009 @ 13:42

  8. Very Nice. Thanx a lot.

    Comment by Michael — 25. February 2009 @ 18:36

  9. Very nice example. Thanks.

    Comment by davidallen — 30. October 2009 @ 06:42

  10. This example helped me a lot.
    Thank you.

    Comment by Laurent — 21. May 2010 @ 12:02

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress