Zacateras
Zacateras

Reputation: 295

Eclipse plug-in popup's action initial availability

I am developing a simple Eclipse plug-in. I added a popup as an extension and assigned an Action class to it. Everything works fine except disabling the action at the first time, when popup is opened.

I check whether the action can be performed in the selectionChanged method. But it cannot be called before MyAction object is constructed. It is performed only after clicking on menu's button (it should be disabled, if prerequisites are not fullfilled).

How to handle such problem? How can MyAction be constructed before opening popup?

I attach a sample of essential code in my project (plugin.xml, MyAction.java):

<plugin>
   <extension
         point="org.eclipse.ui.popupMenus">
      <objectContribution
            id="action.contribution1"
            objectClass="myobject">
         <menu
            id="action.menu1"
            label="Menu"
            path="additions">
         </menu>
         <actionm
               class="action.popup.actions.MyAction"
               enablesFor="1"
               id="action.newAction"
               label="Play"
               menubarPath="action.menu1">
         </action>
      </objectContribution>
   </extension>
</plugin>
public class MyAction implements IObjectActionDelegate {
    public MyAction() {
        super();
    }

    public void setActivePart(IAction action, IWorkbenchPart targetPart) {}

    public void run(IAction action) {
        //some logic
    }

    @Override
    public void selectionChanged(IAction action, ISelection selection) {
        boolean enabled = false;

        //some logic concerning enabled variable

        action.setEnabled(enabled);
    }

}

Upvotes: 0

Views: 315

Answers (1)

greg-449
greg-449

Reputation: 111217

setActivePart should be called before the menu is shown. You can set the action enablement in that.

You can specify initial enablement of the action using the <enablement> element in the plugin.xml, for example:

<action
     class="action.popup.actions.MyAction"
     enablesFor="1"
     id="action.newAction"
     label="Play"
     menubarPath="action.menu1">
   <enablement>
       <with variable="selection">
           ... tests
       </with>
    </enablement>
</action>

sets enablement by testing the current selection.

Note: The extension point org.eclipse.ui.popupMenus is now deprecated, you should look to move to the org.eclipse.ui.menus extension poin.

Upvotes: 1

Related Questions