下面列出了org.eclipse.swt.widgets.Table#setSortDirection ( ) 实例代码,或者点击链接到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));
}
}
private void setSortColumn(
TableColumn column, int colIndex, int direction) {
ITableLabelProvider labelProvider =
(ITableLabelProvider) kindViewer.getLabelProvider();
ViewerComparator sorter = new AlphabeticSorter(
new LabelProviderToString(labelProvider, colIndex));
if (SWT.UP == direction) {
sorter = new InverseSorter(sorter);
}
Table tableControl = (Table) kindViewer.getControl();
kindViewer.setComparator(sorter);
tableControl.setSortColumn(column);
tableControl.setSortDirection(direction);
}
private void sortTable(final TableColumn column, final boolean asc)
{
Table table = tableViewer.getTable();
table.setSortColumn(column);
table.setSortDirection(asc? SWT.UP : SWT.DOWN);
tableViewer.setSorter(new ViewerSorter() {
public int compare(Viewer viewer, Object o1, Object o2)
{
int result;
switch(tableViewer.getTable().indexOf(column))
{
case 0: default:
result = ( (ItemContent) o1 ).getDisplayName().compareTo(( (ItemContent) o2 ).getDisplayName());
break;
case 1:
result = ( (ItemContent) o1 ).getCustomName().compareTo(( (ItemContent) o2 ).getCustomName());
break;
case 2:
result = ( (ItemContent) o1 ).getDescription().compareTo(( (ItemContent) o2 ).getDescription());
break;
}
return asc? result : result * -1;
}
});
}
/**
* Handles the given {@link PropertyChangeEvent}.
*
* @param event the event to handle
*/
private void handlePropertyChange(PropertyChangeEvent event) {
switch (event.getProperty()) {
case WorkbenchPreferencePage.WINDOW_SIZE:
LogUtil.getLogger().fine("Setting new window size."); //$NON-NLS-1$
shell.setSize(Configuration.getWindowSize());
break;
case WorkbenchPreferencePage.WINDOW_LOCATION:
LogUtil.getLogger().fine("Setting new window location."); //$NON-NLS-1$
shell.setLocation(Configuration.getWindowLocation());
break;
case WorkbenchPreferencePage.SORT_COLUMN:
LogUtil.getLogger().fine("Setting new sort column."); //$NON-NLS-1$
final int columnIndex1 = Column.indexForValue(configuration.getSortColumn());
final Table table1 = view.getTableViewer().getTable();
comparator.setColumn(columnIndex1);
table1.setSortColumn(table1.getColumn(columnIndex1));
view.getTableViewer().refresh();
break;
case WorkbenchPreferencePage.SORT_DIRECTION:
LogUtil.getLogger().fine("Setting new sort direction."); //$NON-NLS-1$
final int columnIndex = comparator.getColumn();
final Table table = view.getTableViewer().getTable();
comparator.setColumn(columnIndex);
table.setSortColumn(table.getColumn(columnIndex));
table.setSortDirection(configuration.getSortDirection());
view.getTableViewer().refresh();
break;
default:
break;
}
}
@Override
protected void sort(SelectionEvent e) {
// 1) Get table column which fire this selection event
TableColumn tableColumn = (TableColumn) e.getSource();
// 2) Get the owner table
Table table = tableColumn.getParent();
// 3) Modify the SWT Table sort
table.setSortColumn(tableColumn);
table.setSortDirection(getSortDirection());
}
@Override
public void widgetSelected(SelectionEvent e) {
viewerComparator.setColumn(fColumnIndex);
int dir = viewerComparator.getDirection();
Table table = tableViewer.getTable();
table.setSortDirection(dir);
table.setSortColumn(fTableColumn);
tableViewer.refresh();
}
@Override
public void widgetSelected(SelectionEvent e) {
viewerComparator.setColumn(fColumnIndex);
int dir = viewerComparator.getDirection();
Table table = tableViewer.getTable();
table.setSortDirection(dir);
table.setSortColumn(fTableColumn);
tableViewer.refresh();
}
private void setSortColumn(
TableColumn column, int colIndex, int direction) {
ViewerComparator sorter = buildColumnSorter(colIndex);
if (SWT.UP == direction) {
sorter = new InverseSorter(sorter);
}
Table tableControl = (Table) propViewer.getControl();
propViewer.setComparator(sorter);
tableControl.setSortColumn(column);
tableControl.setSortDirection(direction);
}
private void setSortColumn(
TableColumn column, int colIndex, int direction) {
ViewerComparator sorter = buildColumnSorter(colIndex);
if (SWT.UP == direction) {
sorter = new InverseSorter(sorter);
}
Table tableControl = (Table) propViewer.getControl();
propViewer.setComparator(sorter);
tableControl.setSortColumn(column);
tableControl.setSortDirection(direction);
}
private void setSortColumn(
TableColumn column, int colIndex, int direction) {
ViewerComparator sorter = buildColumnSorter(colIndex);
if (SWT.UP == direction) {
sorter = new InverseSorter(sorter);
}
Table tableControl = (Table) relSetViewer.getControl();
relSetViewer.setComparator(sorter);
tableControl.setSortColumn(column);
tableControl.setSortDirection(direction);
}
public ChangePathsTableProvider(Composite parent, IContentProvider contentProvider) {
super(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
TableLayout layout = new TableLayout();
GridData data = new GridData(GridData.FILL_BOTH);
Table table = (Table) getControl();
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.setLayoutData(data);
table.setLayout(layout);
table.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
if(currentPathFont != null) {
currentPathFont.dispose();
}
}
});
createColumns(table, layout);
setLabelProvider(new ChangePathLabelProvider());
setContentProvider(contentProvider);
ChangePathsSorter sorter = new ChangePathsSorter(COL_PATH);
setSorter(sorter);
table.setSortDirection(SWT.UP);
}
/**
* Create a TableViewer that can be used to display a list of ILogEntry instances.
* Ths method provides the labels and sorter but does not provide a content provider
*
* @param parent
* @return TableViewer
*/
public TableViewer createTable(Composite parent) {
Table table = new Table(parent, style);
table.setHeaderVisible(true);
table.setLinesVisible(true);
GridData data = new GridData(GridData.FILL_BOTH);
data.horizontalIndent = 0;
data.verticalIndent = 0;
table.setLayoutData(data);
TableLayout layout = new TableLayout();
table.setLayout(layout);
TableViewer viewer = new TableViewer(table);
createColumns(table, layout, viewer);
viewer.setLabelProvider(new HistoryLabelProvider());
HistorySorter sorter = new HistorySorter(COL_REVISION);
viewer.setSorter(sorter);
table.setSortDirection(SWT.DOWN);
table.setSortColumn(table.getColumn(0));
table.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
if(currentRevisionFont != null) {
currentRevisionFont.dispose();
}
}
});
this.viewer = viewer;
return viewer;
}
private void tableColumnClicked(TableColumn column) {
Table table = column.getParent();
if (column.equals(table.getSortColumn())) {
table.setSortDirection(table.getSortDirection() == SWT.UP ? SWT.DOWN : SWT.UP);
}
else {
table.setSortColumn(column);
table.setSortDirection(SWT.UP);
}
tableViewer.refresh();
}
private void tableColumnClicked(TableColumn column){
Table table = column.getParent();
if (column.equals(table.getSortColumn())) {
table.setSortDirection(table.getSortDirection() == SWT.UP ? SWT.DOWN : SWT.UP);
} else {
table.setSortColumn(column);
table.setSortDirection(SWT.UP);
}
tableViewer.refresh();
}
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);
}
/**
* Build rule table viewer
*/
private Table createDetectorsTableViewer(Composite parent, IProject project) {
final BugPatternTableSorter sorter = new BugPatternTableSorter(this);
int tableStyle = SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION | SWT.CHECK;
availableFactoriesTableViewer = CheckboxTableViewer.newCheckList(parent, tableStyle);
availableFactoriesTableViewer.addCheckStateListener(new ICheckStateListener() {
@Override
public void checkStateChanged(CheckStateChangedEvent event) {
syncUserPreferencesWithTable();
}
});
int currentColumnIdx = 0;
Table factoriesTable = availableFactoriesTableViewer.getTable();
TableColumn factoryNameColumn = createColumn(currentColumnIdx, factoriesTable, getMessage("property.detectorName"), 230,
COLUMN.DETECTOR_NAME);
addColumnSelectionListener(sorter, factoryNameColumn, COLUMN.DETECTOR_NAME);
currentColumnIdx++;
TableColumn bugsAbbrevColumn = createColumn(currentColumnIdx, factoriesTable, getMessage("property.bugCodes"), 75,
COLUMN.BUG_CODES);
addColumnSelectionListener(sorter, bugsAbbrevColumn, COLUMN.BUG_CODES);
currentColumnIdx++;
TableColumn speedColumn = createColumn(currentColumnIdx, factoriesTable, getMessage("property.speed"), 70,
COLUMN.DETECTOR_SPEED);
addColumnSelectionListener(sorter, speedColumn, COLUMN.DETECTOR_SPEED);
currentColumnIdx++;
TableColumn pluginColumn = createColumn(currentColumnIdx, factoriesTable, getMessage("property.provider"), 100,
COLUMN.PLUGIN);
addColumnSelectionListener(sorter, pluginColumn, COLUMN.PLUGIN);
currentColumnIdx++;
TableColumn categoryColumn = createColumn(currentColumnIdx, factoriesTable, getMessage("property.category"), 75,
COLUMN.BUG_CATEGORIES);
addColumnSelectionListener(sorter, categoryColumn, COLUMN.BUG_CATEGORIES);
factoriesTable.setLinesVisible(true);
factoriesTable.setHeaderVisible(true);
// initial sort indicator
factoriesTable.setSortDirection(sorter.revertOrder ? SWT.UP : SWT.DOWN);
factoriesTable.setSortColumn(factoryNameColumn);
sorter.setSortColumnIndex(COLUMN.DETECTOR_NAME);
availableFactoriesTableViewer.setContentProvider(new DetectorFactoriesContentProvider());
availableFactoriesTableViewer.setLabelProvider(new DetectorFactoryLabelProvider(this));
availableFactoriesTableViewer.setSorter(sorter);
populateAvailableRulesTable(project);
factoriesTable.setEnabled(true);
return factoriesTable;
}
public void setColumn(TableColumn selectedColumn) {
if (column == selectedColumn) {
switch (direction) {
case ASC:
direction = DESC;
break;
case DESC:
direction = ASC;
break;
default:
direction = ASC;
break;
}
} else {
this.column = selectedColumn;
this.direction = ASC;
}
Table table = viewer.getTable();
switch (direction) {
case ASC:
table.setSortColumn(selectedColumn);
table.setSortDirection(SWT.UP);
break;
case DESC:
table.setSortColumn(selectedColumn);
table.setSortDirection(SWT.DOWN);
break;
default:
table.setSortColumn(null);
table.setSortDirection(SWT.NONE);
break;
}
TableColumn[] columns = table.getColumns();
for (int i = 0; i < columns.length; i++) {
TableColumn theColumn = columns[i];
if (theColumn == this.column) columnIndex = i;
}
viewer.setComparator(null);
viewer.setComparator(this);
}