A child of five would understand this. Send someone to fetch a child of five.
In the previous section we've learned how to asynchronously create a Node with Children by using a ChildFactory. In this section we'll learn how to create a Node when its Children are known in advance.
In our sample application we don't need synchronous children, so the code below is not real. Think of the code in this section as an exercise for you: add multiple-query support to the sample application
Let's imagine we have a list of Query entities and that we want these to be the children of a "QueryListNode" we're building.
Of course we can use the ChildFactory method we saw in the previous section to build the Children of our QueryListNode. We could pass it a 'false' argument to the Children.create call to make thins synchronous.
But the NetBeans Platform offers another way to create the children object synchronously: instead of using a ChildFactory you could use the Children.Keys class. You basically extend this class like so:
public class QueryListChildren extends Children.Keys<Query> { private List<Query> queries; public QueryListChildren( List<Query> queries ) { super(); this.queries = queries; }
Then you have to override two other methods: addNotify and removeNotify. In these methods you specify the list of entities by using the setKeys method, like so:
@Override protected void addNotify() { super.addNotify(); // Set the set of entities that this node represents setKeys( queries ); } @Override protected void removeNotify() { // Use an empty set to clean up // the list of entities that this node represents setKeys( Collections.<Query>emptySet() ); super.removeNotify(); }
After that you have to tell the NetBeans Platform what kind of nodes you want to use to represent your child entitites. You do this by overriding the createNodes method, like so:
@Override protected Node[] createNodes( Query query ) { return new Node[] { new QueryNode( query ) }; }
Finally you associate your parent node (the QueryListNode) with its children in its constructor, like this.
public QueryListNode extends AbstractNode { private List<Query> entities; public QueryListNode( List<Query> entities ) { super( new QueryListChildren(entities) ); this.entities = entities; }
Simple, isn't it? If you want to see real-code examples of Children.Keys you can browse the results of this Google query
In this section we've quickly seen how to use a Children.Keys base object to represent the children of a node for cases where the child entities are known in advance. We could also have used a ChildFactory as in the previous section.
In the next section we'll quickly talk about ExplorerManagers and will then proceed with Swing UI fun. Patience!
blog comments powered by Disqus