blob: 6c281e0bbe468cd55fd59a5cb3d9e5221a8a74ad [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." >
<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">
<title>
Using code templates
</title>
</head>
<body>
<h1>
Using 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 single-click 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.png" 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.png" 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 references to the same variable
are connected to each other. So changing one changes all the other values
as well.
<p>
<img src="../images/qs-17c1.png" 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.png" 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.png" 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.png" 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.png" 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.png" alt="Related concepts" >
</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.png" alt="Related tasks" border="0" >
</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.png" 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>
</body>
</html>