按多列排序JFace TableViewer

时间:2016-04-11 07:10:07

标签: java jface

我很确定我所要求的是不可能的,但我无法在任何地方找到明确的答案,所以这就是:

考虑以下工作示例:

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表是否可以按多列排序?如果是这样,怎么样?

2 个答案:

答案 0 :(得分:1)

目前没有标准方法来指示多个排序列。有一个长期存在的错误133154请求支持多个setSortColumm列,但从未做过任何事情。

答案 1 :(得分:0)

是。 greg-449是正确的,到目前为止还没有标准的实现。如果你真的热衷于一些如何使它工作,我有一个使用TableColumn.setImage(image)的解决方法,在列上设置正确的排序图像。维护TableColumn.setData(key,value)中的排序顺序以及Table.setData(key,value)中所有已排序的列,绝不使用或设置Table.setSortDirection(direction)Table.setSortColumn().

enter image description here