The New Actions Wizard in NetBeans 7.0 creates a class that implements an interface ActionListenerand has several annotations. These annotations place links to this action in the Toolbar / Menu specified in the Wizard. Most of this is explained in the NetBeans wiki and works as expected.
The problem arises when I want to add such an action to the Node context menu. Putting actions there means that you must return specific instances of your actions from the Node.getActions (..) method . The missing snippet here is what I have ActionListenerwith some fantastic annotations, but instead I need an instance Actionthat is supported by this listener. Trying to fill this gap, I stumbled upon a blog post from Geertjan , which seems somehow related and led to a static method, which I added to my ActionListener:
public static Action findAction() throws Exception {
final FileObject fo = FileUtil.getConfigFile(
"Actions/Tools/foo-bar-Action.instance");
final DataObject dob = DataObject.find(fo);
final InstanceCookie ic = dob.getLookup().lookup(InstanceCookie.class);
if (ic != null) {
final Object instance = ic.instanceCreate();
if (instance instanceof Action) {
return (Action) instance;
}
}
return null;
}
While this works, it is certainly not the most beautiful piece of code with everything that happens through the file system API, and this is an inappropriate line of the action name. It seems very fragile to me.
, , , , node? , , - ( ).