views:

116

answers:

2

I am using the org.eclipse.ui.popupMenus extension point for adding a sub-menu whose Action that is bounded to the following class:

  public class MyAction implements IObjectActionDelegate {

     private Logic logic = Logic.getInstance(); // Singleton

     public void setActivePart(IAction a, IWorkbenchPart targetPart) {
        // Nothing here
     }

     public void run(IAction a) {      
        // Do something...
     }

     public void selectionChanged(IAction a, ISelection s) {
        a.setEnabled(logic.isEnabled(s));
     }
  }

This action is working correctly in most cases (including the call a.setEnabled() in selectionChanged()). My problem at the very first time my action is being invoked. The selectionChanged method is called only after the menu item has been displayed (and not when the user has made the selection) which means that the call to a.setEnabled() will have no affect.

Any ideas on how to make my action receive selectionChanged() notifications even before the fist time it is being invoked?

A: 

The words first time and after make me wonder about a synchronization problem. If the initialization of Logic.getInstance() is deferred, you might look at the Initialization On Demand Holder Idiom, also discussed in item 71 of Effective Java.

trashgod
No, it's related to Eclipse's way of handling plugins. Fortunately no synchronization involved!
ShiDoiSi
A: 

Eclipse uses so-called lazy plugin activation, so it first derives as much as possible from plugin.xml, and the behaviour you are observing is well-documented in the API. See this related question.

ShiDoiSi