2.8.10

The Continuing March of ActionListener in the NetBeans Platform

One of the interesting enhancements to the NetBeans Platform in NetBeans Platform 6.5 was the fact that you can now use the standard JDK ActionListener class when creating 'aways enabled' menu items and toolbar buttons in your NetBeans Platform applications.

In the next release of the NetBeans Platform, i.e., after 6.7, this will go a step further. When you create 'conditionally enabled' actions, such as those that appear in the Java editor's contextual menu, you will be able to define your action like this:

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import org.openide.cookies.EditorCookie;

public final class BSomeAction implements ActionListener {

private final EditorCookie context;

public BSomeAction(EditorCookie context) {
this.context = context;
}

public void actionPerformed(ActionEvent ev) {
// TODO use context
}

}

So, again, you're using a plain old ActionListener, with the context that you're working with (the editor) being the only foreign class in your code.

Assuming you want the action to be invoked from a menu item on a node in the explorer view, the layer entries for the above would be as follows:

    <folder name="Actions">
<folder name="Build">
<file name="org-demo-bla2-BSomeAction.instance">
<attr name="delegate" methodvalue="org.openide.awt.Actions.inject"/>
<attr name="displayName" bundlevalue="org.demo.bla2.Bundle#CTL_BSomeAction"/>
<attr name="injectable" stringvalue="org.demo.bla2.BSomeAction"/>
<attr name="instanceCreate" methodvalue="org.openide.awt.Actions.context"/>
<attr name="noIconInMenu" boolvalue="false"/>
<attr name="selectionType" stringvalue="EXACTLY_ONE"/>
<attr name="type" stringvalue="org.openide.cookies.EditorCookie"/>
</file>
</folder>
</folder>
<folder name="Loaders">
<folder name="text">
<folder name="x-java">
<folder name="Actions">
<file name="org-demo-bla2-BSomeAction.shadow">
<attr name="originalFile" stringvalue="Actions/Build/org-demo-bla2-BSomeAction.instance"/>
<attr name="position" intvalue="0"/>
</file>
</folder>
</folder>
</folder>
</folder>

If, on the other hand, the action class were to appear within the editor itself, the layer registration would be as follows:

   <folder name="Actions">
<folder name="Build">
<file name="org-demo-bla3-BSomeAction.instance">
<attr name="delegate" methodvalue="org.openide.awt.Actions.inject"/>
<attr name="displayName" bundlevalue="org.demo.bla3.Bundle#CTL_BSomeAction"/>
<attr name="injectable" stringvalue="org.demo.bla3.BSomeAction"/>
<attr name="instanceCreate" methodvalue="org.openide.awt.Actions.context"/>
<attr name="noIconInMenu" boolvalue="false"/>
<attr name="selectionType" stringvalue="EXACTLY_ONE"/>
<attr name="type" stringvalue="org.openide.cookies.EditorCookie"/>
</file>
</folder>
</folder>
<folder name="Editors">
<folder name="text">
<folder name="x-java">
<folder name="Popup">
<file name="org-demo-bla3-BSomeAction.shadow">
<attr name="originalFile" stringvalue="Actions/Build/org-demo-bla3-BSomeAction.instance"/>
<attr name="position" intvalue="400"/>
</file>
</folder>
</folder>
</folder>
</folder>

The above is already possible with dev builds. Nevertheless, I'm looking forward to being able to do the above via annotations instead of layer entries.

Further reading:

http://bits.netbeans.org/dev/javadoc/org-openide-awt/apichanges.html#Actions.context

http://blogs.sun.com/geertjan/entry/the_continuing_march_of_actionlistener

No comments: