Download NetBeans!

20071002 Tuesday October 02, 2007

FreeMarker: Baked into NetBeans IDE 6.0 (Part 3)

Talking about templates again... in recent blog entries I've written about how the NetBeans module author can send values from an instantiate() method to fill a FreeMarker template's variables. The new method DataObject.createFromTemplate(targetFolder, targetName, mapWithParameters) makes this possible. So, by using that method, you will not be limited to the default variables that are available to FreeMarker templates in NetBeans IDE. These default variables are the following:

  • date
  • encoding
  • name
  • nameAndExt
  • time
  • user

The above can be used in FreeMarker templates as is, i.e., without your needing to define a map with these values. So you could put ${nameAndExt} in your FreeMarker template and then, when the file is ultimately generated, i.e., from that template, the name and extension of your file will appear in your file.

Let's now turn our gaze away from the NetBeans module author. Let's now wear the hat of the NetBeans user. The NetBeans user has the Template Manager under the Tools menu. Some of the templates in there are defined in FreeMarker. For example, this is the FreeMarker template for the Java interface:

<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">

<#if package?? && package != "">
package ${package};

</#if>
/**
 *
 * @author ${user}
 */
public interface ${name} {

}

So, the NetBeans user is able to use any of the items in the list above as variables in their templates. They can open the template in the editor and then they can change the template and save it. Then the rewritten template will be used next time they use the wizard that makes use of that template. So, just like the NetBeans module author, the list of items above is available to the NetBeans user.

"So," you might think. "The NetBeans module author can do a lot more. After all, the NetBeans module author can define new variables, such as what was done in FreeMarker: Baked into NetBeans IDE 6.0 (Part 2). In other words, while the NetBeans module author can create new variables in the FreeMarker template, the user is unable to do so, being restricted to those that are in the list above."

And the answer to the above is: "Not so!" It took a while to get to this point of the story, but I thought it might be necessary. The point is that the user, since the last few days, thanks to Jarda Tulach, can go to this place in the Template Manager:

When you open that file, you will see this, exactly this:

#
# Here, or in other properties files in this directory, you can define
# various properties that you want to make available to the template
# processor while creating various templates.
#

# uncomment the next line and specify your user name to be used in new templates
#user=Your Name <your.name at your.org>

Now... the NetBeans user can add properties here (or override existing ones, such as "user"). So, for example, let's add this line:

greeting=hello world

And now, in any of the FreeMarker templates in the Template Manager, I (as the NetBeans user) can put ${greeting}. Then, when I choose the wizard that makes use of that FreeMarker template, all instances of ${greeting} will be replaced with "hello world". In this way, the user has complete control over the FreeMarker templates in the Template Manager. I think this is pretty cool.

In other news. Check out yet another Schliemann implementation, here in Henry Story's blog. He helpfully even includes the code. Thanks a lot!

Oct 02 2007, 01:34:22 PM PDT Permalink

Trackback URL: http://blogs.sun.com/geertjan/entry/freemarker_baked_into_netbeans_ide2
Comments:

ggf

Posted by 210.78.54.213 on October 02, 2007 at 06:40 PM PDT #

I have just seen this post, and thank god for you Geertjan!

My question is, where does this come into play?
<#include "../Licenses/license-${project.license}.txt">

I can't find it anywhere?

thanks,
Ror

Posted by Roridge on November 22, 2007 at 10:18 AM PST #

Hi Roridge, I keep meaning to investigate that specific question, but haven't got round to it yet. I hope to blog about it soon. (Feel free to ping me if I keep on forgetting.) I guess it has something to do with licenses, than one can set a license somewhere and then use the license across all templates. That's basically my understanding but, again, I will try to track down the answer soon. Thanks for the question and the encouragement.

Posted by Geertjan on November 22, 2007 at 10:32 AM PST #

Thanks G

I have tried to put files in places to get them to be put in the top as headers, but so far all I get is errors.

Keep up the good work.

Ror

Posted by Roridge on November 23, 2007 at 01:19 AM PST #

As a further update, this works great, although I can't work out how to auto generate the Year (rather than just the date).

Also the User Preferences do not work in the UML code generation template, is this a bug or a feature request?

Ror

Posted by Roridge on November 23, 2007 at 03:23 AM PST #

If that doesn't work, I would say it is a bug and I would file an issue in Issuezilla, describing the steps to take. Other questions you ask I don't know how to answer right now, but will do in the coming days.

Posted by Geertjan on November 23, 2007 at 03:25 AM PST #

Hi Roridge, for info on the license statement in the FreeMarker templates, see this blog entry:
http://blogs.sun.com/geertjan/date/20071126

Posted by Geertjan on November 26, 2007 at 09:03 AM PST #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed