天天看點

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]