7

I created the menu item in the "File" menu as a command. For this command there is a handler implementing the IHandler interface. This handler contains the isEnabled method. I am trying to use this method to enable/disable my menu item, but that method is called only once when I click on the "File" menu. When clicked for the second, third etc. times, the isEnabled method is not called again even if I changed the state of page (open/close editors) before.

What should I do? Maybe this method is not intended for control menu items?

Lii
  • 9,906
  • 6
  • 53
  • 73
algrom
  • 71
  • 1
  • 1
  • 2

2 Answers2

3

Are you subclassing org.eclipse.core.commands.AbstractHandler? You should use setBaseEnabled(boolean) to update the state of your handler (which would update your command).

It's only valid to change enabled state in your handler as long as you also fire the HandlerEvent. It's usually easier to call setBaseEnabled(boolean) which will fire the event for you.

Paul Webster
  • 10,496
  • 1
  • 22
  • 32
2

If you're trying to enable/disable the menu, than you should use core expressions. I've already explained how to do that in this answer:

Eclipse RCP menus & actions: Configure or code?

The part that you're interested in starts with:

For activating/deactivating a menu[...]

I hope this is what you're looking for.

Community
  • 1
  • 1
Andrei B.
  • 1,000
  • 9
  • 14
  • What if I have the handle to the MenuManager, how can I enable / disable the menu item programmatically? – Neel Jan 30 '13 at 17:12
  • 1
    @Neel Well, (in the example mentioned) you should call CommandState.setNotLoaded() which will change the value of the variable "myapp.commands.sourceprovider.active" to NOT_LOADED which in turn will be detected by the condition in the handler. The condition in the handler states that if "myapp.commands.sourceprovider.active" is not equal to PLAYING or PAUSED, the button should be inactive (so disabled). In short, use the provider class (here, CommandState) to change the value of the variable which triggers the handler. – Andrei B. Feb 18 '13 at 16:18