本文介绍了使用Eclipse Commands。它描述了如何创建命令,处理程序,添加到菜单命令,弹出窗口,视图和编辑器和表达式的使用限制用户界面的贡献。该快捷键的使用和解释。
本文是基于Eclipse Helios(3.6)。
1。 Eclipse的命令 - 概述
1.1。概述
Eclipse的一个commands是一个组件的说明和声明的实施细则,从独立。一个commands可以被归类,分配给用户界面和一键绑定可以为命令定义。commands的行为可以通过定义一个处理程序。
因此,你必须要定义和使用一个commands
.你需要 - 描述该组件的声明
Handler - 定义行为
UI的分配 - 在哪里,怎样的commands包含在用户界面
commands是通过定义扩展点org.eclipse.ui.commands。
下面的例子使用的是基于Eclipse RCP的,但这个概念也适用于一般的Eclipse插件开发。
1.2。位置的URI - 用户界面
命令可以用来在菜单,工具栏和/或上下文菜单。在何处以及如何显示这些命令的定义是通过一个位置的URI。
Table 1.
Contribution to | Description | Uri |
Application menu | Displays the command in the menu of the application | "menu:org.eclipse.ui.main.menu" |
Application toolbar | displays the command in the toolbar of the application | "toolbar:org.eclipse.ui.main.toolbar" |
View toolbar | displays the command in the toolbar of the view | "toolbar:viewId". For example to display a menu to view with the Id "View1" use "toolbar:Views1". |
Content menu / pop-up | Command is displayed in a content menu, e.g. right mouse click on an object | n.a |
Uri
Application menu Displays the command in the menu of the application "menu:org.eclipse.ui.main.menu"
Application toolbar displays the command in the toolbar of the application "toolbar:org.eclipse.ui.main.toolbar"
c"toolbar:viewId". For example to display a menu to view with the Id "View1" use "toolbar:Views1".
1.3. Command Handler
一个command行为是通过handler定义的,您可以一个command定义几个handlers,但只有唯一的handler可以是在某点有效的command,否则该command是不活跃。handler是将执行一次该command的类调用。一个command handler必须实现该接口org.eclipse.core.commands.IHandler。
IHandler(由org.eclipse.core.commands.AbstractHandler实施)定义了可实现以下重要的方法:
isEnabled: 实例化过程中调用,定义 这个动作是否启用
isHandled: 定义handler可以不可以调用
execute: 执行操作的编码
fireHandlerChanged: isEnabled改变时需要调用
类HandlerUtil提供访问的方法是Eclipse重要的服务。
handler可以定义与其它条件下它们是有效command的处理程序,如果不能选择一个有效的handler, Eclipse运行时不能有效决定哪一个要使用,该command将无法启用。
2. Commands and menus
2.1. 假设
我假设你已经知道如何创建一个Eclipse RCP应用程序.可以看Eclipse RCP - 详细教程。
2.2。command定义
我们将创建一个command将退出该应用程序。创建一个新的RCP工程项目“de.vogella.rcp.commands.first”使用的“Hello RCP”的模板。点击plugin.xml中,选择扩展选项卡。按“添加”按钮。
在扩展点搜索 "org.eclipse.ui.commands". 选择它,然后按完成
右键点击你的扩展点创建一个command,选择new - >command
如果你只看到一个“Generic”项你最有可能还没有下载"Eclipse for RCP/Plug-in Developers".请查询Eclipse安装方法
设置ID为“de.vogella.rcp.commands.first.commands.Exit”和name为Exit输入类“de.vogella.rcp.commands.first.commands.ExitHandler”为defaultHandler。按超连结“defaultHandler”创建这个类,然后选择“org.eclipse.core.commands.AbstractHandler作为超类”。
2.3. 在menus里使用commands
我们定义的command应该在菜单使用。添加扩展点“org.eclipse.ui.menus”到你的应用程序类似添加扩展名“org.eclipse.ui.commands”。右键单击扩展点,并选择new - > menuContribution。
创建一个新的菜单确认URI位置为“menu:org.eclipse.ui.main.menu”。确保这个网址是正确的,否则你的菜单将不会显示.
右键单击您menucontribution并选择new - >menu。添加一个标签菜单“File”和id“fileMenu”。
选择你的菜单,右键点击它,选择New ->command。保持你的commandID。设置label为“exit”和tooltip标签“Exits the application”。
你上面工作应生成的plugin.xml文件,该文件看起来像下面那样。
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
id="application"
point="org.eclipse.core.runtime.applications">
<application>
<run
class="de.vogella.rcp.commands.first.Application">
</run>
</application>
</extension>
<extension
point="org.eclipse.ui.perspectives">
<perspective
name="RCP Perspective"
class="de.vogella.rcp.commands.first.Perspective"
id="de.vogella.rcp.commands.first.perspective">
</perspective>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
defaultHandler="de.vogella.rcp.commands.first.commands.ExitHandler"
id="de.vogella.rcp.commands.first.commands.Exit"
name="Exit">
</command>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu">
<menu
id="fileMenu"
label="File">
<command
commandId="de.vogella.rcp.commands.first.commands.Exit"
label="Exit"
style="push"
tooltip="Exit the application">
</command>
</menu>
</menuContribution>
</extension>
</plugin>
运行该示例。您应该看到的文件菜单,如果你选择“exit”项你应该退出程序。
3. Commands and toolbars
3.1。概述
您可以添加command到应用程序工具栏,或者到一个视图的工具栏上。
您可以通过使用模式定义了一个command的相对位置?前= id或?后=id。该ID可以是现有的分隔符的名称,menu ID或item ID。该command将被放置之前或之后与相应对应的id元素。
3.2. 应用程序工具栏 (coolbar)
创建一个新的项目“de.vogella.rcp.intro.commands.toolbar”。选择使用“RCP application with a view“模板.
创建一个command为“de.vogella.rcp.intro.commands.toolbar.Hello”同时创建default handler为“de.vogella.rcp.intro.commands.toolbar.handler.Hello”。
package de.vogella.rcp.intro.commands.toolbar.handler;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.handlers.HandlerUtil;
public class Hello extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(
event).getShell(), "Info", "Info for you");
return null;
}
}
添加一个menucontribution在“org.eclipse.ui.menus”扩展点上。在URI位置上设置为“toolbar:org.eclipse.ui.main.toolbar”。将其添加到您的工具栏菜单。
在工具栏上添加command“de.vogella.rcp.intro.commands.toolbar.Hello”的。在标签栏输入标签名,并在图标栏输入一个图标。
打开应用程序中ApplicationWorkbenchWindowAdvisor.java类文件,设置configurer.setShowCoolBar(true);
package de.vogella.rcp.intro.commands.toolbar;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
public ApplicationWorkbenchWindowAdvisor(
IWorkbenchWindowConfigurer configurer) {
super(configurer);
}
public ActionBarAdvisor createActionBarAdvisor(
IActionBarConfigurer configurer) {
return new ApplicationActionBarAdvisor(configurer);
}
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
configurer.setInitialSize(new Point(400, 300));
configurer.setShowStatusLine(false);
configurer.setShowCoolBar(true);
configurer.setTitle("RCP Application");
}
}
结果应该如下所示:
3.3. 将工具栏展示在视图上
您也可以直接添加一个command到一个视图工具栏。为此,我们将扩展前面的例子。
更改Perspective.java如下(一个独立的视图并没有自己的工具栏)。
package de.vogella.rcp.intro.commands.toolbar;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
public class Perspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) {
String editorArea = layout.getEditorArea();
layout.setEditorAreaVisible(false);
layout.setFixed(true);
layout.addView(View.ID, IPageLayout.LEFT, 1.0f, editorArea);
}
}
同样在"org.eclipse.ui.menus"点上扩展一个新的menucontribution ,在URI位置上输入"toolbar:de.vogella.rcp.intro.commands.toolbar.view".
工具栏告诉系统将它添加到工具栏,而第二个参数是你的视图id
然后为这个menucontribution创建一个新的command并设置command ID为“de.vogella.rcp.intro.commands.toolbar.Hello”。标签栏输入“Say Hello”。
运行应用程序,将看你的新视图的效果。
3.4. 下拉列表
下面的下拉列表中添加一个CoolBar栏中的应用程序。
这个创举,是有点奇怪。您创建一个帮助下拉命令后来其他(真实)的command将被分配到。
因此,创建一个command ID为“referenceToMenuId”。同此也是default handler。对于你这个样例可以重新使用“de.vogella.rcp.intro.commands.toolbar.handler.Hello”。
基于“org.eclipse.ui.menus”添加新menucontribution扩展点。设置URI位置为的“toolbar:org.eclipse.ui.main.toolbar”。添加这个扩展工具栏和一个新的command,以这个新的工具栏。ID使用“referenceToMenuId”同时给它一个标签和一个图标和更改样式为“pull down”。
创建一个新的menucontribution并设置locationURI为:“menu:referenceToMenuId
referenceToMenuId是我们较早使用ID在命令中
在这个菜单里添加您的exit命令“de.vogella.rcp.intro.commands.toolbar.Hello”两次。并使用不同的标签。
运行您的应用程序,现在应用程序应该有一个工具栏下拉列表的。
同样你也能将该下拉工具栏命令“referenceToMenuId“添加到你的视图工具栏上。
(版权归原作者所有)