Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 363687

Summary: Menu label shows as active even though its underlying command is disabled
Product: [Eclipse Project] Platform Reporter: Mihai Codoban <codobanmihai>
Component: UIAssignee: Platform-UI-Inbox <Platform-UI-Inbox>
Status: RESOLVED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: codobanmihai, pwebster
Version: 3.7.1   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Mihai Codoban CLA 2011-11-14 05:01:22 EST
Build Identifier: 20110615-0604

I have a strange problem with menu commands.

I want to make a menu command that is active on some conditions but inactive on others (ex: close command is active when a particular entity is opened but inactive when there is no such entity)

I have tried two strategies:

    - overriding the isEnabled() method of a Handler to contain the logic that should tell it whether the command is active or not

    - extending an AbstractSourceProvider that listens to the state and changes the provided variabile.

In both approaches the code works ok, the menu label getting activated and deactivead. However, and this is the problem, it does this only if I switch to another application(alt tab to something else) and switch back. If don't do this switching only the command gets inactive but the menu label still shows it as active, resulting in a NotEnabledException.

The SourceProviderApproach extension point looks something like this:

 <extension
     point="org.eclipse.ui.menus">
  <menuContribution
        locationURI="menu:org.eclipse.ui.main.menu?before=window">
     <menu
           label="File">
        <command
              commandId="profile.close"
              label="Close"
              style="push">
           <activeWhen
              checkEnabled="false">
           <with
                 variable="profile.gui.ActiveProfile">
              <equals
                    value="active">
              </equals>
           </with>
        </activeWhen>
        </command>
  </menuContribution>

If I change it to validWhen everything is ok, the label is shown and hid accordingly. But If I switch it to activeWhen then the situation above appears, I have to switch to another application and come back in order for that menu label to appear active or inactive.


So the temporary solution that I found is to manually trigger the filling of the menus. This can be done with the following code snippet:

    public static void refillAllActionBars() {
        IWorkbench workbench = PlatformUI.getWorkbench();
        WorkbenchWindow ww = (WorkbenchWindow)workbench.getActiveWorkbenchWindow();

        ww.fillActionBars(ActionBarAdvisor.FILL_STATUS_LINE);
}

It does not look pretty, but it works in my case.

If I have time in the following weekend, I'll create a minimal project that exposes this problem.


Reproducible: Always
Comment 1 Paul Webster CLA 2011-11-14 07:55:59 EST
When contributing a source provider and using it, org.eclipse.ui.menus only works with visibleWhen.

activeWhen (supplying that handler) and enabledWhen (is the command enabled) apply to the handler (not the menu item) as provided in org.eclipse.ui.handlers.

If you have questions about your setup you can ask on the Platform or RCP forums at http://www.eclipse.org/forums/

Please re-open if you have 

PW