Geertjan's Blog

  • March 30, 2008

Extension to Extended Java Project (Part 2)

Geertjan Wielenga
Product Manager
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"/>
</file><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.

Join the discussion

Comments ( 6 )
  • mirc Sunday, March 30, 2008

    Thanks a lot..

  • evden eve nakliyat Monday, August 4, 2008

    veryyy goood

  • mirc indir Thursday, September 18, 2008

    mirc admin thenks very good mirc

  • Eczac&#305;oglu Nakliyat Wednesday, April 15, 2009

    evden eve nakliyat

  • evden eve nakliyat gazioglu nakliyat Sunday, May 24, 2009

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


  • mirc sohbet Tuesday, February 21, 2012

    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

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