blob: 7eedb068124b5adc1c2647968acf6a67671cc21d [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 7.2</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. Changes can be made by the user in the Options window, either
to customize the existing code templates or to add new ones:</p>
<p><img alt="" src="../images/tutorials/code-templates/72/pic2.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 class="notes">Note:</b> This document uses NetBeans IDE 7.2 and NetBeans Platform 7.2. If you
are using an earlier version, see <a href="71/nbm-code-template.html">the previous version
of this document</a>.</p>
<p><b>Contents</b></p>
<p><img src="../images/articles/72/netbeans-stamp.gif" class="stamp" width="114" height="114" alt="Content on this page applies to NetBeans IDE 7.2" title="Content on this page applies to NetBeans IDE 7.2"/></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 7.2 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 above</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>
<div class="indent">
<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. Click Next.</li>
<li>In the Basic Module Configuration panel, type <tt>org.netbeans.htmlcodetemplate</tt>
in Code Name Base. Click Finish.</li>
</ol>
</div>
<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><p>Type <tt>org-netbeans-modules-htmleditor-snippets</tt>
and in "Folder" browse to the "htmlcondetemplate" folder:</p>
<br/>
<p><img alt="" src="../images/tutorials/code-templates/72/pic1.png"/></p>
<br/>
<p>Click Next and then click Finish.</p></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>
</li>
</ol>
</div>
<p>You now have two code templates. The first will be shown when "snf" is typed, followed
by the expansion key, while the second requires "pe" to be typed, prior to the
expansion key being pressed.</p>
<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>
<div class="indent">
<ol>
<li>Right-click the <tt>org.netbeans.modules.htmlcodetemplate</tt> node and
choose New &gt; Other &gt; Module Development &gt; XML Layer.
Click Next and then click Finish.</li>
<li>Replace the content of the layer.xml file
with the following:
<pre class="examplecode">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "https://netbeans.org/dtds/filesystem-1_2.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>
</pre></li>
</ol>
</div>
<!-- ======================================================================================= -->
<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>
<div class="indent">
<ol>
<li><p>In the Projects window, right-click the <tt>HTMLCodeTemplates</tt> project and
choose Run. 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>Look in the Options window and notice your two code templates:</p>
<br/>
<p><img alt="" src="../images/tutorials/code-templates/72/pic2.png"/></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>
</div>
<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>
<div class="indent">
<ol>
<li><p>In the Projects window, right-click the <tt>HTMLCodeTemplates</tt> project
and choose Create NBM. 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>
<div class="feedback-box"><a href="https://netbeans.org/about/contact_form.html?to=3&amp;subject=Feedback:%20Code%20Template%20Module%20Tutorial%207.2">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>
<table width="76%" border="1">
<tbody>
<tr>
<td>
<b>Version</b>
</td>
<td>
<b>Date</b>
</td>
<td>
<b>Changes</b>
</td>
<td>
<b>Open Issues</b>
</td>
</tr>
<tr>
<td>
1
</td>
<td>
14 November 2008
</td>
<td>
Initial version
</td>
<td>
</td>
</tr>
<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>
<tr>
<td>
3
</td>
<td>
18 November 2011
</td>
<td>
Updated for 7.1.
</td>
<td>
</td>
</tr>
<tr>
<td>
4
</td>
<td>
24 July 2012
</td>
<td>
Updated for 7.2.
</td>
<td>
</td>
</tr>
</tbody>
</table>-->
</body>
</html>