This is based on the existing tutorial. That tutorial, lets you get started with the NetBeans Plugin-Development, by creating an Always Enabled (see CallableSystemAction), without using any Wizard and that too smoothly.
Now, you're ready for transformation. So, lets get started then!
public final class SayCheez extends CookieAction {
Now, don’t remove the already existing implementations of abstract methods of 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;
}
@Override
protected String iconResource() {
//Replace org/nvarun/tat with your path/to/icon
//see attachments to download icon24.png
return "org/nvarun/tat/icon24.png";
}
// Newly Added
@Override
protected void initialize() {
super.initialize();
// see org.openide.util.actions.SystemAction.iconResource() Javadoc for more details
putValue("noIconInMenu", Boolean.TRUE);
}
// Newly Added
@Override
protected boolean asynchronous() {
return false;
}
}
Now, just add the following 2 methods TO let the class SayCheez implement all the abstract methods defined by CookieAction.
@Override
protected Class<?>[] cookieClasses() {
return new Class[]{{$Interface}.class};
}
protected int mode() {
return CookieAction.{$mode};
}
Please note, that {$Interface} needs to be replaced by appropriate interface, i.e. it could be either of the following Cookie class(es), which are basically interfaces/abstract class(es);
I will be using EditorCookie for this transformation. Also, {$mode} needs to be replaced with either MODE_EXACTLY_ONE[1] or MODE_ALL[2].
I will be using MODE_EXACTLY_ONE for this transformation.
In our case, its EditorCookie! Now, alter the performAction method like this,
@Override
protected void performAction(Node[] activatedNodes) {
{$Interface} ref = activatedNodes[0].getLookup().lookup({$Interface}.class);
// TODO use {$Interface}
}
Here, ref is basically the reference to either the interface/class being made, which is being assigned a subclass reference, it could be of great use. Now, add your code for DialogDisplayer used in reference tutorial, in place of TODO comment.
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.
<folder name="Shortcuts">
<file name="O-F3.shadow">
<attr name="originalFile" stringvalue="Actions/Tools/org-nvarun-tat-SayCheez.instance"/>
</file>
</folder>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
//package {$your-package};
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.cookies.EditorCookie;
import org.openide.nodes.Node;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CookieAction;
/**
*
* @author Varun Nischal (nvarun@NetBeans.org)
*/
public class SayCheez extends CookieAction {
@Override
protected void performAction(Node[] activatedNodes) {
EditorCookie ref = activatedNodes[0].getLookup().lookup(EditorCookie.class);
String msg = "I'm plugged in!";
NotifyDescriptor d = new NotifyDescriptor.Message(msg,
NotifyDescriptor.INFORMATION_MESSAGE);
DialogDisplayer.getDefault().notify(d);
}
@Override
public String getName() {
return NbBundle.getMessage(SayCheez.class, "CTL_SayCheez");
}
@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.png
return "org/nvarun/tat/icon24.png";
}
public HelpCtx getHelpCtx() {
return HelpCtx.DEFAULT_HELP;
}
@Override
protected boolean asynchronous() {
return false;
}
@Override
protected int mode() {
return CookieAction.MODE_EXACTLY_ONE;
}
@Override
protected Class<?>[] cookieClasses() {
return new Class[]{EditorCookie.class};
}
}
<?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>
<!--Code To Be Added-->
<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>
</filesystem>
Thanks for following, enjoy and have fun!
| icon24.png | ![]() |
1908 bytes |