Monday Oct 13, 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.

How Users Can Let Children Move Up & Move Down (Part 1)

"Move up" and "Move down" support for nodes is provided by the NetBeans Platform, as described in this "NetBeans Definitive Guide" page on-line. I tried to get it to work for some of my child nodes today and managed to get it working, with Jarda's help. The menu items are enabled, if I select one child node at a time, and they work as one would expect them to:

Let's say I define my children like this:

public class PropChildren extends Index.ArrayChildren {

    @Override
    protected java.util.List initCollection() {
        ArrayList childrenNodes = new ArrayList();
        Properties p = System.getProperties();
        Enumeration e = p.keys();
        while (e.hasMoreElements()) {
            childrenNodes.add(new PropNode(e.nextElement().toString()));
        }
        return childrenNodes;
    }

    public class PropNode extends AbstractNode {

        public PropNode(String key) {
            super(Children.LEAF, Lookups.fixed());
            setDisplayName(key);
        }

        //You need to add the 'MoveUpAction' and 'MoveDownAction',
        //which are part of the Actions API:
        @Override
        public Action[] getActions(boolean popup) {
            return new Action[]{
                        SystemAction.get(MoveUpAction.class),
                        SystemAction.get(MoveDownAction.class)
                    };
        }
    }
    
}

For the above, you need the following in the TopComponent's constructor (of course, the TopComponent implements ExplorerManager.Provider and has an explorer view added to it):

//Create an instance of the children class:
PropChildren prop = new PropChildren();

//Add the map of actions to the TopComponent's Lookup:
associateLookup(ExplorerUtils.createLookup(explorerManager, getActionMap()));

//Create the children and
//include an implementation of the Index class in the Lookup of the root node.
//Since our children class above is such an implementation, we'll include that:
explorerManager.setRootContext(new AbstractNode(prop, Lookups.singleton(prop)));

//Set the display name of the root node:
explorerManager.getRootContext().setDisplayName("Properties");

The key to the above is that I have extended Index.ArrayChildren, which provides me with reorderable children out of the box (but only if I include the class in the Lookup of the root node, as shown above). If you're using Children.Keys or ChildFactory, the children class could implement Index. Or you could create a new class that extends Index.Support, which should then be added to the Lookup as shown above. But then you would have to handle the reordering yourself, unlike with Index.ArrayChildren which, as can be seen above, gives you that functionality for free.

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
« October 2008 »
SunMonTueWedThuFriSat
   
2
7
8
11
12
20
24
25
27
31
 
       
Today