惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
腾讯CDC
Y
Y Combinator Blog
L
LangChain Blog
B
Blog
U
Unit 42
P
Proofpoint News Feed
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 【当耐特】
WordPress大学
WordPress大学
月光博客
月光博客
Vercel News
Vercel News
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 叶小钗

博客园 - 刘刚

Google Map [导入]GEF中修改默认的FeedBack [导入](接上篇)关于GEF Feedback的补充 [导入]GEF中导视图的使用 [导入]修改GEF中Connection的端点 [导入]Eclipse中Job API使用总结如下三 [导入]Eclipse中Job API使用总结如下二 [导入]Eclipse中Job API使用总结如下一 [导入]Eclipse调试框架的学习与理解二 [导入]Eclipse调试框架的学习与理解一 [导入]使用JET自动生成代码 [导入]定义自己的Common Navigator三 [导入]定义自己的Common Navigator二 [导入]定义自己的Common Navigator一 [导入]Eclipse运行(launcher)框架三 [导入]Eclipse运行(launcher)框架二 [导入]Eclipse运行(launcher)框架一 [导入]Eclipse属性页的支持 [导入]GEF理解系列九
[导入]Editor中嵌入Editor
刘刚 · 2008-07-14 · via 博客园 - 刘刚

作者: liugang594  链接:http://liugang594.javaeye.com/blog/208495  发表时间: 2008年06月27日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

Editor中嵌入Editor

                                                            ---Eclipse系列(刘刚)

一、背景

有时按照功能的需要,我们希望能在自己的Editor中嵌入其他已经存在的Editor,比如JavaEditor,比如XMLEditor,以方便的使用这些Editor已经有的一些方便特性。

二、思路

   粗看来,好像无计可施,实际不然。如果你曾经实现过自己的MultiEditor,并且在其他的某一页中是直接引用的其他的Editor,那么你肯定记得:在MultiEditoraddPage()系列方法中,有一个方法是:

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,这里省略过程。然后我定义了一个名为ComboEditorEditor。我要在这其中嵌入JavaEditorTextEditor

我一开始设想代码如下(在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推荐

文章来源:http://liugang594.javaeye.com/blog/208495