banner



How To Create Template In Java

NetBeans File Template Module Tutorial

  • Introduction to FreeMarker
  • Creating the Module Project
  • Creating the File Template
    • Creating the Template File
    • Creating the Description File
    • Getting an Icon
  • Registering the File Template
  • Building and Installing the Module
  • Providing Additional Variables
  • Providing a Project License
  • Next Steps

This tutorial demonstrates how to create a NetBeans module providing file templates. Once your users have installed the module in the IDE, the file templates are available to them in the New File wizard. Sharing file templates is easy once you have a module that contains them—the IDE lets you create a binary that you can make available to others, who can then install it through the Plugin Manager.

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, that is, in the layer.xml file.

Introduction to FreeMarker

Since NetBeans IDE 6.0, you have been able to optionally use the FreeMarker template language to define your file templates. Several of the templates that are bundled with the IDE are defined in this way. For example, the Java class template is defined as follows:

                <#assign licenseFirst = "/*"> <#assign licensePrefix = " * "> <#assign licenseLast = " */"> <#include "../Licenses/license-${project.license}.txt">  <#if package?? &amp;&amp; package != ""> package ${package};  </#if> /**  *  * @author ${user}  */ public class ${name} {  }              

The benefit of using FreeMarker can be seen in the template above, that is, you can add logic to your templates, via directives such as if/elseif/else and loop constructs. For a full description of FreeMarker template language, see the FreeMarker Manual, in particular, the Directives chapter. In this tutorial, you will be shown, among other things, the steps you need to take to incorporate FreeMarker into your file templates.

Creating the Module Project

We begin by going through the New Module Project wizard, which will create a source structure, with all the minimum requirements, for our new module.

  1. Choose File > New Project (Ctrl+Shift+N). Under Categories, select NetBeans Modules. Under Projects, select Module. Click Next.

  1. In the Name and Location panel, type AdditionalFileTemplates in the Project Name field. Change the Project Location to any directory on your computer. Click Next.

  1. In the Basic Module Configuration panel, type org.myorg.additionalfiletemplates in Code Name Base. Click Finish.

The IDE creates the AdditionalFileTemplates 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).

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

The template file defines the content that the template will generate for the user.

  1. Right-click the AdditionalFileTemplates node and choose New > Other. In the New File wizard, under Categories, choose Other and under File Types, choose HTML. Click Next.

1. Type HTML in File Name. Click Browse and browse to src/org/myorg/additionalfiletemplates . Click Select Folder. Click Finish. A new HTML file, named HTML.html , opens in the Source Editor, containing the standard HTML file's content shown below:

                  <!DOCTYPE html> <html>   <head>     <title></title>     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   </head>   <body>      TODO write content   </body> </html>                
  1. Change the HTML file according to your needs. You can add the following predefined variables, if needed:

    • ${date} inserts the current date, in this format: Feb 16, 2008

    • ${encoding} inserts the default encoding, such as: UTF-8

    • ${name} inserts the name of the file.

    • ${nameAndExt} inserts the name of the file, together with its extension.

    • ${package} inserts the name of the package where the file is created.

    • ${time} inserts the current time, in this format: 7:37:58 PM

    • ${user} inserts the user name.

Your users will be able to set values for these variables in the Template Manager, which is under the Tools menu. There, they will click the Settings button and a file named user.properties will open in the Source Editor. In this file, users can set the above values, to override those provided by default. Typically, however, they will not do this, because the default values of the above variables will do the job fine.

In addition to the predefined variables, you can provide additional variables to your users, via your module. This is explained later in this tutorial. The full list of FreeMarker directives can also be used to add logic to the template:

  • #assign

  • #else

  • #elseif

0 Response to "How To Create Template In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel