天天看點

eclipse 插件編寫(一)

由于項目開發程序中有一些重複性的代碼進行編寫,沒有任何業務邏輯,粘貼複制又很麻煩且容易出錯,故想起做一個eclipse插件來滿足一下自己的工作需要,同時記錄一下,以供以後參考與共同學習。本文主要講解一步一步開發eclipse插件的過程,沒有對每一步進行詳細的講解,如需檢視詳細介紹請自行百度、Google。

參考網站:

http://www.ibm.com/developerworks/cn/java/os-ecplug/

開發環境:window7、jdk1.6、eclipse4.4

1、建立eclipse插件 File > New > Project

eclipse 插件編寫(一)

選擇 Plug-in Project ,建立一個eclipse插件工程,填入工程名稱 TestPlugin,使用預設路徑、預設項目設定,選擇插件運作在eclipse的版本。

eclipse 插件編寫(一)

點選下一步進入插件項目的詳細設定頁面,

eclipse 插件編寫(一)

ID: 插件的ID

Version: 插件版本号

Name: 插件名稱

Vendor: 插件的作者資訊

Execution Environment: 插件的執行環境

option中的 Generate an activator, a Java class that controls the plug-in's life cycle ,下面緊跟着有一個 Activator的輸入框,這兒選中後代表生成一個啟動器,這個java類用來控制插件的聲明周期,這個啟動器類似java的main方法,Activator後面輸入框内的内容代表要生成啟動器的java類名稱

進入下一步後有一些模闆可以選擇,為了友善選擇Hello,World執行個體項目。

eclipse 插件編寫(一)
eclipse 插件編寫(一)

點選完成,項目建立成功。目錄如下:

eclipse 插件編寫(一)

src : 項目源代碼目錄

icons:項目圖檔資源目錄

META-INF/MANIFEST.MF: 項目基本配置資訊,版本、名稱、啟動器等

build.properties: 項目的編譯配置資訊,包括,源代碼路徑、輸出路徑、

plugin.xml:插件的操作配置資訊,包含彈出菜單及點選菜單後對應的操作執行類等,

2、代碼分析

MANIFEST.MF:

包含了版本、名稱、啟動器類、必須加載的插件、運作環境等

Manifest-Version: 1.0
      
Bundle-ManifestVersion: 2
      
Bundle-Name: TestPlugin
      
Bundle-Version: 1.0.0.qualifier
      
Bundle-Activator: testplugin.Activator
      
Require-Bundle: org.eclipse.ui,
      
org.eclipse.core.runtime
      
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
      
Bundle-ActivationPolicy: lazy
      
對應的在通過Plug-in Manifest Editor中打開如下:      
build.properties
檔案包含了源檔案路徑、編譯輸出路徑及編譯包含的目錄等      
source.. = src/
      
output.. = bin/
      
bin.includes = plugin.xml,\
      
META-INF/,\
      
.,\
      
icons/      

對應的在通過Plug-in Manifest Editor中打開如下:

eclipse 插件編寫(一)
plugin.xml

相對來講 plugin.xml 是内容最多也最容易更改的地方,包含了插件的操作集合,内容如下:

</plugin>      

extension: 代表一個擴充點, point=org.eclipse.ui.actionSets 代表改擴充點用來添加菜單、工具欄按鈕

actionSet : 一組工具集

menu: 菜單

action: 菜單項,這裡的action中有一個屬性class=”testplugin.actions.SampleAction”代表在點選該菜單項時将觸發此action類的run方法。

SimpleAction:

SimpleAction 為自動生成的一個類,裡面包含了一個簡單的實作hello world的程式代碼,如下:

package testplugin.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(),      
"TestPlugin",      
"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方法,該方法彈出一個提示框,提示Hello, Eclipse world;      
Activator:

此類為插件的啟動類,控制插件的生命周期,内容如下:

package testplugin;
      
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 {
      
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);
      
}
      
}
      

3、運作一下,看看效果

首先在項目名稱上點選右鍵 > Run as > Eclipse Application 會新打開一個eclipse視窗,在裡面可以看到剛剛建立的插件,如圖:

eclipse 插件編寫(一)

把滑鼠移入插件圖示的上面會出現提示文字,點選檢視效果:

eclipse 插件編寫(一)

最簡單的插件就這樣了,後面會在裡面添加一些其他的功能,以完成一個代碼生成器的基本功能,由于剛接觸eclipse插件,文中如有不正确的地方敬請指正。

轉載于:https://www.cnblogs.com/haifeng1990/p/5192804.html