eclipse 插件編寫(一)


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

參考網站:

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

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

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

QQ截圖20160216092153

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

image

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

image

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

image

image

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

image

src : 項目源代碼目錄

icons:項目圖片資源目錄

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

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

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

2、代碼分析

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);
	}
}
 

3、運行一下,看看效果

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

image

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

image

 

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM