简体   繁体   English

排序后的JFace Table Viewer选择问题

[英]JFace Table Viewer Selection issue after sorting

In an eclipse RCP application , I have a tableviewer with a VIRTUAL style for diplaying a large amount of data. 在Eclipse RCP应用程序中,我有一个具有VIRTUAL样式的tableviewer用于显示大量数据。

I am using a custom content provider implementing IStructuredContentProvider & a custom table viewer implementing ITableLabelProvider as part of the table viewer.. 我正在使用实现IStructuredContentProvider的自定义内容提供程序和实现ITableLabelProvider的自定义表查看器作为表查看器的一部分。

I have a requirement to sort the table data via all columns. 我需要通过所有列对表数据进行排序。

The sorting works perfectly fine for all columns. 排序对于所有列都非常合适。 However, I have a selection event on the table and there are some operations based on the table selection. 但是,我在表上有一个选择事件,并且有一些基于表选择的操作。

Let us say the following items get displayed on the view in the order 让我们说以下项目按顺序显示在视图上

  • A 一种
  • B
  • C C
  • D d
  • E Ë

After clicking on a particular column, the view is sorted in the below mentioned order 单击特定列后,按以下提到的顺序对视图进行排序

  • E Ë
  • D d
  • C C
  • B
  • A 一种

But, on selecting E (1st item of the table) , the details of Item A are loaded rather than details of item E. 但是,在选择E(表的第一项)时, 将加载项A的详细信息,而不是项E的详细信息。

It looks like the model is not updated with the sorted data and hence the mismatch. 看起来该模型未使用排序后的数据进行更新,因此不匹配。 It seems that sorting is done only on the UI and model is not updated accordingly. 似乎仅在UI上完成了排序,并且模型没有相应地更新。

Can u please help me out on this? 你能帮我这个忙吗?

I dont wish to write a custom comparator and write the same logic written as part of in getColumnText of the label provider to sort the individual columns and use that comparator for sorting the model. 我不希望编写一个自定义比较器,并编写与标签提供程序的getColumnText中的一部分相同的逻辑来对各个列进行排序,并使用该比较器对模型进行排序。

NOTE: If I take out the VIRTUAL style and inspect on the selected item, it works perfectly fine. 注意:如果我取出虚拟样式并检查所选项目,它会很好地工作。 The details of the selected item are loaded as expected. 所选项目的详细信息将按预期加载。

PFB, the code pieces used for sorting in my application. 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);

The custom TableColumnSorter class is as below 自定义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); 
}

From the JavaDoc for TableViewer 从JavaDoc for TableViewer

TableViewer now supports the SWT.VIRTUAL flag. TableViewer现在支持SWT.VIRTUAL标志。 If the underlying table is SWT.VIRTUAL, the content provider may implement ILazyContentProvider instead of IStructuredContentProvider . 如果基础表是SWT.VIRTUAL,则内容提供程序可以实现ILazyContentProvider而不是IStructuredContentProvider。 Note that in this case, the viewer does not support sorting or filtering. 请注意,在这种情况下,查看器不支持排序或过滤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM