简体   繁体   English

Eclipse:如何以编程方式打开编辑器

[英]Eclipse: How to open an editor programmatically

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);
....

First you must define your editor to Eclipse using the org.eclipse.ui.editors extension point:首先,您必须使用org.eclipse.ui.editors扩展点将编辑器定义为 Eclipse:

<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在指定您的编辑器 ID 的IFile上打开编辑器,或者对于指定的extension ,编辑器将是默认值,您可以使用

IDE.openEditor(page, file);

You can also use the contentTypeBinding child element of editor to specify content types then editor will handle.您还可以使用editorcontentTypeBinding子元素来指定内容类型,然后编辑器将处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM