我正在开发一个RCP应用程序。该应用程序有一个执行模式,我想启用各种键绑定来控制启动,停止,继续,重复等。绑定将使用在任何相关视图被激活时设置的'ExecutionContext'启用。
上下文切换在每个“执行”视图中完成。
@Override
public final void createPartControl(Composite parent)
{
addPartListener();
...
}
private void addPartListener()
{
this.getViewSite().getPage().addPartListener(new IPartListener2()
{
IContextActivation token = null;
@Override
public void partDeactivated(IWorkbenchPartReference partRef)
{
if (token != null)
{
System.out.println("End exec context");
IContextService contextService = (IContextService) PlatformUI.getWorkbench().getService(
IContextService.class);
contextService.deactivateContext(token);
token = null;
}
}
@Override
public void partActivated(IWorkbenchPartReference partRef)
{
System.out.println("Set exec context");
IContextService contextService = (IContextService) PlatformUI.getWorkbench().getService(
IContextService.class);
token = contextService.activateContext("AtfExecutionContext");
}
});
}
我可以通过Console消息看到我的上下文正在设置,并且一些键绑定正在按预期工作。
但是,如果已从另一个插件分配了键绑定,则该绑定具有优先级。例如。我想使用Ctrl + F8停止但是当按下它时,我得到“下一个透视”动作,这是工作台的默认值。
绑定定义是
<extension
point="org.eclipse.ui.bindings">
<scheme
id="atfscheme"
name="atfscheme"
parentId="org.eclipse.ui.defaultAcceleratorConfiguration">
</scheme>
<key
commandId="com.xxx.atf.model.ui.commands.ExecKey.Start"
contextId="AtfExecutionContext"
schemeId="atfscheme"
sequence="M1+M2+F5">
<!-- F5 does not work but Ctrl-Shift-F5 does -->
</key>
</extension>
<extension
point="org.eclipse.ui.contexts">
<context
id="AtfExecutionContext"
name="AtfExecutionContext"
parentId="org.eclipse.debug.ui.debugging">
<!-- have tried various parentid values... -->
</context>
</extension>
似乎只有以前未定义的加速器才有效。在设置上下文时,我需要做什么来覆盖现有定义并激活我的定义?
每个部分都有单独的上下文服务,您必须使用正确的上下文服务。
没有必要激活/停用部件激活/停用的上下文。单独的上下文服务将自动处理。
所以在createPartControl
激活:
IContextService contextService = getSite().getService(IContextService.class);
token = contextService.activateContext("AtfExecutionContext");
当零件关闭时停用。
您还要定义一个新的密钥绑定方案 - 必须单独激活,而不是您想要的。只需删除它,只需使用org.eclipse.ui.defaultAcceleratorConfiguration
作为schemeId