【问题标题】:Toggling the state of a menu item切换菜单项的状态
【发布时间】:2012-09-16 13:51:08
【问题描述】:

我正在开发一个 Eclipse RCP 应用程序。它有一些特定于视图的菜单,其中一个菜单项是我想在启用相应功能时在旁边显示一个勾号的项目。同样,下次选择该项目时,该项目应取消勾选以反映相应的功能已禁用。

我的问题是:如何设置这些菜单项的切换状态? I have an IHandler to deal with the event when the menu item is selected but I'm unsure how to update the GUI element itself.

StackOverflow 社区对我如何解决这个问题有任何想法吗?

【问题讨论】:

  • 你不需要标记你的标题,这就是标记的用途。

标签: java eclipse


【解决方案1】:

解决方案涉及让命令处理程序实现 IElementUpdater 接口。然后可以这样更新 UI 元素:

public void updateElement(UIElement element, Map parameters) 
{
    element.setChecked(isSelected);     
}

updateElement 作为 UI 刷新的一部分被调用,可以从处理程序的执行命令中调用,如下所示:

     ICommandService service = (ICommandService) HandlerUtil
       .getActiveWorkbenchWindowChecked(event).getService(
           ICommandService.class);
   service.refreshElements(event.getCommand().getId(), null);

更多信息here(参见单选按钮命令更新检查状态条目)

【讨论】:

    【解决方案2】:

    也许事情发生了变化,也许 RCP 和 Eclipse 插件之间存在差异,但我只是将我的操作样式设置为 toggle,它会自动处理切换。可以在github.com看到源码。

         <action
               class="live_py.EnableAction"
               id="live-py.enable.action"
               label="Li&amp;ve Coding"
               menubarPath="pyGeneralMenu/pyNavigateGroup"
               state="false"
               style="toggle">
    

    要查看该功能当前是否已启用,我只需通过其 id 查找操作,然后调用isChecked()

    【讨论】:

    • 您为我节省了很多时间和精力 - 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-12-15
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多