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) {
}
}
今天的收获是制作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) {
}
}
相关文章推荐
- Eclipse插件开发(一)Hello World
- eclipse插件开发 Hello World
- Eclipse安装CXF插件开发java web service 集成Spring
- Eclipse常用开发插件
- eclipse插件开发
- eclipse 插件开发中如何实现刷新和重编译
- magento2插件开发(1):magento2 Hello World
- Eclipse插件开发之基础篇(3) 插件的测试与调试
- eclipse插件开发总结
- Eclipse插件开发思路
- 开发 Eclipse 插件
- Eclipse入门-- Eclipse的使用简介及插件开发
- Eclipse插件开发之基础篇(1) 插件开发的基础知识
- Eclipse插件开发中各种路径的获取(一)
- Eclipse开发环境安装插件查看字节码bytecode
- 使用ant实现svn管理得eclipse插件开发项目的日构建功能(ant1.7.1+svn1.6.3+eclipse3.4.2)
- ubuntu下eclipse scala开发插件(Scala IDE for Eclipse)安装
- eclipse插件开发案例讲解——更新JSP文件时间戳
- eclipse插件开发中全局对象的获取
- 通过eclipse 的axis1插件,快速开发webservice服务端和客户端