Over a million developers have joined DZone.
Platinum Partner

Playing with NetBeans XML Layer Files (Part 1)

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

I am back with another cool trick and some tips to remember as well. Let me share with you another trick, which I have been making use of for a few weeks, and it is really cool! It is based on the NetBeans Platform, in particular, on the XML layer file, which is a plugin's optional configuration file. This is my first full-fledged post on the NetBeans Platform, as I’m dealing with it almost daily for over a week, for some cool plugin developments.

 

Prerequisites

This trick makes use of an existing tutorial, which is the basis on which I have carried out the trick. Also, you must read this tutorial, if you don’t have any prior experience with NetBeans Platform.

Problem Description

As mentioned in the tutorial, I created an Action Type (CallableSystemAction), made it a Global MenuItem and Toolbar Button. I named the Action Type (Java file) as SayCheez, and display Name as Say Cheez…

Now, I have made this action, as expected in the tutorial. Now, right-click Project & click on Install/Reload in Target Platform in the context-sensitive popup menu (context-menu). Its running successfully!

Now, let me show you the structure of XML Layer (layer.xml), its similar to the one shown in the tutorial, i.e. how it registers the action into the NetBeans IDE.

<?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>
</filesystem>

So, the trick is how to manipulate the XML Layer and don’t waste time, placing actions anywhere else in the IDE, by recreating them.

Solution

Note, all the following tricks would work for CallableSystemAction only, I have not tested for CookieAction, though it should work for CookieAction as well, if you are working with only MenuItem, Toolbar Button OR Shortcut Keys! For Editor and File Type Context MenuItem, there would be some other way round, I will see if its comfortable playing with them as well ;)

By Code Deduction

Trick #1- If you want to remove either one of the separators, OR even both of them, then see the following code, the code present in that remove it from the XML Layer, as per your need.

            <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>

Trick #2 - If you want to remove the Action, from Toolbar, then remove the following code;

    <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>

Trick #3 - If you want to remove the Action from Tools Menu, and retain it only on the Toolbar, then remove the following code;
    <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>

By Code Replacement

Trick #4 - If you want to make a Shortcut key for this action, add the following code,

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

Trick #5 - Sometimes, it might happen, that your action is not suitable for any existing Menu, and you are looking to create your own Menu. By the time, you get ready with your Menu, you can just have this Shortcut Keys alone, i.e. no Menu Item, no Toolbar Button.

<?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>
</folder>
</folder-->
<folder name="Shortcuts">
<file name="O-F3.shadow">
<attr name="originalFile" stringvalue="Actions/Tools/org-nvarun-tat-SayCheez.instance"/>
</file>
</folder>
</filesystem>

Tips to Remember

This code has a shortcut key “Alt+F3″, which is represented as “O-F3.shadow”, so this means, if you have some key combination, that uses “Alt”, then use “O”, and for each “+” sign use “-” ,finally concatenate with the just formed combination with “.shadow”! If you use “Ctrl”, then use “D” and for “Shift”, use “S”!

Following are some key equivalents used in XML Layer;

  1. A to Z (as it is), F1 to F12 (as it is), 0 to 9 (as it is)
  2. / as SLASH, \ as BACK_SLASH
  3. ; as SEMI_COLON
  4. . as PERIOD
  5. as QUOTE

These were some of the frequently used keys in Shortcut Key combination, if you want to know some more, you can post a comment over here. I would be glad to help you out! Actually, I was developing a module for past one week, and more intensely on the weekend, and have observed this by hit-and-trial, and thought of sharing the trick with you people. Anyways, thanks for following, enjoy and have fun! To know more about the surprise, keep an eye on my blog!

References

[1] More Tips and Tricks!
[2] Reposted from this blog entry!
[3] You may subscribe to all of my blog's tips and Tricks!

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}