ComboBoxCellEditor不会更改Label

时间:2014-10-31 10:40:01

标签: java eclipse swt

我目前正在开发Eclipse4 RCP应用程序并试图获得一个带有两列的JFace TableViewer,其中一列有可选的ComboBoxCellEditor

一个大问题如下:如果我从ComboBox中选择一个值,则列的Label不会改变。如果我再次单击ComboBox,它会显示应显示的值。

因此,例如,如果我可以选择“Expr”和“String”并且当前选择是“Expr”,如果我从Combo中选择“String”并单击其他位置,则Combo仍然显示“Expr”。如果我再次单击ComboBox并将其聚焦,则会显示“String”。如果我再次点击其他地方,它仍会显示“Expr”。

我使用几个类来运行Table。

我的模特课程:

private String element;
private String type;

public TableModelElement(String element, String type) {
    this.element = element;
    this.type = type;
}

public TableModelElement(String element) {
    this(element, SetTypeEnum.SET_TYPE_STRING.toString());
}

public TableModelElement() {
    this("", SetTypeEnum.SET_TYPE_STRING.toString());
}
//And Getter/Setter

我的SetTypeEnum:

public enum SetTypeEnum {
SET_TYPE_EXPR("expr"), SET_TYPE_STRING("string");

private final String setType;

private SetTypeEnum(String setType) {
    this.setType = setType;
}

@Override
public String toString() {
    return setType;
}

public static String[] toArray() {
    String[] values = new String[SetTypeEnum.values().length];
    int i = 0;
    for (SetTypeEnum value : SetTypeEnum.values()) {
        if (i < values.length) {
            values[i] = value.toString();
            i++;
        }
    }
    return values;
}
}

TableViewer发起人:

public class SetTable {

    private TableViewer viewer;
    private TableLayout layout;
    private TableViewerColumn colElementText;
    private TableViewerColumn colElementType;

    public SetTable(Composite parent) {
        layout = new TableLayout();
        layout.addColumnData(new ColumnWeightData(3));
        layout.addColumnData(new ColumnWeightData(1));
        Table table = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION
                | SWT.V_SCROLL);
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        table.setLayout(layout);
        viewer = new TableViewer(table);
        viewer.getControl().setLayoutData(
                new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        colElementText = new TableViewerColumn(viewer, SWT.NONE);
        colElementText.getColumn().setResizable(true);
        colElementText.getColumn().setText("Wert");
        colElementType = new TableViewerColumn(viewer, SWT.NONE);
        colElementType.getColumn().setResizable(false);
        colElementType.getColumn().setText("Typ");
        EditingSupport exampleEditingSupport = new SetTableEditingSupport(
                colElementType.getViewer());
        colElementType.setEditingSupport(exampleEditingSupport);
        viewer.setContentProvider(new ArrayContentProvider());
        viewer.setLabelProvider(new SetTableLabelProvider());

        TableModelElement[] model = new TableModelElement[] {
                new TableModelElement("Test1",
                        SetTypeEnum.SET_TYPE_EXPR.toString()),
                new TableModelElement("Test2",
                        SetTypeEnum.SET_TYPE_EXPR.toString()) };
        viewer.setInput(model);
    }

还有EditingSupport:

public class SetTableEditingSupport extends EditingSupport {

    private ComboBoxViewerCellEditor cellEditor = null;

    public SetTableEditingSupport(ColumnViewer colViewer) {
        super(colViewer);
        cellEditor = new ComboBoxViewerCellEditor((Composite) getViewer()
                .getControl(), SWT.READ_ONLY);
        cellEditor.setLabelProvider(new LabelProvider());
        cellEditor.setContentProvider(new ArrayContentProvider());
        cellEditor.setInput(SetTypeEnum.toArray());
    }

    @Override
    protected CellEditor getCellEditor(final Object element) {
        return cellEditor;
    }

    @Override
    protected boolean canEdit(Object element) {
        return true;
    }

    @Override
    protected Object getValue(Object element) {
        if (element instanceof TableModelElement) {
            TableModelElement data = (TableModelElement) element;
            return data.getType();
        }
        return null;
    }

    @Override
    protected void setValue(Object element, Object type) {
        if (element instanceof TableModelElement && type instanceof String) {
            TableModelElement data = (TableModelElement) element;
            String newType = (String) type;
            /* only set new value if it differs from old one */
            if (!data.getType().equals(newType)) {
                data.setType(newType);
//              String[] items =((CCombo)cellEditor.getControl()).getItems(); 
//              for(int i=0;i<items.length;i++) {
//                  if(items[i].equalsIgnoreCase(newType)) {
//                      System.out.println("Will select Combo index i=" + i);
//                      ((CCombo)cellEditor.getControl()).select(i);
//                  }
//              }
            }
        }
    }
}

评论部分是我尝试手动设置组合值的地方,但它不起作用。我认为选择的值可能不是问题,就像我关注ComboBox一样,显示正确的值。我还尝试将自定义LabelProvider添加到ComboBoxCellEditor,但这也不会改变行为(这并不令人惊讶,因为标准实现使用toString方法)

1 个答案:

答案 0 :(得分:1)

在编辑支持setValue方法调用中:

getViewer().update(element, null);

在你打电话之后把它放好:

data.setType(newType);

这告诉表查看器它需要从内容提供者更新元素。