在EditingSupport

时间:2018-01-03 09:14:46

标签: java swt jface tableviewer

如果用户选择ComboBoxCellEditor项,我想让" "可编辑。我尝试使用两个CellEditor,一个是TextCellEditor,一个是ComboBoxCellEditor。如果用户选择" "项,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           在com.highgo.admin.migrator.ui.SecondColEdittingSupport.setValue(SecondColEdittingSupport.java:77)           在org.eclipse.jface.viewers.EditingSupport.saveCellEditorValue(EditingSupport.java:113)           在org.eclipse.jface.viewers.ColumnViewerEditor.saveEditorValue(ColumnViewerEditor.java:433)           在org.eclipse.jface.viewers.ColumnViewerEditor.applyEditorValue(ColumnViewerEditor.java:299)           在org.eclipse.jface.viewers.ColumnViewerEditor.handleEditorActivationEvent(ColumnViewerEditor.java:416)           在org.eclipse.jface.viewers.ColumnViewer.triggerEditorActivationEvent(ColumnViewer.java:677)           在org.eclipse.jface.viewers.ColumnViewer.handleMouseDown(ColumnViewer.java:652)           在org.eclipse.jface.viewers.ColumnViewer.access $ 0(ColumnViewer.java:648)           在org.eclipse.jface.viewers.ColumnViewer $ 1.mouseDown(ColumnViewer.java:97)           at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:193)           在org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)           在org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)           在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)           在org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238)           在org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817)           在org.eclipse.jface.window.Window.runEventLoop(Window.java:818)           在org.eclipse.jface.window.Window.open(Window.java:794)           在com.highgo.admin.migrator.handler.OpenMigratorHandler.execute(OpenMigratorHandler.java:28)           在com.highgo.admin.NavigatorActionExecuteAdvanceTool.executeTool(NavigatorActionExecuteAdvanceTool.java:46)           在com.highgo.admin.NavigatorActionExecuteAdvanceTool.run(NavigatorActionExecuteAdvanceTool.java:37)           在org.jkiss.dbeaver.ui.ActionUtils $ 1.run(ActionUtils.java:267)           在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 $ 4(ActionContributionItem.java:397)           at org.eclipse.jface.action.ActionContributionItem $$ Lambda $ 148 / 1073564434.handleEvent(Unknown Source)           在org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)           在org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)           在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)           在org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238)           在org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817)           at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine $ 5.run(PartRenderingEngine.java:1155)           在org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)           在org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1044)           在org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153)           在org.eclipse.ui.internal.Workbench.lambda $ 3(Workbench.java:680)           在org.eclipse.ui.internal.Workbench $$ Lambda $ 14 / 1717433286.run(未知来源)           在org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)           在org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:594)           在org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:148)           在org.jkiss.dbeaver.core.application.DBeaverApplication.start(DBeaverApplication.java:160)           在org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)           在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)           在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)           在org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)           在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)           在org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)           在org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)           在org.eclipse.equinox.launcher.Main.run(Main.java:1499)           在org.eclipse.equinox.launcher.Main.main(Main.java:1472)

2 个答案:

答案 0 :(得分:0)

对于可编辑的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; } 可编辑。

$("div").on("mouseup",function(){

  var that = $(this)[0];
  
  // Get the position
  var pos = getCaretPosition(that);
  //console.log(pos);

  var otherDiv;
  if($(this).attr("id")=="t1"){
    otherDiv = $("#t2")[0];
  }else{
    otherDiv = $("#t1")[0];
  }

  // Set the position
  otherDiv.focus();
  setCaretPosition(otherDiv,pos);
});

// From https://stackoverflow.com/a/6249440/2159528
function setCaretPosition(editableDiv,pos){
  var range = document.createRange();
  var sel = window.getSelection();
  range.setStart(editableDiv.childNodes[0], pos);
  range.collapse(true);
  sel.removeAllRanges();
  sel.addRange(range);
}

// From https://stackoverflow.com/a/3976125/2159528
function getCaretPosition(editableDiv) {
  var caretPos = 0,
      sel, range;
  if (window.getSelection) {
    sel = window.getSelection();
    if (sel.rangeCount) {
      range = sel.getRangeAt(0);
      if (range.commonAncestorContainer.parentNode == editableDiv) {
        caretPos = range.endOffset;
      }
    }
  } else if (document.selection && document.selection.createRange) {
    range = document.selection.createRange();
    if (range.parentElement() == editableDiv) {
      var tempEl = document.createElement("span");
      editableDiv.insertBefore(tempEl, editableDiv.firstChild);
      var tempRange = range.duplicate();
      tempRange.moveToElementText(tempEl);
      tempRange.setEndPoint("EndToEnd", range);
      caretPos = tempRange.text.length;
    }
  }
  return caretPos;
}

答案 1 :(得分:0)

ComboBoxCellEditor本身是可编辑的,因为它使用的是JFace Combo(完全是CCombo)。人们可以轻松覆盖所选文本。在这种情况下,setValue会将-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);
}