【插件開發】—— 2 插件入門

  最近因爲特殊須要,開始學習插件開發。

之前接觸java也算有些時日,可是一直不知道有插件開發這樣一種技術路徑,本想着這開發工具都給你備好了,直接用不就好了。可是總有些IT工廠,爲了要節約成本,開發本身的開發工具,可是又要節省時間,總不能一切都本身來。畢竟開發一個eclipse也要很長時間的。所以,插件開發出如今歷史舞臺。java

  首先要了解插件開發,就得從SWT/JFACE提及了。SWT是一種開源的界面開發框架,之前學java的時候,老是用一些panel,就相似這個。JFace又是一種基於SWT的UI不見的API。Eclipse就是用這個開發出來的,它提供了Eclipse強大的擴展性,所以可讓用戶任意的插入本身想要的插件,開發本身的IDE。框架

  下面就直接弄一個簡單的插件吧!

  

首先認識一下Eclipse,這個你們應該很熟了!eclipse

  1 紅色部分是咱們的工具欄編輯器

  2 藍色部分是視圖ide

  3 黃色部分是編輯器函數

  一般咱們使用編輯器,進行代碼操做,或者業務操做。在視圖,進行一些資源的查看等。紅色引入一些經常使用的功能,輔助咱們的操做。工具

咱們先作一個簡單的工具欄的控件,瞭解一下eclipse的插件開發流程!學習

  1 新建一個插件工程

  2 建立本身的插件名字,這個名字最好特殊一點,一遍融合到eclipse的時候,不會發生衝突。

  首先,寫入本身的插件名字。開發工具

  source folder 是插件的代碼路徑。ui

  output folder是插件輸出的目標路徑。

  下面是開發插件的eclipse的版本。

  3 下一步,進行插件的具體詳情設置

ID 是插件的標識

version 是插件的版本

Name是插件的名字

Provider是開發者的信息

 

下面的Activator,是插件的激活類,用來管理插件的生命週期。

 

最後是選擇是否開發RCP,富客戶端應用,暫且不用,選否就好了。

  4 使用插件模板,建立一個插件工具

選擇hello world.這是一個工具欄的按鈕。

 5 下一步,進行按鈕的信息設置。

  默認會生成類的名字,路徑(包名),以及工具欄按鈕出發的消息提示。

  6 點擊finish,完成插件的建立。


 

  這樣,咱們就完成了一個插件的建立,那麼看一下,eclipse都爲咱們生成了什麼。

1 導入了插件所須要用到的jar包

2 導入了插件依賴的庫

3 源文件

4 插件按鈕圖片

5 插件的配置信息

  MANIFEST.MF 插件的捆綁信息

Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: 個人插件 Bundle-SymbolicName: com.test.myplugin; singleton:=true Bundle-Version: 1.0.0.qualifier Bundle-Activator: com.test.myplugin.Activator Bundle-Vendor: xingoo Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime Bundle-ActivationPolicy: lazy

  

  Name 是咱們以前設置的插件名字

  SymblicName 是咱們插件的包名

  version 是插件的版本

  Activator 是插件的激活類

  Vendor 是插件開發者的信息

  Bundle 是插件以來的庫

  這些信息都對應着插件的overview頁面的信息。

  

  plugin.xml 插件的詳細設置文檔,包含插件的擴展點信息,以及插件本身的信息

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

   <extension point="org.eclipse.ui.actionSets">
      <actionSet label="Sample Action Set" visible="true" id="com.test.myplugin.actionSet">
         <menu label="Sample &amp;Menu" id="sampleMenu">
            <separator name="sampleGroup">
            </separator>
         </menu>
         <action label="&amp;Sample Action" icon="icons/sample.gif" class="com.test.myplugin.actions.SampleAction" tooltip="Hello, Eclipse world" menubarPath="sampleMenu/sampleGroup" toolbarPath="sampleGroup" id="com.test.myplugin.actions.SampleAction">
         </action>
      </actionSet>
   </extension>

</plugin>

  

  <plugin>元素清單的主體

  <extension>插件的功能擴展,裏面包括 point 擴展點的標識、id 擴展實例的標識、name 提供的用戶的名稱等等

  build.properties 構建的元素列表

source.. = src/ output.. = bin/ bin.includes = plugin.xml,\ META-INF/,\ .,\ icons/

  裏面包括插件的源文件目錄,生成文件目錄,還有一些配置信息的引入。

  提供的插件類Activator.java

 1 package com.test.myplugin;  2 
 3 import org.eclipse.jface.resource.ImageDescriptor;  4 import org.eclipse.ui.plugin.AbstractUIPlugin;  5 import org.osgi.framework.BundleContext;  6 
 7 /**
 8  * The activator class controls the plug-in life cycle  9  */
10 public class Activator extends AbstractUIPlugin { 11 
12     // The plug-in ID
13     public static final String PLUGIN_ID = "com.test.myplugin"; 14 
15     // The shared instance
16     private static Activator plugin; 17     
18     /**
19  * The constructor 20      */
21     public Activator() { 22  } 23 
24     /*
25  * (non-Javadoc) 26  * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) 27      */
28     public void start(BundleContext context) throws Exception { 29         super.start(context); 30         plugin = this; 31  } 32 
33     /*
34  * (non-Javadoc) 35  * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) 36      */
37     public void stop(BundleContext context) throws Exception { 38         plugin = null; 39         super.stop(context); 40  } 41 
42     /**
43  * Returns the shared instance 44  * 45  * @return the shared instance 46      */
47     public static Activator getDefault() { 48         return plugin; 49  } 50 
51     /**
52  * Returns an image descriptor for the image file at the given 53  * plug-in relative path 54  * 55  * @param path the path 56  * @return the image descriptor 57      */
58     public static ImageDescriptor getImageDescriptor(String path) { 59         return imageDescriptorFromPlugin(PLUGIN_ID, path); 60  } 61 }

  start()和stop()分別用於插件開始與中止調用的函數。

  


 

 

  最後讓咱們運行一下這個插件吧!

 

  啓動方式1 直接在overview界面點擊;

   啓動方式2 也能夠點擊運行或者DEBUG按鈕,運行方式選擇Eclipse Application。

  

  點擊啓動後,會爲咱們從新開啓一個Eclipse,這個Eclipse就是帶有咱們建立的插件的新Eclipse。啓動效果以下:

  這樣一個簡單的插件就開發完啦!讓咱們就此真正的起航吧!!!

相關文章
相關標籤/搜索