taT | Play with XML Layer (Part 2)
Wow!
I am really enjoying playing with XML Layer. I had mentioned an important note in my previous blog entry- Part One of this series! Its time to work with the CookieAction, i.e. make some Conditionally Enabled Actions and play with it.
By the way, I came to know, that CookieAction does support Shortcut Keys, however I used to think it doesn’t as it was always disabled, when I used Action Wizard to create it.
Its a bug, that’s present since 5.5 and I never realised- I have filed a bug report!
Pre-requisites
Read my previous blog entry- Part One of this series! You should be thorough with all that was explained previously. If not, get it clarified by me OR on Netbeans Community mailing lists.
Problem Description
Here, we are going to transform an Action from one type to another, i.e. from CallableSystemAction TO CookieAction, sounds cool! Though, never tried. Well, here I am sharing with you another trick to make it possible, without using New File Type Wizard. We will make use of SayCheez.java, which was made in previous entry.
Solution
So, to start with, remove the CallableSystemAction and let the class SayCheez extend CookieAction, like this;
public final class SayCheez extends CookieAction {
Now, don’t remove the already existing implementations of abstract methods of CallableSystemAction, just add following two 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);
- Project
- OpenCookie
- EditCookie
- EditorCookie
- DataObject (Abstract Class, NOT an Interface)
Also, {$mode} needs to be replaced with either MODE_EXACTLY_ONE[1] or MODE_ALL[2].
- User Selects One Node
- User May Select Multiple Nodes
MODE_EXACTLY_ONE -
Action will be enabled if there is exactly one selected node and it supports the given cookies.MODE_ALL -
Action will be enabled if there are one or more selected nodes and all of them support the given cookies.In our case, its EditorCookie!
Now, alter the performAction method like this,
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 previous blog entry, in place of TODO comment.
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 - If you had made a Global Menu Item OR Toolbar Button with CallableSystemAction, then no changes required.
Trick #2 - If you had made use of Shortcut Keys earlier, now you have to remove them, as CookieAction doesn’t support Shortcut Keys! Well, I don’t know why. So, remove following code from our XML Layer, formed earlier.
Thanks for following, enjoy and have fun! Surprise to be revealed soon…keep an eye on my blog!
Previously posted on NetBeans Zone -
Playing with NetBeans XML Layer Files (Part 1)
sTay Tuned for More…




