Geertjan's Blog

  • May 31, 2005

Create File Templates for NetBeans Module Development

Geertjan Wielenga
Product Manager
There are several files that I've been creating over and over each time I make a new NetBeans Module. It's been slowing things down and been a little bit frustrating as well. I don't want to wait for the next NetBeans IDE release -- which will simplify module development in various ways -- so, instead, I've created a module that installs module-related templates in the New File wizard, as illustrated below (click to enlarge):

The key to templates is, as almost always with NetBeans Modules, the magical layer.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.0//EN"
<folder name="Templates">
<folder name="NetBeans Module Support">
<file name="Action.java" url="../templates/Action.template">
<attr name="template" boolvalue="true"/>
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.netbeans.modules.nbmtemplates.Bundle"/>
<attr name="SystemFileSystem.icon" urlvalue="nbresloc:/org/netbeans/modules/nbmtemplates/resources/class.gif"/>
<attr name="templateWizardIterator" methodvalue="org.netbeans.modules.java.ui.wizard.JavaWizardIterator.singleton" />
<attr name="templateWizardURL" urlvalue="nbresloc:/org/netbeans/modules/nbmtemplates/descriptions/DescForActionTemplate.html"/>

The above layer.xml file creates the category "NetBeans Module Support" in the New File wizard, with the file type "Action.java". Very importantly, I don't want my template to be blank. The only reason I will choose to create the Action.java template is because, unlike the normal Java Class file, it subclasses CallableSystemAction. Hence, after naming my template Action.java, I include a url attribute that points to the content of my template (which is created from an Empty File template in the New File wizard's Other category):

\* __NAME__.java
\* Created on __DATE__, __TIME__
\* To change this template, choose Tools | Options and locate the template under
\* the Source Creation and Management node. Right-click the template and choose
\* Open. You can then make changes to the template in the Source Editor.
package Templates.Classes;
\* @author __USER__
//TODO: Import packages from Open API Support (available in Update Center).
public class Class extends CallableSystemAction {
//TODO: Replace body of HelpCtx() with your own code.
public HelpCtx getHelpCtx() {
return HelpCtx.DEFAULT_HELP;
//TODO: Replace body of getName() with your own code.
public String getName() {
return "Start Test";
//TODO: Replace body of performAction() with your own code.
public void performAction() {
String msg = "Test succeeded...";

As you can see, I've not only included all the methods that are required by a class that subclasses CallableSystemAction, but I've also added some sample code. This gives me an idea of what should go in the body of the methods -- and, thanks to the TODO comments, I know that the bodies should be replaced by my own code. Most importantly, the first TODO tells me to get the required JAR files for NetBeans Module development -- the Open API JARs that I can get from the Update Center. Importantly, when I create a template, the Class , User, Date, and Time in the template above will be replaced by my own settings and values.

In addition to the content of the template, the entries in the layer.xml file allow me to specify supporting files, such as an icon, an HTML file that contains the description displayed in the New File wizard's Description area (at the bottom of the New File wizard), and the location of the localizing bundle for the template's label.

So, at the end of the day, when you add templates for other classes that you frequently need in NetBeans Module development, this is the structure of the file templates module for NetBeans Modules (click to enlarge):

As shown above, you can even add a template for the manifest.mf file and the layer.xml file. It's kind of funny to add a template for the layer.xml file, because you're installing the template in the New File wizard via another layer.xml file. Anyway, I think it's all pretty cool and not all that difficult to set up.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.