Your users will not deal with these file templates, at least not necessarily. Instead, they will use a wizard that will generate a file. Under the hood, the wizard will pass the values typed by the user (in the wizard's panels) to a FreeMarker file template for processing. In this article, I will show how you create such a file template and how to hook it into a wizard. In short, you will create a NetBeans module that provides a wizard that passes values to a FreeMarker template to generate a document when the user clicks "Finish" in the wizard.
Take the steps below:

We don't need to know much about the generated code. We'll deal with the files one by one, as we need them. First, we will work with the iterator class, above it is called "ItsNatServletWizardIterator". We are going to modify it in such a way that we will add an existing panel, from the NetBeans sources, to our sequence of panels. Currently, we only have one and it is blank (look at "ItsNatServletVisualPanel1" in Design mode). So, we are going to replace this panel with an existing panel.
The existing panel provides the fields "Class Name", "Project", and "Package", which we therefore will not need to create ourselves. Before continuing, open the layer file and add one attribute below the other attributes (or anywhere in the list) that modify the file template, to specify that we will be making use of the FreeMarker template engine:
<attr name="javax.script.ScriptEngine" stringvalue="freemarker"/>
private WizardDescriptor.Panel[] getPanels() {
if (panels == null) {
panels = new WizardDescriptor.Panel[]{
new ItsNatServletWizardPanel1()
};private WizardDescriptor.Panel packageChooserPanel;
private WizardDescriptor.Panel[] getPanels() {
Project project = Templates.getProject(wizard);
Sources sources = (Sources)project.getLookup().lookup(Sources.class);
SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
packageChooserPanel = JavaTemplates.createPackageChooser(project,groups);
if (panels == null) {
panels = new WizardDescriptor.Panel[] {
packageChooserPanel,
};
The above is a bit of magic that replaces the wizard panel that we created with a panel that all Java source file wizards have in NetBeans IDE.

Create the FreeMarker file template. When you click Finish above, nothing happens. That's what this step is all about. Let's create our FreeMarker template. Right-click the package where your Java class is found, choose New | Other and then, in the New File wizard, choose Other | Empty File. (You'll see a FreeMarker category, installed by the plugin that we installed in step 1 above, but let's leave that for a future article.) Type "ItsNatServlet.ftl" (or something else, so long as the extension is ".ftl"). Then... specify your FreeMarker template, as shown in the screenshot below.
And now you can see the syntax coloring and code completion provided by NetBeans IDE 6.0, via the plugin you installed at the start of this tutorial. (It is at this point that FreeMarker becomes relevant, so apologies for the time it took to get here, but this is the exact context where FreeMarker begins to be useful in this scenario.)

<file name="ItsNatServlet.java" url="ItsNatservlet/ItsNatServlet.ftl">
public Set instantiate() throws IOException {
return Collections.EMPTY_SET;
}public Set instantiate() throws IOException {
String className = Templates.getTargetName(wizard);
FileObject pkg = Templates.getTargetFolder(wizard);
DataFolder targetFolder = DataFolder.findFolder(pkg);
TemplateWizard template = (TemplateWizard) wizard;
DataObject doTemplate = template.getTemplate();
doTemplate.createFromTemplate(targetFolder, className);
FileObject createdFile = doTemplate.getPrimaryFile();
return Collections.singleton(createdFile);
}Now install the module again. This time, when you complete the wizard, you will have a new "ItsNat" servlet:

If you don't have the "ItsNat" JAR files on the classpath, you'll also have... helpful red error marks in the editor.
At this point, there are several open items to explore in the next part of this series—for example, we defined a set of variables in the FreeMarker template. They were magically replaced when we created the file via the wizard. How did that happen? Also, the FreeMarker template starts with lines of code that seem to relate to a project license. But... when the file was created, there was no project license. Why? These two questions and others will be handled in the next part of this series.
Links:
[1] http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=3755
[2] http://itsnat.sourceforge.net/