0

In an eclipse RCP application , I have a tableviewer with a VIRTUAL style for diplaying a large amount of data.

I am using a custom content provider implementing IStructuredContentProvider & a custom table viewer implementing ITableLabelProvider as part of the table viewer..

I have a requirement to sort the table data via all columns.

The sorting works perfectly fine for all columns. However, I have a selection event on the table and there are some operations based on the table selection.

Let us say the following items get displayed on the view in the order

  • A
  • B
  • C
  • D
  • E

After clicking on a particular column, the view is sorted in the below mentioned order

  • E
  • D
  • C
  • B
  • A

But, on selecting E (1st item of the table) , the details of Item A are loaded rather than details of item E.

It looks like the model is not updated with the sorted data and hence the mismatch. It seems that sorting is done only on the UI and model is not updated accordingly.

Can u please help me out on this?

I dont wish to write a custom comparator and write the same logic written as part of in getColumnText of the label provider to sort the individual columns and use that comparator for sorting the model.

NOTE: If I take out the VIRTUAL style and inspect on the selected item, it works perfectly fine. The details of the selected item are loaded as expected.

PFB, the code pieces used for sorting in my application.

TableColumnSorter cSorter = new TableColumnSorter(tabViewer, column.getColumn()) {
        protected int doCompare(Viewer v, Object e1, Object e2) {
                ITableLabelProvider lp = ((ITableLabelProvider) tabViewer
                .getLabelProvider());
                String t1 = lp.getColumnText(e1, colIdx);
                String t2 = lp.getColumnText(e2, colIdx);

                return t1.compareTo(t2);
        }
};

cSorter.setSorter(cSorter, TableColumnSorter.ASC);

The custom TableColumnSorter class is as below

abstract class TableColumnSorter extends ViewerComparator {

    public static final int ASC = 1;

    public static final int NONE = 0;

    public static final int DESC = -1;

    private int direction = 0;

    private TableColumn column;

    private TableViewer viewer;

    public TableColumnSorter(TableViewer viewer, TableColumn column) {
            this.column = column;
            this.viewer = viewer;
            this.column.addSelectionListener(new SelectionAdapter() {

                    public void widgetSelected(SelectionEvent e) {
                            if (TableColumnSorter.this.viewer.getComparator() != null) {
                                    if (TableColumnSorter.this.viewer.getComparator() == TableColumnSorter.this) {
                                            int tdirection = TableColumnSorter.this.direction;

                                            if (tdirection == ASC) {
                                                    setSorter(TableColumnSorter.this, DESC);
                                            } else if (tdirection == DESC) {
                                                    setSorter(TableColumnSorter.this, ASC);
                                            }
                                    } else {
                                            setSorter(TableColumnSorter.this, ASC);
                                    }
                            } else {
                                    setSorter(TableColumnSorter.this, ASC);
                            }
                    }
            });
    }

    public void setSorter(TableColumnSorter sorter, int direction) {
            if (direction == NONE) {
                    column.getParent().setSortColumn(null);
                    column.getParent().setSortDirection(SWT.NONE);
                    viewer.setComparator(null);
            } else {
                    column.getParent().setSortColumn(column);
                    sorter.direction = direction;

                    if (direction == ASC) {
                            column.getParent().setSortDirection(SWT.DOWN);
                    } else {
                            column.getParent().setSortDirection(SWT.UP);
                    }

                    if (viewer.getComparator() == sorter) {
                            viewer.refresh();
                    } else {
                            viewer.setComparator(sorter);
                    }

            }
    }

    public int compare(Viewer viewer, Object e1, Object e2) {
            return direction * doCompare(viewer, e1, e2);
    }

    protected abstract int doCompare(Viewer TableViewer, Object e1, Object e2); 
}
flavio.donze
  • 6,077
  • 9
  • 41
  • 69
user1166031
  • 248
  • 6
  • 14

1 Answers1

3

From the JavaDoc for TableViewer

TableViewer now supports the SWT.VIRTUAL flag. If the underlying table is SWT.VIRTUAL, the content provider may implement ILazyContentProvider instead of IStructuredContentProvider . Note that in this case, the viewer does not support sorting or filtering.

greg-449
  • 102,836
  • 220
  • 90
  • 127
  • Thanks for the reply . But the issue is not with the sorting. Sorting works perfectly. It is the issue with the table selection after sorting. As mentioned in the question, the item selection gives a different model object rather than the selected one. Can Sorting never be done for a table with the virtual flag set ? – user1166031 Jul 03 '14 at 00:10