您的位置:首页 > 编程语言 > Java开发

Eclipse插件开发之一:Hello World

2006-05-26 21:16 337 查看
近日开始对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"?>

[align=left]<?eclipse version="3.0"?>[/align]
[align=left]<plugin>[/align]
[align=left] [/align]
[align=left]   <extension[/align]
[align=left]         point="org.eclipse.ui.actionSets">[/align]
[align=left]      <actionSet[/align]
[align=left]            label="Sample Action Set"[/align]
[align=left]            visible="true"[/align]
[align=left]            id="Hello.actionSet">[/align]
[align=left]         <menu[/align]
[align=left]               label="Sample &Menu"[/align]
[align=left]               id="sampleMenu">[/align]
[align=left]            <separator[/align]
[align=left]                  name="sampleGroup">[/align]
[align=left]            </separator>[/align]
[align=left]         </menu>         [/align]
[align=left]         <action[/align]
[align=left]               label="&Sample Action"[/align]
[align=left]               icon="icons/sample.gif"[/align]
[align=left]               class="hello.actions.SampleAction"[/align]
[align=left]               tooltip="Hello, Eclipse world"[/align]
[align=left]               menubarPath="sampleMenu/sampleGroup"[/align]
[align=left]               toolbarPath="sampleGroup"[/align]
[align=left]               id="hello.actions.SampleAction">[/align]
[align=left]         </action>[/align]
[align=left]         <action[/align]
[align=left]               label="&Action2"[/align]
[align=left]               icon="icons/sample.gif"[/align]
[align=left]               class="hello.actions.actions2"[/align]
[align=left]               tooltip="Hello, Eclipse world2"[/align]
[align=left]               menubarPath="sampleMenu/sampleGroup"[/align]
[align=left]               toolbarPath="sampleGroup"[/align]
[align=left]               id="hello.actions.actions2">[/align]
[align=left]         </action>[/align]
[align=left]      </actionSet>      [/align]
[align=left]   </extension>[/align]
[align=left] [/align]
</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) {
  
 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息