`

编写Eclipse扩展点

阅读更多
扩展(Extension)是Eclipse中一个关键的机制,plug-in利用扩展向Eclipse平台添加新功能。但是扩展不能随意地创建,必须按照扩展点(extension point)定义的规范进行明确的声明,Eclipse才能认出这些扩展。我们不仅可以使用Eclipse提供的众多现成的扩展点,而且还可以定义新的扩展点,并在该扩展点上进行扩展。
当然,扩展点的定义比较复杂。不过Eclipse为用户提供了图形化的编辑界面,我们只要输入一些信息,Eclipse就会自动生成代码,使扩展点的定义变得非常简单。
下面我们就来看看如何在Eclipse中创建一个新的扩展点,并在这个扩展点上进行扩展。
我们需要做以下的工作:
1.设计该扩展点
2.定义扩展点,即编写扩展点的清单文件
3.编写代码来载入该扩展点的扩展
我们以创建workList扩展点为例,进行详细介绍。
worklist完成的功能是:创建一个view,在其中以树状显示系统中可用的功能模块,通过双击某个模块节点,执行该扩展定义的方法(method)。其实相当于一个控制台,通过控制台来运行不同的功能。
由于Eclipse是由一个运行时核心(runtime core)和众多插件组成的,我们也将workList扩展点定义在一个插件中,有关workList的代码文件也放在这个插件中,这样便于查找和修改,也不影响Eclipse本身的代码。

1. 定义扩展点
首先我们要创建一个存放新扩展点信息的插件net.softapp.worklist,这个插件对org.eclipse.ui.views进行扩展,以下是插件的plugin.xml文件在views扩展点的信息:
<extension
point="org.eclipse.ui.views">
<category
name="WorkListCategory"
id="WorkListCategory"/>
<view
icon="icons/sample.gif"
class="net.softapp.internal.worklist.WorkListView"
category="WorkListCategory"
name="WorkList视图"
id="net.softapp.internal.worklist.WorkListView"/>
</extension>
这样就可以通过“window->show view->other”,在弹出的“Show view”对话框中选择“WorkList视图”,打开视图,我们用这个视图显示workList扩展点的所有扩展信息。“Show View”对话框显示了Eclipse中定义所有视图,即所有org.eclipse.views扩展点的扩展。了解这一点很重要,因为我们在编写workList扩展点代码时,就可以模仿甚至拷贝views扩展点的代码。
下面,我们要在net.softapp.worklist插件中定义workList扩展点。
扩展点的定义文件按照Eclipse的存放方式,一般存放在schema目录下,我们把文件命名为worklist.exsd。内容如下,此内容由PDE生成:
<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
<schema targetNamespace="mtn.esip.worklist">
<annotation>
<appInfo>
<meta.schema plugin="net.softapp.worklist" id="workList" name="workList"/>
<!--通过这个定义,我们可以看出,定义的扩展点的id是 net.softapp.worklist.workList,以后引用时要注意,同时注意大小写-->
</appInfo>
<documentation>
[Enter description of this extension point.]
</documentation>
</annotation>

<element name="extension">
<complexType>
<choice minOccurs="0" maxOccurs="unbounded">
<element ref="category" minOccurs="0" maxOccurs="1"/>
<element ref="worklist" minOccurs="0" maxOccurs="1"/>
</choice>
<attribute name="point" type="string" use="required"><!--定义point-->
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="id" type="string"><!--定义id-->
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="name" type="string"><!--定义name-->
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
</complexType>
</element>

<!--定义category-->
<element name="category">
<complexType>
<attribute name="name" type="string"><!--定义category/name-->
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="id" type="string"><!--定义category/id。引用category时,必须指出应用的id,而name给出了一个可供显示的直观的名字-->
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="parentCategory" type="string"><!--定义父category,也就是说我们的category可以嵌套形成树状结构-->
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
</complexType>
</element>

<!--定义worklist,注意大小写-->
<element name="worklist">
<complexType>
<attribute name="name" type="string"><!--定义worklist/name,可供显示的直观的名字-->
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="icon" type="string"><!--定义worklist/icon,可供显示的直观的图标-->
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="category" type="string">!--定义worklist/category,存放的category位置。如果引用嵌套形式的category,则采用 parent_id/child_id的形式 -->
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="class" type="string"><!--定义worklist/class,实现功能的类名称-->
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute kind="java"/>
</appInfo>
</annotation>
</attribute>
<attribute name="id" type="string" use="required"><!--定义worklist/id,唯一标志-->
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
</complexType>
</element>
<!--以下内容为PDE自动生成,与我们的编程无关-->
<annotation>
<appInfo>
<meta.section type="since"/>
</appInfo>
<documentation>
[Enter the first release in which this extension point appears.]
</documentation>
</annotation>

<annotation>
<appInfo>
<meta.section type="examples"/>
</appInfo>
<documentation>
[Enter extension point usage example here.]
</documentation>
</annotation>

<annotation>
<appInfo>
<meta.section type="apiInfo"/>
</appInfo>
<documentation>
[Enter API information here.]
</documentation>
</annotation>

<annotation>
<appInfo>
<meta.section type="implementation"/>
</appInfo>
<documentation>
[Enter information about supplied implementation of this extension point.]
</documentation>
</annotation>

<annotation>
<appInfo>
<meta.section type="copyright"/>
</appInfo>
<documentation>
</documentation>
</annotation>

</schema>
这样我们就定义好了扩展的属性。
然后在plugin.xml加入:
<extension-point id="workList" name="workList" schema="schema/workList.exsd"/>
就定义好了!

2. 实现扩展
定义完扩展之后,接下来要编写解析此扩展的相关代码。可喜的是,Eclipse为我们提供了大量的API可以调用,省下了若干代码的编写。另外我们还可以借鉴Eclipse实现的其他代码,通过模仿来编写我们自己的解析代码。本例参考了View的解析部分。同View,我们定义了WorkListDescriptor,WorkListRegistry,WorkListRegistryReader.其中WorkListDescriptor完成对上述定义的解析,WorkListRegistry存放了其他插件对workList扩展的相关信息,WorkListRegistryReader则从WorkListRegistry读取信息供我们使用。
此处代码从略,具体请参考View实现部分的ViewDescriptor,ViewRegistry,ViewRegistryReader相关代码。

3. 编写界面部分
根据1对View的扩展,我们需要编写界面部分。此处请参考View插件的编写。我们在此对WorkListPlugin添加了一个方法用以从注册表中读取扩展信息:
public IWorkListRegistry getWorkListRegistry() {
if (workListRegistry == null) {
workListRegistry = new WorkListRegistry();
try {
WorkListRegistryReader reader = new WorkListRegistryReader();
reader.readWorkList(Platform.getExtensionRegistry(), workListRegistry);
} catch (CoreException e) {
// cannot safely show a dialog so log it
WorkbenchPlugin.log("Unable to read workList registry.", e.getStatus()); //$NON-NLS-1$
}
}
return workListRegistry;
}
其中WorkListRegistryReader.readWorkList定义如下:
/**
* Read the workList extensions within a registry.
*/
public void readWorkList(IExtensionRegistry in, WorkListRegistry out)
throws CoreException {
// this does not seem to really ever be throwing an the exception
workListRegistry = out;
readRegistry(in, WorkListPlugin.getDefault().getPluginId(), "workList");
out.mapWorkListToCategories();
}
可见,我们不需要编写复杂的代码就可以读取注册表中存放的扩展信息!
我们对workList扩展的显示是采用了TreeView,代码如下(WorkListView):
protected TreeViewer createViewer(Composite parent) {
TreeViewer viewer =
new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setUseHashlookup(true);
viewer.setContentProvider(new WorkListContentProvider());
viewer.setLabelProvider(new WorkListLabelProvider());
workListReg = WorkListPlugin.getDefault().getWorkListRegistry();
viewer.setInput(workListReg);
initListeners(viewer);
return viewer;
}
这样,就可以实现显示了。
那么,怎样实现选择某个扩展后,通过双击执行其功能呢?我们对TreeViewer添加了鼠标双击事件支持,关键代码如下:
protected void handleDoubleClick(DoubleClickEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
Object element = selection.getFirstElement();

TreeViewer viewer = getWorkListViewer();
if (viewer.isExpandable(element)) {
viewer.setExpandedState(element, !viewer.getExpandedState(element));
}else {
WorkListDescriptor workList = (WorkListDescriptor)element;
try {
IWorkListPart workListPart = (IWorkListPart) workList.createWorkList();
workListPart.run();
} catch (CoreException e) {
// should add something to handle the exception
}
}

}
其中IWorkListPart很简单,使所有实现workList扩展必须实现的接口:
public interface IWorkListPart {

public void run();

}
只有一个run方法(可以自行添加其他的支持)。
其中WorkListDescriptor.createWorkList方法实现根据class的字符串创建一个对象,也是超级简单,因为Eclipse已经为我们编好了:
public Object createWorkList() throws CoreException {
Object obj = WorkbenchPlugin.createExtension(configElement, "class");
return obj;
}
这样就可以执行扩展的功能了。
但是别忘了,还要编写pluin.xml,否则Eclipse可不认吆:
<extension
point="net.softapp.worklist.workList">
<category
name="HelloTest"
id="HelloTest"/>
<worklist
icon="icons/example.ico"
class="net.softapp.internal.worklist.Hello"
category="HelloTest"
name="Hello"
id="net.softapp.internal.worklist.Hello"/>
</extension>

4.测试新扩展点

OK,开始运行Eclipse的plugin调试环境,打开WorkList视图,看看在树状列表里是不是有一个HelloTest目录,下面有Hello。双击它,你编写的代码出来了吧!
分享到:
评论
1 楼 Breather.杨 2012-07-06  
http://rfid.hongbo.net.cn/products/jira

相关推荐

    Eclipse权威开发指南2.pdf

    9.2 扩展和扩展点入门...... 217 9.3 实现插件的基本步骤...... 220 9.3.1 声明自己的插件清单..... 222 9.3.2 定义自己的插件类..... 225 9.3.3 安装自己的插件..... 226 9.4 使用插件开发环境...... 227 9.4.1...

    Eclipse插件开发之添加简单的GUI元素

    编写Eclipse插件的方法很简单,你只需要编写一个应用程序,并把它"添加"到Eclipse上。但是与编写音乐类似,在能够创作一部著作之前,首先必须学习大量的相关知识。本文将讨论少许几个简单的GUI元素:·工具条按钮·...

    eclipse 开发c/c++

    CDT 扩展了标准的 Eclipse Debug 视图, 使之具备了调试 C/C++ 代码的功能。Debug 视图允许您在工作台中管理程序的调试或运行。要开始调试当前项目,只要切换到 Debug 视图, 您将能够在代码中设置(并在执行过程中...

    Eclipse权威开发指南3.pdf

    9.2 扩展和扩展点入门...... 217 9.3 实现插件的基本步骤...... 220 9.3.1 声明自己的插件清单..... 222 9.3.2 定义自己的插件类..... 225 9.3.3 安装自己的插件..... 226 9.4 使用插件开发环境.........

    Eclipse权威开发指南1.pdf

    9.2 扩展和扩展点入门...... 217 9.3 实现插件的基本步骤...... 220 9.3.1 声明自己的插件清单..... 222 9.3.2 定义自己的插件类..... 225 9.3.3 安装自己的插件..... 226 9.4 使用插件开发环境.........

    Eclipse RCP Plugin 开发自学入门指南(CEC首发)

    1.2 ECLIPSE RCP 建设风格——插件,扩展和扩展点 ..................................................................... 9 1.3 RCP与PLUGIN ....................................................................

    Eclipse_Swt_Jface_核心应用_部分19

    1.6.1 JFace是SWT的扩展 9 1.6.2 Eclipse的UI界面基于JFace 10 1.7 本章小结 10 第2章 配置SWT开发环境 11 2.1 下载和安装Eclipse 11 2.1.1 Eclipse下载页面介绍 11 2.1.2 下载Eclipse 12 2.1.3 安装...

    如何利用Xmind进行文献的整理

    XMind采用Java语言开发,具备跨平台运行的性质 [1] ,且基于 Eclipse RCP体系结构,可支持 插件 ,插件通过编写XML 清单 文件可以扩展 系统定义 好的扩展点。 本文件展示了如何利用xmind进行文献的整理和汇总

    Python打造出适合自己的定制化Eclipse IDE

    然而要想添加一小点额外功能,大家都不可避免地需要面临新插件的编写与部署工作,这显然有点令人头痛。现在在EASE的帮助下,我们能够以更理想的方式完成这项任务——而且整个过程不需要涉及任何一代Java代码。EASE...

    Eclipse开发分布式商城系统+完整视频代码及文档

    │ 11.freemarker的模板的编写方法.avi │ 12.项目中使用freemarker-分析.avi │ 13.静态页面生成完毕.avi │ 14.解决请求406问题.avi │ 打开必读.txt │ 淘淘商城第九天笔记.docx │ ├─10.第十天 │ 01.课后...

    在Eclipse中使用SVN与CVS代码管理工具管理项目

    ClearCase 通过多点复制支持多个服务器和多个点的可扩展性,并擅长设置复杂的开发过程。安全性:★★★★ClearCase 的权限设置功能与 SVN 相比, SVN 有独立的安全管理机制, ClearCase 没有专用的安全性管理机制,...

    开发一个基于JUnit的存储过程自动化测试的Eclipse插件

    5自动生成测试代码6编写一个Eclipse插件7插件扩展了JUnit框架8执行测试用例9自动验证测试结果10不同时间点的测试结果的比较11SPTestSuite插件的运行步骤12SPTestSuite插件提高了程序员的工作效率了吗?13总结14资源...

    imp.runtime:对于具有现有前端的语言以及使用编译器和解释器生成框架生成的语言,IMP从根本上简化并加速了Eclipse中的IDE开发过程。

    通过使用扩展点实例化UniversalEditor以及有关您自己语言的信息,您可以逐步创建基本的IDE。 您主要通过实现许多简单的接口来提供此信息。 在OOPSLA的2009年中对IMP进行了描述 目前没有该项目的积极维护者。 带有...

    Maven实战 许晓斌 著

    扩展性地讲解了如何Maven和Archetype插件,这部分内容对需要编写插件扩展Maven或需要编写Archetype维护自己的项目骨架以更便于团队开发的读者来说尤为有帮助。它实战性强,不仅绝大部分知识点都有相应的案例,而且...

    Maven实战.pdf

    扩展性地讲解了如何Maven和Archetype插件,这部分内容对需要编写插件扩展Maven或需要编写Archetype维护自己的项目骨架以更便于团队开发的读者来说尤为有帮助。它实战性强,不仅绝大部分知识点都有相应的案例,而且...

    《Maven实战》

    扩展性地讲解了如何Maven和Archetype插件,这部分内容对需要编写插件扩展Maven或需要编写Archetype维护自己的项目骨架以更便于团队开发的读者来说尤为有帮助。它实战性强,不仅绝大部分知识点都有相应的案例,而且...

    Maven实战 高清版pdf

    扩展性地讲解了如何Maven和Archetype插件,这部分内容对需要编写插件扩展Maven或需要编写Archetype维护自己的项目骨架以更便于团队开发的读者来说尤为有帮助。它实战性强,不仅绝大部分知识点都有相应的案例,而且...

Global site tag (gtag.js) - Google Analytics