如何强制为同一输入打开第二个编辑器?

时间:2012-09-25 23:06:43

标签: eclipse eclipse-plugin

我正在编写一个Eclipse插件,可以更轻松地编辑Android资源。当用户单击项目中的任何XML资源文件时,将打开一个编辑器,允许一次编辑项目中的所有资源。

我想添加一个功能,在一个单独的默认Android资源编辑器中打开同一个文件。我知道那个编辑器的id,但是我没有访问它的类。

调用IDE.openEditor什么都不做,因为已经为该文件打开了一个编辑器,即使我指定了另一个Android编辑器的id。

如何强制Eclipse为同一输入打开另一个编辑器?

另一方面,如果我只能访问其id,而不是它的类,是否可以在MultiPageEditorPart中嵌入另一个编辑器?

2 个答案:

答案 0 :(得分:4)

IDE.openEditor方法最后调用相应的IWorkbenchPage方法来打开编辑器。

在您的情况下可能有用的方法是 org.eclipse.ui.IWorkbenchPage.openEditor(IEditorInput, String, boolean, int)

    /**
     * Opens an editor on the given input.
     * <p>
     * If this page already has an editor open that matches the given input
     * and/or editor id (as specified by the matchFlags argument), that editor
     * is brought to the front; otherwise, a new editor is opened. Two editor
     * inputs are considered the same if they equal. See
     * <code>Object.equals(Object)<code>
     * and <code>IEditorInput</code>. If <code>activate == true</code> the editor
     * will be activated.  
     * </p><p>
     * The editor type is determined by mapping <code>editorId</code> to an editor
     * extension registered with the workbench.  An editor id is passed rather than
     * an editor object to prevent the accidental creation of more than one editor
     * for the same input. It also guarantees a consistent lifecycle for editors,
     * regardless of whether they are created by the user or restored from saved 
     * data.
     * </p>
     * 
     * @param input the editor input
     * @param editorId the id of the editor extension to use
     * @param activate if <code>true</code> the editor will be activated
     * @param matchFlags a bit mask consisting of zero or more of the MATCH_* constants OR-ed together
     * @return an open editor, or <code>null</code> if an external editor was opened
     * @exception PartInitException if the editor could not be created or initialized
     * 
     * @see #MATCH_NONE
     * @see #MATCH_INPUT
     * @see #MATCH_ID
     * @since 3.2
     */
    public IEditorPart openEditor(final IEditorInput input,
        final String editorId, final boolean activate, final int matchFlags)
        throws PartInitException;

你需要调用它并传递它MATCH_ID | MATCH_INPUT,以便在尝试确定是否应该重用现有编辑器或是否应该创建新编辑器时考虑编辑器ID。

答案 1 :(得分:2)

编辑器扩展点org.eclipse.ui.editors允许向扩展程序添加matchingStrategy。这使您能够在Eclipse尝试确定给定ID的编辑器和给定编辑器输入是否已打开时影响行为。

实施相当容易。您只需提供接口org.eclipse.ui.IEditorMatchingStrategy的实现。它只有一种方法

boolean matches(IEditorReference editorRef, IEditorInput input);

如果你在这里返回false,Eclipse每次都会打开一个新的编辑器,即使编辑器ID和编辑器输入是相同的。