Extension to Extended Java Project (Part 2)

I published version 2 of the Extended Java Project Sample. There is one very big improvement: FreeMarker is used. As a result, you can perform iterations and other code logic right within the template, instead of needing to do so in the code. There's also a lot less code in general in the sample now. One reason for that is that I discovered a simpler way of accessing a file inside the module's sources. This is how the WordLibrary.ftl file is located, instead of reading input streams and so on:

URL wordLibraryClass = new URL("nbfs:/SystemFileSystem/Templates/Project/Standard/file/WordLibrary");

The "nbfs" protocol lets you get files from the System FileSystem! You can register the files you need your module to have access to like shown below (register the file anywhere in the System Filesystem, but probably preferable to put it very close to the resources that will be needing it, as in this case, where I put the file very close to the project template that needs it):

<folder name="Templates">
    <folder name="Project">
        <folder name="Standard">
            <file name="MyAnagramGameProject.zip" url="MyAnagramGameProject.zip">
                <attr name="SystemFileSystem.icon" urlvalue="nbresloc:/org/netbeans/modules/java/j2seproject/ui/resources/j2seProject.png"/>
                <attr name="SystemFileSystem.localizingBundle" stringvalue="org.nb.extendedjavaproject.Bundle"/>
                <attr name="instantiatingIterator" methodvalue="org.nb.extendedjavaproject.MyAnagramGameWizardIterator.createIterator"/>
                <attr name="instantiatingWizardURL" urlvalue="nbresloc:/org/nb/extendedjavaproject/MyAnagramGameDescription.html"/>
                <attr name="template" boolvalue="true"/>
            <folder name="file">
                <file name="WordLibrary" url="WordLibrary.ftl">
                    <attr name="template" boolvalue="true"/>
                    <attr name="javax.script.ScriptEngine" stringvalue="freemarker"/>

The layer.xml file and the WordLibrary.ftl file are in the same directory, hence the url attribute above is simply set to WordLibrary.ftl.

And so now I can simply do this:

private FileObject insertSpecifiedWords() throws IOException {

    //Get the folder where the files should be created,
    //note that we don't give the user an option here:
    File projectFolder = FileUtil.normalizeFile((File) wiz.getProperty("projdir"));
    FileObject targetFolder = FileUtil.toFileObject(new File(projectFolder + "/src/com/toy/anagrams/lib"));
    DataFolder df = DataFolder.findFolder(targetFolder);

    //Find the template in the System Filesystem:
    URL wordLibraryClass = new URL("nbfs:/SystemFileSystem/Templates/Project/Standard/file/WordLibrary");

    //Map the template to a FileObject:
    FileObject fo = URLMapper.findFileObject(wordLibraryClass);

    //Convert to a DataObject:
    DataObject dTemplate = DataObject.find(fo);

    //Get the unscrambled words from the panel:
    String[] unscrambleds = new String[]{
        (String) wiz.getProperty("unScrambledWord1"),
        (String) wiz.getProperty("unScrambledWord2"),
        (String) wiz.getProperty("unScrambledWord3"),
        (String) wiz.getProperty("unScrambledWord4"),
        (String) wiz.getProperty("unScrambledWord5"),
        (String) wiz.getProperty("unScrambledWord6"),
        (String) wiz.getProperty("unScrambledWord7")

    //Get the scrambled words from the panel:
    String[] scrambleds = new String[]{
        (String) wiz.getProperty("scrambledWord1"),
        (String) wiz.getProperty("scrambledWord2"),
        (String) wiz.getProperty("scrambledWord3"),
        (String) wiz.getProperty("scrambledWord4"),
        (String) wiz.getProperty("scrambledWord5"),
        (String) wiz.getProperty("scrambledWord6"),
        (String) wiz.getProperty("scrambledWord7")

    //Create a map for passing the words to the FreeMarker template,
    //where it will be processed:
    Map args = new HashMap();
    args.put("unscrambleds", unscrambleds);
    args.put("scrambleds", scrambleds);

    //Create a new DataObject from the template that we defined above,
    //by providing a folder, the name of the file to be created,
    //and the map that should be passed to the FreeMarker template for processing:
    DataObject dobj = dTemplate.createFromTemplate(df, "WordLibrary.java", args);

    //Get the FileObject from the DataObject:
    FileObject createdFile = dobj.getPrimaryFile();

    //Return the FileObject:
    return createdFile;


And then in the instantiate method, I add the above to the result set.

And, thanks to FreeMarker, I can send in my two string arrays via a map, constructed above, which I can then iterate through (using syntax coloring and code completion from my own module which is in the Plugin Portal) in the template:

Pretty cool. But the best trick or, at least, the only one that was news to me, was the thing with the "nbfs" protocol. That provides a very easy way to access files in the System FileSystem, including those that you register there yourself, of course.


Thanks a lot..

Posted by mirc on March 29, 2008 at 09:42 PM PDT #

veryyy goood

Posted by evden eve nakliyat on August 04, 2008 at 08:13 AM PDT #

mirc admin thenks very good mirc

Posted by mirc indir on September 18, 2008 at 01:04 AM PDT #

evden eve nakliyat

Posted by Eczacıoglu Nakliyat on April 14, 2009 at 09:35 PM PDT #

1971den beri istanbul kartalda evden eve nakliyat hizmeti vermekten gürür duyuyoruz.

Posted by evden eve nakliyat gazioglu nakliyat on May 23, 2009 at 11:22 PM PDT #

I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon

Posted by mirc sohbet on February 21, 2012 at 06:14 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed

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.


« July 2016