- 介绍
如果你的插件写的有特色,想拿来用,但是还不能或者不适合直接修改你的代码,怎么办呢?解决方案当然是Eclipse插件系统最推荐的了——增加扩展点。
- 概念
一个扩展点(Extension Point)包括ID、Name及Schema文件,shema文件以ID命名,后缀为.exsd,存放在插件schema目录下。
- 定义
[codesyntax lang="xml"]
<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
<schema targetNamespace="org.suren.littlebird" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
<appinfo>
<meta.schema plugin="org.suren.littlebird" id="org.suren.littlebird.bundles.list" name="BundlesList"/>
</appinfo>
<documentation>
对bundle列表的扩展
</documentation>
</annotation>
<element name="extension">
<annotation>
<appinfo>
<meta.element />
</appinfo>
</annotation>
<complexType>
<attribute name="id" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="point" type="string" use="required">
<annotation>
<documentation>
测试啊
</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
</complexType>
</element>
<element name="column">
<annotation>
<documentation>
新增一列
</documentation>
</annotation>
<complexType>
<attribute name="text" type="string" use="required">
<annotation>
<documentation>
列名
</documentation>
</annotation>
</attribute>
<attribute name="sorter" type="string">
<annotation>
<documentation>
</documentation>
<appinfo>
<meta.attribute kind="java" basedOn="org.suren.littlebird.views.listener.SorterAdapter:"/>
</appinfo>
</annotation>
</attribute>
</complexType>
</element>
<element name="action">
<annotation>
<documentation>
对列表操作的扩展
</documentation>
</annotation>
<complexType>
<attribute name="reload" type="string">
<annotation>
<documentation>
</documentation>
<appinfo>
<meta.attribute kind="java" basedOn="org.suren.littlebird.actions.bundle.SuRenBundleReload:"/>
</appinfo>
</annotation>
</attribute>
<attribute name="install" type="string">
<annotation>
<documentation>
</documentation>
<appinfo>
<meta.attribute kind="java" basedOn="org.suren.littlebird.actions.bundle.SuRenBundleInstallAction:"/>
</appinfo>
</annotation>
</attribute>
<attribute name="uninstall" type="string">
<annotation>
<documentation>
</documentation>
<appinfo>
<meta.attribute kind="java" basedOn="org.suren.littlebird.actions.bundle.SuRenBundleUninstallAction:"/>
</appinfo>
</annotation>
</attribute>
<attribute name="update" type="string">
<annotation>
<documentation>
</documentation>
<appinfo>
<meta.attribute kind="java" basedOn="org.suren.littlebird.actions.bundle.SuRenBundleUpdateAction:"/>
</appinfo>
</annotation>
</attribute>
<attribute name="start" type="string">
<annotation>
<documentation>
</documentation>
<appinfo>
<meta.attribute kind="java" basedOn="org.suren.littlebird.actions.bundle.SuRenBundleStartAction:"/>
</appinfo>
</annotation>
</attribute>
<attribute name="stop" type="string">
<annotation>
<documentation>
停止bundle
</documentation>
<appinfo>
<meta.attribute kind="java" basedOn="org.suren.littlebird.actions.bundle.SuRenBundleStopAction:"/>
</appinfo>
</annotation>
</attribute>
</complexType>
</element>
<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>
http://surenpi.com
</documentation>
</annotation>
</schema>
[/codesyntax]
上面的定义文件org.suren.littlebird.bundles.list.exsd,放在schema目录中。
- 注册
[codesyntax lang="xml"]
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension-point id="org.suren.littlebird.bundles.list" name="BundlesList" schema="schema/org.suren.littlebird.bundles.list.exsd"/>
</plugin>
[/codesyntax]
上面的文件是plugin.xml
- 挂载扩展点
[codesyntax lang="java"]
private void regExtension()
{
IExtensionRegistry reg = Platform.getExtensionRegistry();
IConfigurationElement[] extensions = reg.getConfigurationElementsFor("org.suren.littlebird.bundles.list");
if(extensions != null)
{
for(IConfigurationElement ele : extensions)
{
if("column".equals(ele.getName()))
{
String textAttr = ele.getAttribute("text");
String sorterCls = ele.getAttribute("sorter");
TableColumn col = new TableColumn(table, SWT.None);
col.setText(textAttr);
try {
SorterAdapter ext = (SorterAdapter) WorkbenchPlugin.createExtension(ele, "sorter");
ext.setViewer(viewer);
col.addSelectionListener(ext);
} catch (CoreException e) {
e.printStackTrace();
}
}
}
}
}
[/codesyntax]
- 使用扩展点
这时候,你需要新建一个插件工程,不知道怎么新建插件工程?这里有入门教程。
[codesyntax lang="xml"]
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.suren.littlebird.bundles.list">
<column text="test" sorter="test.extension.TestSorter"/>
<column text="two" />
</extension>
</plugin>
[/codesyntax]
上面的类test.extension.TestSorter是对列排序的扩展,然后增加了两列——test、two。
- 参考
http://www.cnblogs.com/maxupeng/archive/2011/07/27/2118975.html
查看原文:http://surenpi.com/2015/07/10/eclipse%e6%8f%92%e4%bb%b6%e5%bc%80%e5%8f%91%e8%87%aa%e5%ae%9a%e4%b9%89%e6%89%a9%e5%b1%95%e7%82%b9/