KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > ChildrenArray


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.nodes;
20
21 import java.lang.ref.Reference JavaDoc;
22 import java.util.*;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25
26
27 /** Holder of nodes for a children object. Communicates
28 * with children to notify when created/finalized.
29 *
30 * @author Jaroslav Tulach
31 */

32 final class ChildrenArray extends NodeAdapter {
33     /** children */
34     public Children children;
35
36     /** nodes associated */
37     private Node[] nodes;
38
39     private Map<Children.Info,Collection<Node>> map;
40     /** the reference that points to us */
41     private Reference JavaDoc<ChildrenArray> ref;
42
43     private static final Logger JavaDoc LOG_NODES_FOR = Logger.getLogger(
44             "org.openide.nodes.ChildrenArray.nodesFor"); // NOI18N
45

46     /** Creates new ChildrenArray */
47     public ChildrenArray() {
48     }
49
50     public Children getChildren() {
51         return children;
52     }
53
54     /** When finalized notify the children.
55     */

56     protected void finalize() {
57         children.finalizedChildrenArray(ref);
58     }
59     
60     /** Now points to me */
61     final void pointedBy(Reference JavaDoc<ChildrenArray> ref) {
62         this.ref = ref;
63     }
64
65     /** Getter method to receive a set of computed nodes.
66     */

67     public Node[] nodes() {
68         if (children == null) {
69             // not fully initialize
70
return null;
71         }
72
73         if (nodes == null) {
74             nodes = children.justComputeNodes();
75
76             for (int i = 0; i < nodes.length; i++) {
77                 // keeps a hard reference from the children node to this
78
// so we can be GCed only when child nodes are gone
79
nodes[i].reassignTo(children, this);
80             }
81
82             // if at least one node => be weak
83
children.registerChildrenArray(this, nodes.length > 0);
84         }
85
86         return nodes;
87     }
88
89     /** Clears the array of nodes.
90     */

91     public void clear() {
92         if (nodes != null) {
93             nodes = null;
94
95             // register in the childrens to be hold by hard reference
96
// because we keep no reference to nodes, we can be
97
// hard holded by children
98
children.registerChildrenArray(this, false);
99         }
100     }
101
102     /** Finalizes nodes by calling get on weak hash map,
103     * all references stored in the map, that are finalized
104     * will be cleared.
105     */

106     public void finalizeNodes() {
107         Map m = map;
108
109         if (m != null) {
110             // processes the queue of garbage
111
// collected keys
112
m.remove(null);
113         }
114     }
115
116     /** Initilized if has some nodes.
117     */

118     public boolean isInitialized() {
119         return nodes != null;
120     }
121
122     private String JavaDoc logInfo(Children.Info info) {
123         return info.toString() + '[' + Integer.toHexString(System.identityHashCode(info)) + ']';
124     }
125
126     /** Gets the nodes for given info.
127     * @param info the info
128     * @return the nodes
129     */

130     public synchronized Collection<Node> nodesFor(Children.Info info) {
131         final boolean IS_LOG = LOG_NODES_FOR.isLoggable(Level.FINE);
132         if (IS_LOG) {
133             LOG_NODES_FOR.fine("nodesFor(" +logInfo(info) + ") on " + Thread.currentThread()); // NOI18N
134
}
135         if (map == null) {
136             map = new WeakHashMap<Children.Info,Collection<Node>>(7);
137         }
138         Collection<Node> nodes = map.get(info);
139
140         if (IS_LOG) {
141             LOG_NODES_FOR.fine(" map size=" + map.size() + ", nodes=" + nodes); // NOI18N
142
}
143
144         if (nodes == null) {
145             nodes = info.entry.nodes();
146             info.length = nodes.size();
147             map.put(info, nodes);
148             if (IS_LOG) {
149                 LOG_NODES_FOR.fine(" created nodes=" + nodes); // NOI18N
150
}
151         }
152
153         if (IS_LOG) {
154             LOG_NODES_FOR.fine(" leaving nodesFor(" +logInfo(info) + ") on " + Thread.currentThread()); // NOI18N
155
}
156         return nodes;
157     }
158
159     /** Refreshes the nodes for given info.
160     * @param info the info
161     * @return the nodes
162     */

163     public synchronized void useNodes(Children.Info info, Collection<Node> list) {
164         if (map == null) {
165             map = new WeakHashMap<Children.Info,Collection<Node>>(7);
166         }
167         
168         info.length = list.size();
169
170         map.put(info, list);
171     }
172
173     public String JavaDoc toString() {
174         return super.toString() + " " + getChildren(); //NOI18N
175
}
176 }
177
Popular Tags