A String Is An Action Too?

Very funny thing I learned from Toni Epple today:

import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;

public class MyUtilities {

            displayName = "#CTL_CopyAll", 
    @ActionID(category = "Edit", id = "copyAll")
    @ActionReference(path="Menu/Edit", position=1150)
    public static final String ACTION_MAP_COPYALL = "copy-all";

What you see above is, in fact, an Action. Yet, at the same time it is a String.

Compile a module with the above annotated String and you'll have this:

<folder name="Actions">
    <folder name="Edit">
        <file name="copyAll.instance">
            <attr bundlevalue="org.my.utilities.Bundle#CTL_CopyAll" name="displayName"/>
            <attr methodvalue="org.openide.awt.Actions.callback" name="instanceCreate"/>
            <attr name="key" stringvalue="copy-all"/>
            <attr name="iconBase" stringvalue="org/my/utilities/copyall.jpg"/>
            <attr boolvalue="false" name="noIconInMenu"/>
<folder name="Menu">
    <folder name="Edit">
        <file name="copyAll.shadow">
            <attr name="originalFile" stringvalue="Actions/Edit/copyAll.instance"/>
            <attr intvalue="1150" name="position"/>

All that, from a String, annotated as an Action.

You can call back to the "copy-all" key that has now been registered above, as follows:

ActionMap actionMap = new ActionMap();

actionMap.put("copy-all", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        StatusDisplayer.getDefault().setStatusText("Simulate copy all...");

associateLookup(ExplorerUtils.createLookup(new ExplorerManager(), actionMap));

Now each TopComponent can make use of the shared "Copy All" action, providing an Action relevant to their context.


My application consists of several modules chosen by the user via a ComboBox on the menu bar (similar to so-called perspectives). When one closes one module, all the menus / toolbars that belong to the module hidden dynamically. My idea is to remove (hide) Action instances and therefore must also menus / toolbars away!
I have tried several ways, by using the file system API and add "_hidden" on the Action instances (this works on the standard Actions, such as Save, Save As, etc., but not on my Actions that are created by NB wizard and have Annotations instead of registration in layer.xml). Have also tried using putValue (DynamicMenuContent.HIDE_WHEN_DISABLED, Boolean.TRUE), but nothing helps!
I am very grateful if you or anyone else who reads this can help me!
Thanks in advance

Posted by zeljko on July 19, 2011 at 09:06 AM PDT #

Is again this Action enabled and disabled depending wether the key exists in the actionmap? Otherwise I'd still stick to my CookieActions in most cases.

Posted by Johannes Boesl on July 19, 2011 at 04:15 PM PDT #

Hi Johannes. Yes, if the key is not in the action map of the TopComponent, the action is disabled.

Posted by Geertjan on July 19, 2011 at 04:52 PM PDT #

Part of the purpose of annotating a String constant is so that you can later use that constant to refer to the action identity!

actionMap.put(MyUtilities.ACTION_MAP_COPYALL, action);

Posted by Jesse Glick on July 19, 2011 at 07:58 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« July 2016