KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > viewmodel > TreeModelRoot


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
20 package org.netbeans.modules.viewmodel;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.WeakHashMap JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import org.netbeans.spi.viewmodel.Models;
28
29 import org.netbeans.spi.viewmodel.TreeModel;
30 import org.netbeans.spi.viewmodel.ModelEvent;
31 import org.netbeans.spi.viewmodel.ModelListener;
32
33 import org.openide.nodes.Node;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.RequestProcessor;
36
37
38 /**
39  * Implements root node of hierarchy created for given TreeModel.
40  *
41  * @author Jan Jancura
42  */

43 public class TreeModelRoot implements ModelListener {
44     /** generated Serialized Version UID */
45     static final long serialVersionUID = -1259352660663524178L;
46
47     
48     // variables ...............................................................
49

50     private Models.CompoundModel model;
51     private TreeModelNode rootNode;
52     private WeakHashMap JavaDoc objectToNode = new WeakHashMap JavaDoc ();
53     private TreeTable treeTable;
54     
55     /** The children evaluator for view if this root. */
56     private TreeModelNode.LazyEvaluator childrenEvaluator;
57     /** The values evaluator for view if this root. */
58     private TreeModelNode.LazyEvaluator valuesEvaluator;
59
60
61     public TreeModelRoot (Models.CompoundModel model, TreeTable treeTable) {
62         this.model = model;
63         this.treeTable = treeTable;
64         model.addModelListener (this);
65     }
66     
67     public TreeTable getTreeTable () {
68         return treeTable;
69     }
70
71     public TreeModelNode getRootNode () {
72         if (rootNode == null)
73             rootNode = new TreeModelNode (model, this, model.getRoot ());
74         return rootNode;
75     }
76     
77     void registerNode (Object JavaDoc o, TreeModelNode n) {
78         objectToNode.put (o, new WeakReference JavaDoc (n));
79     }
80     
81     TreeModelNode findNode (Object JavaDoc o) {
82         WeakReference JavaDoc wr = (WeakReference JavaDoc) objectToNode.get (o);
83         if (wr == null) return null;
84         return (TreeModelNode) wr.get ();
85     }
86     
87     public void modelChanged (final ModelEvent event) {
88         SwingUtilities.invokeLater (new Runnable JavaDoc () {
89             public void run () {
90                 if (model == null)
91                     return; // already disposed
92
if (event instanceof ModelEvent.TableValueChanged) {
93                     ModelEvent.TableValueChanged tvEvent = (ModelEvent.TableValueChanged) event;
94                     Object JavaDoc node = tvEvent.getNode();
95                     if (node != null) {
96                         TreeModelNode tmNode = findNode(node);
97                         if (tmNode != null) {
98                             String JavaDoc column = tvEvent.getColumnID();
99                             if (column != null) {
100                                 tmNode.refreshColumn(column);
101                             } else {
102                                 tmNode.refresh();
103                             }
104                             return ; // We're done
105
}
106                     }
107                 }
108                 if (event instanceof ModelEvent.NodeChanged) {
109                     ModelEvent.NodeChanged nchEvent = (ModelEvent.NodeChanged) event;
110                     Object JavaDoc node = nchEvent.getNode();
111                     if (node != null) {
112                         TreeModelNode tmNode = findNode(node);
113                         if (tmNode != null) {
114                             tmNode.refresh(nchEvent.getChange());
115                             return ; // We're done
116
}
117                     }
118                 }
119                 rootNode.setObject (model.getRoot ());
120             }
121         });
122 // Iterator i = new HashSet (objectToNode.values ()).iterator ();
123
// while (i.hasNext ()) {
124
// WeakReference wr = (WeakReference) i.next ();
125
// if (wr == null) continue;
126
// TreeModelNode it = (TreeModelNode) wr.get ();
127
// if (it != null)
128
// it.refresh ();
129
// }
130
}
131     
132 // public void treeNodeChanged (Object parent) {
133
// final TreeModelNode tmn = findNode (parent);
134
// if (tmn == null) return;
135
// SwingUtilities.invokeLater (new Runnable () {
136
// public void run () {
137
// tmn.refresh ();
138
// }
139
// });
140
// }
141

142     synchronized TreeModelNode.LazyEvaluator getChildrenEvaluator() {
143         if (childrenEvaluator == null) {
144             childrenEvaluator = new TreeModelNode.LazyEvaluator();
145         }
146         return childrenEvaluator;
147     }
148
149     synchronized TreeModelNode.LazyEvaluator getValuesEvaluator() {
150         if (valuesEvaluator == null) {
151             valuesEvaluator = new TreeModelNode.LazyEvaluator();
152         }
153         return valuesEvaluator;
154     }
155
156     public void destroy () {
157         if (model != null)
158             model.removeModelListener (this);
159         model = null;
160         objectToNode = new WeakHashMap JavaDoc ();
161     }
162 }
163
164
Popular Tags