Eclipse插件开发自定义扩展点

  • 介绍
如果你的插件写的有特色,想拿来用,但是还不能或者不适合直接修改你的代码,怎么办呢?解决方案当然是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

转载于:https://my.oschina.net/surenpi/blog/605012

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值