禁用 Jface 菜单管理器(灰显)

发布于 2024-12-15 01:32:35 字数 388 浏览 1 评论 0原文

我有一个菜单管理器“menuManager”,其中包含菜单项(操作)和一个子菜单,另一个菜单管理器“subMenu”包含更多操作。

final MenuManager subMenu = new MenuManager("Main",null);
subMenu.add(mActionClose);    

MenuManager menuManager = new MenuManager("#PopupMenu", "contextMenu");
menuManager.add(action1);
menuManager.add(action2);
menuManager.add(subMenu);

我只能在操作上设置 setEnabled(false) 而不能在菜单管理器上设置。

I have a menumanager "menuManager" that holds menu items (actions) and a sub-menu, another menumanager "subMenu" that contains more actions.

final MenuManager subMenu = new MenuManager("Main",null);
subMenu.add(mActionClose);    

MenuManager menuManager = new MenuManager("#PopupMenu", "contextMenu");
menuManager.add(action1);
menuManager.add(action2);
menuManager.add(subMenu);

I can only set setEnabled(false) on actions and not on menumanager.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飞烟轻若梦 2024-12-22 01:32:35

重写 MenuManager 没有任何效果。您可以做的是访问相关的 MenuItem 并尝试禁用它。

首先,添加 IMenuListener 到您的“上下文菜单”管理器以访问关联的 < code>Menu

public void menuAboutToShow(IMenuManager manager) {
    manager.getMenu().addListener(SWT.Show, showContextMenuListener);
}

在侦听器中,您可以找到与子菜单管理器匹配的 MenuItem

public void handleEvent(Event event) {
    for (MenuItem item: ((Menu)event.widget).getItems())
        //MenuItem data should be our MenuManager
        if (item.getData().equals(subMenu)) {
            //Disable the sub-menu item
            item.setEnabled(false);
            //Add listener to sub-menu (see comments below the code block)
            item.getMenu().addListener(SWT.Show, showSubMenuListener);
        }
}

问题在于 MenuManager 中有一个错误修复代码 类将单击后重新启用子菜单项(如果它包含任何项目)。要覆盖此设置,您还需要向子菜单添加 SWT.Show 侦听器,以便在单击后菜单变得可见时更新子菜单项的启用:

public void handleEvent(Event event) {
    ((Menu)event.widget).getParentItem().setEnabled(false);
}

Overriding isEnabled() method of MenuManager doesn't have any effect. What you can do is get access to related MenuItem and try to disable it.

First, you add IMenuListener to your "context menu" manager to access associated Menu:

public void menuAboutToShow(IMenuManager manager) {
    manager.getMenu().addListener(SWT.Show, showContextMenuListener);
}

In the listener you find the MenuItem that matches your sub-menu manager:

public void handleEvent(Event event) {
    for (MenuItem item: ((Menu)event.widget).getItems())
        //MenuItem data should be our MenuManager
        if (item.getData().equals(subMenu)) {
            //Disable the sub-menu item
            item.setEnabled(false);
            //Add listener to sub-menu (see comments below the code block)
            item.getMenu().addListener(SWT.Show, showSubMenuListener);
        }
}

The problem with this is that there's a bug fix code in MenuManager class that will re-enable your sub-menu item once it is clicked (in case it contains any items). To override this you also need to add SWT.Show listener to the sub-menu that will update the enablement of sub-menu item when the menu becomes visible after a click:

public void handleEvent(Event event) {
    ((Menu)event.widget).getParentItem().setEnabled(false);
}
亢潮 2024-12-22 01:32:35

无法在jface MenuManager 上设置enabled==false,并且当我查看代码时,我没有看到它在渲染期间使用isEnabled()。我知道在大多数情况下,如果该菜单为空,它根本不会呈现子菜单。

在弹出菜单中,如果在 SWT.Show 事件上它可以确定没有子菜单,它将禁用子菜单,但我认为这种行为有点违反直觉。

一种体面的用户体验行为是始终保留该子菜单,但在应该使用时将其扩展为一个禁用的菜单项:“<无可用操作>”或“<未启用>”或类似的东西。

There is no way to set enabled==false on the jface MenuManager, and when I looked through the code I didn't see it using isEnabled() during its rendering. I know in most cases it simply doesn't render a submenu if that menu is empty.

In popup menus, it will disable a submenu if on an SWT.Show event it can determine that there are no children, but I think that behaviour is a little counter-intuitive.

A decent UX behaviour is to always have that submenu there, but have it expand to one disabled menu item when it should be used: "<no actions available>" or "<not enabled>" or something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文