blob: fece18f3794f19d5c17d860d12b687b859854b20 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<meta name="copyright" content="Copyright (c) IBM Corporation and others 2000, 2005. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." >
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>
Using templates
</title>
<link rel="stylesheet" href="../book.css" charset="ISO-8859-1" type="text/css">
</head>
<body>
<h1>
Using templates
</h1>
<p>
To use templates:
</p>
<ol>
<li>
In the Java editor, position the caret in a place where you want to insert a template.
</li>
<li>
Invoke content assist by pressing <kbd>Ctrl</kbd>+<kbd>Space</kbd>.
</li>
<li>
Templates appear in the presented list. Note that the list is filtered as you type, so typing a few first characters of a template name will reveal it.
</li>
<li>
Note that a preview is presented for each selected template.
</li>
</ol>
<p>
<strong>Notes:</strong>
</p>
<p>
Templates can have variables, which are place-holders for the dynamic part of a template pattern, e.g. subject to change with every application of the particular template.
</p>
<p>
When a template is inserted in the Java editor and the template pattern contained a template variable, the editor enters the template edit mode.
</p>
<p>
A box is drawn around all variables. The first variable is selected and can be modified by typing in the editor.
If the same variable existed multiple times in the template pattern, all
instances of the same variable are highlighted in blue and updated instantaneously to save typing.
</p>
<p>
Pressing <kbd>Tab</kbd> navigates to the next unique template variable, <kbd>Shift-Tab</kbd> navigates to the previous unique template variable.
</p>
<p>
The template edit mode is left by either pressing <kbd>Tab</kbd> on the last template variable or pressing <kbd>Esc</kbd> or <kbd>Enter</kbd>.
</p>
<p>
Example:
</p>
<ul>
<li>
Create a method <code>void m(int[] intarray){}</code> and position the caret inside the method.
</li>
<li>
Type <code>for</code> and press <kbd>Ctrl+Space</kbd> to open Code Assist
</li>
<li>
Select the first entry from the list (i.e.<strong>for - iterate over array</strong>). Note the template preview window.
</li>
<li>
Note also that the name of the array (i.e. <code>intarray</code>) is automatically detected.
</li>
<li>
The local variable <code>i</code> is now selected and you are in the template edit mode.
Typing another name instantaneously updates all occurrences of it.
</li>
<li>
Press <kbd>Tab</kbd>. You can now modify the suggested name for the array (pressing <kbd>Shift-Tab</kbd> will let you modify the name of the local variable again).
</li>
<li>
To leave the template edit mode
<ul>
<li>
press <kbd>Tab</kbd> or <kbd>Enter</kbd>, which will move the caret so that you can enter the body of the newly created loop or
</li>
<li>
press <kbd>Esc</kbd>, which will not move the caret and preserves the current selection in the editor.
</li>
</ul>
</li>
</ul>
<p>
<img src="../images/ngrelc.png" alt="Related concepts" border="0"><br>
<a href="../concepts/concepts-7.htm">Java editor</a><br>
<a href="../concepts/ctemplates.htm">Templates</a><br>
</p>
<p>
<img border="0" src="../images/ngrelt.png" alt="Related tasks"><br>
<a href="tasks-54.htm">Using the Java editor</a><br>
<a href="tasks-203.htm">Writing your own templates</a>
</p>
<p>
<img border="0" src="../images/ngrelr.png" alt="Related reference"><br>
<a href="../reference/ref-preferences-templates.htm">Templates preference page</a>
</p>
</body>
</html>