dsto.dfc.swt.viewers
Class TableColumnSorter

java.lang.Object
  extended by dsto.dfc.swt.viewers.TableColumnSorter
All Implemented Interfaces:
org.eclipse.swt.widgets.Listener

public class TableColumnSorter
extends Object
implements org.eclipse.swt.widgets.Listener

Adds click-column-to-sort behaviour to a table viewer. Clicking a column sorts it if a sorter is defined. Clicking again inverts sort order. Clicking a third time clears sort.

Example use:

   TableColumnSorter columnSorter  = new TableColumnSorter (tableView);
   
   TableColumn column = new TableColumn (tableView.getTable (), SWT.LEFT);
   column.setText ("Name");
   column.setWidth (40);
   
   columnSorter.addColumn (column, "name", new DataObjectViewerSorter ("name"));
   
   column = new TableColumn (tableView.getTable (), SWT.RIGHT);
   column.setText ("Age");
   column.setWidth (20);
   
   columnSorter.addColumn (column, "age", new DataObjectViewerSorter ("age"));
   
   ...
   
   columnSorter.setSortColumn ("name");
   tableView.setColumnProperties (columnSorter.getColumnProperties ());
 

Author:
Matthew Phillips

Field Summary
static int ASCENDING
           
static int DESCENDING
           
static int UNSORTED
           
 
Constructor Summary
TableColumnSorter(org.eclipse.jface.viewers.TableViewer viewer)
           
TableColumnSorter(org.eclipse.jface.viewers.TableViewer viewer, boolean allowUnsorted)
          Instantiate a TableColumnSorter, specifying whether the sort column may be 'UNSORTED'.
 
Method Summary
 org.eclipse.swt.widgets.TableColumn addColumn(String property, String title, int columns)
          Shortcut to add a left-aligned column with a set property, title and suggested column width.
 org.eclipse.swt.widgets.TableColumn addColumn(String property, String title, int columns, int style)
          Shortcut to add a column with a set property, title and suggested column width.
 org.eclipse.swt.widgets.TableColumn addColumn(String property, String title, org.eclipse.jface.viewers.ViewerSorter sorter, int columns, int style)
          Shortcut to add a column with a set property, title, sorter and suggested column width.
 void addColumn(org.eclipse.swt.widgets.TableColumn column, String property)
          Add a column with no sorter.
 void addColumn(org.eclipse.swt.widgets.TableColumn column, String property, org.eclipse.jface.viewers.ViewerSorter sorter)
          Add a column.
 String[] getColumnProperties()
          Shortcut to get the array of properties associated with the columns using addColumn(TableColumn, String, ViewerSorter).
 void handleEvent(org.eclipse.swt.widgets.Event e)
           
 void setSortColumn(String property)
          Set the currently sorted column to be the one displaying a given property.
 void setSortColumn(String property, int newOrdering)
          Set the currently sorted column to be the one displaying a given property.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UNSORTED

public static final int UNSORTED
See Also:
Constant Field Values

ASCENDING

public static final int ASCENDING
See Also:
Constant Field Values

DESCENDING

public static final int DESCENDING
See Also:
Constant Field Values
Constructor Detail

TableColumnSorter

public TableColumnSorter(org.eclipse.jface.viewers.TableViewer viewer)

TableColumnSorter

public TableColumnSorter(org.eclipse.jface.viewers.TableViewer viewer,
                         boolean allowUnsorted)
Instantiate a TableColumnSorter, specifying whether the sort column may be 'UNSORTED'. See UNSORTED

Parameters:
viewer -
allowUnsorted - Specify whether the sort column can be 'UNSORTED'. Note if passed in as false, it is equivalent to calling TableColumnSorter(TableViewer)
Method Detail

getColumnProperties

public String[] getColumnProperties()
Shortcut to get the array of properties associated with the columns using addColumn(TableColumn, String, ViewerSorter). This can be passed into ColumnViewer.setColumnProperties(java.lang.String[]).


addColumn

public void addColumn(org.eclipse.swt.widgets.TableColumn column,
                      String property)
Add a column with no sorter.

Parameters:
column - The column.
property - The property associated with the column.

addColumn

public void addColumn(org.eclipse.swt.widgets.TableColumn column,
                      String property,
                      org.eclipse.jface.viewers.ViewerSorter sorter)
Add a column.

Parameters:
column - The column.
property - The property associated with the column.
sorter - The sorter for the column. May be null.

addColumn

public org.eclipse.swt.widgets.TableColumn addColumn(String property,
                                                     String title,
                                                     int columns)
Shortcut to add a left-aligned column with a set property, title and suggested column width.

Parameters:
property - The property the column is displaying.
title - The title of the column.
columns - The suggested column width in characters.
Returns:
The column item created.
See Also:
addColumn(String, String, ViewerSorter, int, int)

addColumn

public org.eclipse.swt.widgets.TableColumn addColumn(String property,
                                                     String title,
                                                     int columns,
                                                     int style)
Shortcut to add a column with a set property, title and suggested column width.

Parameters:
property - The property the column is displaying.
title - The title of the column.
columns - The suggested column width in characters.
style - The column style (SWT.LEFT, SWT.RIGHT, SWT.CENTER).
Returns:
The column item created.
See Also:
addColumn(String, String, ViewerSorter, int, int)

addColumn

public org.eclipse.swt.widgets.TableColumn addColumn(String property,
                                                     String title,
                                                     org.eclipse.jface.viewers.ViewerSorter sorter,
                                                     int columns,
                                                     int style)
Shortcut to add a column with a set property, title, sorter and suggested column width.

Parameters:
property - The property the column is displaying.
title - The title of the column.
sorter - The sorter for the column (may be null).
columns - The suggested column width in characters.
style - The column style (SWT.LEFT, SWT.RIGHT, SWT.CENTER).
Returns:
The column item created.

setSortColumn

public void setSortColumn(String property)
Set the currently sorted column to be the one displaying a given property. Sort order is ASCENDING.

See Also:
setSortColumn(String, int)

setSortColumn

public void setSortColumn(String property,
                          int newOrdering)
Set the currently sorted column to be the one displaying a given property.

Parameters:
property - The property for the sorted column.
newOrdering - The new sort order: UNSORTED, ASCENDING, DESCENDING.

handleEvent

public void handleEvent(org.eclipse.swt.widgets.Event e)
Specified by:
handleEvent in interface org.eclipse.swt.widgets.Listener


Copyright © 2008 Commonwealth of Australia