Next in the JFugue Music NotePad, I am working on creating a "cut action", which means integrating with the NetBeans Platform cut action. As you can see below, when (and only when) one or more notes are selected, the cut action (look in the toolbar) becomes enabled:
How to do this yourself? Inside the TopComponent, I have these two lines of code, which links the "cut-to-clipboard" action to an action registered in the layer:
final ActionMap actionMap = getActionMap();
Then, in the layer, the first action registered in "Actions/Bla" is as follows:
<attr name="delegate" methodvalue="org.openide.awt.Actions.inject"/>
<attr name="displayName" bundlevalue="org.netbeans.modules.musician.actions.Bundle#CTL_NoteCutAction"/>
<attr name="injectable" stringvalue="org.netbeans.modules.musician.actions.NoteCutAction"/>
<attr name="instanceCreate" methodvalue="org.openide.awt.Actions.context"/>
<attr name="noIconInMenu" boolvalue="false"/>
<attr name="iconBase" stringvalue="org/netbeans/modules/musician/resources/Play16.gif"/>
<attr name="selectionType" stringvalue="ANY"/>
<attr name="type" stringvalue="org.netbeans.modules.musician.common.Note"/>
Take note of the fact that the "type" element enables all (thanks to "ANY") available Note objects to be injected into the action registered for the "injectable" element.
That's all. Now the "injectable" action can receive a List of Note objects and can do with them what they want. Which Note objects are injected? Those that are available via the Lookup. Personally, I'm using Wade Chandler's central lookup, which is an amazing thing, meaning that I am not tied to any windows or even to the current selection.