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.
I am working on a Desktop app with the Netbeans RCP. I have a
Share
One way to do this in the NetBeans Platform is to register a Presenter.Menu in the menu:
When you register a
Presenter.Menuin the menu thegetMenuPresenter()method is called by the platform to get the actualJMenuItemthat is added to the menu.Normally you would just construct a
JMenuItemhere 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.One way to do this is to register all of your ACL’d menu items as a
ServiceProvider. In this way you canLookupall of them when you need to enable/disable them.A
ServiceProviderinterface:A
ControllableMenuItemimplementation registered as aServiceProvider:Now you can
Lookupall of theControllableMenuItems when you need to enable/disable them:However there’s one more piece for this to work properly. You need a way to guarantee that the
Presenter.Menuis getting the same instance that theLookupis getting. One way to do this – admittedly not very elegant – is to register theMenuItemas a@ServiceProviderfor itself and look this up ingetMenuPresenter():In this way you are guaranteed to get the same instance whenever you
LookupyourControllableMenuItems.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.