Eclipse plugin: enable action based on current selection
I am using the extension point org.eclipse.ui.popupMenus to add a submenu that is limited 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 works correctly in most cases (including a call a.setEnabled()
to selectionChanged()
). My problem is calling the action the first time. The method selectionChanged
is only called after the menu item has been displayed (and not when the user has made a selection), which means the call a.setEnabled()
will not be affected.
Any ideas on how to make my action get notified selectionChanged()
even before the first moment it is called?
a source to share
Eclipse uses what's called "lazy plugin activation", so it pulls as much of the plugin.xml file as possible first, and the behavior you see is well documented in the API. See this related question.
a source to share
Words the first time and after I wonder about the sync issue. If deferring initialization is Logic.getInstance()
deferred, you can look at the On-Demand Initialization idiom , also discussed in clause 71 Effective Java .
a source to share