Eclipse: How to open an editor programmatically

断了今生、忘了曾经 提交于 2019-12-08 02:01:19

问题


I'm wondering how I can open an editor programmatically. I first created the appropriated file and then I want to open the editor for this type of file. But I'm not able to open the editor then.

...
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
PlcEditor editor = new PlcEditor(emfResource);
page.openEditor(editor, "test");
...

I already had the following solution (which works), but here I wasn't able to call the constructr of my editor:

....
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart openEditor = IDE.openEditor(page, plcFile);
....

回答1:


First you must define your editor to Eclipse using the org.eclipse.ui.editors extension point:

<extension
     point="org.eclipse.ui.editors">
  <editor
        name="Sample Multi-page Editor"
        extensions="mpe"
        icon="icons/sample.gif"
        contributorClass="tested.editors.MultiPageEditorContributor"
        class="tested.editors.MultiPageEditor"
        id="tested.editors.MultiPageEditor">
  </editor>
</extension>

(above is as created by the provided multi-page editor example).

You can then use:

IDE.openEditor(page, file, "tested.editors.MultiPageEditor");

to open the editor on an IFile specifying your editor id, or for the extension specified the editor will be the default and you can just use

IDE.openEditor(page, file);

You can also use the contentTypeBinding child element of editor to specify content types then editor will handle.



来源:https://stackoverflow.com/questions/19817742/eclipse-how-to-open-an-editor-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!