【问题标题】:JFace Table Viewer Selection issue after sorting排序后的 JFace 表查看器选择问题
【发布时间】:2016-03-30 19:37:43
【问题描述】:

在一个 Eclipse RCP 应用程序中,我有一个用于显示大量数据的 VIRTUAL 样式的 tableviewer。

我正在使用实现 IStructuredContentProvider 的自定义内容提供程序和实现 ITableLabelProvider 的自定义表格查看器作为表格查看器的一部分..

我需要通过所有列对表数据进行排序。

排序对所有列都非常有效。但是,我在表格上有一个选择事件,并且有一些基于表格选择的操作。

假设以下项目按顺序显示在视图上

  • 一个
  • B
  • C
  • D
  • E

单击特定列后,视图按以下顺序排序

  • E
  • D
  • C
  • B
  • 一个

但是,在选择 E(表格的第一项)时,会加载项目 A 的详细信息,而不是项目 E 的详细信息。

看起来模型没有用排序后的数据更新,因此不匹配。似乎只在 UI 上进行排序,模型没有相应更新。

你能帮我解决这个问题吗?

我不希望编写自定义比较器并编写作为标签提供程序的 getColumnText 的一部分编写的相同逻辑来对各个列进行排序并使用该比较器对模型进行排序。

注意:如果我取出 VIRTUAL 样式并检查所选项目,它工作得非常好。所选项目的详细信息已按预期加载。

PFB,在我的应用程序中用于排序的代码片段。

TableColumnSorter cSorter = new TableColumnSorter(tabViewer, column.getColumn()) {
        protected int doCompare(Viewer v, Object e1, Object e2) {
                ITableLabelProvider lp = ((ITableLabelProvider) tabViewer
                .getLabelProvider());
                String t1 = lp.getColumnText(e1, colIdx);
                String t2 = lp.getColumnText(e2, colIdx);

                return t1.compareTo(t2);
        }
};

cSorter.setSorter(cSorter, TableColumnSorter.ASC);

自定义的 TableColumnSorter 类如下

abstract class TableColumnSorter extends ViewerComparator {

    public static final int ASC = 1;

    public static final int NONE = 0;

    public static final int DESC = -1;

    private int direction = 0;

    private TableColumn column;

    private TableViewer viewer;

    public TableColumnSorter(TableViewer viewer, TableColumn column) {
            this.column = column;
            this.viewer = viewer;
            this.column.addSelectionListener(new SelectionAdapter() {

                    public void widgetSelected(SelectionEvent e) {
                            if (TableColumnSorter.this.viewer.getComparator() != null) {
                                    if (TableColumnSorter.this.viewer.getComparator() == TableColumnSorter.this) {
                                            int tdirection = TableColumnSorter.this.direction;

                                            if (tdirection == ASC) {
                                                    setSorter(TableColumnSorter.this, DESC);
                                            } else if (tdirection == DESC) {
                                                    setSorter(TableColumnSorter.this, ASC);
                                            }
                                    } else {
                                            setSorter(TableColumnSorter.this, ASC);
                                    }
                            } else {
                                    setSorter(TableColumnSorter.this, ASC);
                            }
                    }
            });
    }

    public void setSorter(TableColumnSorter sorter, int direction) {
            if (direction == NONE) {
                    column.getParent().setSortColumn(null);
                    column.getParent().setSortDirection(SWT.NONE);
                    viewer.setComparator(null);
            } else {
                    column.getParent().setSortColumn(column);
                    sorter.direction = direction;

                    if (direction == ASC) {
                            column.getParent().setSortDirection(SWT.DOWN);
                    } else {
                            column.getParent().setSortDirection(SWT.UP);
                    }

                    if (viewer.getComparator() == sorter) {
                            viewer.refresh();
                    } else {
                            viewer.setComparator(sorter);
                    }

            }
    }

    public int compare(Viewer viewer, Object e1, Object e2) {
            return direction * doCompare(viewer, e1, e2);
    }

    protected abstract int doCompare(Viewer TableViewer, Object e1, Object e2); 
}

【问题讨论】:

    标签: java swt eclipse-rcp jface tableviewer


    【解决方案1】:

    来自TableViewer的JavaDoc

    TableViewer 现在支持 SWT.VIRTUAL 标志。如果基础表 是 SWT.VIRTUAL,内容提供者可以实现 ILazyContentProvider 而不是 IStructuredContentProvider 。 请注意 在这种情况下,查看器不支持排序或过滤。

    【讨论】:

    • 感谢您的回复。但问题不在于排序。排序工作完美。这是排序后表格选择的问题。如问题中所述,项目选择提供了不同的模型对象而不是选定的模型对象。设置了虚拟标志的表永远不能进行排序吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    相关资源
    最近更新 更多