X

Geertjan's Blog

  • July 26, 2009

The Deprecation of CallbackSystemAction

Geertjan Wielenga
Product Manager
In the past, you'd integrate your own action into the NetBeans Platform's CutAction like this, i.e., by using a TopComponent's ActionMap:

final ActionMap actionMap = instance.getActionMap();
CallbackSystemAction callCutAction = (CallbackSystemAction) SystemAction.get(CutAction.class);
actionMap.put(callCutAction.getActionMapKey(), new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message("Cut cut cut...");
DialogDisplayer.getDefault().notify(msg);
}
});

Post 6.7, CallbackSystemAction will start moving to a black hole in space. Wherever you're using "CallbackSystemAction", you'll not be using it in future. In the case of the above, you'll simply type the string that defines the key of the action. In the case of CutAction, the related key is "cut-to-clipboard". Hence, I can comment out the "CallbackSystemAction" definition below and replace it in the action map with "cut-to-clipboard":

final ActionMap actionMap = instance.getActionMap();
//CallbackSystemAction callCutAction = (CallbackSystemAction) SystemAction.get(CutAction.class);
actionMap.put("cut-to-clipboard", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message("Cut cut cut...");
DialogDisplayer.getDefault().notify(msg);
}
});

The above implies that there needs to be documentation listing all the keys for all the existing actions in the NetBeans Platform. That list of keys should probably be in the upcoming NetBeans Platform DZone Refcard.

Now, let's say you have defined the original action yourself. That's different to the above original action, i.e., the CutAction, which is defined by the NetBeans Platform. So, in the case where you yourself, i.e., in your own custom module, have defined the action, you'd declare a menu item like this, as always:

<folder name="Menu">
<folder name="Window">
<file name="SomeAction.shadow">
<attr name="originalFile" stringvalue="Actions/Window/org-demo-bla4-SomeAction.instance"/>
</file>
</folder>
</folder>

So, you can see above that there's a reference to the original file in the "Actions" folder. In this case, i.e., where you'd like other actions to determine what happens when the menu item is clicked (hence, you're wanting other modules to provide a callback), you'd register the action above as follows:

<folder name="Actions">
<folder name="Window">
<file name="org-demo-bla4-SomeAction.instance">
<attr name="instanceCreate" methodvalue="org.openide.awt.Actions.callback"/>
<attr name="key" stringvalue="bla"/>
<attr name="surviveFocusChange" boolvalue="false"/> // defaults to false
<attr name="displayName" bundlevalue="org.demo.bla4.Bundle#CTL_Bla4Action"/>
<attr name="noIconInMenu" boolvalue="false"/>
</file>
</folder>
</folder>

Notice that we have defined our key as "bla" and our "instanceCreate" as org.openide.awt.Actions.callback. That means that anyone who wants to callback to the above action (i.e., implement their own Bla action, in the same way as implementing their own Cut action above), would do so like this, taking note of the line in bold:

final ActionMap actionMap = instance.getActionMap();
actionMap.put("bla", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message("Bla bla bla...");
DialogDisplayer.getDefault().notify(msg);
}
});

Therefore, note that we're not using CallbackSystemAction at all anymore, in post 6.7 builds. Also note that the original action is registered in the layer as a callback action. Unfortunately the NetBeans IDE XML editor sucks, since you're not able to use code completion to see the possible values to fill in for all those attributes. That's why I mentioned above that all these values need to be listed in a refcard. (And that the XML editor needs to be improved.) (And that the New Action wizard needs to let you register these kinds of actions into the layer file.)

One other thing is that we're moving more things into the layer XML file, which is good in that it makes the way you're using callbacks similar to how you're using any other kind of action. However, that's less good in that I was hoping we were moving away from the layer XML file. So I'm hoping this consolidation is a first step towards reimplementing all of this as annotations.

Further reading:

http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/actions/CallbackSystemAction.html

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

Join the discussion

Comments ( 1 )
  • Jesper Sahner Pedersen Saturday, June 5, 2010

    Hi Geertjan,

    Since 'key' is meant to represent only certain pre-defined values you could consider using e.g. Enum instead of String to make 'key' typesafe. Furthermore using an Enum makes javadoc-documentation straightforward for every Enum-constant inside the API.

    Regards,

    Jesper


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