Example usage for org.eclipse.swt.widgets Tree setSortColumn

List of usage examples for org.eclipse.swt.widgets Tree setSortColumn

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Tree setSortColumn.

Prototype

public void setSortColumn(TreeColumn column) 

Source Link

Document

Sets the column used by the sort indicator for the receiver.

Usage

From source file:org.eclipse.swt.examples.controlexample.TreeTab.java

/**
 * Sets the initial sort indicator state and adds a listener
 * to cycle through sort states and columns.
 *///from   w  ww  . j  ava2  s . com
void initializeSortState(final Tree tree) {
    /* Reset to known state: 'down' on column 0. */
    tree.setSortDirection(SWT.DOWN);
    TreeColumn[] columns = tree.getColumns();
    for (int i = 0; i < columns.length; i++) {
        TreeColumn column = columns[i];
        if (i == 0)
            tree.setSortColumn(column);
        SelectionListener listener = widgetSelectedAdapter(e -> {
            int sortDirection = SWT.DOWN;
            if (e.widget == tree.getSortColumn()) {
                /* If the sort column hasn't changed, cycle down -> up -> none. */
                switch (tree.getSortDirection()) {
                case SWT.DOWN:
                    sortDirection = SWT.UP;
                    break;
                case SWT.UP:
                    sortDirection = SWT.NONE;
                    break;
                }
            } else {
                tree.setSortColumn((TreeColumn) e.widget);
            }
            tree.setSortDirection(sortDirection);
        });
        column.addSelectionListener(listener);
        column.setData("SortListener", listener); //$NON-NLS-1$
    }
}