KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > NodeTreeModel


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.explorer.view;
20
21 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
22 import org.openide.nodes.Node;
23 import org.openide.util.*;
24
25 import java.beans.*;
26
27 import java.lang.ref.Reference JavaDoc;
28 import java.lang.ref.WeakReference JavaDoc;
29
30 import javax.swing.*;
31 import javax.swing.event.*;
32 import javax.swing.tree.*;
33
34
35 /** Model for displaying the nodes in tree.
36 *
37 * @author Jaroslav Tulach
38 */

39 public class NodeTreeModel extends DefaultTreeModel {
40     static final long serialVersionUID = 1900670294524747212L;
41
42     /** listener used to listen to changes in trees */
43     private transient Listener listener;
44
45     // Workaround for JDK issue 6472844 (NB #84970)
46
// second part is in the listener and third in the TreeView
47
private CopyOnWriteArrayList JavaDoc<TreeView> views = new CopyOnWriteArrayList JavaDoc<TreeView>();
48     void addView(TreeView tw) {
49         views.add(tw);
50     }
51     
52     /** Creates new NodeTreeModel
53     */

54     public NodeTreeModel() {
55         super(VisualizerNode.EMPTY, true);
56     }
57
58     /** Creates new NodeTreeModel
59     * @param root the root of the model
60     */

61     public NodeTreeModel(Node root) {
62         super(VisualizerNode.EMPTY, true);
63         setNode(root);
64     }
65
66     /** Changes the root of the model. This is thread safe method.
67     * @param root the root of the model
68     */

69     public void setNode(final Node root) {
70         Mutex.EVENT.readAccess(
71             new Runnable JavaDoc() {
72                 public void run() {
73                     VisualizerNode v = (VisualizerNode) getRoot();
74                     VisualizerNode nr = VisualizerNode.getVisualizer(null, root);
75
76                     if (v == nr) {
77                         // no change
78
return;
79                     }
80
81                     v.removeNodeModel(listener());
82
83                     nr.addNodeModel(listener());
84                     setRoot(nr);
85                 }
86             }
87         );
88     }
89
90     /** Getter for the listener. Only from AWT-QUEUE.
91     */

92     private Listener listener() {
93         if (listener == null) {
94             listener = new Listener(this);
95         }
96
97         return listener;
98     }
99
100     /**
101     * This sets the user object of the TreeNode identified by path
102     * and posts a node changed. If you use custom user objects in
103     * the TreeModel you'returngoing to need to subclass this and
104     * set the user object of the changed node to something meaningful.
105     */

106     public void valueForPathChanged(TreePath path, Object JavaDoc newValue) {
107         if (path == null) {
108             return;
109         }
110
111         Object JavaDoc o = path.getLastPathComponent();
112
113         if (o instanceof VisualizerNode) {
114             nodeChanged((VisualizerNode) o);
115
116             return;
117         }
118
119         MutableTreeNode aNode = (MutableTreeNode) o;
120
121         aNode.setUserObject(newValue);
122         nodeChanged(aNode);
123     }
124
125     /** The listener */
126     private static final class Listener implements NodeModel {
127         /** weak reference to the model */
128         private Reference JavaDoc<NodeTreeModel> model;
129
130         /** Constructor.
131         */

132         public Listener(NodeTreeModel m) {
133             model = new WeakReference JavaDoc<NodeTreeModel>(m);
134         }
135
136         /** Getter for the model or null.
137         */

138         private NodeTreeModel get(VisualizerEvent ev) {
139             NodeTreeModel m = model.get();
140
141             if ((m == null) && (ev != null)) {
142                 ev.getVisualizer().removeNodeModel(this);
143
144                 return null;
145             }
146
147             return m;
148         }
149
150         /** Notification of children addded event. Modifies the list of nodes
151         * and fires info to all listeners.
152         */

153         public void added(VisualizerEvent.Added ev) {
154             NodeTreeModel m = get(ev);
155
156             if (m == null) {
157                 return;
158             }
159
160             m.nodesWereInserted(ev.getVisualizer(), ev.getArray());
161         }
162
163         /** Notification that children has been removed. Modifies the list of nodes
164         * and fires info to all listeners.
165         */

166         public void removed(VisualizerEvent.Removed ev) {
167             NodeTreeModel m = get(ev);
168
169             if (m == null) {
170                 return;
171             }
172             
173             for (TreeView tw : m.views) tw.removedNodes(ev.removed);
174             
175             m.nodesWereRemoved(ev.getVisualizer(), ev.getArray(), ev.removed.toArray());
176         }
177
178         /** Notification that children has been reordered. Modifies the list of nodes
179         * and fires info to all listeners.
180         */

181         public void reordered(VisualizerEvent.Reordered ev) {
182             NodeTreeModel m = get(ev);
183
184             if (m == null) {
185                 return;
186             }
187
188             m.nodeStructureChanged(ev.getVisualizer());
189         }
190
191         /** Update a visualizer (change of name, icon, description, etc.)
192         */

193         public void update(VisualizerNode v) {
194             NodeTreeModel m = get(null);
195
196             if (m == null) {
197                 return;
198             }
199
200             m.nodeChanged(v);
201         }
202
203         /** Notification about large change in the sub tree
204          */

205         public void structuralChange(VisualizerNode v) {
206             NodeTreeModel m = get(null);
207
208             if (m == null) {
209                 return;
210             }
211
212             m.nodeStructureChanged(v);
213         }
214     }
215 }
216
Popular Tags