Connecting the Cut Action to a Context Aware Action

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();
actionMap.put("cut-to-clipboard", Utilities.actionsForPath("Actions/Bla/").get(0));

Then, in the layer, the first action registered in "Actions/Bla" is as follows:

<folder name="Actions">
    <folder name="Bla">
        <file name="org-netbeans-modules-musician-actions-NoteCutAction.instance">
            <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.


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 2015