| <!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><img border="1" src="../images/tutorials/code-templates/html-template-1.png"/> | |
| <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><img border="1" src="../images/tutorials/code-templates/html-template-2.png"/> | |
| <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><img src="../images/tutorials/code-templates/html-template-3.png"/> | |
| <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><b>Contents</b></p> | |
| <img src="../images/articles/65/netbeans-stamp65.gif" class="stamp" width="114" height="114" alt="Content on this page applies to NetBeans IDE 6.5" title="Content on this page applies to NetBeans IDE 6.5"> </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><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><a href="#share-plugin">Creating a Shareable Module Binary</a> | |
| </ul> | |
| </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.5</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 > 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>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> | |
| <br /> | |
| <!-- ===================================================================================== --> | |
| <h2><a name="coding-module"></a>Creating the Code Templates</h2> | |
| <p>In this section, you create the code templates for HTML files. | |
| <br /> | |
| <div class="indent"> | |
| <ol> | |
| <li>Right-click the <tt>org.netbeans.modules.htmlcodetemplate</tt> node and | |
| choose New > Other > XML > XML Document. | |
| Click Next. | |
| <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: | |
| <p><pre class="examplecode"><?xml version="1.0" encoding="UTF-8"?> | |
| <!DOCTYPE codetemplates PUBLIC "-//NetBeans//DTD Editor Code Templates settings 1.0//EN" "https://netbeans.org/dtds/EditorCodeTemplates-1_0.dtd"> | |
| <codetemplates> | |
| <codetemplate abbreviation="snf" xml:space="preserve"> | |
| <code>Select "New File" (Ctrl-N) in the File menu</code> | |
| <description>Code template for selecting "New File".</description> | |
| </codetemplate> | |
| <codetemplate abbreviation="pe" xml:space="preserve"> | |
| <code>Press Enter.</code> | |
| <description>Code template for clicking the "Enter" key.</description> | |
| </codetemplate> | |
| </codetemplates> | |
| </pre> | |
| <p>Right-click in the Source Editor and choose Format (Alt-Shift-F) and then save the file. | |
| </ol> | |
| </div> | |
| <br /> | |
| <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>Add the following tags to the <tt>layer.xml</tt> file, between the <tt><filesystem></tt> tags: | |
| <pre class="examplecode"><?xml version="1.0" encoding="UTF-8"?> | |
| <!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN" "https://netbeans.org/dtds/filesystem-1_1.dtd"> | |
| <filesystem> | |
| <folder name="Editors"> | |
| <folder name="text"> | |
| <folder name="html"> | |
| <folder name="CodeTemplates"> | |
| <file name="org-netbeans-modules-htmleditor-snippets.xml" url="org-netbeans-modules-htmleditor-snippets.xml"/> | |
| </folder> | |
| </folder> | |
| </folder> | |
| </folder> | |
| </filesystem> | |
| </pre> | |
| <br /> | |
| <!-- ======================================================================================= --> | |
| <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. | |
| <ol> | |
| <li><p>Check that your module's source structure is as follows: | |
| <p><img src="../images/tutorials/code-templates/html-template-4.png"/> | |
| <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><p>Open an HTML file and type "snf". Press the Tab key and notice that the | |
| abbreviation expands. Do the same for "pe". | |
| </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. | |
| <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>Make the module available to others via, for example, the | |
| <a href="http://plugins.netbeans.org/PluginPortal/">Plugin Portal</a>. | |
| <li>The recipient can install the module by using their IDE's Plugin Manager. They | |
| would choose Tools > Plugins | |
| from the main menu. | |
| </ol> | |
| </div> | |
| <br /> | |
| <br> | |
| <div class="feedback-box"><a href="https://netbeans.org/about/contact_form.html?to=3&subject=Feedback:%20Code%20Template%20Module%20Tutorial">Send Us Your Feedback</a></div> | |
| <br style="clear:both;" /> | |
| <!-- ======================================================================================== --> | |
| <h2><a name="nextsteps"></a>Next Steps</h2> | |
| <p>For more information about creating and developing NetBeans modules, see the following resources: | |
| <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> | |
| <hr> | |
| <!-- ======================================================================================== --> | |
| <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> | |
| ... | |
| </td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| </body> | |
| </html> |