blob: 57f38c45cc8b90e6e2c81c0746a1f8ea254ff9ce [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- -*- xhtml -*- -->
<title>NetBeans Code Template Module Tutorial for NetBeans Platform 6.5</title>
<link rel="stylesheet" type="text/css" href="https://netbeans.org/netbeans.css"/>
<meta name="AUDIENCE" content="NBUSER"/>
<meta name="TYPE" content="ARTICLE"/>
<meta name="EXPIRES" content="N"/>
<meta name="developer" content="gwielenga@netbeans.org"/>
<meta name="indexed" content="y"/>
<meta name="description"
content="A short guide to providing code templates for NetBeans Platform
applications."/>
<!-- Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. -->
<!-- Use is subject to license terms.-->
</head>
<body>
<h1>NetBeans Code Template Module Tutorial</h1>
<p>This tutorial demonstrates how to create a NetBeans module that provides
code templates. In this particular scenario, you will enable the user
to type "snf", as below...</p>
<p><img border="1" src="../images/tutorials/code-templates/html-template-1.png"/></p>
<p>...which, when the user presses Tab, will expand
to the full text "Select "New File" (Ctrl-N) in the File menu", as shown here:</p>
<p><img border="1" src="../images/tutorials/code-templates/html-template-2.png"/></p>
<p>The abbreviation will only expand in this way if the user is typing in
an HTML file. Changes can then be made in the Options window, either
to customize the existing code templates or to add new ones:</p>
<p><img src="../images/tutorials/code-templates/html-template-3.png"/></p>
<p>To provide this functionality, you will not need to use any Java code
at all. As shown in this tutorial, you will only need to provide an
XML file that defines your code templates, by registering it in your module's
<tt>layer.xml</tt> file.</p>
<p><b>Contents</b></p>
<p><img src="../images/articles/69/netbeans-stamp7-8-9.png" class="stamp" width="114" height="114" alt="Content on this page applies to NetBeans IDE 6.5, 6.7, 6.8" title="Content on this page applies to NetBeans IDE 6.5, 6.7, 6.8"/></p>
<ul class="toc">
<li><a href="#creatingthemoduleproject">Setting Up the Module Project</a>
</li>
<li><a href="#coding-module">Creating the Code Templates</a></li>
<li><a href="#registering-module">Declaring and Registering the Code Template</a>
</li>
<li><a href="#building">Building and Installing the Code Template</a>
<ul>
<li><a href="#try-plugin">Trying Out the Code Template</a></li>
<li><a href="#share-plugin">Creating a Shareable Module Binary</a></li>
</ul></li>
</ul>
<p><b>To follow this tutorial, you need the software and resources listed in the following
table.</b></p>
<table>
<tbody>
<tr>
<th class="tblheader" scope="col">Software or Resource</th>
<th class="tblheader" scope="col">Version Required</th>
</tr>
<tr>
<td class="tbltd1"><a href="https://netbeans.org/downloads/index.html">NetBeans IDE</a></td>
<td class="tbltd1">version 6.7 or above</td>
</tr>
<tr>
<td class="tbltd1"><a href="http://java.sun.com/javase/downloads/index.jsp">Java Developer Kit (JDK)</a></td>
<td class="tbltd1">Version 6 or<br/>version 5</td>
</tr>
</tbody>
</table>
<h2 class="tutorial"><a name="creatingthemoduleproject"></a>Setting up the Module Project</h2>
<p>Before you start writing the module, you have to make sure you
that your project is set up correctly. The IDE provides a wizard that sets up all the basic files
needed for a module.</p>
<ol>
<li>Choose File &gt; New Project (Ctrl+Shift+N). Under Categories, select NetBeans Modules.
Under Projects, select Module. Click Next.</li>
<li>In the Name and Location panel, type <tt>HTMLCodeTemplates</tt> in the Project Name field.
Change the Project Location to any directory on your computer. Leave the Standalone Module option
and Set as Main Project checkbox selected. Click Next.</li>
<li>In the Basic Module Configuration panel, type <tt>org.netbeans.htmlcodetemplate</tt>
in Code Name Base.</li>
<li>Select "Generate XML Layer". Leave the
locations of both the localizing bundle and the XML layer file
so that they will be stored in a package with
the name <tt>org/netbeans/modules/htmlcodetemplate</tt>. Click Finish.</li>
</ol>
<p> The IDE creates the <tt>HTMLCodeTemplates</tt>
project. The project contains all of your sources and
project metadata, such as the project's Ant build script. The project
opens in the IDE. You can view its logical structure in the Projects window (Ctrl-1) and its
file structure in the Files window (Ctrl-2).</p>
<!-- ===================================================================================== -->
<h2><a name="coding-module"></a>Creating the Code Templates</h2>
<p>In this section, you create the code templates for HTML files.</p>
<div class="indent">
<ol>
<li>Right-click the <tt>org.netbeans.modules.htmlcodetemplate</tt> node and
choose New &gt; Other &gt; XML &gt; XML Document.
Click Next.</li>
<li>Type <tt>org-netbeans-modules-htmleditor-snippets</tt>
and browse to the main package is selected in the Package drop-down list.
Click Next and then click Finish.</li>
<li>Replace the default content of the <tt>org-netbeans-modules-htmleditor-snippets.xml</tt> file with the following:
<pre class="examplecode">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE codetemplates PUBLIC "-//NetBeans//DTD Editor Code Templates settings 1.0//EN" "https://netbeans.org/dtds/EditorCodeTemplates-1_0.dtd"&gt;
&lt;codetemplates&gt;
&lt;codetemplate abbreviation="snf" xml:space="preserve"&gt;
&lt;code&gt;Select "New File" (Ctrl-N) in the File menu&lt;/code&gt;
&lt;description&gt;Code template for selecting "New File".&lt;/description&gt;
&lt;/codetemplate&gt;
&lt;codetemplate abbreviation="pe" xml:space="preserve"&gt;
&lt;code&gt;Press Enter.&lt;/code&gt;
&lt;description&gt;Code template for clicking the "Enter" key.&lt;/description&gt;
&lt;/codetemplate&gt;
&lt;/codetemplates&gt;
</pre>
<p>Right-click in the Source Editor and choose Format (Alt-Shift-F) and then save the file.</p>
</li>
</ol>
</div>
<h2><a name="registering-module"></a>Declaring and Registering the Code Templates</h2>
<p>Code templates are registered in the <tt>layer.xml</tt> file for
the MIME type to which they apply.</p>
<p>Add the following tags to the <tt>layer.xml</tt> file, between the <tt>&lt;filesystem&gt;</tt> tags:</p>
<pre class="examplecode">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN" "https://netbeans.org/dtds/filesystem-1_1.dtd"&gt;
&lt;filesystem&gt;
&lt;folder name="Editors"&gt;
&lt;folder name="text"&gt;
&lt;folder name="html"&gt;
&lt;folder name="CodeTemplates"&gt;
&lt;file name="org-netbeans-modules-htmleditor-snippets.xml" url="org-netbeans-modules-htmleditor-snippets.xml"/&gt;
&lt;/folder&gt;
&lt;/folder&gt;
&lt;/folder&gt;
&lt;/folder&gt;
&lt;/filesystem&gt;
</pre>
<!-- ======================================================================================= -->
<h2><a name="building"></a>Building and Installing the Code Templates</h2>
<p>Now we need to think about installation and distribution.
In the first section below, we install the code templates,
next we create an NBM file and examine distribution channels.</p>
<div class="indent">
<h3 class="tutorial"><a name="try-plugin"></a>Trying Out the Code Templates</h3>
<p>Install and try out the code templates,
by following the steps below.</p>
<ol>
<li><p>Check that your module's source structure is as follows:</p>
<p><img src="../images/tutorials/code-templates/html-template-4.png"/></p></li>
<li><p>In the Projects window, right-click the <tt>HTMLCodeTemplates</tt> project and choose Run. </p>
<p>The module is built and installed in the target platform. The target platform opens so that you
can try out your new module. The default target platform is the
installation used by the current instance of the development IDE.</p></li>
<li><p>Open an HTML file and type "snf". Press the Tab key and notice that the
abbreviation expands. Do the same for "pe".</p></li>
</ol>
<h3 class="tutorial"><a name="share-plugin"></a>Creating a Shareable Module Binary</h3>
<p>An NBM file is the binary version of the module that
provides the code templates. Below, using one menu item, we create
the NBM file.</p>
<ol>
<li><p>In the Projects window, right-click the <tt>HTMLCodeTemplates</tt> project and choose Create NBM.</p>
<p>The NBM file is created and you can view it in the Files window (Ctrl-2).</p></li>
<li>Make the module available to others via, for example, the
<a href="http://plugins.netbeans.org/PluginPortal/">Plugin Portal</a>.</li>
<li>The recipient can install the module by using their IDE's Plugin Manager. They
would choose Tools &gt; Plugins
from the main menu.</li>
</ol>
</div>
<div class="feedback-box"><a href="https://netbeans.org/about/contact_form.html?to=3&amp;subject=Feedback:%20Code%20Template%20Module%20Tutorial">Send Us Your Feedback</a></div>
<!-- ======================================================================================== -->
<h2><a name="nextsteps"></a>Next Steps</h2>
<p>For more information about creating and developing NetBeans modules, see the following resources:</p>
<ul>
<li><a href="https://netbeans.org/kb/trails/platform.html">Other Related Tutorials</a></li>
<li><a href="http://bits.netbeans.org/dev/javadoc/index.html">NetBeans API Javadoc</a></li>
</ul>
<!-- ======================================================================================== -->
<!--
<h2><a name="version"></a>Versioning </h2>
<p>
<table width="76%" border="1">
<tbody>
<tr>
<td>
<div align="left"><b>Version</b></div>
</td>
<td>
<div align="left"><b>Date</b></div>
</td>
<td>
<div align="left"><b>Changes</b></div>
</td>
<td>
<div align="left"><b>Open Issues</b></div>
</td>
</tr>
<tr>
<td>
1
</td>
<td>
14 November 2008
</td>
<td>
Initial version
</td>
<td>
<tr>
<td>
2
</td>
<td>
23 November 2010
</td>
<td>
Fixing broken elements to remove red error marks in the editor.
</td>
<td>
...
</td>
</tr>
</tbody>
</table>-->
</body>
</html>