X

Geertjan's Blog

  • March 15, 2010

File-Based Document-Centric NetBeans Platform Application

Geertjan Wielenga
Product Manager
Not all document-centric applications need a full-blown project system. So here is a small application that lets you create files. The files could open in a multiview editor, of course, but for sake of argument, let's simply open them in a plain text editor.

In the end, you'll be able to open the New File dialog:

...and choose the Letter wizard, set some properties:

...which is then generated (via FreeMarker) into your template:

To attain the above result, use the Wizard wizard to create a one panel file wizard. Design the panel as shown above. Then use the following content in your "instantiate" method within the iterator class:

@SuppressWarnings(value = "unchecked")
public Set instantiate() throws IOException {
DataObject activatedDObj = WindowManager.getDefault().getRegistry().getActivated().getLookup().lookup(DataObject.class);
String targetName = wizard.getProperty(LetterVisualPanel1.NAME).toString();
DataFolder df = DataFolder.findFolder(activatedDObj.getPrimaryFile());
Object[] data = (Object[]) wizard.getProperty(LetterVisualPanel1.PRODUCTS.toString());
HashMap hashMap = new HashMap();
hashMap.put("url", wizard.getProperty(LetterVisualPanel1.URL));
hashMap.put("name", targetName);
hashMap.put("types", data);
FileObject template = FileUtil.getConfigFile("Templates/Letters/letter.letter");
DataObject dTemplate = DataObject.find(template);
DataObject dobj = dTemplate.createFromTemplate(df, targetName, hashMap);
FileObject createdFile = dobj.getPrimaryFile();
return Collections.singleton(createdFile);
}

Read about the DataObject.createFromTemplate here, as well as about how to register the template in the layer file:

<folder name="Templates">
<folder name="Letters">
<file name="letter.letter" url="letter.ftl">
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.car.templates.Bundle"/>
<attr name="displayName" bundlevalue="org.car.templates.Bundle#Templates/Letters/letter.letter"/>
<attr name="instantiatingIterator" newvalue="org.car.templates.LetterWizardIterator"/>
<attr name="template" boolvalue="true"/>
<attr name="javax.script.ScriptEngine" stringvalue="freemarker"/>
<attr name="instantiatingWizardURL" urlvalue="nbresloc:org/car/templates/letterWizardDescription.html"/>
</file>
</folder>
</folder>

And here's the related "letter.ftl" file referred to above:

<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>
Welcome ${name}<#if name = "Tom">, you wonderful person!</#if>!
</h1>
<p>URL: <a href="${url}">${url}</a>
<table border=1>
<tr><th>Chosen Products<th></tr>
<#list types as product>
<tr><td>${product}</td></tr>
</#list>
</table>
</body>
</html>

To let FreeMarker process the above file, you need the "javax.script.ScriptEngine" element in your layer, as shown above, together with the following line in your manifest:

OpenIDE-Module-Needs: javax.script.ScriptEngine.freemarker

Then set dependencies on the DataSystems API, as well as the FreeMarker Integration module (until 6.9, after which you won't need this at all), which is within the "ide" cluster.

And now you're done. Simply brand the Favorites window as shown elsewhere and then you have a file-based document-centric application, i.e., you're not using any project-related APIs (which would have added a lot of superfluous features in the context of this small file-based application).

Join the discussion

Comments ( 1 )
  • Gabriele Monday, August 9, 2010

    It would be great if you specify which modules are needed from ide cluster. Including all of it provides many many not needed features, and make it difficult to track bugs (I'm having a heapOutOfSpace I suspect it's not my fault, but ...).

    It looks like some of these are what one needs:

    core.ide,\\

    editor,\\

    editor.actions,\\

    editor.errorstripe,\\

    editor.errorstripe.api,\\

    editor.fold,\\

    editor.indent,\\

    editor.lib,\\

    editor.lib2,\\

    editor.settings,\\

    editor.settings.storage,\\

    editor.util

    but I'm having difficulties, with friend dependencies, especially with maven.


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