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

问题描述 投票:0回答:1

我正在尝试使用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示例”); GridLayout layout = new GridLayout(); shell.setLayout(布局); Composite composite = new Composite(shell,SWT.NONE); composite.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true)); composite.setLayout(new GridLayout(1,false));表testTable = new Table(composite,SWT.BORDER); testTable.setLinesVisible(真); testTable.setHeaderVisible(真); GridData tableData = new GridData(SWT.FILL,SWT.FILL,true,false); tableData.heightHint = 300; testTable.setLayoutData(资料表); 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(真); 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 = 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和setter方法}

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

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;
}
}

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

Exception

swt eclipse-rcp rcp e4
1个回答
2
投票

ComboCellEditor值是在构造函数中为其提供的值列表中的整数索引。

你的getValue类的EditingSupport方法必须将Integer索引返回到值列表中。

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

© www.soinside.com 2019 - 2024. All rights reserved.