下面列出了org.eclipse.swt.widgets.Table#getSortDirection ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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 ) );
}
}
@SuppressWarnings({
"rawtypes", "unchecked"
})
private int compareElements(Object e1, Object e2){
IColumnContentProvider columnValueProvider =
(IColumnContentProvider) tableViewer.getContentProvider();
Table table = tableViewer.getTable();
int index = Arrays.asList(table.getColumns()).indexOf(table.getSortColumn());
int result = 0;
if (index != -1) {
Comparable c1 = columnValueProvider.getValue(e1, index);
Comparable c2 = columnValueProvider.getValue(e2, index);
if (c1 instanceof String && c2 instanceof String) {
String _c1 = (String) c1;
String _c2 = (String) c2;
result = _c1.compareToIgnoreCase(_c2);
} else {
result = c1.compareTo(c2);
}
}
return table.getSortDirection() == SWT.UP ? result : -result;
}
@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;
}
private int compareElements(Object e1, Object e2) {
Table table = tableViewer.getTable();
int index = Arrays.asList(table.getColumns()).indexOf(table.getSortColumn());
int result = 0;
if (index != -1) {
Comparable c1 = ((TableLine)e1).getValueOf(index);
Comparable c2 = ((TableLine)e2).getValueOf(index);
result = c1.compareTo(c2);
}
return table.getSortDirection() == SWT.UP ? result : -result;
}
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);
}