Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Creating and Customizing File Templates

You can customize the templates that you create files from in the IDE and create your own templates. This might be useful if you need to add standard elements in all of your files (such as copyright notices) or want to change the way other elements are generated.

You can also create your own templates and make them available in the New File wizard.

There are several macros available for use in templates to generate text dynamically in the created files. These macros are identifiable by the double underscores that appear both before and after the macro name. See Table 5-3 for a list of the macros available.

Table 5-3. Java File Template Macros

Macro

Substituted Information

__USER__

Your username. If you would like to change the value of __USER__, choose Tools | Options, click Advanced Options, and select the Editing | Java Sources node. Then click the button in the Strings Table property and change the value of USER.

__DATE__

The date the new file is created.

__TIME__

The time the new file is created.

__NAME__

The name of the class (without the file extension). It is best not to use this macro for the class and constructor name in the file (instead, use the filename).

__PACKAGE__

The name of the package where the class is created.

__PACKAGE_SLASHES__

The name of the class' package with slash (/) delimiters instead of periods (.).

__PACKAGE_AND_NAME__

The fully qualified name of the file (such as com.mydomain.mypackage.MyClass).

__PACKAGE_AND_NAME_SLASHES__

The fully qualified name of the file with slash (/)delimiters instead of periods (.).

__QUOTES__

A double quote mark ("). Use this macro if you want the substituted text to appear in quotes in the generated file. (If you place a macro within quote marks in the template, text is not substituted for the macro name in the created file.)

To edit a template:

  1. Choose Tools | Template Manager.
  2. Expand the appropriate category node and select the template that you want to edit.
  3. Click Open in Editor.
  4. Edit the template and then save it.

To create a new file template based on another template:

  1. Choose Tools | Template Manager.
  2. Navigate to and select the template on which you want to model the new template and click Duplicate.

    A new node appears for the copied template. _1 is appended to the template's name.

  3. Click Open in Editor.
  4. Edit the file, incorporating any of the template macros that you want to use (see Table 5-3), and save it.

    If the template is for a Java class, you can use the filename for the class name and constructor name. These are automatically adjusted in the files you create from the template.

To import a file template:

  1. Choose Tools | Template Manager.
  2. Select the category folder for the template.
  3. Click Add to open the Add Template dialog box.
  4. Navigate to and select the file that you want to import as a template. Then click Add.
  • + Share This
  • 🔖 Save To Your Account