X

Geertjan's Blog

  • July 24, 2009

The Continuing March of ActionListener in the NetBeans Platform

Geertjan Wielenga
Product Manager
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

Join the discussion

Comments ( 2 )
  • Tom Wheeler Friday, July 24, 2009

    I'm really pleased at how the NetBeans APIs have evolved.

    When I started developing for the platform, there were marker interfaces like Node.Cookie and specialized classes like SystemAction. Even saving data (until recently) required using the Nodes API!

    Since the NetBeans APIs started moving away from these in favor of POJOs, annotations and Swing actions, it's made the NetBeans Platform much more accessible to Java developers. The platform's rise in popularity over the past couple of years is probably no coincidence.


  • Vincent Cantin Tuesday, February 16, 2010

    The documentation of NetBeans has a bug every where about the line:

    <attr name="delegate" methodvalue="org.openide.awt.Actions.inject"/>

    The "s" of "Actions" was missing. Your blog saved me, as it was not obvious where to find this inject method (was not public, so not displayed in Javadoc).


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.