在 EditingSupport 中编辑 ComboBoxCellEditor

Editing ComboBoxCellEditor in EditingSupport

如果用户 select 是 " " 项,我想让 ComboBoxCellEditor 可编辑。我试着用了两个CellEditor,一个是TextCellEditor,一个是ComboBoxCellEditor。如果用户 select " " 项,CellEditor 将切换为 TextCellEditor。但它不起作用。因为 comboIndex 有一个 ArrayOutOfBoundException。谁能帮我修改我的代码?原EditingSupport的代码如下所示:

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;

public class SecondColEdittingSupport extends EditingSupport {
    private final TableViewer viewer;
    private final CellEditor comboEditor;
    private final CellEditor textEditor;
    Logger logger = Logger.getLogger(this.getClass());
    public static String[] names ={ " ","SMALLINT ", "INTEGER", "BIGINT", 
    "DECIMAL", "NUMERIC", 
            "REAL", "TIMESTAMP", "VARCHAR", "CHAR", "TEXT"};

    public SecondColEdittingSupport(TableViewer viewer) {
        super(viewer);
        this.viewer = viewer;
        comboEditor = new ComboBoxCellEditor(viewer.getTable(), names);
        textEditor = new TextCellEditor(viewer.getTable());
    }

    private int getNameIndex(String name) {
        for (int i = 1; i < SecondColEdittingSupport.names.length; i++) {
            if (SecondColEdittingSupport.names[i].equals(name))
                return i;
        }
        return 0;
    }

    @Override
    protected CellEditor getCellEditor(Object element) {
        String[] str = (String[]) element;
        if (getNameIndex(str[1]) != 0) {
            return comboEditor;
        }
        return textEditor;
    }

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

    @Override
    protected Object getValue(Object element) {
        String[] str = (String[]) element;
        if (getNameIndex(str[1]) != 0) {
            return getNameIndex(str[1]);
        }
        return  str[1];
    }

    @Override
    protected void setValue(Object element, Object userInputValue) {
        String[] str = (String[]) element;
        if (getNameIndex(str[1]) != 0) {
            Integer comboIndex = (Integer) userInputValue;
            str[1] = SecondColEdittingSupport.names[comboIndex];
        } else {
            str[1] = String.valueOf(userInputValue);
        }
        viewer.update(element, null);
    }

}

堆栈跟踪显示如下:

java.lang.ArrayIndexOutOfBoundsException: -1 at com.highgo.admin.migrator.ui.SecondColEdittingSupport.setValue(SecondColEdittingSupport.java:77) at org.eclipse.jface.viewers.EditingSupport.saveCellEditorValue(EditingSupport.java:113) at org.eclipse.jface.viewers.ColumnViewerEditor.saveEditorValue(ColumnViewerEditor.java:433) at org.eclipse.jface.viewers.ColumnViewerEditor.applyEditorValue(ColumnViewerEditor.java:299) at org.eclipse.jface.viewers.ColumnViewerEditor.handleEditorActivationEvent(ColumnViewerEditor.java:416) at org.eclipse.jface.viewers.ColumnViewer.triggerEditorActivationEvent(ColumnViewer.java:677) at org.eclipse.jface.viewers.ColumnViewer.handleMouseDown(ColumnViewer.java:652) at org.eclipse.jface.viewers.ColumnViewer.access[=22=](ColumnViewer.java:648) at org.eclipse.jface.viewers.ColumnViewer.mouseDown(ColumnViewer.java:97) at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:193) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86) at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079) at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817) at org.eclipse.jface.window.Window.runEventLoop(Window.java:818) at org.eclipse.jface.window.Window.open(Window.java:794) at com.highgo.admin.migrator.handler.OpenMigratorHandler.execute(OpenMigratorHandler.java:28) at com.highgo.admin.NavigatorActionExecuteAdvanceTool.executeTool(NavigatorActionExecuteAdvanceTool.java:46) at com.highgo.admin.NavigatorActionExecuteAdvanceTool.run(NavigatorActionExecuteAdvanceTool.java:37) at org.jkiss.dbeaver.ui.ActionUtils.run(ActionUtils.java:267) at org.eclipse.jface.action.Action.runWithEvent(Action.java:473) at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:565) at org.eclipse.jface.action.ActionContributionItem.lambda(ActionContributionItem.java:397) at org.eclipse.jface.action.ActionContributionItem$$Lambda8/1073564434.handleEvent(Unknown Source) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86) at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079) at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1155) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1044) at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153) at org.eclipse.ui.internal.Workbench.lambda(Workbench.java:680) at org.eclipse.ui.internal.Workbench$$Lambda/1717433286.run(Unknown Source) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:594) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:148) at org.jkiss.dbeaver.core.application.DBeaverApplication.start(DBeaverApplication.java:160) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590) at org.eclipse.equinox.launcher.Main.run(Main.java:1499) at org.eclipse.equinox.launcher.Main.main(Main.java:1472)

对于可编辑的ComboBoxCellEditor,我们可以使用一个新的ComboBoxCellEditor来扩展旧的ComboBoxCellEditor,并覆盖它的doSetValue()方法和doGetValue()方法。这可以使 ComboBoxCellEditor 可编辑。

protected void doSetValue(final Object value) {
    if (value instanceof String) {
        ((CCombo) getControl()).setText((String) value);
    } else {
        super.doSetValue(value);
    }
}

@Override
protected Object doGetValue() {
    final Object value = super.doGetValue();
    if (value instanceof Integer && (Integer) value == -1) {
        return ((CCombo) getControl()).getText();
    }
    return value;
}

A ComboBoxCellEditor 本身是可编辑的,因为它使用 JFace Combo(恰好是 CCombo)。可以轻松覆盖所选文本。在这种情况下 setValue returns -1 作为 value (在 OP 的代码中 userInputValue)。

因此,要获得可编辑的 ComboBoxCellEditor 足以在 setValue 方法中处理此值:

@Override
protected void setValue(Object element, Object value) {
    if ((int)value == -1) {
        str[1] = ((CCombo)editor.getControl()).getText();
    } else {
        str[1] = names[(int)value];
    }
    viewer.update(element, null);
}