Download NetBeans!

20081014 Tuesday October 14, 2008

World's Simplest NetBeans Node Implementation

While looking into some small corners of the Nodes API, I realized that this is probably the simplest Nodes implementation. It is perfect for demos and presentations and trainings, etc. Anyone can code this and it is easy to explain what's going on:

class SystemPropertyNode extends Children.Keys {

    @Override
    protected void addNotify() {
        Properties props = System.getProperties();
        setKeys(props.stringPropertyNames());
    }

    @Override
    protected Node[] createNodes(String key) {
        return new Node[]{new PropNode(key)};
    }

    private class PropNode extends AbstractNode {
        private PropNode(String key) {
            super(Children.LEAF, Lookups.fixed());
            setDisplayName(key);
        }
    }

}

Once you've got that, you need this in your view component (which could be a TopComponent or a JFrame):

ExplorerManager.setRootContext(new AbstractNode(new SystemPropertyNode()));

In addition, you'd need to implement ExplorerManager.Provider and add an explorer view. But most of that part can be done via the ui in the IDE. Short, simple, nice demo of Nodes API, ending with you swapping different explorer views around to show that the model is disconnected from the view.

Oct 14 2008, 12:00:00 AM PDT Permalink