views:

116

answers:

2

I have an action class which I would like to enable depending on the file extension.
I have written this logic in the selectionChanged() of the action class.

But when I start my eclipse, and click on the file for the context menu, this method is not getting called.
And when I click on any action, there after any click on the file invokes the selectionChanged() method.

How can I make the selectionChanged() method to be called always on click of files in eclipse in order to disable the actions before clicking on the action?

+1  A: 

There are plenty of actions enabled/disabled based on the type of element currently selected.
See for instance the "Copy" actions on an element not meant to be copied:

alt text

That means you can check how a org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart manages its own contextual menu and their associated actions.

Start from the method menuAboutToShow(), using a PackageExplorerActionGroup class, including a CCPActionGroup which manage to Copy, Cut and Paste actions.
That last class illistrates the registration of Actions, amongst them the CopyToClipboardAction:
It does implement a selectionChanged method.

public void selectionChanged(IStructuredSelection  selection) {
 try {
   List JavaDoc elements= selection.toList();
   IResource[] resources= ReorgUtils.getResources(elements);
   IJavaElement[] javaElements= ReorgUtils.getJavaElements(elements);
   if (elements.size() != resources.length + javaElements.length)
    setEnabled(false);
      else
    setEnabled(canEnable(resources, javaElements));
  } catch (JavaModelException e) {
   //no ui here - this happens on selection changes
   // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
   if (JavaModelUtil.isExceptionToBeLogged(e))
     JavaPlugin.log(e);
   setEnabled(false);
  }
}
VonC
But problem is that selectionChanged is getting invoked only after the action is performed. Is there a way to load the action when plugin is loaded.In my case only after clicking on the action that is when IActionDelegate is loaded, I am able to enable/disable the action.
reek
@reek: Not sure; the initial selection is more likeky to be set each time the selection change because it is based upon the nature of the selected element.
VonC
A: 

It seems you are "suffering" from Eclipse's lazy plugin loading (just like me). You can check the Eclipse Plugin-book, or the manual for popups (which I assume you are using). You have to use the so-called declarative mechanism for enabling your item. In your case, it should be as simple as just adding a nameFilter. That way, Eclipse can avoid loading your plugin until it actually needs to run (decoupling of menu items and plugin execution).

ShiDoiSi