Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Dynamic Menu Entry enablement
Dynamic Menu Entry enablement [message #779751] Mon, 16 January 2012 13:23 Go to next message
Catalin Gerea is currently offline Catalin Gerea
Messages: 89
Registered: July 2009
Location: Bucharest, Romania
Member

I have a view containing a table viewer. The view has a popup menu created using org.eclipse.ui.menus extension point (locationURI:my_view_id).
The menu has N entries -> N commands with a handler extending the same BaseHandler that overrides isEnabled() method.
The user opens the menu and the command is enabled (the handler's is Enabled() method returns true).
Now an external event happens (a subsystem is stopped/the network cable is unplugged). For this event isEnabled() method of the handler will return false.
NOTE: The menu remains opened and it is not closed.

QUESTION:
How can I programmatically disable/enable the menu entries when this external event happens?
If I do not disable them when the user clicks on the menu entry it will receive "org.eclipse.core.commands.NotEnabledException: Trying to execute the disabled command".
I tried to make BaseHandler class implement IElementUpdater interface, but this does not cover the enablement of the menu entries.


Time is what you make of it.
Re: Dynamic Menu Entry enablement [message #779879 is a reply to message #779751] Mon, 16 January 2012 18:19 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Are you calling setBaseEnabled(*) and allowing the AbstractHandler to fire a HandlerEvent ?

PW


Re: Dynamic Menu Entry enablement [message #780185 is a reply to message #779879] Tue, 17 January 2012 10:21 Go to previous messageGo to next message
Catalin Gerea is currently offline Catalin Gerea
Messages: 89
Registered: July 2009
Location: Bucharest, Romania
Member

Hello Paul

Thank you for the quick answer. I am not calling setBaseEnabled(*). I also do not fire a HandlerEvent.

1. When I should call setBaseEnabled(*)? Also when I should fire the HandlerEvent ?
2. The purpose of the HandlerEvent is to inform the listeners of the handler that the handler has changed? In this case I can assume that I should have at least one handler listener defined for each handler extending the BaseHandler (because this handler is triggered from the menu and not the BaseHandler).
3. I do not understand (at least at the moment) how the menu that is already open will be 'reevaluated'. How to trigger this re-evaluation? I can assume that the handler listener should be responsible for this but I do not know the API to do this.
For IElementUpdater I saw that there is callback registered and the re-evaluation can be forced via refreshElements() method.

A little background on my situation (I can provide also code samples if needed):
- all handlers are defined only in code (there is no org.eclipse.ui.handlers in plugin.xml)
- the handlers extending BaseHandler are overriding isEnabled() by calling super.isEnabled() plus their specific condition
- the commands defined in plugin.xml have default handlers that are extending BaseHandler
- the view has also a 'status bar' with N buttons (buttons are the same as the menu entries); each button is 'linked' with a eclipse command defined in plugin.xml
- the update of the view (table viewer and 'status bar') is done through an class extending WorkbenchJob --> so ideally here I should trigger also the update of the open menu
- the 'status bar' is correctly updated by the update job: when the external event happens the buttons are disabled (which is consistent with the return value of isEnabled() method from BaseHandler) --> that is why I need to make also the menu entries disabled


Time is what you make of it.
Re: Dynamic Menu Entry enablement [message #780485 is a reply to message #780185] Wed, 18 January 2012 09:26 Go to previous messageGo to next message
Catalin Gerea is currently offline Catalin Gerea
Messages: 89
Registered: July 2009
Location: Bucharest, Romania
Member

I have tried to use MenuManager.updateAll(true) and MenuManager.updateAll(false) but the visual aspect of the menu does not change. All ContributionItem objects of the MenuManager have the corect enablement (disabled ... consistent with the return value of isEnabled() from the handler) but still the visual aspect of the menu still does not change: some entries are still displayed enabled.

Am I doing something wrong? Or the API does not support the update of an already opened popup menu?


Time is what you make of it.
Re: Dynamic Menu Entry enablement [message #780752 is a reply to message #779751] Thu, 19 January 2012 10:01 Go to previous messageGo to next message
Catalin Gerea is currently offline Catalin Gerea
Messages: 89
Registered: July 2009
Location: Bucharest, Romania
Member

Problem solved.

I have added a <visibleWhen> block for each command in the popup menu defined using org.eclipse.ui.menus extension point.
Now the visual aspect of the menu is updated when the external event happens (which leads to the handler's disablement).

Now my question is why the menu is re-evaluated in this case? I can assume that a callback is registered in the background that triggers this re-evaluation. But in this case I assume that the same effect can be achieved also programmatic via API. Does anybody has a hint in this direction?


Time is what you make of it.
Re: Dynamic Menu Entry enablement [message #786583 is a reply to message #780752] Mon, 30 January 2012 15:55 Go to previous message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Just a quick note: When your handler's programmatic enabled state changes, you must fire a HandlerEvent. That's the API (we provide setBaseEnabled(*) as a convenience method). Now how you communicate that to your instantiated handler is up to you (listeners for the event on instantiation, etc).

But using the IEvalutionService and visibleWhen/activeWhen/enabledWhen in the plugin.xml is also acceptable.

PW


Previous Topic:programmatically call source format (CTRL+SHIFT+F)
Next Topic:Question about UI / non-UI listeners
Goto Forum:
  


Current Time: Tue Sep 16 15:36:47 GMT 2014

Powered by FUDForum. Page generated in 0.01886 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software