{"id":204,"date":"2012-02-22T19:36:24","date_gmt":"2012-02-22T17:36:24","guid":{"rendered":"http:\/\/www.pellissier.co.za\/hermien\/?p=204"},"modified":"2013-02-10T09:04:58","modified_gmt":"2013-02-10T07:04:58","slug":"how-to-get-hold-of-an-action-instance","status":"publish","type":"post","link":"https:\/\/www.pellissier.co.za\/hermien\/?p=204","title":{"rendered":"How to Get Hold of an Action Instance"},"content":{"rendered":"<p>The NetBeans Platform manages the creation of actions at run time. When you create a new action with the wizard (File &gt; New File&#8230; &gt; Module Development &gt; Action) everything gets generated that is needed for the Platform to load the action and call your actionPerformed() method when required.<\/p>\n<p>However, the question does sometimes arise whether it is possible to get hold of the action that is created. And there is indeed a way. Whether this is desirable in general, or the the best way to solve a particular problem, is a debate for another day. It does, however, illustrate an interesting way in which the Platform can be extended. And so I thought it worth documenting here. \ud83d\ude42<\/p>\n<p>Note that the generated actions discussed below are Always Enabled actions.<\/p>\n<p>With NetBeans 7.1, a class implementing ActionListener is generated with some annotations on it. At compile time, the annotations are used to generate a layer file where the action is registered. (With earlier versions of NetBeans, such as 6.9.1, actions used to be specified directly in the module&#8217;s layer file. And the wizard generated the layer file entries instead of the annotations.)<\/p>\n<p>The interesting attribute that is not visible in the annotations (but still in the generated layer file) is the attribute called instanceCreate. It has as default value the method org.openide.awt.Actions.alwaysEnabled. This means that the method that is called to create the action is alwaysEnabled in the class org.openide.awt.Actions. It is possible to replace this method with our own.<\/p>\n<p>Assume that a singleton class called Controller exists, that is interested in the action instance. Now create a new class that will be containing the custom version of the method:<\/p>\n<p>[java]public class ActionCreator<br \/>\n{<br \/>\n    public static Action alwaysEnabled(java.util.Map TheMap)<br \/>\n    {<br \/>\n        javax.swing.Action newAction =<br \/>\n                org.openide.awt.Actions.alwaysEnabled(<br \/>\n                (java.awt.event.ActionListener) TheMap.get(&#8220;delegate&#8221;),<br \/>\n                (String) TheMap.get(&#8220;displayName&#8221;),<br \/>\n                (String) TheMap.get(&#8220;iconBase&#8221;),<br \/>\n                (Boolean) TheMap.get(&#8220;noIconInMenu&#8221;));<br \/>\n        Controller.instance().setAction(newAction);<br \/>\n        return newAction;<br \/>\n    }<br \/>\n}[\/java]<\/p>\n<p>And add to the layer file (create the file if it does not yet exist, and remember to change the paths as applicable):<\/p>\n<p>[xml]<folder name=\"Actions\"><br \/>\n    <folder name=\"Edit\"><br \/>\n        <file name=\"za-co-pellissier-example-TestAction.instance\"><br \/>\n            <attr name=\"instanceCreate\" methodvalue=\n                \"za.co.pellissier.example.ActionCreator.alwaysEnabled\"\/><br \/>\n            <attr name=\"noIconInMenu\" boolvalue=\"false\"\/><br \/>\n        <\/file><br \/>\n    <\/folder><br \/>\n<\/folder>[\/xml]<\/p>\n<p>That is it. Now the controller will have an instance of that particular action, and it can do with it whatever it wants.<\/p>\n<p>Note that the same instance will be registered on the menu as well as the toolbars (if you choose to put it in both places in the wizard). So for example disabling it will disable the action in both locations.<\/p>\n<p><strong>Edit:<\/strong> If you have trouble disabling the action that you now have a reference to in the above code, create a new class extending AbstractAction and instantiate that instead of calling the alwaysEnabled method to create an action for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The NetBeans Platform manages the creation of actions at run time. When you create a new action with the wizard (File &gt; New File&#8230; &gt; Module Development &gt; Action) everything gets generated that is needed for the Platform to load the action and call your actionPerformed() method when required. However, the question does sometimes arise &#8230; <a title=\"How to Get Hold of an Action Instance\" class=\"read-more\" href=\"https:\/\/www.pellissier.co.za\/hermien\/?p=204\" aria-label=\"More on How to Get Hold of an Action Instance\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[3,5],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1v8WL-3i","_links":{"self":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts\/204"}],"collection":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=204"}],"version-history":[{"count":3,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":820,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions\/820"}],"wp:attachment":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}