0

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);
    }

}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Zacateras
  • 295
  • 5
  • 19

1 Answers1

1

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.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • That is true, but it doesn't affect the first time an action is called. The action isn't constructed until I click on the linked menu - setActivePart isn't called at the first time. Therefore I cannot do the first validation and set enablement in MyAction object. – Zacateras Nov 02 '13 at 22:51
  • Added details of element – greg-449 Nov 03 '13 at 09:09