1

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?

  • You can right-click on the file and use the "Open With..." menu at any point. Dedicating space on the toolbar is kind of a waste. – nitind Mar 03 '21 at 16:32
  • 1
    A plug-in can specify the editor id to be used when opening an editor. See `IWorkbenchPage.openEditor` and `IDE.openEditor` – greg-449 Mar 03 '21 at 17:28
  • @greg-449 I am currently using "IDE.openEditor" option with the "Editor 2" id. But it is still doesn't open with the "Editor 2". It doesn't call "Editor 2"s "init" method as well. If "Editor 1" is already open then on mouse click in toolbar, it just bring the "Editor 1" to front. – bhimashankar poddar Mar 04 '21 at 03:15
  • @nitind Its the project requirement that the "Open With" should happen programmatically – bhimashankar poddar Mar 04 '21 at 03:15
  • Which version of openEditor are you calling? What's the full signature? – nitind Mar 04 '21 at 04:17
  • @nitind Do you mean Eclipse IDE version? Eclipse Luna 4.4. IDE.openEditor(IWorkbenchPage page, IFile input, String editorId). – bhimashankar poddar Mar 04 '21 at 04:23

1 Answers1

0

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.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Works perfect like "Automatic Open with" functionality. Small issue is that if "Editor 1" is already open and when user selects option from Toolbar to open "Editor 2" , it opens "Editor 2" but when we double click on same resource "File 1.cfg" in "Project Explorer", "Editor 1" doesn't come to front as "Editor 2 is already in front" . – bhimashankar poddar Mar 04 '21 at 17:16