java - 根据切换状态禁用/启用工具栏菜单项

标签 java eclipse-plugin swt eclipse-rcp

我正在处理工具栏项的启用/禁用状态。

我需要禁用第二个菜单,直到第一个菜单处于打开状态。为此,我尝试获取第一个菜单项的切换状态。但状态始终为 NULL。

ICommandService service = (ICommandService) PlatformUI.getWorkbench().
                                                   getService(ICommandService.class);
Command command = service.getCommand("com.commandID");
State state     = command.getState("org.eclipse.ui.commands.toggleState");

有人知道是什么问题吗?也许有更好的方法来解决它?

最佳答案

您是否为您的命令注册了 ToggleState?

Since it is possible to have multiple menu contributions for the same command, we have to keep track of the state in a central place. Imagine a toggle button triggerable from the main menu and a views toolbar. The state of these buttons are keept in sync by storing the state directly in the command. The key to this is the org.eclipse.jface.commands.ToggleState.

<command id="org.eclipse.example.command.toggle"
        name="Toggle Me">
  <state class="org.eclipse.jface.commands.ToggleState"
         id="org.eclipse.example.command.toggleState" />
</command>

请阅读this blog post了解更多详情。

关于java - 根据切换状态禁用/启用工具栏菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20546360/

相关文章:

java - Eclipse RCP - 以编程方式更改 plugin.xml?重新加载?

java - 如何在 Eclipse Debug模式下保持突出显示已执行的 Java 行?

ubuntu - JRuby + SWT + 系统托盘 : What am I doing wrong?

Java:JTable 中的控制台输出

java - 我应该使用 Jain SIP (java) 中的哪些类来制作 SIP 客户端?

java - 搜索大量有序字符串

java - Eclipse TreeViewer/ViewPart 后台点击

c# - 什么是更好的编码标准 x != null 或 null != x?

java - Eclipse 插件 : Run code right after startup

Java:尝试从不同线程更新 SWT 标签时出现 NullPointerException