4. 命令和菜单上下文
现在让我们将ContextMenu添加到表。创建一个新的项目“de.vogella.rcp.intro.commands.popup”基于“RCP application with a view”的例子。
创建一个ID为“de.vogella.rcp.intro.commands.popup.showSelected”的新Command,名称为“Show”。
在这个例子中,我们将不使用default handler。因此,添加扩展点“org.eclipse.ui.handlers”到您的plugin.xml中,添加一个handler。第一个参数是commandId,第二为handler的类。我们将用“de.vogella.rcp.intro.commands.popup.handler.ShowSelected”。
现在为您处理运行编码。我只打印选定的内容到控制台。
package de.vogella.rcp.intro.commands.popup.handler;
import java.util.Iterator;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;
public class ShowSelected extends AbstractHandler {
@SuppressWarnings("unchecked")
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
.getActivePage().getSelection();
if (selection != null & selection instanceof IStructuredSelection) {
IStructuredSelection strucSelection = (IStructuredSelection) selection;
for (Iterator<Object> iterator = strucSelection.iterator(); iterator
.hasNext();) {
Object element = iterator.next();
System.out.println(element.toString());
}
}
return null;
}
}
添加新menuContribution其URI输入“popup:de.vogella.rcp.intro.commands.popup.view”,其ID为“de.vogella.rcp.intro.commands.popup.view”视图是系统自动为你创建的。
右键单击你新menuContribution项,并选择new - >command。分别输入你的command字段为“commandId”。标签是“One Item selected”。
现在你有一个MenuManager添加到您的视图。选择View.java对方法createPartControl添加以下内容:
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setInput(getViewSite());
// 这是新增编码
// 第一我们创建一个菜单管理
MenuManager menuManager = new MenuManager();
Menu menu = menuManager.createContextMenu(viewer.getTable());
// 设置菜单管理
viewer.getTable().setMenu(menu);
getSite().registerContextMenu(menuManager, viewer);
// 选择可用
getSite().setSelectionProvider(viewer);
}
运行您的应用程序。单击右键鼠标菜单应该可见。如果你选中它然后选定菜单项的名称应写入到控制台。
5。启用时(可见时)
命令框架允许,限制通过核心展示可用性和可视性的命令,处理程序和用户界面的处理。
在这个例子中,我们希望如果一个从项目列表项被选中命令仅启用。
创建一个新的项目“de.vogella.rcp.commands.enable”基于“RCP application with a view”的示例中。
添加一个command其ID为“de.vogella.rcp.commands.enable.command”。这个命令添加到菜单和工具栏,并建立下列默认处理程序。
package de.vogella.rcp.commands.enable.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 Command extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(
event).getShell(), "Info", "Info for you");
return null;
}
}
plugin.xml中选择并添加“org.eclipse.core.expressions”作为依赖。然后选择扩展选项卡,并添加扩展org.eclipse.core.expressions.definitions。
用鼠标右键点击添加定义“oneElementSelected”。添加“with”变量“selection”。
"selection”是一个预定义的变量,将由Eclipse自动计算。见额外的变量可以使用的核心表达。对于一个弹出菜单的例子
“activeMenuSelection”提供了选择。
在“activeMenuSelection”点击右鼠标并添加一个计数其变量为“1”。结果应该看起来像如下
在你的handler上点击鼠标右键,选择enabledWhen,鼠标右键点击添加一个“reference”,它价值“oneElementSelected”。结果应该看起来像。
正在使用的展示是基于“selection”。因此,名单必须登记为选择提供者本身告知的情况下工作台的东西被选中。因此,在View.java类createPartControl方法里加上“getSite().setSelectionProvider(viewer);”
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setInput(getViewSite());
// Makes the selection available to the workbench
getSite().setSelectionProvider(viewer);
}
如果您现在运行的应用程序中如果一个列表中的元素被选中,command应该为启用。
你还可以只显示菜单,如果一个项目被选中。该方法是相同的,但你只定义限制在菜单上直接促成作用(可见当在菜单上的命令)。对于一个工具栏命令的定义如下所示:
6。快捷键的命令
6.1。概述
你可以通过快捷键定义为您的命令快捷方式。 Eclipse使用默认密钥配置方案。 Eclipse RCP应用程序的挑战是,你的应用程序通常不希望有标准的Eclipse IDE的快捷方式可以在使用。
因此,以下介绍了如何创建快捷键,以及如何覆盖默认的键绑定的Eclipse架构。
您需要定义和使用自己的方案:
- 定义一个扩展点计划“org.eclipse.ui.bindings”
- 按需分配这个快捷键定义
- 创建一个产品
- 定义文件“plugin_customization.ini”,或者通过product属性设置方案。
6.2。按键绑定
创建一个新的项目“de.vogella.rcp.intro.commands.keybinding”使用的“Hello RCP”模板,声明一个command为“de.vogella.rcp.intro.commands.keybinding.hello”其default handler为 “de.vogella.rcp.intro.commands.keybindingHelloHandler“其打印输出”Hello“到控制台
添加扩展点“org.eclipse.ui.bindings”到您的项目。就在这个扩展点单击鼠标右键,选择New ->Key。
选择schemaID输入“org.eclipse.ui.defaultAcceleratorConfiguration”这是平台默认值,其会确保你按键绑定是在整个应用程序上有效。该commandId是你刚刚创建的command的ID。这里sequence是指定命令快捷键。M1代表Cntr键。
如果您现在运行它,按键绑定应该工作,如果您按Cntrl +1在控制台上应该可见的信息。
6.3。定义你自己的模式
现在添加另一个快捷键“Cntr+N”的command,以“de.vogella.rcp.intro.commands.keybinding.hello”。试试吧。现在这还无法工作,因为它是用Eclipse默认模式冲突。
就在“org.eclipse.ui.bindings”这个扩展点按鼠标右键,选择New ->Scheme。创建一个schema的ID为“MyScheme”。
指定你的command的参数scheme ID。
定义一个product。详情请参阅定义product for Eclipse RCP。
创建以下文件“plugin_customization.ini”,并把它方在你的主目录下
org.eclipse.ui/KEY_CONFIGURATION_ID=myscheme
如果您现在运行你的产品的“Cntr+N”的快捷方式应该工作
7。 Eclipse的标准命令
7.1。概述
Eclipse提供很多标准的命令让你可以重复使用。只需按下“Browse button”,同时确定你的commandId看到可用的标准命令。例如,屏幕显示关于对话框命令的标准用法。
使用标准的命令的好处是你可以绑定按键,图标等等。
标准command往往是map类型的action,在类ApplicationActionBarAdvisor中通过ActionFactory调用。如果ActionFactory返回
IAction您需要注册这个动作。如果没有这些命令是不活跃的在您的菜单。例如下面的复位视图和欢迎命令活动。
// Method belongs to class ApplicationActionBarAdvisor
@Override
protected void makeActions(IWorkbenchWindow window)
{
IWorkbenchAction quickStartAction = ActionFactory.INTRO.create(window);
register(quickStartAction);
IWorkbenchAction resetView = ActionFactory.RESET_PERSPECTIVE
.create(window);
register(resetView);
}
7.2。例子
您也可以使用标准的命令,并定义一个新的这个命令处理程序,例如你可以使用标准的Eclipse删除命令(org.eclipse.ui.edit.delete)。
要查找现有的command的ID,你可以利用你Eclipse Plugin Spy。
你可以使用扩展点“org.eclipse.ui.handlers”,以确定新的标准命令处理程序。
Eclipse要求在时间段内只有唯一有效的handler,因此在你要使用的处理程序时有“activeWhen”的限制,使其更加具有约束力的标准,特别那么只有一个活动的处理程序。例如实例的删除在若干视图中有不同的处理程序时,要使用“activeWhen”与变量“activePartId”和你View的id值。
创建一个新的RCP应用程序“de.vogella.rcp.commands.standardcommands”与模板的“Hello RCP的”。
创建一个命令处理程序“org.eclipse.ui.edit.delete”,这显示一个消息框。添加命令“org.eclipse.ui.edit.delete”菜单。
结果应该如下所示。
8。从一个按钮调用command
你可以调用一个命令,例如在这个例子中“add.command”从一个按钮通过以下编码。
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
IHandlerService handlerService = (IHandlerService) getSite()
.getService(IHandlerService.class);
try {
handlerService.executeCommand("add.command", null);
} catch (Exception ex) {
throw new RuntimeException("add.command not found");
}
}
});
9。提供自己的表达式
你可以编写自己的Java类,它可以用来定义一个可以被定义的变量,如某一个用户界面元素是否激活。例如,你可以读出自己的类用户的授权,然后返回的值是分配给用户。这个返回值可以被用在可见/启用的声明。这是类似的核心,我们看到刚才表达的用法。
创建RCP应用程序“de.vogella.rcp.commands.sourceprovider”,用“Hello RCP”的模板。
我们将创建一个供应源。此来源提供者可以提供可在能见度定义的命令使用的变量。添加扩展点“org.eclipse.ui.services”到你的plugin,并创建一个新的service provider。
这定义了一个services,提供变量“de.vogella.rcp.commands.sourceprovider.active”到工作台。这个类必须实现ISourceProvider接口。这个值可以用来表达类似的核心。编写下面的代码
package de.vogella.rcp.commands.sourceprovider;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.ui.AbstractSourceProvider;
import org.eclipse.ui.ISources;
public class CommandState extends AbstractSourceProvider {
public final static String MY_STATE = "de.vogella.rcp.commands.sourceprovider.active";
public final static String ENABLED = "ENABLED";
public final static String DISENABLED = "DISENABLED";
private boolean enabled = true;
@Override
public void dispose() {
}
// We could return several values but for this example one value is sufficient
@Override
public String[] getProvidedSourceNames() {
return new String[] { MY_STATE };
}
// You cannot return NULL
@SuppressWarnings("unchecked")
@Override
public Map getCurrentState() {
Map map = new HashMap(1);
String value = enabled ? ENABLED : DISENABLED;
map.put(MY_STATE, value);
return map;
}
// This method can be used from other commands to change the state
// Most likely you would use a setter to define directly the state and not use this toogle method
// But hey, this works well for my example
public void toogleEnabled() {
enabled = !enabled ;
String value = enabled ? ENABLED : DISENABLED;
fireSourceChanged(ISources.WORKBENCH, MY_STATE, value);
}
}
创建两个command“de.vogella.rcp.commands.sourceprovider.command1”和“de.vogella.rcp.commands.sourceprovider.command2”。创建您想要的任何处理的第一个command。 command2的将被用于改变我们的ISourceProvider的状态。为保持处理下面的代码。
package de.vogella.rcp.commands.sourceprovider.handler;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.services.ISourceProviderService;
import de.vogella.rcp.commands.sourceprovider.CommandState;
public class Command2 extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// Get the source provider service
ISourceProviderService sourceProviderService = (ISourceProviderService) HandlerUtil
.getActiveWorkbenchWindow(event).getService(
ISourceProviderService.class);
// Now get my service
CommandState commandStateService = (CommandState) sourceProviderService
.getSourceProvider(CommandState.MY_STATE);
commandStateService.toogleEnabled();
return null;
}
}
同时添加命令到菜单。
你现在使用你的菜单提供的申报ISourceProvider状态。
这看起来像下面的plugin.xml。
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu">
<command
commandId="de.vogella.rcp.commands.sourceprovider.command1"
label="Command1"
style="push">
<visibleWhen
checkEnabled="false">
<with
variable="de.vogella.rcp.commands.sourceprovider.active">
<equals
value="ENABLED">
</equals>
</with>
</visibleWhen>
</command>
<command
commandId="de.vogella.rcp.commands.sourceprovider.command2"
label="Command2"
style="push">
</command>
</menuContribution>
</extension>
如果你现在运行你的应用程序,点击选择第二个命令时第一个命令将不可用了。如果你再次按下它第一个命令将被再次显示。
10。利用command参数
您也可以在command中定义的参数。创建工程“de.vogella.rcp.commands.parameterfirst”使用的“Hello RCP”程序模板。
创建一个command其ID为“de.vogella.rcp.commands.parameterfirst.helloName”和默认处理程序是“de.vogella.rcp.commands.parameterfirst.handler.HelloName”。
右键点击你的command,选择New - > commandParameter
使用ID的参数为“de.vogella.rcp.commands.parameterfirst.commandParameter1
在处理程序中你必须处理参数。
package de.vogella.rcp.commands.parameterfirst.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 HelloName extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
String name = event
.getParameter("de.vogella.rcp.commands.parameterfirst.commandParameter1");
MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
"Hello", "Hello " + name);
return null;
}
}
将这个command添加到菜单。在command上,点击右键并选择 New - >Parameter
相同的命令再次添加到菜单,并通过另一个参数。
其name和参数里名称相同,你要在你的handler里实现值与值传递。
如果你运行你的应用程序,并选择菜单项,其值应在消息框中显示。
11。在运行时定义的command
你可以在运行时创建的command。创建工程“de.vogella.rcp.commands.runtimecommands”使用的“Hello RCP”的模板。
定义一个菜单。继续保维持这个菜单的class为“de.vogella.rcp.commands.runtimecommands.DefineCommands”。
创建下面的类。
package de.vogella.rcp.commands.runtimecommands;
import org.eclipse.swt.SWT;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;
import org.eclipse.ui.menus.ExtensionContributionFactory;
import org.eclipse.ui.menus.IContributionRoot;
import org.eclipse.ui.services.IServiceLocator;
public class DefineCommands extends ExtensionContributionFactory {
@Override
public void createContributionItems(IServiceLocator serviceLocator,
IContributionRoot additions) {
CommandContributionItemParameter p = new CommandContributionItemParameter(
serviceLocator, "",
"org.eclipse.ui.file.exit",
SWT.PUSH);
p.label = "Exit the application";
p.icon = Activator.getImageDescriptor("icons/alt_window_16.gif");
CommandContributionItem item = new CommandContributionItem(p);
item.setVisible(true);
additions.addContributionItem(item, null);
}
}
运行该示例,在菜单上的应该由退出命令。
12。谢谢
非常感谢你使用本教程练习
(版权归原作者所有)