You can use code templates to speed up the entry of commonly used
sequences of reserved words and common code patterns. For example,
if you enter forc and press the Tab key, it expands into the
following:
for (Iterator it = col.iterator(); it.hasNext();) { Object object = it.next();
When you create code templates, there are several constructs
that you can use to customize the way the code template behaves.
You can look at the default IDE code
templates in the Options window, under the Tools
menu, for examples from which you can learn.
In general, a code template parameter can be specified by its name and a
set of optional hints. Hints serve as guidance when
computing the values assigned by the infrastructure to the parameters on template expansion.
Syntax for parameter definition is as follows:
${param_name hint=value hint=value ...}
However, boolean hints that can be written without the value part:
${param_name hint} translates to ${param_name hint=true}
Reserved Parameter and Hint Names
Some parameter names are reserved by the code template infrastructure:
${cursor} defines the position where
the caret will be located after the editing of the code template values finishes.
${selection} defines the position for pasting the content of
the editor selection, which is used by the 'selection templates'
that appear as hints whenever the user selects text in the editor.
Similarly, some of the hint names are reserved by the code template infrastructure:
${param_name default="value"} defines the parameter's default value.
${param_name editable=false} can be used to disable user's editing of the parameter.
${param_name instanceof="java.util.Collection"} requires the parameter value to be an instance of the given type.
${param_name array} requires the parameter value to be of an array type (including arrays of primitive data types).
${param_name iterable} requires the parameter value to be of an array type or an instance of "java.lang.Iterable". Can
be used in 'for-each' cycles.
${param type="java.util.Iterator"} requires the parameter value to be the given type. The infrastructure will try to use
short name Iterator and import java.util.Iterator if possible.
${param_name iterableElementType} requires the parameter value to be the type of the iterable element. Can be used in
'for-each' cycles.
${param_name leftSideType} requires the parameter value to be the type of the expression on the assignment's left side.
${param_name rightSideType} requires the parameter value to be the type of the expression on the assignment's right side.
${param_name cast} defines that the parameter value would be a type cast if necessary.
${param_name newVarName} defines that the parameter value should be a 'fresh' unused variable name in the given context.
Pre-defined Parameters
NetBeans comes with a number of pre-defined parameters that you can use when creating code templates. You can add parameter
definitions in the Template Manager. Note also special rules for changing the
the format of the ${date} variable.
${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. (Java only)
${time} inserts the current time, in this format: 7:37:58 PM
${project.license} based on the value (e.g., 'CDDL'), the license header is found and is inserted into the template.
${project.name} inserts the project name.
${project.displayName} inserts the project displayname.
Adding Parameter Definitions
You can define additional parameters in the Template Manager.
To define a parameter:
In the IDE's menu bar, choose Tools > Templates. The Template Manager opens.
Expand the Other category. Double-click on Properties. The User.properties file opens in the editor.
Define a parameter. For example, define the ${user} parameter for adding your name to templates you create. The syntax is
user=LorumIpsum@mycompany.com
When a code template is expanded, all occurrences of ${user} in that template are replaced with 'LorumIpsum@mycompany.com'.
Add as many additional parameters as you want, with the syntax name=value.
Parameter definition follows the FreeMarker Java template language. For more information, see the
.
Formatting the ${date} Parameter
The ${date} variable is of String type, not Date type. You have to cast
${date} to a Date type in order to change its format. For example,
to show the year only, add the following parameter to your code template: