Thursday Aug 21, 2008

Create Lazy Children with NetBeans Platform 6.5

The NetBeans System Properties Module Tutorial uses Children.Keys to create the children of the node. There are two other (better) ways of doing this instead:
  • org.openide.nodes.ChildFactory. Use when creation of nodes would otherwise take a long time, because it creates them asynchronously. To experiment with it, replace the "AllPropsChildren" class in the tutorial with the following:

    public class AllPropsChildren extends ChildFactory<String> {
    
        @Override
        protected boolean createKeys(List<String> keys) {
            Properties p = System.getProperties();
            Enumeration e = p.propertyNames();
            while (e.hasMoreElements()) {
                keys.add(e.nextElement().toString());
            }
            Collections.sort(keys);
            return true;
        }
    
        @Override
        protected Node createNodeForKey(String key) {
            return new OnePropNode(key);
        }
          
    }

    Read the ChildFactory Javadoc, as well as this example for details.

  • org.openide.nodes.Children.Keys(boolean lazy). From 6.5 Beta onwards (as explained here in the API Changes document), children can be created lazily. I.e., say you have +1000 nodes that need to be created. If the children are lazy, they will only be created as the user makes them visible by scrolling in the explorer view. Until that point, they're not created. The only difference between using the standard org.openide.nodes.Children.Keys and the lazy version is to pass in the boolean true via the constructor (i.e., you're saying 'yes I want my children to be lazy'). This approach is/will be used throughout the IDE to speed up performance and is, thanks to the API, also possible to use in your own implementations.

So, when deciding to use one or the other, ask yourself this: "Will I need to create +1000 nodes?" If your answer is "yes", create the children lazily by passing in true to Children.Keys. If your answer is "no", then use ChildFactory instead, particularly if you anticipate a lot of processing time being required, because ChildFactory will create the children asynchronously.

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
« August 2008 »
SunMonTueWedThuFriSat
     
2
4
5
6
7
11
14
17
20
23
27
29
30
31
      
Today