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.

Comments:

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.

Posted by Ibon Urrutia on January 12, 2008 at 08:40 PM PST #

Cool, good to hear this was helpful, Ibon!

Posted by Geertjan on January 13, 2008 at 04:08 AM PST #

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

Posted by Geoff Longo on January 13, 2008 at 09:30 AM PST #

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

Posted by silhanek on November 02, 2008 at 03:02 AM PST #

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

Posted by silhanek on November 02, 2008 at 03:34 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« July 2014
SunMonTueWedThuFriSat
  
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today