TransformConditionallyToAlwaysEnabledAction

Transforming CookieAction Into CallableSystemAction

Contributed By Varun Nischal


This is based on the existing tutorial. That tutorial, lets you get started with the NetBeans Plugin-Development, by creating a Conditionally Enabled (see CookieAction), without using any Wizard and that too smoothly.

[[{TableOfContentsTitle=TableOfContents} | {TableOfContents title='Table of Contents'}]]

Pre-requisites


If you don’t have prior experience with NetBeans Platform, read that tutorial. That's a must! Now, after you have done that, you might like to explore a bit more. So, you should give the following a read as well(if you want you can skip it for a while and read it along with my tutorial),

This overview will quickly familiarize you with how NetBeans plug-in modules interact with the NetBeans infrastructure and with each other.

Now, you're ready for transformation. So, lets get started then!


Getting Started


Earlier, I did transformation from CallableSystemAction TO CookieAction. Now, we are going to do the reversal, i.e. from CookieAction TO CallableSystemAction, sounding similar! Though, never tried! Well, here I am sharing with you some tips and tricks to make it possible, without using New File Type Wizard. We will make use of SayCheez.java, which was made in the reference tutorial.


Solution


So, to start with, remove the
CookieAction
and let the class {SayCheez} inherit from CallableSystemAction, like this;
public final class SayCheez extends CallableSystemAction {
  • Now, we will remove some of the already existing implementations of abstract methods of
    CookieAction
    ,
    @Override
    protected int mode() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return CookieAction.MODE_EXACTLY_ONE;
    }

    @Override
    protected Class<?>[] cookieClasses() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return new Class[]{EditorCookie.class};
    }
  • Leave the following implementations as it is;
 
    @Override	
    public String getName() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return NbBundle.getMessage(SayCheez.class, "CTL_SayCheez");
    }

    // Newly Added
    @Override
    protected void initialize() {
        super.initialize();
        // see org.openide.util.actions.SystemAction.iconResource() Javadoc for more details
        putValue("noIconInMenu", Boolean.TRUE);
    }    

    @Override
    public HelpCtx getHelpCtx() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return HelpCtx.DEFAULT_HELP;
    }

    @Override      
    protected String iconResource() {  
        //Replace org/nvarun/tat with your path/to/icon  
        //see attachments to download icon24_TransformConditionallyToAlwaysEnabledAction.png  
        return "org/nvarun/tat/icon24_TransformConditionallyToAlwaysEnabledAction.png";  
    }      

    // Newly Added
    @Override
    protected boolean asynchronous() {
        return false;
    }

  • Alter the method
    performAction()
    , as following-
Change the access specifier from
protected
to {public}, and remove the parameters;
    @Override
    public void performAction() {
and remove the following line of code,
        EditorCookie editorCookie = activatedNodes[0].getLookup().lookup(EditorCookie.class);
Leave the remaining part as it is,
        //throw new UnsupportedOperationException("Not supported yet.");       
        String msg = "I'm plugged in!";
	NotifyDescriptor d = new NotifyDescriptor.Message(msg, 
                NotifyDescriptor.INFORMATION_MESSAGE);
	DialogDisplayer.getDefault().notify(d);     
    }
  • See, it was too easy! Anyways, the changes in XML Layer, which shall be done with respect to the change in the Action type, some of them are as follows and rest will be the mentioned in the next part of this series.

Tricks for Menu Item, Toolbar Button, Shortcut Keys

Trick #1

As you had made a Global Menu Item and Toolbar Button with
CookieAction
, so no changes required, for transformation.

Trick #2

If you had not made use of Shortcut Keys earlier, then you may register Shortcut Keys now! As
CookieAction
doesn’t support Shortcut Keys, but {CallableSystemAction} does! So, add following code in our XML Layer, if not present in reference tutorial OR in your plug-in module XML Layer;

    <folder name="Shortcuts">
        <file name="O-F3.shadow">
            <attr name="originalFile" stringvalue="Actions/Tools/org-nvarun-tat-SayCheez.instance"/>
        </file>
    </folder>

  • Replace "
    org-nvarun-tat
    " with your package name, in which {SayCheez} resides.

