gpt4 book ai didi

java - ViewerComparator 构造函数上的 AssertionFailedException

转载 作者:太空宇宙 更新时间:2023-11-04 11:18:25 25 4
gpt4 key购买 nike

我正在尝试为我的 TableViewer 实现排序器,它允许我在单击标题时对列进行排序。但是,当我尝试构建 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$7.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 更强大。

最佳答案

您收到的错误来自 setComparator 执行的refresh。它实际上不是由比较器代码引起的。

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

不支持直接创建TableItems。

您的断言是因为项目未按查看者的预期设置,使用内容提供程序可以正确执行此操作。

关于java - ViewerComparator 构造函数上的 AssertionFailedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45193590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com