#118 patch: sorting by click in result-tableview's head

open
nobody
None
5
2007-07-22
2007-07-22
arne_anka
No

i always tried to click a column's head in the result-tableview to sort the table, but it does not work.
thus, i hacked the ResultSetViewer class to append a listener and a sorter.
i'm still not sure wether it is the most efficient or elegant solution -- but it works well and reasonable fast on a table (SWT Table!) with about 11000 rows, 14 columns containing int, long SQLDate, String ...

it's based upon the cvs module quantum-plugin, but i am unable to export the plugin, because SQLEditor.java is missing.

finally, it's not really a patch but the whole java-file ...

Discussion

  • arne_anka

    arne_anka - 2007-07-22

    modified class-file w/ sorting facility

     
  • arne_anka

    arne_anka - 2007-08-06

    Logged In: YES
    user_id=1477014
    Originator: YES

    does anybody read those forums at all?
    btw: in the code, remove the lengthy castings statemanet and acst everything except the quantum-sql*-types to comparable.

     
  • Marco

    Marco - 2009-05-09

    Here a similar fix:

    public class ResultSetViewer implements PropertyChangeListener {

    ..........

    class SorterImpl extends ViewerSorter {

    private static final int ASCENDING = SWT.UP;

    private static final int DESCENDING = SWT.DOWN;

    private int column = -1;

    private int direction;

    /**
    * Does the sort. If it's a different column from the previous sort, do an
    * ascending sort. If it's the same column as the last sort, toggle the sort
    * direction.
    *
    * @param column
    */
    public void doSort(int column, TableViewer viewer) {

    if (column == this.column) {
    // Same column as last sort; toggle the direction
    direction = direction == ASCENDING ? DESCENDING : ASCENDING;
    } else {
    // New column; do an ascending sort
    this.column = column;
    direction = ASCENDING;
    }

    viewer.getTable().setSortDirection(direction);
    }

    /**
    * Compares the object for sorting
    */
    public int compare(Viewer viewer, Object e1, Object e2) {

    int rc = 0;

    if(column != -1){

    Object element1 = ((SQLResultSetResults.Row) e1).get(column + 1);
    Object element2 = ((SQLResultSetResults.Row) e2).get(column + 1);

    if(element1 instanceof SQLNull){

    rc = -1;
    }
    else if(element2 instanceof SQLNull){

    rc = 1;
    }
    else{
    rc = ((Comparable)element1).compareTo(element2);
    }

    // If descending order, flip the direction
    if (direction == DESCENDING)
    rc = -rc;

    }
    return rc;
    }
    }

    ............

    private void createTable(Composite composite) {
    final Table table = new Table(composite, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
    table.setLayout(new GridLayout());
    table.setLayoutData(new GridData(GridData.FILL_BOTH));
    table.addMouseListener(this.tableView.actionGroup.mouseDblClk);

    addColumnsToTable(table);
    this.tableViewer = new TableViewer(table);
    this.tableViewer.setLabelProvider(new LabelProviderImpl());
    this.tableViewer.setContentProvider(new ContentProviderImpl());
    this.tableViewer.setSorter(new SorterImpl());
    this.tableViewer.setInput(this.resultSet);

    packColumns(table);
    }

    private int addColumnsToTable(final Table table) {
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    int columnCount = this.resultSet.getColumnCount();
    for (int i = 0; i < columnCount; i++) {

    final int sortingColumn = i;
    TableColumn column = new TableColumn(table, SWT.NONE);
    column.setText(this.resultSet.getColumnName(i+1).toString());
    column.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent event) {
    ((SorterImpl) tableViewer.getSorter()).doSort(sortingColumn, tableViewer);

    tableViewer.getTable().setSortColumn((TableColumn) event.widget);
    tableViewer.refresh();
    }
    });
    }
    return columnCount;
    }

    ......
    }

     

Log in to post a comment.