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="net.sourceforge.luatopping.wow.addon.project.Bundle#Templates/WoW/TocTemplate.toc"/>
<attr name="template" boolvalue="true"/>
</file>
<file name="WoWUIxmlTemplate.xml" url="WoWUIxmlTemplate.xml">
<attr name="displayName" bundlevalue="net.sourceforge.luatopping.wow.addon.project.Bundle#Templates/WoW/WoWUIxmlTemplate.xml"/>
<attr name="template" boolvalue="true"/>
</file>
<file name="Template.pkgmeta" url="Template.pkgmeta">
<attr name="displayName" bundlevalue="net.sourceforge.luatopping.wow.addon.project.Bundle#Templates/WoW/Template.pkgmeta"/>
<attr name="template" boolvalue="true"/>
</file>
</folder>
</folder>


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:
    @Override
    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[]{
    "Templates/WoW/TocTemplate.toc",
    "Templates/Other/LuaTemplate.lua",
    "Templates/WoW/WoWUIxmlTemplate.xml",
    "Templates/WoW/Template.pkgmeta",
    };

    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