eclipse 插件編寫(一)

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

參考網站:shell

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

開發環境:window七、jdk1.六、eclipse4.4 eclipse

一、新建eclipse插件 File > New > Project

QQ截圖20160216092153

選擇 Plug-in Project ,新建一個eclipse插件工程,填入工程名稱 TestPlugin,使用默認路徑、默認項目設置,選擇插件運行在eclipse的版本。ide

image

點擊下一步進入插件項目的詳細設置頁面,工具

image

ID: 插件的ID學習

Version: 插件版本號網站

Name: 插件名稱ui

Vendor: 插件的做者信息this

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實例項目。

image

image

點擊完成,項目新建成功。目錄以下:

image

src : 項目源代碼目錄

icons:項目圖片資源目錄

META-INF/MANIFEST.MF: 項目基本配置信息,版本、名稱、啓動器等

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

plugin.xml:插件的操做配置信息,包含彈出菜單及點擊菜單後對應的操做執行類等,

二、代碼分析

MANIFEST.MF:

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

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: TestPlugin
Bundle-SymbolicName: TestPlugin;singleton:=true
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中打開以下:
image
 
build.properties
文件包含了源文件路徑、編譯輸出路徑及編譯包含的目錄等
source.. = src/
output.. = bin/
bin.includes = plugin.xml,\
               META-INF/,\
               .,\
               icons/

對應的在經過Plug-in Manifest Editor中打開以下:

image

 

plugin.xml

相對來說 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="TestPlugin.actionSet">
         <menu
               label="Sample &amp;Menu"
               id="sampleMenu">
            <separator
                  name="sampleGroup">
            </separator>
         </menu>
         <action
               label="&amp;Sample Action"
               icon="icons/sample.gif"
               class="testplugin.actions.SampleAction"
               tooltip="Hello, Eclipse world"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="testplugin.actions.SampleAction">
         </action>
      </actionSet>
   </extension>
 
</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 {
	// The plug-in ID
	public static final String PLUGIN_ID = "TestPlugin"; //$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);
	}
}
 

三、運行一下,看看效果

首先在項目名稱上點擊右鍵 > Run as > Eclipse Application 會新打開一個eclipse窗口,在裏面能夠看到剛剛新建的插件,如圖:

image

把鼠標移入插件圖標的上面會出現提示文字,點擊查看效果:

image

 

 

最簡單的插件就這樣了,後面會在裏面添加一些其餘的功能,以完成一個代碼生成器的基本功能,因爲剛接觸eclipse插件,文中若有不正確的地方敬請指正。

相關文章
相關標籤/搜索