Creating a new file wizard

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.

Getting ready

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.

How to do it...

Our recipe starts by creating a template. Right-click on the projects node, select New and Other....

  1. Under Choose File Type: On the Categories side select Other and under File Types select Empty File and click Next >.
  2. Under Name and Location: Under File Name enter pythonTemplate.py.
  3. The Folder field click on the Browse... button.
  4. Then on the Browse Folders window expand the NBCookbookFileTemplates until reaching the com.packtpub package and click Select Folder.
  5. Back on the New Empty File dialog click Finish.
How to do it...

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....

  1. Under Choose File Type: On the Categories side select Other and under File Types select HTML File and click Next >.
  2. Under Name and Location: Under File Name enter: descriptor.
  3. On the Folder field click on the Browse... button.
  4. On the Browse Folders window expand the NBCookbookFileTemplates until reaching the com.packtpub package and click Select Folder.
  5. Back on the New HTML File dialog click Finish.

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:

  1. Expand Source Packages and com.packtpub package.
  2. Double-click on Bundle.properties and append the following line:
    Templates/Other/Python.py=Modified Python Template
    

Save the file.

How it works...

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:

  1. Run the main project.
  2. When the new NetBeans instance is running create a new project.
  3. When the project is created right-click on the Project node and select New and Other....
  4. On the New File dialog select under Categories navigate to Other and on the File Types side select Modified Python Templates.
How it works...

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"""
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.42.149