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.ArrayList ; 26 import java.util.Comparator ; 27 import java.util.Set ; 28 import java.util.TreeSet ; 29 import java.util.Vector ; 30 31 import org.netbeans.api.debugger.Breakpoint; 32 import org.netbeans.api.debugger.DebuggerManager; 33 import org.netbeans.api.debugger.DebuggerManagerAdapter; 34 import org.netbeans.spi.debugger.ui.Constants; 35 import org.netbeans.spi.viewmodel.ModelEvent; 36 import org.netbeans.spi.viewmodel.TreeModel; 37 import org.netbeans.spi.viewmodel.ModelListener; 38 import org.netbeans.spi.viewmodel.UnknownTypeException; 39 40 41 44 public class BreakpointsTreeModel implements TreeModel { 45 46 private Listener listener; 47 private Vector listeners = new Vector (); 48 49 50 54 public Object getRoot () { 55 return ROOT; 56 } 57 58 62 public Object [] getChildren (Object parent, int from, int to) 63 throws UnknownTypeException { 64 if (parent == ROOT) { 65 Breakpoint[] bs = DebuggerManager.getDebuggerManager (). 66 getBreakpoints (); 67 ArrayList l = new ArrayList (); 68 int i, k = bs.length; 69 for (i = 0; i < k; i++) { 70 String gn = bs[i].getGroupName(); 71 if (gn.equals("")) { 72 l.add (bs [i]); 73 } else { 74 if (!l.contains(gn)) { 75 l.add(gn); 76 } 77 } 78 } 79 if (listener == null) 80 listener = new Listener (this); 81 if (to == 0) 82 return l.toArray (); 83 to = Math.min(l.size(), to); 84 from = Math.min(l.size(), from); 85 return l.subList (from, to).toArray (); 86 } else 87 if (parent instanceof String ) { 88 String groupName = (String ) parent; 89 Breakpoint[] bs = DebuggerManager.getDebuggerManager (). 90 getBreakpoints (); 91 ArrayList l = new ArrayList (); 92 int i, k = bs.length; 93 for (i = 0; i < k; i++) 94 if (bs [i].getGroupName ().equals (groupName)) 95 l.add (bs [i]); 96 if (listener == null) 97 listener = new Listener (this); 98 if (to == 0) 99 return l.toArray (); 100 to = Math.min(l.size(), to); 101 from = Math.min(l.size(), from); 102 return l.subList (from, to).toArray (); 103 } else 104 throw new UnknownTypeException (parent); 105 } 106 107 116 public int getChildrenCount (Object node) throws UnknownTypeException { 117 if (node == ROOT) { 118 return Integer.MAX_VALUE; 120 } else 122 if (node instanceof String ) { 123 return Integer.MAX_VALUE; 125 } else 127 throw new UnknownTypeException (node); 128 } 129 130 public boolean isLeaf (Object node) throws UnknownTypeException { 131 if (node == ROOT) return false; 132 if (node instanceof Breakpoint) return true; 133 if (node instanceof String ) return false; 134 throw new UnknownTypeException (node); 135 } 136 137 public void addModelListener (ModelListener l) { 138 listeners.add (l); 139 } 140 141 public void removeModelListener (ModelListener l) { 142 listeners.remove (l); 143 } 144 145 private void fireTreeChanged () { 146 Vector v = (Vector ) listeners.clone (); 147 int i, k = v.size (); 148 for (i = 0; i < k; i++) 149 ((ModelListener) v.get (i)).modelChanged ( 150 new ModelEvent.TreeChanged (this) 151 ); 152 } 153 154 private void fireTreeChanged (ModelEvent me) { 155 Vector v = (Vector ) listeners.clone (); 156 int i, k = v.size (); 157 for (i = 0; i < k; i++) 158 ((ModelListener) v.get (i)).modelChanged (me); 159 } 160 161 162 164 private static class Listener extends DebuggerManagerAdapter implements 165 PropertyChangeListener { 166 167 private WeakReference model; 168 169 public Listener ( 170 BreakpointsTreeModel tm 171 ) { 172 model = new WeakReference (tm); 173 DebuggerManager.getDebuggerManager ().addDebuggerListener ( 174 DebuggerManager.PROP_BREAKPOINTS, 175 this 176 ); 177 Breakpoint[] bs = DebuggerManager.getDebuggerManager (). 178 getBreakpoints (); 179 int i, k = bs.length; 180 for (i = 0; i < k; i++) 181 bs [i].addPropertyChangeListener (this); 182 } 183 184 private BreakpointsTreeModel getModel () { 185 BreakpointsTreeModel m = (BreakpointsTreeModel) model.get (); 186 if (m == null) { 187 DebuggerManager.getDebuggerManager ().removeDebuggerListener ( 188 DebuggerManager.PROP_BREAKPOINTS, 189 this 190 ); 191 Breakpoint[] bs = DebuggerManager.getDebuggerManager (). 192 getBreakpoints (); 193 int i, k = bs.length; 194 for (i = 0; i < k; i++) 195 bs [i].removePropertyChangeListener (this); 196 } 197 return m; 198 } 199 200 public void breakpointAdded (Breakpoint breakpoint) { 201 BreakpointsTreeModel m = getModel (); 202 if (m == null) return; 203 breakpoint.addPropertyChangeListener (this); 204 m.fireTreeChanged (); 205 } 206 207 public void breakpointRemoved (Breakpoint breakpoint) { 208 BreakpointsTreeModel m = getModel (); 209 if (m == null) return; 210 breakpoint.removePropertyChangeListener (this); 211 m.fireTreeChanged (); 212 } 213 214 public void propertyChange (PropertyChangeEvent evt) { 215 BreakpointsTreeModel m = getModel (); 216 if (m == null) return; 217 if (! (evt.getSource () instanceof Breakpoint)) 218 return; 219 if (evt.getPropertyName () == Breakpoint.PROP_GROUP_NAME) { 220 m.fireTreeChanged (); 221 } else { 222 m.fireTreeChanged (new ModelEvent.NodeChanged( 223 m, evt.getSource ())); 224 if (evt.getPropertyName () == Breakpoint.PROP_ENABLED) { 225 Breakpoint bp = (Breakpoint) evt.getSource (); 226 String groupName = bp.getGroupName(); 227 if (groupName != null) { 228 m.fireTreeChanged(new ModelEvent.TableValueChanged( 229 m, 230 groupName, Constants.BREAKPOINT_ENABLED_COLUMN_ID)); 231 } 232 } 233 } 234 } 235 } 236 237 } 238 | Popular Tags |