Geertjan's Blog

  • January 12, 2008

Privileged and Recommended Templates

Geertjan Wielenga
Product Manager
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):

<folder name="Projects">
<folder name="org-netbeans-modules-java-j2seproject">
<folder name="Lookup">
<file name="org.netbeans.modules.abctesttemplate.LookupProviderImpl.instance"/>
<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"/>

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[] {
public String[] getPrivilegedTemplates() {

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.

Join the discussion

Comments ( 5 )
  • Ibon Urrutia Sunday, January 13, 2008

    Yes!! This was exactly what I needed recently. Thank you very much!! I thought that it will be more complex to do, but it's trivial.

  • Geertjan Sunday, January 13, 2008

    Cool, good to hear this was helpful, Ibon!

  • Geoff Longo Sunday, January 13, 2008

    Thanks again for the great tips. Your blog has been unbelievably helpful to me. Keep 'em coming.

  • silhanek Sunday, November 2, 2008

    Hi, why do you recomend way through additional lookup ? Declaration in layer.xml Templates/Privileged is used in ToDo List Manager sample suite from Tom Wheeler.

    Is your way recomended for NB 6.x ?

    Thanks. Milos

  • silhanek Sunday, November 2, 2008

    It is Lookup for Project folder. I have application on Platform. It does not need Project modules.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.