blob: a2b1dd3499f8a9ad25995e1691c9bfec50360abe [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqNodesChildFactory
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqNodesChildFactory
:description: Apache NetBeans wiki DevFaqNodesChildFactory
:toc: left
:toc-title:
:syntax: true
=== Computing Node Children Asynchronously
If you have a Node that needs to provide child Nodes, and computing the objects the child nodes represent is slow or expensive (i.e. you need to parse a file, connect to a database, or do some sort of I/O), you do not want to compute the child nodes in the event thread (which is what happens by default).
NetBeans 6.0 introduces `org.openide.nodes.ChildFactory` and `Children.create(ChildFactory factory, boolean asynchronous)`. Simply subclass `ChildFactory` and implement `protected boolean createKeys(List <T> toPopulate)` to build the list of objects that will be represented by the child nodes. Implement `protected Node createNodeForKey(T key)` to create a Node - it will be passed each object in the list of child objects. `createKeys` will be called on a background thread.
Typically you'll want to make the model object from `createKeys` available on the Node you create. So a simple implementation of `createNodeForKey` would look something like:
[source,java]
----
protected Node createNodeForKey(T key) {
AbstractNode result = new AbstractNode (Children.LEAF, Lookups.singleton (key));
result.setDisplayName (key.getName()); //or whatever
result.setIcon (Utilities.loadImage ("path/in/jar/to/image.gif"));
return result;
}
----
ChildFactory can also simplify creating Nodes synchronously, and has the convenience that by using generics, your code can be type safe with respect to key objects. Generally it can be used anywhere `Children.Keys` would be used (it uses `Children.Keys` under the hood).
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqNodesChildFactory[http://wiki.netbeans.org/DevFaqNodesChildFactory] ,
that was last modified by NetBeans user Jtulach
on 2010-07-24T18:45:00Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.