Saturday January 12, 2008
Privileged and Recommended Templates
Let's say you create a new NetBeans module to provide (among other things) file templates to your users. How do you get your file templates to appear in the list that appears when you right-click a project node and choose New? For example, how do you ensure that a template called "AbcTestTemplate.test" appears in that list, as shown below:
Answer: org.netbeans.spi.project.ui.PrivilegedTemplates
You need to implement the above interface and then add it to the project's lookup. Typically, you would add it to the lookup of a project type that you've created yourself. However, as explained in NetBeans Project Type Extension Module Tutorial, you can also extend an existing project type's lookup.
For example, here I've registered an extended lookup in the layer file, for Java SE projects (and I've also registered my file template):
<filesystem>
<folder name="Projects">
<folder name="org-netbeans-modules-java-j2seproject">
<folder name="Lookup">
<file name="org.netbeans.modules.abctesttemplate.LookupProviderImpl.instance"/>
</folder>
</folder>
</folder>
<folder name="Templates">
<folder name="Other">
<file name="AbcTestTemplate.test" url="AbcTestTemplate.test">
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.netbeans.modules.abctesttemplate.Bundle"/>
<attr name="template" boolvalue="true"/>
</file>
</folder>
</folder>
</filesystem>
And here's my lookup provider implementation which, thanks to the layer registration above, only applies to Java SE projects:
public class LookupProviderImpl implements LookupProvider {
public Lookup createAdditionalLookup(Lookup lookup) {
return Lookups.fixed(new PrivilegedTemplatesImpl());
}
}
Above, you can see I added the PrivilegedTemplates implementation to the applicable project type's lookup. Below is the definition of that implementation. Here I promote my template to a privileged template (note that we are dealing with a string array, so you could have many privileged templates):
public final class PrivilegedTemplatesImpl implements PrivilegedTemplates {
private static final String[] PRIVILEGED_NAMES = new String[] {
"Templates/Other/AbcTestTemplate.test",
};
public String[] getPrivilegedTemplates() {
return PRIVILEGED_NAMES;
}
}
And why would you want your file templates to appear in the privileged templates list in the first place? To make them easier to find. That's all. To give them a prominence they wouldn't have if they'd only ended up in the New File wizard. To let your users begin developing the artifacts of your framework, or whatever, without needing to dig around for your templates. (For example, when you install the RESTful Web Services module, you'll see a bunch of new file templates in the privileged list, exactly for this purpose.)
For further reading, see Package org.netbeans.spi.project.ui.templates.support, where you'll also find out about RecommendedTemplates, which determines which file templates are available for a given project, in the New File wizard.
Jan 12 2008, 12:14:27 PM PST Permalink


