The new file wizard works similarly as the previous chapter wizard function with the difference that this works, as the name suggests, by allocating a placeholder for a desired file type, say a Python source file.
Another difference from the previous wizard is that in this one we will use FreeMarker, a template engine to generate text. By creating the FreeMarker templates the developer can then combine the templates with data, generating text of pages as a result.
For more information visit: http://freemarker.org.
We will need to create a New Project based on the NetBeans Module. Follow the Getting Ready section of the previous recipe and when Project Name is asked enter NBCookbookFileTemplates,
using the other values as shown in the Getting Ready section.
Our recipe starts by creating a template. Right-click on the projects node, select New and Other....
pythonTemplate.py
. NBCookbookFileTemplates
until reaching the com.packtpub
package and click Select Folder.When the empty file is opened insert the following:
"""On ${date} at ${time} the ${nameAndExt} was created by ${user}""" class ${name}: """TODO: Remove comment"""
Now we will need a descriptor. Right-click on the projects node, select New and Other....
descriptor
. NBCookbookFileTemplates
until reaching the com.packtpub
package and click Select Folder.When the HTML file is opened, in between the<body>
tags, insert the following:
Python Template
Finally we will tie the components together by editing layer.xml
. Replace the following tag:
<filesystem/>
with:
<filesystem> <folder name="Templates"> <folder name="Other"> <file name="Python.py" url="pythonTemplate.py"> <attr name="template" boolvalue="true"/> <attr name="SystemFileSystem.localizingBundle" stringvalue="com.packtpub.Bundle"/> <attr name="templateWizardURL" urlvalue="nbresloc:/com/packtpub/descriptor.html"/> <attr name="javax.script.ScriptEngine" stringvalue="freemarker"/> </file> </folder> </folder> </filesystem>
Now is the turn to edit the Bundle.properties:
com.packtpub
package. Bundle.properties
and append the following line:Templates/Other/Python.py=Modified Python Template
Save the file.
The template file we have created is similar to the normal Java file template that NetBeans creates every time we ask for a new Java class.
This time we have decided to create a Python Template. The File is annotated with the following tags:
${date}
${time}
${nameAndExt}
${user}
${name}
The names of the tags and their meanings are self-explanatory.
Following the creation of the Python template the descriptor is the next step. This is the information that is going to be shown to the user when clicking a file on the New File dialog. This file is HTML-formatted and we have just included a brief description of what our file is going to be. Note that formatting styles, such as bold and italics, might be applied with the HTML tags to give the description field a better visualization.
layer.xml
is the file where we actually bind the code and all of the created resources together.
The<folder>
tag indicates to the IDE where the placing of our Python template is going to be located. Both tags then indicate that this is under Templates
and Other
.
As said in the beginning of the recipe, FreeMarker is used as an engine for text output. The tag:
<attr name="javax.script.ScriptEngine" stringvalue="freemarker"/>
Specifies which script engine is used for that purpose.
And:
<attr name="SystemFileSystem.localizingBundle" stringvalue="com.packtpub.Bundle"/>
Shows the IDE where the bundle file, which contains the String that is used for the name on the left side of the New File Wizard, is located.
To finally create our own Python file:
After following the wizard steps the file created has the following output:
"""On Dec 07, 2010 at 10:47:12 PM the testepython.py was created by dantas""" class testepython: """TODO: Remove comment"""
18.116.42.149