Thursday, July 29, 2010

Project Type Module + Privileged Templates

I started working on a Project Module for managing WoW addons (Lua Topping). After fidling arround I got the nodes to work as intended (posted here) I wanted to have a nice set of templates for the basic addon files: lua, toc and xml, plus some specific to WowAce. You can look at the NetBeans tutorial here.

Taking a look at the NetBeans API and the Project class I found that for the lookup you could add a PrivilegedTemplates interface. This interface only has one method:

String[] getPrivilegedTemplates()

According to the API documentation the returned string array should contain "full paths to privileged templates, e.g. Templates/Other/XmlFile.xml". But what is the correct full path for a template?. The simplest way to find out is to view your layer.xml file (you should read about NetBeans and layer files if you want details on how they work). In my case, the Templates section of my layer.xml file looks like this:

<folder name="Templates">
<folder name="WoW">
<file name="TocTemplate.toc" url="TocTemplate.toc">
<attr name="displayName" bundlevalue=""/>
<attr name="template" boolvalue="true"/>
<file name="WoWUIxmlTemplate.xml" url="WoWUIxmlTemplate.xml">
<attr name="displayName" bundlevalue=""/>
<attr name="template" boolvalue="true"/>
<file name="Template.pkgmeta" url="Template.pkgmeta">
<attr name="displayName" bundlevalue=""/>
<attr name="template" boolvalue="true"/>

It turns out the correct full path for a template is the path starting at Templates/ and finishing at the template name (as in the name property of the file entry), containing all the nested sub folders. So in my case, for the TocTemplate.toc template this would be: Templates/WoW/TocTemplate.toc.

Putting it all together (assuming you have followed the NetBeans tutorial):
  1. Add a PrivilegedTemplates implementation to your project lookup

    //The project type's capabilities are registered in the project's lookup:
    public Lookup getLookup() {
    if (lkp == null) {
    lkp = Lookups.fixed(new Object[]{
    state, //allow outside code to mark the project as needing saving
    new ActionProviderImpl(), //Provides standard actions like Build and Clean
    new WowAddonDeleteOperation(),
    new WowAddonCopyOperation(this),
    new Info(), //Project information implementation
    new WowAddonProjectLogicalView(this), //Logical view of project implementation
    new WowAddonProjectCustomizer(this), //Customizer for the project
    new WowAddonProjectPrivilegedTemplates(),
    return lkp;

  2. In you PrivilegedTemplates implementation construct the desired templates names array

    private final class WowAddonProjectPrivilegedTemplates implements PrivilegedTemplates {

    private String[] privileged = new String[]{

    public String[] getPrivilegedTemplates() {
    return privileged;

That's it!. When you run your Project Module, your templates should be visible when you right click the project node and select the New option:

No comments:

Post a Comment