关闭

Eclipse插件开发之一:Hello World

1739人阅读 评论(0) 收藏 举报

近日开始对Eclipse着迷,于是从图书馆里借了本Contributing to Eclipse,这本书是Erric Gamma, Kent Beck合著的,大师之作。

今天的收获是制作Eclipse插件的Hello World。

书中的说法稍微麻烦点,我在Eclipse3.1下做了如下实验:

1新建一个 plug-in的项目

2在向导里面选择Hello World项目实际上就可以了,向导都为我们做了。书上说的稍微繁琐一些,在这里真是令我感到意外的简单。

3这个项目已经可以运行了,运行时候要选择Eclipse插件运行配置,选择Eclipse应用程序配置,如图

3在plugin.xml里面,<menu>元素表示添加菜单分组,而Action表示添加菜单项,同时定义相应菜单的类,这个类继承于IWorkbenchWindowActionDelegate,执行菜单命令只需要实现run接口就可以了。

4我在plugin.xml里面添加了一个action元素,于是菜单又多了一个按钮。

5添加一个继承自IWorkbenchWindowActionDelegate的自定义类,并在plugin.xml里面指定class属性值为那个类,这样就实现了第二个helloword按钮。实现的功能是点击后弹出一个对话框,显示hello world。

附录

plugin.xml

 <?xml version="1.0" encoding="UTF-8"?>

<?eclipse version="3.0"?>
<plugin>
 
   <extension
         point="org.eclipse.ui.actionSets">
      <actionSet
            label="Sample Action Set"
            visible="true"
            id="Hello.actionSet">
         <menu
               label="Sample &amp;Menu"
               id="sampleMenu">
            <separator
                  name="sampleGroup">
            </separator>
         </menu>        
         <action
               label="&amp;Sample Action"
               icon="icons/sample.gif"
               class="hello.actions.SampleAction"
               tooltip="Hello, Eclipse world"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="hello.actions.SampleAction">
         </action>
         <action
               label="&amp;Action2"
               icon="icons/sample.gif"
               class="hello.actions.actions2"
               tooltip="Hello, Eclipse world2"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="hello.actions.actions2">
         </action>
      </actionSet>     
   </extension>
 
</plugin>
 
 
SampleAction.java

package hello.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(),
   "Hello Plug-in",
   "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;
 }
}

actions2.java

package hello.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class actions2 implements IWorkbenchWindowActionDelegate {

 private IWorkbenchWindow window;
 public void dispose() {

 }

 public void init(IWorkbenchWindow window) {
  this.window=window;

 }

 public void run(IAction action) {
  MessageDialog.openInformation(
    window.getShell(),
    "Hello World 2 Title",
    "This is Hello World 2");
 }

 public void selectionChanged(IAction action, ISelection selection) {
  
 }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10997次
    • 积分:229
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:1篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论
    关于我的链接