Eclipse Plug-in Enable/Disable Menu Items Programatically <p>I am writing an eclipse plug-in that extends <em>AbstractDebugView</em>. </p> <p>My plugin has one context menu item, one pull-down menu item, and one toolbar menu item. The action associated with these menu items is a class I created (<em>CopyAction</em>) which extends <em>org.eclipse.jface.action.Action</em>.</p> <pre class="lang-java prettyprint-override"><code>public class CopyAction extends Action { private String text; private String toolTipText; private boolean enabled; private ImageDescriptor imageDescriptor; ... public CopyAction() { text = "Copy"; toolTipText = "Copy Action"; enabled = false; imageDescriptor = ...; } ... @Override public void setEnabled(boolean enabled) { this.enabled = enabled; } @Override public boolean isEnabled() { return enabled; } ... } </code></pre> <p>Note that by default my action is disabled (enabled = false).</p> <p>In my plug-in main view class I create and add the copy action to each of the menus:</p> <pre class="lang-java prettyprint-override"><code>public class MyDebugView extends AbstractDebugView { CopyAction copyAction; ... @Override protected Viewer createViewer(Composite parent) { // do some stuff to create the viewer content createActions(); fillLocalPullDown(); ... } @Override protected void createActions() { copyAction = new CopyAction(); } private void fillLocalPullDown() { IActionBars bars = getViewSite().getActionBars(); bars.getMenuManager().add(copyAction); } @Override protected void fillContextMenu(IMenuManager manager) { manager.add(copyAction); } @Override protected void configureToolBar(IToolBarManager manager) { manager.add(copyAction)); } } </code></pre> <p>Everything works great up to this point. When I test my plug-in I can see the context menu, the toolbar menu, and the pull-down menu.</p> <p>All the menu items are disabled, as I would expect since the constructor for <em>CopyAction</em> sets enabled = false;</p> <p>What I want to do is enable the menu items when a selection has changed in my plug-in view (which happens to be a <em>TreeViewer</em>). In the callback for the tree selection I do the following:</p> <pre class="lang-java prettyprint-override"><code>private class MySelectionChangedListener implements ISelectionChangedListener { public void selectionChanged(SelectionChangedEvent event) { // determine if the copy action should be enabled ... copyAction.setEnabled(true) } } </code></pre> <p>The issue I am running into is that even though I set enabled to true on <em>copyAction</em> the menu items do not become enabled.</p> <p>My suspicion as to what's happening is even though I set the enabled property to true nothing happens because the menu(s) need to be notified that the property has changed. Is there some method I have to call to <em>force</em> the menu managers to update themselves thereby checking the enabled property of the copy action?</p> <p>====</p> <p>I have been thinking about the way I have been coding my plug-in and researching the internet on how to add/enable/disable menu items to an Eclipse plug-in. Nearly all of the examples I have seen use <em>plugin.xml</em> to add the menu contributions instead of doing it programatically. (Most also use commands, not actions.) I have also seen where you can enable/disable/hide/show menus using property testers also configured in <em>plugin.xml</em>.</p> <p>Should I just drop the way I am doing things now and do it using the plugin descriptor and property testers? I hate to throw away all the work I have done up to this point but it's more important for me to do it right rather than fuss over time spent.</p>