自己动手编写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位置。
推荐文章 |
