创建Eclipse Editor

来源:互联网 发布:ubuntu 16.04 selinux 编辑:程序博客网 时间:2024/06/05 13:56

原文链接:http://www.vogella.com/articles/EclipseEditors/article.html

配置要求:Eclipse 3.7 Indigo

Eclipse使用editor和view来处理数据。editor一般需要用户点击“save”来保存数据改动,而view一般直接执行改动。

在RCP系统中通常采用下列步骤来创建和使用editor:

  • 在perspective中把editor区域设置为可见
  • 创建IEditorInput类
  • 定义一个“org.eclipse.ui.editors” extension point
  • 创建editor类,这个类必须实现IEditorPart接口
IEditorInput是editor模型的轻量级展现,Eclipse会缓冲IEditorInput对象,因此这个对象应该相对较小。Eclipse根据IEditorInput中的equals()方法来判断对应的editor是否已经打开,以及是否需要打开一个新的editor(这个方法很重要)。
editor通过“org.eclipse.ui.editors” extension point定义,editor类必须实现IEditorPart接口,通常会继承EditorPart类。editor会通过init()方法获取IEditorSite和IEditorInput对象,必须调用setInput()和setSite()分别进行设置。init()方法在createPartControl()之前被调用,因此在创建UI的时候,IEditorInput就已经可以被用户使用了。
editor类中的setPartName()方法可以用来设置editor的title,override getTitleToolTip()可以设置tooltip。
editor类中的isDirty()方法可以用来告诉workbench editor的内容是否有改动,我们可以fire一个event来告诉workbench editor的dirty属性已经改动:
firePropertyChange(IEditorPart.PROP.DIRTY);
我们可以通过当前活动页面来打开editor,显然我们需要editor的ID(来告诉Eclipse使用哪一个editor),以及一个EditorInput对象(告诉Eclipse的editor使用哪个model):
page.openEditor(new YourEditorInput(), ID_OF_THE_EDITOR);
如果获取当前活动页面,有这么几种方法:
// If you are in a viewgetViewSite().getPage();// If you are in an commandHandlerUtil.getActiveWorkbenchWindow(event).getActivePage();// Somewhere elsePlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

原文中有一个详细的例子和源代码,有兴趣的可以实践一下。