I have an action class which I would like to enable depending on the file extension.
I have written this logic in theselectionChanged()
of the action class.
But when I start my eclipse, and click on th开发者_开发问答e 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 theselectionChanged()
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?
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:
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);
}
}
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).
精彩评论