1 19 20 package org.netbeans.modules.viewmodel; 21 22 import java.lang.ref.WeakReference ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.WeakHashMap ; 26 import javax.swing.SwingUtilities ; 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 43 public class TreeModelRoot implements ModelListener { 44 45 static final long serialVersionUID = -1259352660663524178L; 46 47 48 50 private Models.CompoundModel model; 51 private TreeModelNode rootNode; 52 private WeakHashMap objectToNode = new WeakHashMap (); 53 private TreeTable treeTable; 54 55 56 private TreeModelNode.LazyEvaluator childrenEvaluator; 57 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 o, TreeModelNode n) { 78 objectToNode.put (o, new WeakReference (n)); 79 } 80 81 TreeModelNode findNode (Object o) { 82 WeakReference wr = (WeakReference ) 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 () { 89 public void run () { 90 if (model == null) 91 return; if (event instanceof ModelEvent.TableValueChanged) { 93 ModelEvent.TableValueChanged tvEvent = (ModelEvent.TableValueChanged) event; 94 Object node = tvEvent.getNode(); 95 if (node != null) { 96 TreeModelNode tmNode = findNode(node); 97 if (tmNode != null) { 98 String column = tvEvent.getColumnID(); 99 if (column != null) { 100 tmNode.refreshColumn(column); 101 } else { 102 tmNode.refresh(); 103 } 104 return ; } 106 } 107 } 108 if (event instanceof ModelEvent.NodeChanged) { 109 ModelEvent.NodeChanged nchEvent = (ModelEvent.NodeChanged) event; 110 Object node = nchEvent.getNode(); 111 if (node != null) { 112 TreeModelNode tmNode = findNode(node); 113 if (tmNode != null) { 114 tmNode.refresh(nchEvent.getChange()); 115 return ; } 117 } 118 } 119 rootNode.setObject (model.getRoot ()); 120 } 121 }); 122 } 131 132 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 (); 161 } 162 } 163 164 | Popular Tags |