blob: e2ee74782e16d6f7353ac1f9c52d366c65fc9b38 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<title>
Using source code templates
</title>
<link rel="stylesheet" href="../book.css" charset="ISO-8859-1" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
</head>
<body>
<h1>
Using source code templates
</h1>
<p>
In this section you will use content assist to fill in a template for a common loop structure.
Open <em>junit.samples.VectorTest.java</em> file in the Java editor if you do not already
have it open.
</p>
<ol>
<li>
Start adding a new method by typing the following:<br>
<p><code>
public void testValues() {<br>
&nbsp;&nbsp;&nbsp; Integer[] expected= new Integer[3];<br>
&nbsp;&nbsp;&nbsp; for<br>
</code></p>
</li>
<li>
With the cursor at the end of <code>for</code>, press <kbd>Ctrl</kbd>+<kbd>Space</kbd> to enable content assist.&nbsp;
You will see a list of common templates for &quot;for&quot; loops.&nbsp;
When you hover over a template, you'll see the code for the template in
its help message.&nbsp; Note that the local array name is guessed automatically.
<p>
<img src="../images/qs-17b.gif" width=429 height=215 alt="Content assist for for">
</p>
</li>
<li>
Choose the <code>for - iterate over array</code> entry and press <kbd>Enter</kbd> to confirm the template.&nbsp;
The template will be inserted in your source code.
<p>
<img src="../images/qs-17c.gif" width=438 height=106 alt="Inserted for template">
</p>
</li>
<li>
Next we change the name of the index variable from <em>i</em> to <em>e</em>. To do so simply press <kbd>e</kbd>, as
the index variable is automatically selected.
Observe that the name of the index variable changes at all places. When inserting a template all template
variable with the same name are connected to each other. So changing one changes all the other values
as well.
<p>
<img src="../images/qs-17c1.gif" width=432 height=98 alt="Inserted for template">
</p>
</li>
<li>
Pressing the <kbd>tab</kbd> key moves the cursor to the next variable of the code template. This is the array
<em>expected</em>.
<p>
<img src="../images/qs-17c2.gif" width=437 height=104 alt="Inserted for template">
</p>
Since we don't want to change the name (it was guessed right by the template) we press
tab again, which leaves the template since there aren't any variables left to edit.
</li>
<li>
Complete the <code>for</code> loop as follows:<br>
<p><code>
for (int e= 0; e &lt; expected.length; e++) {<br>
&nbsp;&nbsp;&nbsp; expected[e]= new Integer(e + 1);<br>
}<br>
Integer[] actual= to<br>
</code></p>
</li>
<li>
With the cursor at the end of <code>to</code>, press <kbd>Ctrl</kbd>+<kbd>Space</kbd> to enable content assist. Pick <code>toarray - convert collection to array</code>
and press <kbd>Enter</kbd> to confirm the selection (or double-click the
selection).
<p>
<img src="../images/qs-17d.gif" width=519 height=272 alt="Inserted for template">
</p>
The template is inserted in the editor and <code>type</code> is highlighted and selected.
<p>
<img src="../images/qs-17e.gif" width=638 height=118 alt="Inserted for template">
</p>
</li>
<li>
Overwrite the selection by typing <code>Integer</code>. The type of array constructor changes
when you change the selection.
</li>
<li>
Press <kbd>Tab</kbd> to move the selection to <code>collection</code> and
overwrite it by typing <code>fFull</code>.
<p>
<img src="../images/qs-17f.gif" width=588 height=113 alt="Inserted for template">
</p>
</li>
<li>
Add the following lines of code to complete the method:<br>
<p><code>
assertEquals(expected.length, actual.length);<br>
for (int i= 0; i &lt; actual.length; i++)<br>
&nbsp;&nbsp;&nbsp; assertEquals(expected[i], actual[i]);<br>
}</code></p>
</li>
<li>
Save the file.
</li>
</ol>
<p>
<img border="0" src="../images/ngrelc.gif" alt="Related concepts" width="159" height="27">
</p>
<p>
<a href="../concepts/concepts-7.htm">Java editor</a><br>
<a href="../concepts/ctemplates.htm">Templates</a><br>
</p>
<p>
<img src="../images/ngrelt.gif" alt="Related tasks" border="0" height="27" width="159">
</p>
<p>
<a href="../tasks/tasks-54.htm">Using the Java editor</a><br>
<a href="../tasks/tasks-171.htm">Using templates</a><br>
</p>
<p>
<img src="../images/ngrelr.gif" width=159 height=27 alt="Related reference">
</p>
<p>
<a href="../reference/ref-preferences-templates.htm">Templates Preferences</a><br>
<a href="../reference/ref-21.htm">Java Editor Preferences</a><br>
</p>
<p>
<a href="../hglegal2003.htm"><img src="../images/ngibmcpy2003.gif" width=324 height=14 alt="Copyright (c) 2000, 2003 IBM Corporation and others. All Rights Reserved." border="0"></a>
</p>
</body>
</html>