尝试从setInput()

时间:2015-06-24 17:29:58

标签: eclipse eclipse-plugin editor

我有一个修改多个文件的Eclipse编辑器。当其中任何一个文件出错时,我希望它打开MyEditor,而不是打开它可能打开的标准文本编辑器。

我通过设置标记属性强制标记打开MyEditor:

marker.setAttribute(IDE.EDITOR_ID_ATTR, "com.example.xyz.MyEditorID");

现在,在MyEditor.setInput()内,我有以下代码将输入从传入文件切换到主文件:

    protected void setInput(final IEditorInput input) {
        FileEditorInput fileEditorInput = (FileEditorInput) input; 
        final IFile file = (IFile) input.getAdapter(IFile.class);
        if( file.getName().endsWith(mySuffix) ) {
        // this is a coded values type file. It needs to be opened with the default profile
            fileEditorInput = new FileEditorInput(theCorrectFileName);
        }
        ... 
        super.setInput(fileEditorInput);
        ...
    }

如果输入相等,我将编辑器的equals方法更改为true

public boolean equals(final Object obj) {
    if(obj == null) {
        return false;
    }

    if(this == obj) {
        return true;
    }

    if( !getClass().isInstance(obj) ) {
        return false;
    }
    final MyEdior otherEditor = (MyEditor) obj;
    return getEditorInput().equals( otherEditor.getEditorInput() );
}

但是当我点击我的错误标记时,仍然每次都会打开一个新的编辑器。我做错了什么?

1 个答案:

答案 0 :(得分:0)

equals方法不用于将编辑器与编辑器输入相匹配。相反,您需要使用IEditorMatchingStrategy类来匹配编辑器与输入。

您可以使用org.eclipse.ui.editors属性在matchingStrategy扩展点指定此课程。

例如,这是Manifest.mf / plugin.xml / build.properties编辑器:

<extension
     point="org.eclipse.ui.editors">
  <editor
        default="true"
        name="%editors.pluginManifest.name"
        icon="$nl$/icons/obj16/plugin_mf_obj.gif"
        class="org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor"
        contributorClass="org.eclipse.pde.internal.ui.editor.plugin.ManifestEditorContributor"
        matchingStrategy="org.eclipse.pde.internal.ui.editor.plugin.ManifestEditorMatchingStrategy"
        id="org.eclipse.pde.ui.manifestEditor">
        <contentTypeBinding contentTypeId="org.eclipse.pde.pluginManifest"/>
        <contentTypeBinding contentTypeId="org.eclipse.pde.fragmentManifest"/>
        <contentTypeBinding contentTypeId="org.eclipse.pde.bundleManifest"/>            
  </editor>

匹配策略只有一个方法来匹配编辑器对输入的引用:

public boolean matches(IEditorReference editorRef, IEditorInput input)

所有这些都适用于一次编辑多个文件的编辑器(例如Manifest.mf编辑器),但我认为您可以使用它。