以编程方式禁用菜单项 [英] Disable menu items programmatically

查看:94
本文介绍了以编程方式禁用菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Netbeans RCP开发桌面应用程序。我有一些菜单项通过TopComponents中的注释被添加到菜单中。
我希望能够禁用其中一些菜单项,具体取决于登录用户的访问权限。

I am working on a Desktop app with the Netbeans RCP. I have a number of menu items that are beeing added to the menu through annotations in the TopComponents. I would like to be able to disable several of these menu items, depending on the access rights of the logged user.

推荐答案

在NetBeans平台中执行此操作的一种方法是注册 Presenter.Menu

One way to do this in the NetBeans Platform is to register a Presenter.Menu in the menu:

@ActionID(id = "com.company.MyPresenter", category = "Edit")
@ActionRegistration(displayName = "com.company.Bundle#CTL_MyPresenter")
@ActionReference(path = "Menu/Edit", position = 1)
public class MyPresenter implements Presenter.Menu {

    @Override
    public JMenuItem getMenuPresenter() {
        // return menu item from your ACL'd registry
    }
}

注册演示者时菜单调用菜单中的.Menu 方法,由平台调用 getMenuPresenter()方法以获取实际的 JMenu项目添加到菜单中。

When you register a Presenter.Menu in the menu the getMenuPresenter() method is called by the platform to get the actual JMenuItem that is added to the menu.

通常,您只需构造一个 JMenuItem 在这里,但是由于您需要在应用程序的其他部分中拥有它,因此您需要保留菜单项的某种注册表,以便检索同一实例。

Normally you would just construct a JMenuItem here but since you need to be able to get a hold of it in other parts of the application you'll need to keep some kind of registry of your menu items so that you'll be retrieving the same instance.

一种方法是将所有ACL菜单项注册为 ServiceProvider 。这样,当您需要启用/禁用它们时,您可以查阅所有它们。

One way to do this is to register all of your ACL'd menu items as a ServiceProvider. In this way you can Lookup all of them when you need to enable/disable them.

A ServiceProvider 接口:

public interface ControllableMenuItem {

    void setEnabled(boolean enabled);

    JMenuItem getMenuItem();
}

A ControllableMenuItem 实现注册为 ServiceProvider

@ServiceProvider(service = ControllableMenuItem.class)
public class MyMenuItem implements ControllableMenuItem {

    private JMenuItem menuItem;

    @Override
    public void setEnabled(boolean enabled) {
        getMenuItem().setEnabled(enabled);
    }

    @Override
    public JMenuItem getMenuItem() {
        if (menuItem == null) {
            menuItem = new JMenuItem(new MyAction());
        }
        return menuItem;
    }
}

现在您可以查阅需要启用/禁用它们的所有 ControllableMenuItem

Now you can Lookup all of the ControllableMenuItems when you need to enable/disable them:

Collection<? extends ControllableMenuItem> menuItems = 
    Lookup.getDefault().lookupAll(ControllableMenuItem.class);
for (ControllableMenuItem item : menuItems) {
    item.setEnabled(isAuthorized());
}

不过,还有其他一项可以使其正常工作。您需要一种方法来确保 Presenter.Menu 所获取的实例与 Lookup 所获取的实例相同。一种做到这一点的方法(虽然不是很优雅)是将 MenuItem 注册为自己的 @ServiceProvider 并进行外观 getMenuPresenter()

However there's one more piece for this to work properly. You need a way to guarantee that the Presenter.Menu is getting the same instance that the Lookup is getting. One way to do this - admittedly not very elegant - is to register the MenuItem as a @ServiceProvider for itself and look this up in getMenuPresenter():

// replaces the above annotation of MyMenuItem
@ServiceProviders(value = {
    @ServiceProvider(service = ControllableMenuItem.class),
    @ServiceProvider(service = MyMenuItem.class)
})
public class MyMenuItem implements ControllableMenuItem {...}

public class MyPresenter implements Presenter.Menu {

    @Override
    public JMenuItem getMenuPresenter() {
        MyMenuItem myMenuItem = Lookup.getDefault().lookup(MyMenuItem.class);
        return myMenuItem.getMenuItem();
    }
}

通过这种方式,您可以确保获得同一实例每当您查找您的 ControllableMenuItem s。

In this way you are guaranteed to get the same instance whenever you Lookup your ControllableMenuItems.

这是这样做的唯一方法。这里的要点是,当您需要禁用所有ACL菜单项时,您需要具有一种机制来获取它们的所有实例。

This is only one way of doing this. The point here is that you need to have a mechanism in place to get the same instances of all of your ACL'd menu items when you need to disable them.

另一个控制哪些菜单项实际进入菜单系统的方法是为每个访问级别创建单独的模块,并且只需禁用模块。这在许多方面都是有益的,并且是使用模块化系统的优势之一。

Another approach to controlling what menu items actually make it into the menu system is to create separate modules for each level of access and simply disable modules when users aren't authorized for a particular group of functionality. This is beneficial in many respects and is one of the strengths of using a modular system.

这篇关于以编程方式禁用菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