创建Eclipse Editor

原文链接: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 view
getViewSite().getPage();
// If you are in an command
HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
// Somewhere else
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

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








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值