Search code examples
javaeclipseeclipse-plugineclipse-rcp

Eclipse : Is it possible to open one resource in two different editors as per requirement?


In my current project, there is a situation where we have one file let's say "File1.cfg" in project explorer. There is a default editor "Editor 1" registered using "*.editors" extension.

Requirement Function:

  1. When a user double click on the File1.cfg, it should be opened with an "Editor 1" by default and all the time.
  2. There is one more option provided in Toolbar which will be used to open "Editor 2". And this editor should use the resource "File1.cfg" and display the contents as per the UI.

How can this be achieved in the Eclipse?


Solution

  • A plug-in can specify the editor id to be used when opening an editor. See IWorkbenchPage.openEditor and IDE.openEditor.

    Normally these APIs check for an editor (of any id) already being open on the file. If you want to force the editor with the given id to open regardless you need to use the IWorkbenchPage method:

    IEditorPart openEditor(IEditorInput input, String editorId, boolean activate,
                           int matchFlags)
    

    with the matchFlags value set to:

    IWorkbenchPage.MATCH_ID | IWorkbenchPage.MATCH_INPUT
    

    to only match an existing editor with the same id and input.