NetBeans File Template Module Tutorial
This tutorial demonstrates how to create a module containing file templates. Once you have installed
the module in the IDE, the file templates are available in the New File wizard. Sharing file templates is easy once you have a module that
contains them—NetBeans IDE 5.x lets you create a binary that you can make available to others, who can then install it through the
Update Center.
The tutorial you are now reading was written for
NetBeans IDE 5.5. If you are using NetBeans IDE 6.0,
please use the 6.0 version
of this tutorial instead.
A file template consists of a template file, an HTML description file, and an icon. The HTML description file
displays information about the template in the New File wizard. The icon identifies the template and
distinguishes it from other templates in the New File wizard. In this tutorial,
you create a new file template by copying the content of an existing file template into an empty file.
Then, once you have set up a description file for the New File wizard and a distinguishing icon, you
register the template, the HTML description file, and the icon in the NetBeans configuration file, the layer.xml file.
Note: If you use the New File Type wizard, a file template is automatically created for you. The manual
steps described in this tutorial are not necessary when the New File Type wizard is used. The New File Type wizard
also lets you select an icon and it creates an HTML description file. The New File Type wizard
automatically registers the file template, the HTML description file, and the icon in the layer.xml file. Therefore, if
the file template that you want to create relates to a new file type, it is better to use the New File Type wizard. For details,
see NetBeans DataLoader Module Tutorial.
The following topics are covered below:
Once the software is installed, this tutorial can be completed in 20 minutes.
For more information on working with NetBeans modules, see the
NetBeans Development Project home on the NetBeans website. If you have questions, visit the
NetBeans Developer FAQ or use the feedback link
at the top of this page.
Installing the Software
Before you begin, you need to install the following software on your
computer:
Creating the Module Project
- Choose File > New Project (Ctrl-Shift-N). Under Categories, select NetBeans Plug-in Modules. Under projects,
select Module Project and click Next.
- In the Name and Location panel, type AdditionalFileTemplates in Project Name.
Change the
Project Location to any directory on your computer, such as c:\mymodules. Leave the Standalone Module radiobutton
and the Set as Main Project checkbox selected.
Click Next.
- In the Basic Module Configuration panel, replace yourorghere in Code Name Base with myorg,
so that the whole code name base is org.myorg.additionalfiletemplates.
Add spaces to the default Module Display Name, so that it is changed to Additional File Templates.
Leave the location of the localizing bundle and XML layer, so that they will be stored in a
package with the name org/myorg/additionalfiletemplates. Click Finish.
The IDE creates the Additional File Templates
project. The project contains all of your sources and
project metadata, such as the project's Ant build script. The project
opens in the IDE. You can view its logical structure in the Projects window (Ctrl-1) and its
file structure in the Files window (Ctrl-2). For example, the Projects window should now look as follows:
For basic information on each of the files above, see the
Introduction to NetBeans Module Development.
Creating the File Template
A file template consists of a template file, an HTML description file, and an icon. An easy way to
create a new file template is to copy the content of an existing file template into an empty file.
Then, once you have set up a description file for the New File wizard and a distinguishing icon, you
are ready to register the template in the layer.xml file.
Creating the Template File
- Go to Tools > Template Manager. Expand Java Classes, as shown below:
- Select the Java Class template and click
Open in Editor.
The Java Class template opens in the Source Editor:
/*
* __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__
*/
public class Class {
/** Creates a new instance of __NAME__ */
public Class() {
}
}
Note that the Java Class template includes placeholders that are filled in when you use the New File
wizard to create a file. The placeholders are as follows:
- __NAME__. Replaced by the file name entered in the New File wizard.
- Class. Replaced by the class name entered in the New File wizard.
- __USER__. Replaced by the current user.
- __DATE__. Replaced by the current date.
- __TIME__. Replaced by the current time.
- Right-click the Additional File Templates node and choose New > File/Folder.
In the New File wizard, under Categories, choose Other and under File Types, choose Empty File. Click Next.
- Type BrandedJavaClass.template
in File Name. Click Browse and browse to src/org/myorg/additionalfiletemplates. Click Select Folder. Click Finish.
An empty file, called BrandedJavaClass.template opens in the Source Editor and its node appears in the Projects window.
- Copy the content of the Java Class template into the BrandedJavaClass.template file.
- Add branding information to the top of the BrandedJavaClass.template file. For example, you can add the Sun Microsystems
copyright information to the top of the file:
/*
* Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
* Government Rights - Commercial software. Government users are subject
* to the Sun Microsystems, Inc. standard license agreement and
* applicable provisions of the FAR and its supplements. Use is subject
* to license terms.
*
* This distribution may include materials developed by third parties.
* Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
* or registered trademarks of Sun Microsystems, Inc. in the U.S. and
* other countries.
*
* Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
*
* Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
* commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
* licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
* en vigueur de la FAR (Federal Acquisition Regulations) et des
* supplements a celles-ci. Distribue par des licences qui en
* restreignent l'utilisation.
*
* Cette distribution peut comprendre des composants developpes par des
* tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
* sont des marques de fabrique ou des marques deposees de Sun
* Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
*/
- Press Ctrl-S to save the file.
Creating the Description File
- Right-click the org.myorg.additionalfiletemplates node and choose New > File/Folder.
Under Categories, choose Other. Under File Types, choose HTML File. Click Next. Type Desc_BrandedJavaClass
in File Name. Click Browse and browse to src/org/myorg/additionalfiletemplates. Click Select Folder. Click Finish.
An empty HTML file opens in the Source Editor and its node appears in the Projects window.
- Type "Creates Java Class branded with <b>Sun Microsystems</b> copyright text." (without the quotation marks)
between the <body> tags, so
that the file looks as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
Creates Java Class branded with <b>Sun Microsystems</b> copyright text.
</body>
</html>
- Press Ctrl-S to save the file.
Getting an Icon
The icon accompanies the file template in the New File wizard. It identifies it
and distinguishes it from other file templates. The icon must have a dimension of 16x16 pixels.
If you do not have your own, you can find some 16x16 icons at the following location
within your NetBeans IDE 5.x installation directory:
enterprise2\jakarta-tomcat-5.5.9\server\webapps\admin\images
Or in this NetBeans IDE 5.5 installation directory:
enterprise3\jakarta-tomcat-5.5.17\server\webapps\admin\images
Name the icon Icon_BrandedJavaClass and add it to the org.myorg.additionalfiletemplates package.
The Projects window should now look as follows:
Registering the File Template
Once you have created the file template, you must register it in the NetBeans System Filesystem. The layer.xml file
is made for this purpose.
- Add the following entry between the <filesystem> tags in the layer.xml file:
<folder name="Templates">
<attr name="Classes/BrandFiles" boolvalue="true" />
<attr name="BrandFiles/GUIForms" boolvalue="true" />
<folder name="BrandFiles">
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.myorg.additionalfiletemplates.Bundle"/>
<file name="BrandedJavaClass.java" url="BrandedJavaClass.template">
<attr name="template" boolvalue="true"/>
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.myorg.additionalfiletemplates.Bundle"/>
<attr name="SystemFileSystem.icon" urlvalue="nbresloc:/org/myorg/additionalfiletemplates/Icon_BrandedJavaClass.png"/>
<attr name="instantiatingIterator" methodvalue="org.netbeans.modules.java.ui.wizard.JavaWizardIterator.singleton" />
<attr name="instantiatingWizardURL" urlvalue="nbresloc:/org/myorg/additionalfiletemplates/Desc_BrandedJavaClass.html"/>
</file>
</folder>
</folder>
The tags above are explained below:
- attr name="Classes/BrandFiles". The BrandFiles category will be positioned below the Classes category.
- attr name="BrandFiles/GUIForms". The BrandFiles category will be positioned above the GUIForms category.
- folder name="BrandFiles". Plain name of the Category in the New File wizard, localized in Bundle.properties.
- attr name="SystemFileSystem.localizingBundle". Name and location of the localizing bundle.
- file name="BrandedJavaClass.java". Plain name of the File Type in the New File wizard, localized in Bundle.properties.
- url="BrandedJavaClass.template". Location and name of the template in the source structure.
- File attributes:
- template. Specifies that this is a template.
- SystemFileSystem.localizingBundle. Name and location of the localizing bundle.
- SystemFileSystem.icon. Name and location of the icon.
- instantiatingIterator. (to come)
- instantiatingWizardURL. Name and location of the HTML description file.
- Add the display names to the Bundle.properties file:
Templates/BrandFiles=Branded Files
Templates/BrandFiles/BrandedJavaClass.java=Branded Java Class
The strings above are explained below:
- Templates/BrandFiles. Display name for the folder named "BrandFiles".
- Templates/BrandFiles/BrandedJavaClass.java. Display name for the file named "BrandedJavaClass.java".
The IDE uses an Ant build script to build and install your module. The build script is created for you
when you create the module project.
For more information about creating and developing NetBeans modules, see the following resources: