File-Based Document-Centric NetBeans Platform Application

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).

Comments:

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.

Posted by Gabriele on August 08, 2010 at 07:51 PM PDT #

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
  
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today