下面列出了org.eclipse.swt.widgets.Table#getSortColumn ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* {@inheritDoc}
*/
@Override
public void widgetSelected(SelectionEvent e) {
final TableColumn column = (TableColumn) e.widget;
final TableViewerColumn viewerColumn = (TableViewerColumn) column.getData(COLUMN_VIEWER_KEY);
final Table table = column.getParent();
comparator.setColumn(table.indexOf(column));
if (table.getSortColumn() == column) {
table.setSortDirection(table.getSortDirection() == SWT.UP ? SWT.DOWN : SWT.UP);
} else {
table.setSortColumn(column);
table.setSortDirection(SWT.DOWN);
}
viewerColumn.getViewer().refresh();
configuration.setSortColumn(Column.valueForIndex(table.indexOf(column)));
configuration.setSortDirection(table.getSortDirection());
}
@Override
public void widgetSelected ( final SelectionEvent e )
{
final Table table = this.tableViewer.getTable ();
final TableColumn newColumn = (TableColumn)e.widget;
final TableColumn currentColumn = table.getSortColumn ();
final int currentDir = table.getSortDirection ();
int newDir = SWT.UP;
if ( newColumn == currentColumn )
{
newDir = currentDir == SWT.UP ? SWT.DOWN : SWT.UP;
}
else
{
table.setSortColumn ( newColumn );
}
table.setSortDirection ( newDir );
this.tableViewer.setSorter ( new Sorter ( (Columns)newColumn.getData ( COLUMN_KEY ), newDir ) );
}
@Override
public void widgetSelected ( final SelectionEvent e )
{
final Table table = this.tableViewer.getTable ();
final TableColumn newColumn = (TableColumn)e.widget;
final TableColumn currentColumn = table.getSortColumn ();
final EventTableColumn column = (EventTableColumn)newColumn.getData ( COLUMN_KEY );
if ( column == EventTableColumn.reservedColumnSourceTimestamp || column == EventTableColumn.reservedColumnEntryTimestamp )
{
final int currentDir = table.getSortDirection ();
int newDir = SWT.UP;
if ( newColumn == currentColumn )
{
newDir = currentDir == SWT.UP ? SWT.DOWN : SWT.UP;
}
else
{
table.setSortColumn ( newColumn );
}
table.setSortDirection ( newDir );
this.tableViewer.setSorter ( new EventTableSorter ( column, newDir ) );
}
}
@Override
public void widgetSelected(SelectionEvent e) {
Table table = fTableViewer.getTable();
TableColumn prevSortcolumn = table.getSortColumn();
if (prevSortcolumn == fColumn) {
flipSortDirection();
}
table.setSortDirection(fDirection);
table.setSortColumn(fColumn);
Comparator<T> comparator;
if (fDirection == SWT.DOWN) {
comparator = fComparator;
} else {
comparator = checkNotNull(Collections.reverseOrder(fComparator));
}
IContentProvider contentProvider = fTableViewer.getContentProvider();
if (contentProvider instanceof DeferredContentProvider) {
DeferredContentProvider deferredContentProvider = (DeferredContentProvider) contentProvider;
deferredContentProvider.setSortOrder(comparator);
} else if (contentProvider instanceof ISortingLazyContentProvider) {
ISortingLazyContentProvider sortingLazyContentProvider = (ISortingLazyContentProvider) contentProvider;
sortingLazyContentProvider.setSortOrder(comparator);
} else {
fTableViewer.setComparator(new ElementComparator<>(comparator));
}
}
@Override
public int compare(final Viewer viewer, final Object e1, final Object e2) {
final ActionInformationItem a1 = (ActionInformationItem) e1;
final ActionInformationItem a2 = (ActionInformationItem) e2;
final Table table = (Table) viewer.getControl();
final TableColumn sortColumn = table.getSortColumn();
if (sortColumn == null) {
// a) Compare by distinct states first (we want actions with zero distinct states
// to appear at the top).
if (Long.compare(a1.getUnseen(), a2.getUnseen()) == 0L) {
// b) Compare by location
return a1.getModuleLocation().compareTo(a2.getModuleLocation());
} else {
return Long.compare(a1.getUnseen(), a2.getUnseen());
}
} else {
// User requested to sort a specific column up or down.
@SuppressWarnings("unchecked")
final Comparator<ActionInformationItem> comp = (Comparator<ActionInformationItem>) sortColumn
.getData(CoverageLabelProvider.COVERAGE_COMPARATOR);
if (table.getSortDirection() == SWT.UP) {
return comp.compare(a2, a1);
} else {
return comp.compare(a1, a2);
}
}
}
private int getSortDirection(TableColumn column) {
Table tableControl = (Table) kindViewer.getControl();
if (column != tableControl.getSortColumn()) {
return SWT.DOWN;
}
// If it is unsorted (SWT.NONE), assume down sort
return (SWT.DOWN == tableControl.getSortDirection())
? SWT.UP : SWT.DOWN;
}
private int getSortDirection(TableColumn column) {
Table tableControl = (Table) propViewer.getControl();
if (column != tableControl.getSortColumn()) {
return SWT.DOWN;
}
// If it is unsorted (SWT.NONE), assume down sort
return (SWT.DOWN == tableControl.getSortDirection())
? SWT.UP : SWT.DOWN;
}
private int getSortDirection(TableColumn column) {
Table tableControl = (Table) propViewer.getControl();
if (column != tableControl.getSortColumn()) {
return SWT.DOWN;
}
// If it is unsorted (SWT.NONE), assume down sort
return (SWT.DOWN == tableControl.getSortDirection())
? SWT.UP : SWT.DOWN;
}
private int getSortDirection(TableColumn column) {
Table tableControl = (Table) relSetViewer.getControl();
if (column != tableControl.getSortColumn()) {
return SWT.DOWN;
}
// If it is unsorted (SWT.NONE), assume down sort
return (SWT.DOWN == tableControl.getSortDirection())
? SWT.UP : SWT.DOWN;
}
public static void sortTable(Table table, TableColumn column) {
TableItem[] items = table.getItems();
int rows = items.length;
int dir = table.getSortDirection() == SWT.DOWN ? 1 : -1;
TableColumn currentColumn = table.getSortColumn();
int columnNum = 0;
for (int j = 0; j < table.getColumnCount(); j++) {
if (table.getColumn(j).equals(column)) {
columnNum = j;
break;
}
}
if (column.equals(currentColumn))
dir = -dir;
else
dir = 1;
// sort an index map, then move the actual rows
int[] map = new int[rows];
for (int i = 0; i < rows; i++)
map[i] = i;
for (int i = 0; i < rows - 1; i++) {
for (int j = i + 1; j < rows; j++) {
TableItem a = items[map[i]];
TableItem b = items[map[j]];
if ((a.getText(columnNum).toLowerCase().compareTo(b.getText(columnNum).toLowerCase()) * dir > 0)) {
int t = map[i];
map[i] = map[j];
map[j] = t;
}
}
}
// can't move existing items or delete first, so append new items to the end and then delete existing rows
for (int i = 0; i < rows; i++) {
int n = map[i];
TableItem item = new TableItem(table, SWT.NONE);
for (int j = 0; j < table.getColumnCount(); j++) {
item.setText(j, items[n].getText(j));
}
item.setData(items[n].getData());
items[n].dispose();
}
table.setSortDirection(dir == 1 ? SWT.DOWN : SWT.UP);
table.setSortColumn(column);
}