ViewerComparator 构造函数上的 AssertionFailedException

AssertionFailedException on ViewerComparator constructor

我正在尝试为我的 TableViewer 实现排序器,它允许我在单击 header 时对列进行排序。但是,当我尝试构建我的 ViewerComparator 时出现此错误:

Exception in thread "main" org.eclipse.core.runtime.AssertionFailedException: null argument:
    at org.eclipse.core.runtime.Assert.isNotNull(Assert.java:85)
    at org.eclipse.core.runtime.Assert.isNotNull(Assert.java:73)
    at org.eclipse.jface.viewers.StructuredViewer.disassociate(StructuredViewer.java:640)
    at org.eclipse.jface.viewers.AbstractTableViewer.internalRefreshAll(AbstractTableViewer.java:735)
    at org.eclipse.jface.viewers.AbstractTableViewer.internalRefresh(AbstractTableViewer.java:649)
    at org.eclipse.jface.viewers.AbstractTableViewer.internalRefresh(AbstractTableViewer.java:636)
    at org.eclipse.jface.viewers.StructuredViewer.run(StructuredViewer.java:1457)
    at org.eclipse.jface.viewers.StructuredViewer.preservingSelection(StructuredViewer.java:1392)
    at org.eclipse.jface.viewers.StructuredViewer.preservingSelection(StructuredViewer.java:1353)
    at org.eclipse.jface.viewers.StructuredViewer.refresh(StructuredViewer.java:1455)
    at org.eclipse.jface.viewers.ColumnViewer.refresh(ColumnViewer.java:537)
    at org.eclipse.jface.viewers.StructuredViewer.refresh(StructuredViewer.java:1414)
    at org.eclipse.jface.viewers.StructuredViewer.setComparator(StructuredViewer.java:1752)
    at gui.EMI.portfolioTab(EMI.java:283)
    at gui.EMI.open(EMI.java:112)
    at gui.EMI.main(EMI.java:58)

这是代码:

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public 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 = null;
    private int columnIndex = 0;
    final private TableViewer viewer;

    final private SelectionListener selectionHandler = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            TableColumnSorter sorter = (TableColumnSorter) TableColumnSorter.this.viewer.getComparator();
            Assert.isTrue(TableColumnSorter.this == sorter);
            TableColumn selectedColumn = (TableColumn) e.widget;
            Assert.isTrue(TableColumnSorter.this.viewer.getTable() == selectedColumn.getParent());
            TableColumnSorter.this.setColumn(selectedColumn);
        }
    };

  // HERE IS THE PART I GET ERROR

    public TableColumnSorter(TableViewer viewer) {
        this.viewer = viewer;
        Assert.isTrue(this.viewer.getComparator() == null);
        viewer.setComparator(this);

        for (TableColumn tableColumn : viewer.getTable().getColumns()) {
            tableColumn.addSelectionListener(selectionHandler);
        }
    }

    public void setColumn(TableColumn selectedColumn) {
        if (column == selectedColumn) {
            switch (direction) {
            case ASC:
                direction = DESC;
                break;
            case DESC:
                direction = ASC;
                break;
            default:
                direction = ASC;
                break;
            }
        } else {
            this.column = selectedColumn;
            this.direction = ASC;
        }

        Table table = viewer.getTable();
        switch (direction) {
        case ASC:
            table.setSortColumn(selectedColumn);
            table.setSortDirection(SWT.UP);
            break;
        case DESC:
            table.setSortColumn(selectedColumn);
            table.setSortDirection(SWT.DOWN);
            break;
        default:
            table.setSortColumn(null);
            table.setSortDirection(SWT.NONE);
            break;
        }

        TableColumn[] columns = table.getColumns();
        for (int i = 0; i < columns.length; i++) {
            TableColumn theColumn = columns[i];
            if (theColumn == this.column) columnIndex = i;
        }
        viewer.setComparator(null);
        viewer.setComparator(this);
    }

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

    protected int doCompare(Viewer v, Object e1, Object e2) {
       Assert.isTrue(viewer == this.viewer);
       ILabelProvider labelProvider = (ILabelProvider) viewer.getLabelProvider(columnIndex);
        String t1 = labelProvider.getText(e1);
        String t2 = labelProvider.getText(e2);
        if (t1 == null) t1 = "";
        if (t2 == null) t2 = "";
        return t1.compareTo(t2);
    }
}

我通过以下方式调用方法,同样的错误:

aTableViewer.setComparator(new TableColumnSorter(aTableViewer));

or

TableColumnSorter sorter = new TableColumnSorter(aTableViewer);

这可能是什么问题,为什么对 TableViewer 实施排序器这么难?我真的花了几个小时在这上面,但仍然一无所获。似乎旧的 WindowsFormsApp 比 SWT/JFace.

更强大

您收到的错误来自 refreshsetComparator 所做的。它实际上不是由比较器代码引起的。

使用 TableViewer 时,您必须为 table 内容使用内容提供程序。

不支持直接创建 TableItems。

你的断言是因为项目没有按照观众的预期设置,使用内容提供者可以正确地做到这一点。