org.eclipse.core.runtime.AssertionFailedException:断言失败:

org.eclipse.core.runtime.AssertionFailedException: assertion failed:

我正在尝试创建一个 table with ComboBoxCellEditor 列。当我设置值时,低于异常的时间即将到来。

import org.eclipse.jface.util.Policy;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/......./

public class Test { public static void main(String[] args) { Shell shell = new Shell(); shell.setText("TableViewer Example"); GridLayout layout = new GridLayout(); shell.setLayout(layout); Composite composite = new Composite(shell, SWT.NONE); composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true)); composite.setLayout(new GridLayout(1, false)); Table testTable = new Table(composite, SWT.BORDER); testTable.setLinesVisible(true); testTable.setHeaderVisible(true); GridData tableData = new GridData(SWT.FILL, SWT.FILL, true, false); tableData.heightHint = 300; testTable.setLayoutData(tableData); TableViewerColumn columnViewer = null; String[] columnNames = { "Name", "Laptops" }; TableViewer testTableViewer = new TableViewer(testTable); for (int i = 0; i < columnNames.length; i++) { columnViewer = new TableViewerColumn(testTableViewer, SWT.LEFT); columnViewer.getColumn().setText(columnNames[i]); if (columnNames[i].equals("Name")) { columnViewer.getColumn().setWidth(200); } else if (columnNames[i].equals("Laptops")) { columnViewer.getColumn().setWidth(300); } columnViewer.getColumn().setResizable(true); columnViewer.getColumn().setMoveable(true); columnViewer.setLabelProvider(new TestColumnLabelProvider(i)); } testTableViewer.setContentProvider(new TestContentProvider()); testTableViewer.setColumnProperties(columnNames); TestBean[] testBeans = new TestBean[5]; for (int i = 0; i < 5; i++) { TestBean bean = new TestBean(); TableViewerColumn[] getTableViewerColumns = getTableViewerColumns(testTableViewer); for (int j = 0; j < getTableViewerColumns.length; j++) { getTableViewerColumns[j].setEditingSupport(new TestEditingSuport(testTableViewer, j, bean.getListOfLaptop)); } bean.setName("Debasish" + i); bean.setLaptop(bean.getListOfLaptop[i]); testBeans[i] = bean; } testTableViewer.setInput(testBeans); shell.open(); Display display = shell.getDisplay(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } public static TableViewerColumn[] getTableViewerColumns(TableViewer tableViewer) { TableColumn[] columns = tableViewer.getTable().getColumns(); TableViewerColumn[] viewerColumns = new TableViewerColumn[columns.length]; for (int i = 0; i < columns.length; i++) { TableColumn tableColumn = columns[i]; viewerColumns[i] = (TableViewerColumn) tableColumn.getData(Policy.JFACE + ".columnViewer"); } return viewerColumns; } }

/............../

class TestBean { private String name; private String laptop; public String[] getListOfLaptop = { "Acer", "HP", "Lenovo", "Dell", "Benq" }; //getter and setter method }

/......./

class TestEditingSuport extends EditingSupport {
private int m_column;
private CellEditor m_editor;
public TestEditingSuport(ColumnViewer viewer, int column, 
String[] listOfTestBean) {
super(viewer);
m_column = column;
// Create the correct editor based on the column index
switch (column) {
case 0:
case 1:
m_editor = new ComboBoxCellEditor(
((TableViewer) viewer).getTable(), listOfTestBean);
break;
default:
}
}
@Override
protected CellEditor getCellEditor(Object element) {
    return m_editor;
}
@Override
protected boolean canEdit(Object element) {
    return true;
}
@Override
protected Object getValue(Object element) {
    TestBean bean = (TestBean) element;
    Object value = null;
    switch (m_column) {
    case 0:
        value = bean.getName();
        break;
    case 1:
        value = bean.getLaptop();
        break;
    default:
}
return value;
}
@Override
protected void setValue(Object element, Object value) {
TestBean bean = (TestBean) element;
switch (m_column) {
case 0:
if (valueChanged(bean.getName(), (String) value)) {
bean.setName((String) value);
}
getViewer().update(bean, null);
break;
case 1:
int index = (Integer) value;
String laptop = bean.getListOfLaptop[index];
if (valueChanged(bean.getLaptop(), laptop)) {
bean.setLaptop(laptop);
}
getViewer().update(bean, null);
break;
default:
}
}
private boolean valueChanged(String previousValue, String currentValue) {
boolean changed = false;
if ((previousValue == null) && (currentValue != null)) {
changed = true;
} else if ((previousValue != null) && (currentValue != null) && (!previousValue.equals(currentValue))) {
changed = true;
}
return changed;
}
}

/......./

class TestContentProvider implements IStructuredContentProvider {
@Override
public Object[] getElements(Object inputElement) {
return (Object[]) inputElement;
}
}

/......................./

class TestColumnLabelProvider extends ColumnLabelProvider {
private int m_column;
public TestColumnLabelProvider(int column) {
this.m_column = column;
}
public String getText(Object element) {
String text = null;
if (element instanceof TestBean) {
TestBean testBean = (TestBean) element;
switch (m_column) {
case 0:
text = testBean.getName();
break;
case 1:
text = testBean.getLaptop();
break;
default:
}
}
return text;
}
}

/ ...................../

ComboCellEditor 值是您在构造函数中给它的值列表的整数索引。

您的 EditingSupport class 的 getValue 方法必须 return 值列表中的 Integer 索引。

您的 EditingSupport class 的 setValue 方法将被赋予包含所选索引的 Integer