1 19 package org.openide.explorer.view; 20 21 import java.util.concurrent.CopyOnWriteArrayList ; 22 import org.openide.nodes.Node; 23 import org.openide.util.*; 24 25 import java.beans.*; 26 27 import java.lang.ref.Reference ; 28 import java.lang.ref.WeakReference ; 29 30 import javax.swing.*; 31 import javax.swing.event.*; 32 import javax.swing.tree.*; 33 34 35 39 public class NodeTreeModel extends DefaultTreeModel { 40 static final long serialVersionUID = 1900670294524747212L; 41 42 43 private transient Listener listener; 44 45 private CopyOnWriteArrayList <TreeView> views = new CopyOnWriteArrayList <TreeView>(); 48 void addView(TreeView tw) { 49 views.add(tw); 50 } 51 52 54 public NodeTreeModel() { 55 super(VisualizerNode.EMPTY, true); 56 } 57 58 61 public NodeTreeModel(Node root) { 62 super(VisualizerNode.EMPTY, true); 63 setNode(root); 64 } 65 66 69 public void setNode(final Node root) { 70 Mutex.EVENT.readAccess( 71 new Runnable () { 72 public void run() { 73 VisualizerNode v = (VisualizerNode) getRoot(); 74 VisualizerNode nr = VisualizerNode.getVisualizer(null, root); 75 76 if (v == nr) { 77 return; 79 } 80 81 v.removeNodeModel(listener()); 82 83 nr.addNodeModel(listener()); 84 setRoot(nr); 85 } 86 } 87 ); 88 } 89 90 92 private Listener listener() { 93 if (listener == null) { 94 listener = new Listener(this); 95 } 96 97 return listener; 98 } 99 100 106 public void valueForPathChanged(TreePath path, Object newValue) { 107 if (path == null) { 108 return; 109 } 110 111 Object 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 126 private static final class Listener implements NodeModel { 127 128 private Reference <NodeTreeModel> model; 129 130 132 public Listener(NodeTreeModel m) { 133 model = new WeakReference <NodeTreeModel>(m); 134 } 135 136 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 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 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 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 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 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 |