首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 开源 FAQ 第二书店 博文视点 程序员
频道: 研发 数据库 中间件 信息化 视频 .NET Java 游戏 移动 服务: 人才 外包 培训
    图书品种:235680
       
热门搜索: ASP.NET Ajax Spring Hibernate Java

20.5  加入编辑器(editors)

本节实例如图20.5所示,双击视图1中的列表项,将在透视图中添加相应的编辑器。这种效果就像在Eclipse中双击Java源文件,打开文件的编辑器一样。

图20.5  编辑器效果图

和以前一样,先来修改plugin.xml文件将编辑器的扩展点加入,然后再创建相应的编辑器类,最后编写列表双击的事件代码。

20.5.1  修改plugin.xml文件,设置3个编辑器的扩展点

<extension point="org.eclipse.ui.editors">

  <editor

                   name="中国Editor"

                   icon="icons/project.gif"

                   class="cn.com.chengang.myplugin.ChinaEditor"

                   id="cn.com.chengang.myplugin.ChinaEditor">

  </editor>

  <editor

                   name="美国Editor"

                   icon="icons/prev.gif"

                   class="cn.com.chengang.myplugin.UsaEditor"

                   id="cn.com.chengang.myplugin.UsaEditor">

  </editor>

  <editor

                   name="法国Editor"

                   icon="icons/remove.gif"

                   class="cn.com.chengang.myplugin.FranceEditor"

                   id="cn.com.chengang.myplugin.FranceEditor">

  </editor>

</extension>

代码说明:编辑器的扩展点是org.eclipse.ui.editors,它各项的含义和视图扩展点基本一样,可参照视图扩展点的说明。这里强调一点:icon是必填项。

20.5.2  创建3个编辑器类

在plugin.xml中提前设置了编辑器对应的3个编辑器类:ChinaEditor、UsaEditor、FranceEditor,本小节就来在包cn.com.chengang.myplugin中创建这3个类。

编辑器类必须实现IEditorPart接口,但通常是继承抽象类EditorPart类(EditorPart是IEditorPart的子类)。继承EditorPart的子类必须实现父类的7个方法,在此先实现方法init、createPartControl,其他方法空实现。本例只给出了ChinaEditor的代码,UsaEditor、FranceEditor与之类似,读者可查阅配书光盘的代码。

public class ChinaEditor extends EditorPart {

         // Editor的初始化方法。本方法前两句是固定不变的

         public void init(IEditorSite site, IEditorInput input) throws PartInitException {

                   System.out.println("init");

                   setSite(site);

                   setInput(input);

                   // 下一句设置Editor标题栏的显示名称,否则名称用plugin.xml中的name属性

                   // setPartName(input.getName());

                   // 下一句设置Editor标题栏的图标,否则会自动使用一个默认的图标

                   // setTitleImage(input.getImageDescriptor().createImage());

         }

         // 在此方法中创建Editor中的界面组件

         public void createPartControl(Composite parent) {

                   System.out.println("createPartControl");

                   Composite topComp = new Composite(parent, SWT.NONE);

                   topComp.setLayout(new FillLayout());

                   Text text = new Text(topComp, SWT.BORDER);

                   text.setText("中国之编辑器");

         }

         // 此五个抽象类的方法以后讲解,现在让它们空实现

         public void doSave(IProgressMonitor monitor) {}

         public boolean isSaveAsAllowed() {          return false; }

         public void doSaveAs() {}

         public boolean isDirty() { return false; }

         public void setFocus() {}

}

20.5.3  创建IEditorInput

获取视图对象是用IWorkbenchPage的findView方法,方法参数是视图在plugin.xml中的id标识。获取编辑器对象是用findEditor方法,但该方法的参数却不是id标识,而是一个IEditorInput对象。另外,加载一个编辑器是用IWorkbenchPage的openEditor (editorInput, editorID)方法。

文本框:  
图20.6  编辑器的组成
由上可知,一个编辑器要对应一个IEditorInput和EditorPart,而且在IWorkbenchPage中是根据IEditorInput来取得EditorPart,如图20.6所示。

在本小节将要创建3个Editor相对应的IEditorInput。在这里只给出了ChinaEditor对应的IEditorInput,其他两个与之类似,读者可查阅配书光盘的代码。

public class ChinaEditorInput implements IEditorInput {

         // 返回true,则打开该编辑器后它会出现在Eclipse主菜单“文件”

         // 最下部的最近打开的文档栏中。返回flase则不出现在其中

         public boolean exists() {

                   return true;

         }

         // 编辑器标题栏的图标,不过它还需要在ChinaEditor中用

