我很确定我所要求的是不可能的,但我无法在任何地方找到明确的答案,所以这就是:
考虑以下工作示例:
public class TableViewerSorter {
private static final String DATA_COMPARATOR = "comparator";
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final TableViewer viewer = new TableViewer(shell, SWT.BORDER);
Table table = viewer.getTable();
table.setHeaderVisible(true);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setSorter(new ViewerSorter() {
@Override
public int compare(Viewer v, Object e1, Object e2) {
TableColumn sortColumn = table.getSortColumn();
Comparator comparator = sortColumn == null ? null : (Comparator) sortColumn.getData(DATA_COMPARATOR);
if (comparator != null && table.getSortDirection() == SWT.UP) {
comparator = comparator.reversed();
}
return comparator == null ? 0 : comparator.compare(e1, e2);
}
});
createTableViewerColumn(viewer, "ID", v -> v.ordinal());
createTableViewerColumn(viewer, "Name", v -> v.getDisplayName());
createTableViewerColumn(viewer, "Fruit", v -> v.isFruit());
for (TableColumn column : table.getColumns()) {
column.addListener(SWT.Selection, e -> {
final Item sortColumn = table.getSortColumn();
int direction = table.getSortDirection();
if (column.equals(sortColumn)) {
direction = direction == SWT.UP ? SWT.DOWN : SWT.UP;
} else {
table.setSortColumn(column);
direction = SWT.UP;
}
table.setSortDirection(direction);
viewer.refresh();
});
}
viewer.setInput(Value.values());
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private static <T extends Comparable<T>> TableViewerColumn createTableViewerColumn(TableViewer viewer, String name,
Function<Value, T> propertyFunction) {
TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
viewerColumn.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return propertyFunction.apply((Value) element).toString();
}
});
TableColumn column = viewerColumn.getColumn();
column.setText(name);
column.setWidth(100);
column.setData(DATA_COMPARATOR, (Comparator<Value>) (v1, v2) -> propertyFunction.apply(v1).compareTo(propertyFunction.apply(v2)));
return viewerColumn;
}
static enum Value {
APPLE, BANANA, CABBAGE, DILL, EGGPLANT;
String getDisplayName() {
return name().substring(0, 1) + name().substring(1).toLowerCase();
}
boolean isFruit() {
return this != CABBAGE && this != DILL;
}
}
static class LabelProvider<T> extends ColumnLabelProvider {
private final Function<T, String> toStringFunction;
public LabelProvider(final Function<T, String> toStringFunction) {
this.toStringFunction = toStringFunction;
}
@Override
public String getText(final Object element) {
return element == null ? null : this.toStringFunction.apply((T) element);
}
}
}
我想要的是让用户点击第二列然后点击第三列,并使输入按第三列排序,当值相等时,按第二列排序。
当然,我可以只添加一个单击列的列表到ViewerSorter
,或者(取决于排序算法)只使用最后排序的元素列表作为排序的基础而不是查看者的输入。
这很容易。
困难的部分是如何向用户传达此特定表按两列排序。 API似乎不支持用例,但我想确保:JFace表是否可以按多列排序?如果是这样,怎么样?