[转]Eclipse插件开发之基础篇(2) 第一个Eclipse插件
原文地址:http://www.cnblogs.com/liuzhuo/archive/2010/08/15/eclipse_plugin_1_1_1.html
在Eclipse中使用PDE(Plugin Development Environment)進行插件開發。在PDE中為大家準備了幾個模板供大家參考。第一步咱們先利用模板制作一個簡單的插件作為和PDE的第一次接觸。
1.生成插件工程
通過選擇[文件]->[新建]->[工程...]啟動[新工程(New Project)]向導。在插件開發的時候我們就不能選擇通常的java工程了,要選擇新建一個插件工程。
選擇[插件工程(Plug-in Project)]后點擊[下一步(Next)](圖2-1)。下一步,輸入一個適當的項目名(圖2-2)。習慣上項目名和插件的ID是相同的。插件ID和java包的命名方式相同,必須一個世界范圍唯一的名稱。在這里用cn.sf.amateras.sample作為項目名稱(=插件ID)。點擊[下一步(Next)]。
圖2-1 生成插件工程
?
圖2-2 輸入插件信息
?
輸入[插件ID]、[插件版本]、[插件屬性(插件作者)](圖2-3)等信息。[插件ID]會被自動設成之前輸入的[工程名]。沒有其他特別的設定的話繼續點擊[下一步(Next)]。
圖2-3 輸入生成工程必要的信息
?
圖2-4 模板選擇
?
圖2-5 輸入模板信息
?
根據選擇的模板生成的插件在(圖2-5)的畫面可以修改一些信息。如果沒有特別需要設定的地方點擊[完成(Finish)],生成工程。
PDE透視圖
插件工程生成后,就會切換到PDE(插件開發)透視圖。工程此時根據選擇的[Hello,World]模板生成了插件的源代碼,并且打開了manifest編輯器。
圖2-6 PDE(插件開發)透視圖
?
2. 插件的動作
接下來我們看一下插件的動作。manifest編輯器的[概要]頁面右側[啟動Eclipse應用(Luanch an Eclipse application)](圖2-7)。點擊后,會啟用一個新的Eclipse實例。這個新的實例被稱為[運行時工作臺]。運行工作臺可以以嵌入當前正在開發的插件的狀態啟動,以使開發者可以對插件的動作進行確認。
圖2-7 啟動運行時工作臺
?
啟動的運行時工作臺,會在菜單欄上增加一個新的菜單項。這個菜單項就是插件的代碼追加上去的。在工具欄上也增加了一個相應的按鈕。
工具欄上的按鈕和菜單項目效果相同,都會出現一個[Hello,Eclipse World]的對話框。
Eclipse版的Hello World就完成了。確認完這些動作后,就可以關閉當前的運行時工作臺了,咱們在下個小節的講解中還要去參考插件工程的代碼呢~
圖2-8 示例中新增的菜單項和工具欄按鈕以及相應的動作
?
MANIFEST.MF和plugin.xml
一個插件包含MANIFEST.MF和plugin.xml兩個配置文件。MANIFEST.MF是OSGi的handler配置文件,記錄了插件ID、名稱、作者、和其他插件的依賴關系等。plugin.xml記錄了插件的擴展和擴展點的定義等信息。
Eclipse3.1以前由于還沒有采用OSGi技術,插件的所有信息都記錄在plugin.xml里。所以應用在Eclipse3.1以前版本的插件如果要應用于Eclipse3.1和以后版本時要注意遵守OSGi的配置文件定義規范。
3. PDE的功能
? PDE(插件開發環境·Plugin Development Enviromment)提供了插件開發的各種功能。因為插件的開發使用java語言,所以也會使用到java編碼工具JDT。
Manifest編輯器
PDE提供的功能中最重要的就是Manifest編輯器。編輯器可以為插件的兩個配置文件提供圖形化的編輯功能。(圖2-9)
編輯器的配置項目非常多,可以通過編輯器下面的tab頁切換設定畫面。
圖2-9 Manifest編輯器
表1 Manifest編輯器各tab頁
| 頁面 | 說明 |
| Overview | 可以設定插件的概要信息。生成用于發布的文檔以及啟動運行時工作臺。 |
| Dependencies | 設定插件間的依賴關系。 |
| Runtime | 可以設置導出包和ClassPath。 |
| Extensions | 管理當前插件對擴展點的擴展。 |
| Extension Points | 管理當前插件的擴展點。 |
| Build | 生成工程時的各種設定。 |
| MANIFEST.MF | 顯示MANIFEST.MF的源代碼 |
| plugin.xml | 顯示plugin.xml的源代碼 |
| build.properties | 顯示build.properties的源代碼 |
?
?
?
?
?
?
?
?
?
?
在Overview以外的tab頁上部會顯示一些快捷鍵按鈕,使用這些按鈕也可以進行啟動運行時工作臺,導出等操作。
圖2-10 編輯器上部
下面介紹一下PDE題供的幾種視圖。在[Window]->[Show View]中可以找到沒有被顯示的視圖。
?
[插件(Plug-ins)]視圖
所有的插件列表,包含當前工作區內正在開發的插件。打開插件節點可以顯示插件下包含的資源,但是以jar包提供出來的插件就顯示不出來了。
在插件節點上點擊右鍵選擇[打開依賴關系(Open Dependencies)]可以打開后面要介紹的[依賴關系]視圖,表現插件間的依賴關系。右鍵菜單中也可以把選中的插件作為一個工程引入到當前工作區內。
?
[依賴關系(Plug-in Dependencies)]視圖
? 表現了插件間的依賴關系。可以分別表示當前插件依賴的插件和依賴當前插件的插件。
?
[插件注冊(Plug-in Registry)]視圖
當前工作臺加載的所有插件列表。打開插件節點,顯示插件提供的擴展和擴展點、依賴關系等。
?
[錯誤Log(Error Log)]視圖
錯誤Log視圖雖然不是PDE提供的功能,但是在插件開發的時候非常有用。它可以表現當前工作臺內發生的錯誤,并且在錯誤上雙擊既可以導向到錯誤發生的地方。
?
Plugin-in Selection Spy
同時按下alt+shift+F1
Plugin-in Selection Spy是從Eclipse3.4開始加入的新功能,能夠顯示當前焦點所在的編輯器、視圖、向導、對話框等的內部信息。把已有的插件作為參考,進行插件開發,使用這個功能是再合適不過了。
?
4. 解讀示例插件的代碼
讓我們回到插件工程來,看一看插件工程生成的代碼。首先看一下plugin.xml的代碼。
代碼2-1 plugin.xml
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?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="cn.sf.amateras.sample.actionSet"> ???????? <menu ?????????????? label="Sample &Menu" ?????????????? id="sampleMenu"> ??????????? <separator ????????????????? name="sampleGroup"> ??????????? </separator> ???????? </menu> ???????? <action ?????????????? label="&Sample Action" ?????????????? icon="icons/sample.gif" ?????????????? class="cn.sf.amateras.sample.actions.SampleAction" ② ?????????????? tooltip="Hello, Eclipse world" ?????????????? menubarPath="sampleMenu/sampleGroup" ?????????????? toolbarPath="sampleGroup" ?????????????? id="cn.sf.amateras.sample.actions.SampleAction"> ???????? </action> ????? </actionSet> ?? </extension> </plugin> |
? 為了在菜單欄增加一個項目,文件中使用了extension元素。①的id指定了擴展點的名稱org.eclipse.ui.actionSets。extension里的內容根據擴展點而不同。示例插件中包含了actionSet、menu、action等元素。
②中的class屬性指定了cn.sf.amateras.sample.actions.SampleAction類作為響應菜單或者工具欄按鈕的action類。
代碼2-2 SampleAction.java
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package cn.sf.amateras.sample.actions; import org.eclipse.jface.action.IAction; import org.eclipse.jface.viewers.ISelection; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkbenchWindowActionDelegate; import org.eclipse.jface.dialogs.MessageDialog; /** ?* Our sample action implements workbench action delegate. ?* The action proxy will be created by the workbench and ?* shown in the UI. When the user tries to use the action, ?* this delegate will be created and execution will be ?* delegated to it. ?* @see IWorkbenchWindowActionDelegate ?*/ public class SampleAction implements IWorkbenchWindowActionDelegate { ??? private IWorkbenchWindow window; ??? /** ???? * The constructor. ???? */ ??? public SampleAction() { ??? } ??? /** ???? * The action has been activated. The argument of the ???? * method represents the 'real' action sitting ???? * in the workbench UI. ???? * @see IWorkbenchWindowActionDelegate#run ???? */ ??? public void run(IAction action) {?? //......① ??????? MessageDialog.openInformation( ??????????? window.getShell(), ??????????? "Sample", ??????????? "Hello, Eclipse world"); ??? } ??? /** ???? * Selection in the workbench has been changed. We ???? * can change the state of the 'real' action here ???? * if we want, but this can only happen after ???? * the delegate has been created. ???? * @see IWorkbenchWindowActionDelegate#selectionChanged ???? */ ??? public void selectionChanged(IAction action, ISelection selection) { ??? } ??? /** ???? * We can use this method to dispose of any system ???? * resources we previously allocated. ???? * @see IWorkbenchWindowActionDelegate#dispose ???? */ ??? public void dispose() { ??? } ??? /** ???? * We will cache window object in order to ???? * be able to provide parent shell for the message dialog. ???? * @see IWorkbenchWindowActionDelegate#init ???? */ ??? public void init(IWorkbenchWindow window) { ??????? this.window = window; ??? } } |
?
? ①run()方法內記錄了action執行時的處理。利用MessageDialog.openInformation方法打開了一個對話框。
咱們再看看生成的另外一個類--Activator類。這個類對插件的生命周期進行了管理,被稱為插件類。
代碼2-3 Activator.java
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package cn.sf.amateras.sample; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.plugin.AbstractUIPlugin;? //....① import org.osgi.framework.BundleContext; /** ?* The activator class controls the plug-in life cycle ?*/ public class Activator extends AbstractUIPlugin { ??? // The plug-in ID ??? public static final String PLUGIN_ID = "cn.sf.amateras.sample"; //$NON-NLS-1$ ??? // The shared instance ??? private static Activator plugin; ??? ? ??? /** ???? * The constructor ???? */ ??? public Activator() { ??? } ??? /* ???? * (non-Javadoc) ???? * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) ???? */ ??? public void start(BundleContext context) throws Exception {//....③ ??????? super.start(context); ??????? plugin = this; ??? } ??? /* ???? * (non-Javadoc) ???? * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) ???? */ ??? public void stop(BundleContext context) throws Exception {//....④ ??????? plugin = null; ??????? super.stop(context); ??? } ??? /** ???? * Returns the shared instance ???? * ???? * @return the shared instance ???? */ ??? public static Activator getDefault() {//....② ??????? return plugin; ??? } ??? /** ???? * Returns an image descriptor for the image file at the given ???? * plug-in relative path ???? * ???? * @param path the path ???? * @return the image descriptor ???? */ ??? public static ImageDescriptor getImageDescriptor(String path) { ??????? return imageDescriptorFromPlugin(PLUGIN_ID, path); ??? } } |
?
示例中的插件類繼承自AbstractUIPlugin(①)。如果沒有UI的插件繼承AbstractUIPlugin的父類Plugin就可以了。
插件類的方法如下表所示:
| getDefault() | 取得插件類的實例的方法。插件類是單例的,所以這個方法作為一個靜態方法提供。 |
| start() | 插件開始時的處理。 |
| stop() | 插件停止時的處理。 |
| getLog() | log輸出時取得ILog用的方法。 |
| getImageRegistry() | 取得管理插件內圖像的ImageRegistry類。 |
| getPerferenceStore() | 取得保存插件設定的IPerferenceStore類。 |
| getDialogSettings() | 取得保存對話框設定的IDialogSettings類。 |
| getWorkbench() | 取得IWorkbench的實例。 |
?
以上就是根據[Hello World]模板生成的插件的代碼講解,在PDE中為我們準備了很多的模板,大家可以都試試看。
轉載于:https://www.cnblogs.com/wlzjdm/p/6882116.html
總結
以上是生活随笔為你收集整理的[转]Eclipse插件开发之基础篇(2) 第一个Eclipse插件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Atitit.运行cmd 命令行 php
- 下一篇: Mybatis的动态sql语句的查询