【问题标题】:Trying to switch editor input from within setInput()尝试从 setInput() 中切换编辑器输入
【发布时间】:2015-06-24 17:29:57
【问题描述】:

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

我通过设置marker属性强制marker打开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() );
}

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

【问题讨论】:

    标签: eclipse eclipse-plugin editor


    【解决方案1】:

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

    您使用matchingStrategy 属性在org.eclipse.ui.editors 扩展点上指定此类。

    例如,这是 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 编辑器),但我认为您可以使用它。

    【讨论】:

    • 成功了!谢谢!再次。如果没有你的帮助,我不确定我的项目会如何进行。
    猜你喜欢
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多