Friday Mar 01, 2013

No Expansion Icon When No Children (Part 1)

Sometimes you have Nodes with an expansion icon, i.e., the "plus" sign, even though the Object doesn't have Children. Once the user tries to expand the Node, the "plus" sign disappears, and no Children are shown. Kind of misleading because the user thought that there would be Children because the expansion icon was shown. Would be nicer if there were to be no expansion icon if the Object has no Children.

Here's how to solve that, via Children.createLazy:

public class MyNode extends AbstractNode {

    public MyNode(NodeKey key) {
        super(Children.createLazy(new MyCallable(key)), Lookups.singleton(key));
        setDisplayName(key.toString());
    }

    private static class MyCallable implements Callable<Children> {

        private final NodeKey key;

        private MyCallable(NodeKey key) {
            this.key = key;
        }

        @Override
        public Children call() throws Exception {
            //Check, somehow, that your key has children,
            //e.g., create "hasChildren" on the object
            //to look in the database to see whether
            //the object has children;
            //if it doesn't have children, return a leaf:
            if (!key.hasChildren()) {
                return Children.LEAF;
            } else {
                return Children.create(new MyChildFactory(key), true);
            }
        }

    }

}

Now see part 2.

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
« March 2013 »
SunMonTueWedThuFriSat
     
10
11
12
13
14
15
18
23
24
27
28
      
Today