问题
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