[导入]Editor中嵌入Editor
作者: liugang594 链接:http://liugang594.javaeye.com/blog/208495 发表时间: 2008年06月27日
声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
Editor中嵌入Editor
---Eclipse系列(刘刚)
一、背景
有时按照功能的需要,我们希望能在自己的Editor中嵌入其他已经存在的Editor,比如JavaEditor,比如XMLEditor,以方便的使用这些Editor已经有的一些方便特性。
二、思路
粗看来,好像无计可施,实际不然。如果你曾经实现过自己的MultiEditor,并且在其他的某一页中是直接引用的其他的Editor,那么你肯定记得:在MultiEditor的addPage()系列方法中,有一个方法是:
public int addPage(IEditorPart editor, IEditorInput input);
这个方法表明,可以直接把一个Editor做为一个page加到MultiEditor中去。所以我们的实现可以参考MultiEditor。
我们可以找到MultiEditor中,addPage(editor,input)部分的源码:
public void addPage(int index, IEditorPart editor, IEditorInput input)
throws PartInitException {
IEditorSite site = createSite(editor);
// call init first so that if an exception is thrown, we have created no
// new widgets
editor.init(site, input);
Composite parent2 = new Composite(getContainer(),
getOrientation(editor));
parent2.setLayout(new FillLayout());
editor.createPartControl(parent2);
……
}
看上部分代码,我们可以知道:要把一个editor A作为某个editor B的一部分加进去,我们首先需要有一个A的对象,然后这个A对象要调用init(site,input)方法,最后要创建一个面板,调用A对象的createPartControl(parent)方法,以在这个面板上画出A的界面。
三、实现
有了以上思路,下面我们就可以在自己的editor中嵌入其他的editor了。首先我们要定义一个自己的editor,这里省略过程。然后我定义了一个名为ComboEditor的Editor。我要在这其中嵌入JavaEditor和TextEditor。
我一开始设想代码如下(在createPartControl()方法里):
public void createPartControl(Composite parent) {
try {
parent.setLayout(new GridLayout());
// TODO Auto-generated method stub
TextEditor textEditor = new TextEditor();
textEditor.init(site, input);
Composite parent2 = new Composite(parent,
textEditor.getOrientation());
parent2.setLayout(new FillLayout());
parent2.setLayoutData(new GridData(GridData.FILL_BOTH));
textEditor.createPartControl(parent2);
CompilationUnitEditor javaEditor = new CompilationUnitEditor();
javaEditor.init(site, input);
Composite parent3 = new Composite(parent,
javaEditor.getOrientation());
parent3.setLayout(new FillLayout());
parent3.setLayoutData(new GridData(GridData.FILL_BOTH));
javaEditor.createPartControl(parent3);
} catch (PartInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
其中,init(site,input)方法如下:
private IEditorSite site;
private IEditorInput input;
@Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
this.site = site;
this.input = input;
setSite(site);
setInput(input);
}
需要注意的是:我们在init(site,input)方法中,需要显式的调用setSite(site)和setInput(input),否则在打开editor的时候,总是会有site不对或者是input为空的错误。
四、测试
最后我们可以试一下效果:
Figure 1
五、后记
如果需要实现dirty功能,我们需要监听每一个editor的变化。可以实现如下:
首先增加监听:
textEditor.addPropertyListener(this);
javaEditor.addPropertyListener(this);
这里,我们让我们的editor实现IPropertyListener接口。
然后就是实现接口的方法,如下:
public void propertyChanged(Object source, int propId) {
firePropertyChange(propId);
}
最后实现isDirty()方法:
private List<IEditorPart> nestedEditors = new
ArrayList<IEditorPart>();
public boolean isDirty() {
// use nestedEditors to avoid SWT requests; see bug 12996
for (IEditorPart part:nestedEditors) {
if (part.isDirty()) {
return true;
}
}
return false;
}
其中nestedEditors中保存了所有的内嵌的editor。
本文的讨论也很精彩,浏览讨论>>
JavaEye推荐
- 北京: 千橡集团暨校内网诚聘软件研发工程师
- 快来参加7月17日在成都举行的SOA中国技术论坛
- 搜狐网站诚聘Java、PHP和C++工程师
- JavaEye问答大赛开始了! 从6月23日 至 7月6日,奖品丰厚 !
文章来源:http://liugang594.javaeye.com/blog/208495