Eclipse插件开发之基础篇(2) 第一个Eclipse插件

在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

01 <?xml version="1.0" encoding="UTF-8"?>
02 <?eclipse version="3.4"?>
03 <plugin>
04    <extension
05          point="org.eclipse.ui.actionSets"> ①
06       <actionSet
07             label="Sample Action Set"
08             visible="true"
09             id="cn.sf.amateras.sample.actionSet">
10          <menu
11                label="Sample &Menu"
12                id="sampleMenu">
13             <separator
14                   name="sampleGroup">
15             </separator>
16          </menu>
17          <action
18                label="&Sample Action"
19                icon="icons/sample.gif"
20                class="cn.sf.amateras.sample.actions.SampleAction"
21                tooltip="Hello, Eclipse world"
22                menubarPath="sampleMenu/sampleGroup"
23                toolbarPath="sampleGroup"
24                id="cn.sf.amateras.sample.actions.SampleAction">
25          </action>
26       </actionSet>
27    </extension>
28 </plugin>

   为了在菜单栏增加一个项目,文件中使用了extension元素。①的id指定了扩展点的名称org.eclipse.ui.actionSets。extension里的内容根据扩展点而不同。示例插件中包含了actionSet、menu、action等元素。

  ②中的class属性指定了cn.sf.amateras.sample.actions.SampleAction类作为响应菜单或者工具栏按钮的action类。

代码2-2 SampleAction.java

01 package cn.sf.amateras.sample.actions;
02   
03 import org.eclipse.jface.action.IAction;
04 import org.eclipse.jface.viewers.ISelection;
05 import org.eclipse.ui.IWorkbenchWindow;
06 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
07 import org.eclipse.jface.dialogs.MessageDialog;
08   
09 /**
10  * Our sample action implements workbench action delegate.
11  * The action proxy will be created by the workbench and
12  * shown in the UI. When the user tries to use the action,
13  * this delegate will be created and execution will be 
14  * delegated to it.
15  * @see IWorkbenchWindowActionDelegate
16  */
17 public class SampleAction implements IWorkbenchWindowActionDelegate {
18     private IWorkbenchWindow window;
19     /**
20      * The constructor.
21      */
22     public SampleAction() {
23     }
24   
25     /**
26      * The action has been activated. The argument of the
27      * method represents the 'real' action sitting
28      * in the workbench UI.
29      * @see IWorkbenchWindowActionDelegate#run
30      */
31     public void run(IAction action) {   //......①
32         MessageDialog.openInformation(
33             window.getShell(),
34             "Sample",
35             "Hello, Eclipse world");
36     }
37   
38     /**
39      * Selection in the workbench has been changed. We 
40      * can change the state of the 'real' action here
41      * if we want, but this can only happen after 
42      * the delegate has been created.
43      * @see IWorkbenchWindowActionDelegate#selectionChanged
44      */
45     public void selectionChanged(IAction action, ISelection selection) {
46     }
47   
48     /**
49      * We can use this method to dispose of any system
50      * resources we previously allocated.
51      * @see IWorkbenchWindowActionDelegate#dispose
52      */
53     public void dispose() {
54     }
55   
56     /**
57      * We will cache window object in order to
58      * be able to provide parent shell for the message dialog.
59      * @see IWorkbenchWindowActionDelegate#init
60      */
61     public void init(IWorkbenchWindow window) {
62         this.window = window;
63     }
64 }

 

   ①run()方法内记录了action执行时的处理。利用MessageDialog.openInformation方法打开了一个对话框。

  咱们再看看生成的另外一个类--Activator类。这个类对插件的生命周期进行了管理,被称为插件类。

代码2-3 Activator.java

01 package cn.sf.amateras.sample;
02   
03 import org.eclipse.jface.resource.ImageDescriptor;
04 import org.eclipse.ui.plugin.AbstractUIPlugin;  //....①
05 import org.osgi.framework.BundleContext;
06   
07 /**
08  * The activator class controls the plug-in life cycle
09  */
10 public class Activator extends AbstractUIPlugin {
11   
12     // The plug-in ID
13     public static final String PLUGIN_ID = "cn.sf.amateras.sample"; //$NON-NLS-1$
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 }

 

  示例中的插件类继承自AbstractUIPlugin(①)。如果没有UI的插件继承AbstractUIPlugin的父类Plugin就可以了。

  插件类的方法如下表所示:

getDefault() 取得插件类的实例的方法。插件类是单例的,所以这个方法作为一个静态方法提供。
start() 插件开始时的处理。
stop() 插件停止时的处理。
getLog() log输出时取得ILog用的方法。
getImageRegistry() 取得管理插件内图像的ImageRegistry类。
getPerferenceStore() 取得保存插件设定的IPerferenceStore类。
getDialogSettings() 取得保存对话框设定的IDialogSettings类。
getWorkbench() 取得IWorkbench的实例。

 

  以上就是根据[Hello World]模板生成的插件的代码讲解,在PDE中为我们准备了很多的模板,大家可以都试试看。