         // setTitleImage方法设置,才能出现在标题栏中

         public ImageDescriptor getImageDescriptor() {

                   return WorkbenchImages.getImageDescriptor(IWorkbenchGraphicConstants.IMG_ET
OOL_HOME_NAV
);

         }

         // 编辑器标题栏的显示名称,和上面的getImageDescriptor一样也要

         // 在ChinaEditor中用setPartName方法设置,才能出现在标题栏中

         public String getName() {

                   return "中国的编辑器";

         }

         // 编辑器标题栏的小黄条提示文字,不需像getName那样在ChinaEditor中再设置

         public String getToolTipText() {

                   return "这是视图1列表中的中国项对应的编辑器";

         }

         // 返回一个可以用做保存本编辑输入数据状态的对象

         public IPersistableElement getPersistable() {

                   return null;

         }

         // 得到一个编辑器的适配器

         // IAdaptable a = new ChinaEditorInput();

         // IFoo x = (IFoo)a.getAdapter(IFoo.class);

         // if (x != null) [用x来做IFoo的事情....]

         public Object getAdapter(Class adapter) {

                   return null;

         }

}

20.5.4  打开编辑器

有了EditorPart和IEditorInput后,就可以在Eclipse中打开编辑器了。本例是要实现双击视图1的列表项,则打开对应的编辑器,因此在View1类的List对象添加一个鼠标双击事件监听器。另外还要考虑到,如果已经打开了列表项对应的编辑器,则下次再双击时就不应再打开该项的编辑器,而是将其设成当前编辑器。

得到编辑器对象:IEditorPart editor = IWorkbenchPage.findEditor(IEditorInput);

打开编辑器:IWorkbenchPage.openEditor(IEditorInput, editorID);

View1.java对List对象添加的事件代码如下:

list.addMouseListener(new MouseAdapter() {

         private ChinaEditorInput chinaEditorInput = new ChinaEditorInput();

         private UsaEditorInput usaEditorInput = new UsaEditorInput();

         private FranceEditorInput franceEditorInput = new FranceEditorInput();

         public void mouseDoubleClick(MouseEvent e) {

                   // 根据不同列表项得到其相应的editorInput对象和editorID,其中

                   // editorID指该编辑器在plugin.xml文件中设置id标识值

                   List list = (List) e.getSource();// 由MouseEvent得到列表对象

                   String listStr = list.getSelection()[0];// 得到当前列表项的字符

                   IEditorInput editorInput = null;

                   String editorID = null;

                   if (listStr.equals("中国")) {

                            editorInput = chinaEditorInput;

                            editorID = "cn.com.chengang.myplugin.ChinaEditor";

                   } else if (listStr.equals("美国")) {

                            editorInput = usaEditorInput;

                            editorID = "cn.com.chengang.myplugin.UsaEditor";

                   } else if (listStr.equals("法国")) {

                            editorInput = franceEditorInput;

                            editorID = "cn.com.chengang.myplugin.FranceEditor";

                   }

                   // 如果editorInput或editorID为空则中断返回

                   if (editorInput == null || editorID == null)

                            return;

                   // 取得IWorkbenchPage,并搜索使用editorInput对象对应的编辑器

                   IWorkbenchPage workbenchPage = getViewSite().getPage();

                   IEditorPart editor = workbenchPage.findEditor(editorInput);

                   // 如果此编辑器已经存在,则将它设为当前的编辑器(最顶端),否则

                   // 重新打开一个编辑器

                   if (editor != null) {

                            workbenchPage.bringToTop(editor);

                   } else {

                            try {

                                     workbenchPage.openEditor(editorInput, editorID);

                            } catch (PartInitException e2) {

                                     e2.printStackTrace();

                            }

                   }

         }

});

程序说明:在本程序中为了便于理解,使用了if…else这种简单的方式来判断被双击的列表项,这适合列表项较少的情况,如果列表项太多,则代码会相当长。解决这个问题,可将IEditorInput中没用到的getName方法借用一下,把eidtorID放到此方法里面。这样就可以用下面的方式来得到IEditorInput和eidtorID了。

String key = "" + list.getSelectionIndex();

IEditorInput editorInput = (IEditorInput) list.getData(key);

String eidtorID = editorInput.getName();

20.5.5  总结

在实际开发中很多界面都是创建在编辑器上,虽然在这里只讲了最常用的编辑器使用方法,但已足够应付大部分开发的需要。如果你想了解更多关于编辑器的信息,可以查阅编辑器的帮助文档,它在帮助中的位置是“平台插件开发者指南→程序员指南→编辑器”。

查看所有评论(0)条】

最近评论



正在载入评论列表...
热点评论