Ready to Install/Reload

  • Not yet, firstly, recall if you saw some red underlines present in the code, Add relevant Module Dependencies...(see tutorial) to get rid of them, so finally your code looks like this;

Transformed Source Code


import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CallableSystemAction;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//package {$your-package};

/**
 *
 * @author Varun Nischal (nvarun@NetBeans.org)
 */
public class SayCheez extends CallableSystemAction {

    @Override
    public void performAction() {
        //throw new UnsupportedOperationException("Not supported yet.");
        String msg = "I'm plugged in!";
        NotifyDescriptor d = new NotifyDescriptor.Message(msg, 
                NotifyDescriptor.INFORMATION_MESSAGE);
        DialogDisplayer.getDefault().notify(d);  
    }

    @Override
    public String getName() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return NbBundle.getMessage(SayCheez.class, "CTL_SayCheez");
    }

    @Override
    public HelpCtx getHelpCtx() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return HelpCtx.DEFAULT_HELP;
    }

    // Newly Added	
    @Override
    protected void initialize() {
        super.initialize();
        // see org.openide.util.actions.SystemAction.iconResource() Javadoc for more details
        putValue("noIconInMenu", Boolean.TRUE);
    }    

    @Override      
    protected String iconResource() {  
        //Replace org/nvarun/tat with your path/to/icon  
        //see attachments to download icon24_TransformConditionallyToAlwaysEnabledAction.png  
        return "org/nvarun/tat/icon24_TransformConditionallyToAlwaysEnabledAction.png";  
    }      
		
    // Newly Added	
    @Override
    protected boolean asynchronous() {
        return false;
    }
}

Module's XML Layer

  • By the way, we missed one thing, what to do with the XML Layer. Don't worry, just see the following tips about your existing layer.xml, formed in reference tutorial. This is how it looks;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN" "http://www.netbeans.org/dtds/filesystem-1_1.dtd">
<filesystem>
    <folder name="Actions">
        <folder name="Tools">        
            <file name="org-nvarun-tat-SayCheez.instance"/>
        </folder>
    </folder>
    <folder name="Menu">
        <folder name="Tools">
            <file name="org-nvarun-tat-SayCheez.shadow">
                <attr name="originalFile" stringvalue="Actions/Tools/org-nvarun-tat-SayCheez.instance"/>
                <attr name="position" intvalue="150"/>
            </file>
            <file name="org-nvarun-tat-separatorAfter.instance">
                <attr name="instanceClass" stringvalue="javax.swing.JSeparator"/>
                <attr name="position" intvalue="175"/>
            </file>
            <file name="org-nvarun-tat-separatorBefore.instance">
                <attr name="instanceClass" stringvalue="javax.swing.JSeparator"/>
                <attr name="position" intvalue="125"/>
            </file>
        </folder>
    </folder>
    <folder name="Toolbars">
        <folder name="Build">
            <file name="org-nvarun-tat-SayCheez.shadow">
                <attr name="originalFile" stringvalue="Actions/Tools/org-nvarun-tat-SayCheez.instance"/>
                <attr name="position" intvalue="325"/>
            </file>
        </folder>
    </folder>
    <!-- Add Shortcut Keys here -->
    <folder name="Shortcuts">
        <file name="O-F3.shadow">
            <attr name="originalFile" stringvalue="Actions/Tools/org-nvarun-tat-SayCheez.instance"/>
        </file>
    </folder>
</filesystem>

  • Replace "
    org-nvarun-tat
    " with your package name, in which {SayCheez} resides.

Installing and Using the Plug-in Module


Refer to the reference tutorial, on how to Install and use your plug-in module. If you liked it, feel free to give feedback, because this tutorial can only get better, if you actively participate in community.

Version Compatability


Works with NetBeans 6.0, 6.1 as said above. Try this out on your version, and make sure to notify us if anything goes wrong!

Thanks for following, enjoy and have fun!

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo