Take your time when making Children…

Netbeans helps you with that. Have a look at this video of how NetBeans 7.2 displays Nodes, when you create your Children lazily. Details are below:

The NetBeans Nodes API is great for displaying Domain Objects in the UI. You define your Object hierarchy once and you’re free to display them in a number of specialized Swing Components called ExplorerViews. And when you follow the recommended path, you’ll use ChildFactory for creating the layers of your hierarchy. This helps to create Children lazily (when they’re about to be displayed by the UI) and asynchronously (so your UI stays responsive even if it takes some time to retrieve/create your Domain Objects).

Here’s the sample code, that creates the effect shown above. Add it to the constructor of a “New” -> “Window”. To run the code also make sure your Window “implements ExplorerManager.Provider“, has a member “ExplorerManager em = new ExplorerManager();” and returns it from “public Explorermanager getExplorermanager()“:

        setLayout(new BorderLayout());
        add(new BeanTreeView(), BorderLayout.CENTER);
        AbstractNode root = new AbstractNode(Children.create(new ChildFactory<String>() {
            @Override
            protected boolean createKeys(List<String> toPopulate) {
                try {
                    Thread.sleep(1000);
                    toPopulate.add("Tom");
                    Thread.sleep(1000);
                    toPopulate.add("Dick");
                    Thread.sleep(1000);
                    toPopulate.add("Harry");
                    Thread.sleep(1000);
                    toPopulate.add("Sally");
                    Thread.sleep(1000);
                    toPopulate.add("Carlos");
                    Thread.sleep(1000);
                    toPopulate.add("Frank");
                    Thread.sleep(1000);
                    toPopulate.add("Peter");
                    Thread.sleep(1000);
                    toPopulate.add("Toni");
                } catch (InterruptedException ex) {
                    Exceptions.printStackTrace(ex);
                }
                return true;
            }

            @Override
            protected Node createNodeForKey(String key) {
                AbstractNode node = new AbstractNode(Children.LEAF);
                node.setName(key);
                return node;
            }
        }, true));
        em.setRootContext(root)

You see that Node creation requires two methods, one for creating the Domain Objects called “createKeys“, which can run in a background thread, and one for creating the Node required for displaying them in the UI called “createNodeForKey(s)“, which runs in EDT. While the keys are loaded in the background thread a configurable “WaitNode” is displayed indicating there’s work going on in the background. As soon as all the Domain Objects are there the real Node would be created and all at once displayed in the UI. That’s nice, but sometimes, especially when it takes a long time to create all of your domain objects, you would like to have them appear in the UI one by one, as soon as they’re created.

Up to NetBeans 7.2 this was done by adding some logic to the “createKeys” method to always create only one key at a time, put it in the list you get as a call parameter and then return false, indicating “no this weren’t all keys, call me again to get more” until the list is complete.

Now in 7.2 you no longer need to do that. NetBeans Platform will observe the list it gives you and create Nodes for each key as soon as you add it there. So the Nodes will be created for you, even before the call to createKeys returns.

Really cool feature. For the full project go to https://github.com/eppleton/netbeans-examples.

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>