Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force a second editor to be opened for the same input?

I'm writing an Eclipse plugin that allows easier resource editing for Android. When user clicks on any XML resource file inside the project, an editor opens that allows editing all the resources in the project at once.

I'd like to add a capability to open the same file in a separate, default Android resource editor. I know the id of that editor, but I don't have access to its class.

Calling IDE.openEditor does nothing, because an editor is already opened for that file, even though I specify the id of another, Android editor.

How to force Eclipse to open another editor for the same input?

On the other hand, is it possible to embed another editor in a MultiPageEditorPart if I have access only to its id, not its class?

like image 358
Fixpoint Avatar asked Sep 25 '12 23:09

Fixpoint


2 Answers

The IDE.openEditor methods call at the end the corresponding IWorkbenchPage methods to open the editor.

The method that could be useful in your case is 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;

you need to call it and pass it MATCH_ID | MATCH_INPUT so that it takes the editor id into account when trying to determine whether the existing editor should be reused or a new one should be created.

like image 56
mhussein Avatar answered Oct 19 '22 22:10

mhussein


The editor extension point org.eclipse.ui.editors allows to add a matchingStrategy to the extension. This enables you to influence the behavior when Eclipse tries to determine whether an editor of a given ID and a given editor input is already open.

Implementation is fairly easy. You only have to provide an implementation of interface org.eclipse.ui.IEditorMatchingStrategy. It has only one method

boolean matches(IEditorReference editorRef, IEditorInput input);

If you return false here, Eclipse will open a new editor every time, even if editor ID and editor input are equal.

like image 44
Tillmann Seidel Avatar answered Oct 20 '22 00:10

Tillmann Seidel