X

Geertjan's Blog

  • July 19, 2011

A String Is An Action Too?

Geertjan Wielenga
Product Manager

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 {
@ActionRegistration(
displayName = "#CTL_CopyAll",
iconBase="org/my/utilities/copyall.jpg")
@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"/>
</file>
</folder>
</folder>
<folder name="Menu">
<folder name="Edit">
<file name="copyAll.shadow">
<attr name="originalFile" stringvalue="Actions/Edit/copyAll.instance"/>
<attr intvalue="1150" name="position"/>
</file>
</folder>
</folder>

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() {
@Override
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.

Join the discussion

Comments ( 4 )
  • zeljko Tuesday, July 19, 2011

    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

    //zeljko


  • Johannes Boesl Tuesday, July 19, 2011

    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.


  • Geertjan Tuesday, July 19, 2011

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


  • Jesse Glick Wednesday, July 20, 2011

    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);


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