如何使 Eclipse 启用当表达式在非焦点视图中用于选择时?



>我有一个连接到菜单贡献和命令的处理程序。菜单贡献向视图添加一个按钮,我希望根据调试视图中的选择启用该按钮。

所以这是表达式:

<handler
class="com.example.myhandler"
commandId=" com.example.mycommand">
<enabledWhen>
<with
variable="selection">
<iterate
ifEmpty="false">
<instanceof
value="org.eclipse.cdt.dsf.ui.viewmodel.datamodel.IDMVMContext">
</instanceof>
</iterate>
</with>
</enabledWhen>
</handler>

这绝对适用于调试视图具有焦点的点,这意味着如果我在调试视图中选择元素,则还会启用单独视图中添加的按钮(根据需要)。一旦我单击通过菜单贡献添加按钮的视图,它就会突然被禁用(我猜是因为selection是空的,即使它仍然被选中;但调试视图没有焦点)。如何使这项工作,以便仍独立于调试视图的焦点状态考虑所选内容?

(您似乎在问DSF特定问题,该问题与您的标题所指的"一般"情况有不同的答案。因此,此答案可能会解决您的问题,但可能无法解决一般情况。

扩展 DSF-GDB 的完整示例在org.eclipse.cdt.examples.dsf.gdb捆绑包中的 CDT 源存储库中提供。

该示例定义了一个新命令org.eclipse.cdt.examples.dsf.gdb.command.showVersion

<!-- Example showing how to add a custom command with toolbar/menu contributions with DSF.
The example command id is org.eclipse.cdt.examples.dsf.gdb.command.showVersion.
In this example, when run it will display the version of GDB that is connected. -->
<extension point="org.eclipse.ui.commands">
<command
categoryId="org.eclipse.cdt.debug.ui.category.debugViewLayout"
description="Show the GDB Version in a pop-up"
id="org.eclipse.cdt.examples.dsf.gdb.command.showVersion"
name="Show GDB Version">
</command>
</extension>

它继续演示如何使用org.eclipse.ui.menus扩展点将命令贡献给菜单。然后将命令绑定到具有org.eclipse.ui.handlers扩展点的命令处理程序。

到目前为止,DSF 的行为与"正常"命令相同。但在 DSF(使用平台调试提供的可重定目标命令基础结构)中,处理程序不是您尝试运行的命令,而是 DebugCommandHandler 的子类。

然后,DSF 可以使用适配器将该命令绑定到具体的命令实现,具体取决于"调试"视图中所选调试会话的内容。在显示版本的情况下,这是GdbShowVersionHandler(IDebugCommandHandler的实现)。处理程序有一个canExecute,如果需要,可以连接到后端 (gdb) 以查看当前选择是否适用。canExecute接收可以转换为 DSF 上下文对象的内容,如下所示:

private Optional<ICommandControlDMContext> getContext(final IDebugCommandRequest request) {
if (request.getElements().length != 1 || !(request.getElements()[0] instanceof IDMVMContext)) {
return Optional.empty();
}
final IDMVMContext context = (IDMVMContext) request.getElements()[0];
ICommandControlDMContext controlDmc = DMContexts.getAncestorOfType(context.getDMContext(),
ICommandControlDMContext.class);
if (controlDmc != null)
return Optional.of(controlDmc);
return Optional.empty();
}

PS 不久前我将此示例添加到 CDT 以帮助另一个扩展器。关于cdt-dev的对话也可能有用?这一切都是最初为这个错误添加的,它相关的gerrit将所有更改用于添加新命令拉到一个地方。

最新更新