1 19 20 package org.netbeans.modules.debugger.ui.models; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.lang.ref.WeakReference ; 25 import java.util.Vector ; 26 27 import org.netbeans.api.debugger.Watch; 28 import org.netbeans.api.debugger.DebuggerManager; 29 import org.netbeans.api.debugger.DebuggerManagerAdapter; 30 import org.netbeans.spi.viewmodel.ModelEvent; 31 import org.netbeans.spi.viewmodel.TreeModel; 32 import org.netbeans.spi.viewmodel.ModelListener; 33 import org.netbeans.spi.viewmodel.UnknownTypeException; 34 35 36 39 public class WatchesTreeModel implements TreeModel { 40 41 private Listener listener; 42 private Vector listeners = new Vector (); 43 44 45 49 public Object getRoot () { 50 return ROOT; 51 } 52 53 57 public Object [] getChildren (Object parent, int from, int to) 58 throws UnknownTypeException { 59 if (parent == ROOT) { 60 Watch[] ws = DebuggerManager.getDebuggerManager (). 61 getWatches (); 62 if (listener == null) 63 listener = new Listener (this); 64 to = Math.min(ws.length, to); 65 from = Math.min(ws.length, from); 66 Watch[] fws = new Watch [to - from]; 67 System.arraycopy (ws, from, fws, 0, to - from); 68 return fws; 69 } else 70 throw new UnknownTypeException (parent); 71 } 72 73 82 public int getChildrenCount (Object node) throws UnknownTypeException { 83 if (node == ROOT) { 84 if (listener == null) 85 listener = new Listener (this); 86 return Integer.MAX_VALUE; 88 } else 90 throw new UnknownTypeException (node); 91 } 92 93 public boolean isLeaf (Object node) throws UnknownTypeException { 94 if (node == ROOT) return false; 95 if (node instanceof Watch) return true; 96 throw new UnknownTypeException (node); 97 } 98 99 public void addModelListener (ModelListener l) { 100 listeners.add (l); 101 } 102 103 public void removeModelListener (ModelListener l) { 104 listeners.remove (l); 105 } 106 107 private void fireTreeChanged () { 108 Vector v = (Vector ) listeners.clone (); 109 int i, k = v.size (); 110 for (i = 0; i < k; i++) 111 ((ModelListener) v.get (i)).modelChanged ( 112 new ModelEvent.NodeChanged(this, ROOT, ModelEvent.NodeChanged.CHILDREN_MASK) 113 ); 114 } 115 116 void fireWatchPropertyChanged (Watch b, String propertyName) { 117 Vector v = (Vector ) listeners.clone (); 118 int i, k = v.size (); 119 for (i = 0; i < k; i++) 120 ((ModelListener) v.get (i)).modelChanged ( 121 new ModelEvent.TableValueChanged (this, b, propertyName) 122 ); 123 } 124 125 126 128 private static class Listener extends DebuggerManagerAdapter implements 129 PropertyChangeListener { 130 131 private WeakReference model; 132 133 public Listener ( 134 WatchesTreeModel tm 135 ) { 136 model = new WeakReference (tm); 137 DebuggerManager.getDebuggerManager ().addDebuggerListener ( 138 DebuggerManager.PROP_WATCHES, 139 this 140 ); 141 Watch[] ws = DebuggerManager.getDebuggerManager (). 142 getWatches (); 143 int i, k = ws.length; 144 for (i = 0; i < k; i++) 145 ws [i].addPropertyChangeListener (this); 146 } 147 148 private WatchesTreeModel getModel () { 149 WatchesTreeModel m = (WatchesTreeModel) model.get (); 150 if (m == null) { 151 DebuggerManager.getDebuggerManager ().removeDebuggerListener ( 152 DebuggerManager.PROP_WATCHES, 153 this 154 ); 155 Watch[] ws = DebuggerManager.getDebuggerManager (). 156 getWatches (); 157 int i, k = ws.length; 158 for (i = 0; i < k; i++) 159 ws [i].removePropertyChangeListener (this); 160 } 161 return m; 162 } 163 164 public void watchAdded (Watch watch) { 165 WatchesTreeModel m = getModel (); 166 if (m == null) return; 167 watch.addPropertyChangeListener (this); 168 m.fireTreeChanged (); 169 } 170 171 public void watchRemoved (Watch watch) { 172 WatchesTreeModel m = getModel (); 173 if (m == null) return; 174 watch.removePropertyChangeListener (this); 175 m.fireTreeChanged (); 176 } 177 178 public void propertyChange (PropertyChangeEvent evt) { 179 WatchesTreeModel m = getModel (); 180 if (m == null) return; 181 if (! (evt.getSource () instanceof Watch)) 182 return; 183 Watch w = (Watch) evt.getSource (); 184 m.fireWatchPropertyChanged (w, evt.getPropertyName ()); 185 } 186 } 187 } 188 | Popular Tags |