您的位置:首页 > 编程语言

RCP:打开一个Editor的新窗口的实现代码

2010-07-18 22:35 288 查看
IStructuredSelection selection = (IStructuredSelection)event.getSelection();
File file = (File)(selection.getFirstElement());
if (file != null) {
IWorkbenchPage page = PlatformUI.getWorkbench().
getActiveWorkbenchWindow().getActivePage();
FileEditorInput input = new FileEditorInput(file);
try {
showFile = file;
page.openEditor(input, MyFileEditor.ID);

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

打开窗口的关键代码为: page.openEditor(input, MyFileEditor.ID);
由此可见打开一个新的Editor需要两个参数,一个是实现了IEditorInput对新建的窗口起描述作用(比如Editor的选项卡窗口的名字,提示语...)的input;另一个是扩展org.eclipse.ui.editors的editor的id,为了使用方便,这个id一般保存在了实现类(本例中MyFileEditor由org.eclipse.ui.editors扩展而来的)的ID成员里.

备忘:运行时出现的一个错误:FileEditorInput input = new FileEditorInput(file);这一句需要特别注意,因为实现IEditorInput接口时不允许传回的参数为空,故在创建input时要先确认参数不为空.否则运行时会抛出错误.
分享到:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: