for (int i = 0; i < COLUMNS.length; i++) {
TableColumn idColumn = new TableColumn(table, SWT.LEFT);
cName = StringUtils.getNotEmpty(COLUMNS[i]);
ColumnPixelData columnpixeldata = new ColumnPixelData(COLUMNS_WIDTH[i]);
tableLayout.addColumnData(columnpixeldata);
idColumn.setText(cName);
idColumn.setResizable(true);
final String columnName = cName;
final Collator collator = Collator.getInstance();
idColumn.addSelectionListener(new SelectionListener() {
private boolean sortType = true;
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
sortType = !sortType;
java.util.Collections.sort(input, new Comparator<FElement>() {
public int compare(FElement entry1, FElement entry2) {
int rt = -1;
if (COLUMNS[1].equals(columnName)) {
rt = collator.compare(entry1.getName(), entry2.getName());
} else if (COLUMNS[2].equals(columnName)) {
rt = collator.compare(StringUtils.getNotEmpty(entry1.getValueTxt()), StringUtils.getNotEmpty(entry2.getValueTxt()));
} else if (COLUMNS[3].equals(columnName)) {
rt = collator.compare(StringUtils.getNotEmpty(entry1.getDescription()), StringUtils.getNotEmpty(entry2.getDescription()));
}
if (sortType) {
return (-1) * rt;
}
return rt;
}
});
options.setInput(input);
options.refresh();
}
});
}
这种方法的好处是短小精悍
如果做架构设计就不如使用Annotation封装一下,如果是刚入门就不妨每列增加一个Sorter而且还要增加正反两个.