Subscribe

Archives

  • Categories

  • License

    Creative Commons License


    All work on this site, excepting software and unless otherwise noted, is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.




    Enabling and Disabling org.eclipse.ui.menus (Handler Conflict)

    The later versions of Eclipse (I believe 3.4+) have migrated the menu system for plug-ins from using the antiquated actionSets to org.eclipse.ui.menu’s, commands, and handlers. Using an ISourceProvider, the state of your plug-in’s menu items can be managed dynamically; something that was rather painful to do with the old actionSets (when instead it was necessary to wait for the action to be invoked in order to get a handle to its IAction). There’s a great set of tutorials on the newer technique here and here.

    In the process of migrating my plug-in to the new system, however, I came across a bit of trouble enabling and disabling the items. Seemingly, setting the “enabledWhen” flag for the menu items wasn’t working. Here’s the sample XML for the extension (can you spot the problem?)

    <!-- Managing the UI state (enabled or disabled) of the menu item is accomplished by setting the 
    variable referenced below within a class that extends ISourceProvider. -->
    <extension point="org.eclipse.ui.services">
    	<definition id="com.example.SampleItemSourceProvider">
    		<variable name="com.example.SampleItemState" priorityLevel="workbench" />
    	</definition>
    </extension>
     
    <!-- Menu item definition -->
    <extension point="org.eclipse.ui.menus">
    	<menuContribution locationURI="menu:org.eclipse.ui.main.menu?after=additions">
    		<menu id="com.example.menuitem1" label="Example Menu Item" commandId="com.example.command1" <!-- Defined elsewhere --> >
    			<enabledWhen>
    				<with variable="com.example.SampleItemState"><equals value="stateEnabled"></equals></with>
    			</enabledWhen>
    		</menu>
    	</menuContribution>
    </extension>
     
    <!-- Handler definition -->
    <extension point="org.eclipse.ui.handlers">
    	<handler class="com.example.MenuItem1Handler" commandId="com.example.command1" <!-- Defined elsewhere --> >
    		<activeWhen>
    			<with variable="com.example.SomeOtherVariable"><equals value="stateActive"></equals></with>
    		</activeWhen>
    	</handler>
    </extension>

    So as a note to self in case I forget (likely!), apparently an inactive handler is enough to override the enabledWhen flag of a menu item, which occurs when they are not managed by the same variable (or kept synch’ed). The opposite is also true. It’s easy to forget about those tricky handlers when they don’t define the actual menu item but are merely associated with them.

    Leave a Reply

    Spam Protection by WP-SpamFree