Children.Keys for Presenting Layer Folders & Files

Here's what's in my layer.xml:
<folder name="Words">
    <file name="ajva">
        <attr name="unscrambled" stringvalue="java"/>
    </file>
    <file name="ynamicd">
        <attr name="unscrambled" stringvalue="dynamic"/>
    </file>
</folder>

And here's a Children.Keys class for creating a presentation layer on top of the folders and files above:

public class WordChildren extends Children.Keys<String> {

    @Override
    protected void addNotify() {
        FileObject words = Repository.getDefault().getDefaultFileSystem().findResource("Words");
        FileObject[] fos = words.getChildren();
        String[] s = new String[fos.length];
        for (int i = 0; i < s.length; i++) {
            s[i] = fos[i].getName() + " (" + fos[i].getAttribute("unscrambled") + ")";
        }
        setKeys(s);
    }

    @Override
    protected Node[] createNodes(String name) {
        AbstractNode WordChildrenNode = new AbstractNode(Children.LEAF) {
            @Override
            public String getHtmlDisplayName() {
                String normal = getDisplayName().substring(0, getDisplayName().indexOf("("));
                String greyed = getDisplayName().substring(getDisplayName().indexOf("("));
                return "<b>" + normal + "</b><i><font color=\\"#808080\\">" + greyed + "</font></i>";
            }
        };
        WordChildrenNode.setDisplayName(name);
        return new Node[]{WordChildrenNode};
    }

}

Here's the result, when you instantiate the above via an Explorer Manager, while an explorer view is available to it:

One of the cool things about putting your data in the layer.xml is that then other modules can add their own data to those same folders and they can even be ordered relative to each other. Then the code above will load that data, which is why there are FOUR word combinations in the screenshot above, because the NetBeans runtime container merges all the layer.xml files into one single hierarchical filesystem. Hurray for the NetBeans runtime container!

Comments:

Of course you might prefer to use Children.Keys<FileObject> and delay the calculation of the name until createNodes is called. Especially useful if you call super(true) so that nodes are not created until they become visible in a scroll viewport.

Posted by Jesse Glick on November 21, 2008 at 06:29 AM PST #

You are genius!

Posted by Varun on November 22, 2008 at 02:50 AM PST #

Great tip, this advice is going to save me a lot of hours! Thank you very much.

Posted by JJ on November 23, 2008 at 03:45 AM PST #

Hello, nice tip. One question about it if you don't mind:

Isn't Node#addNotify called in AWT Thread, thus it's "better" not to do an IO in there. Something like:

<code>
@Override
protected void addNotify() {
final SwingWorker worker = new SwingWorker<List<String>, Void>() {

@Override
protected List<String> doInBackground() throws Exception {
final FileObject words = Repository.getDefault().getDefaultFileSystem().findResource("Words");
final FileObject[] fos = words.getChildren();
final List<String> result = new ArrayList<String>(fos.length);
for (FileObject fo : fos) {
result.add(fo.getName() + " (" + fo.getAttribute("unscrambled") + ")");

}
return result;
}

@Override
protected void done() {
try {
setKeys(get());
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
}

};
worker.execute();
}
</code>

Posted by Adam Skalny on November 26, 2008 at 09:16 PM PST #

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
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today