1 19 20 package org.netbeans.modules.j2ee.debug.callstackviewfilterring; 21 22 import com.sun.jdi.AbsentInformationException; 23 import org.netbeans.api.debugger.jpda.CallStackFrame; 24 import org.netbeans.api.debugger.jpda.JPDADebugger; 25 import org.netbeans.spi.debugger.ContextProvider; 26 import org.netbeans.spi.debugger.jpda.SourcePathProvider; 27 import org.netbeans.spi.viewmodel.NodeModel; 28 import org.netbeans.spi.viewmodel.TableModel; 29 import org.netbeans.spi.viewmodel.TableModelFilter; 30 import org.netbeans.spi.viewmodel.TreeModel; 31 import org.netbeans.spi.viewmodel.TreeModelFilter; 32 import org.netbeans.spi.viewmodel.ModelListener; 33 import org.netbeans.spi.viewmodel.UnknownTypeException; 34 import org.openide.util.NbBundle; 35 36 import java.beans.PropertyChangeListener ; 37 import java.io.File ; 38 import java.util.ArrayList ; 39 import java.util.List ; 40 41 45 public class CallStackFilter implements TreeModelFilter, NodeModel, TableModelFilter { 46 47 private ContextProvider lookupProvider; 48 private JPDADebugger debugger; 49 private SourcePathProvider contextProvider; 50 51 public CallStackFilter (ContextProvider lookupProvider) { 52 this.lookupProvider = lookupProvider; 53 debugger = (JPDADebugger) lookupProvider. 54 lookupFirst(null, JPDADebugger.class); 55 } 56 57 private static String convertSlash (String original) { 58 return original.replace (File.separatorChar, '/'); 59 } 60 61 private static String convertClassNameToRelativePath ( 62 String className 63 ) { 64 int i = className.indexOf ('$'); 65 if (i > 0) className = className.substring (0, i); 66 String sourceName = className.replace('.', '/') + ".java"; 67 return sourceName; 68 } 69 70 private SourcePathProvider getSourcePathProvider() { 71 if (contextProvider == null) { 72 List l = lookupProvider.lookup (null, SourcePathProvider.class); 73 contextProvider = (SourcePathProvider) l.get (0); 74 int i, k = l.size (); 75 for (i = 1; i < k; i++) { 76 contextProvider = new CompoundSourcePathProvider ( 77 (SourcePathProvider) l.get (i), 78 contextProvider 79 ); 80 } 81 } 82 return contextProvider; 83 } 84 85 94 String getURL(String relativePath) { 95 return getSourcePathProvider().getURL (relativePath, false); 96 } 97 98 boolean isOnSourcePath(CallStackFrame csf) { 99 100 String url = null; 101 102 try { 103 url = getURL (convertSlash (csf.getSourcePath (null))); 104 } catch (AbsentInformationException e) { 105 url = getURL (convertClassNameToRelativePath (csf.getClassName ())); 106 } 107 108 return url != null; 109 } 110 111 112 113 114 120 public Object getRoot (TreeModel original) { 121 return original.getRoot (); 122 } 123 124 138 public int getChildrenCount ( 139 TreeModel original, 140 Object node 141 ) throws UnknownTypeException { 142 if (node.equals (original.getRoot ())) { 143 Object [] originalCh = original.getChildren ( 144 node, 145 0, 146 original.getChildrenCount (node) 147 ); 148 int i, k = originalCh.length, j = 0; 149 boolean in = false; 150 for (i = 0; i < k; i++) { 151 if (! (originalCh [i] instanceof CallStackFrame)) { 152 j++; 153 continue; 154 } 155 CallStackFrame f = (CallStackFrame) originalCh [i]; 156 if (!isOnSourcePath(f)) { 157 if (!in) { 158 j++; 159 in = true; 160 } 161 } else { 162 in = false; 163 j++; 164 } 165 } 166 return j; 167 } 168 if (node instanceof HiddenFrames) 169 return ((HiddenFrames) node).getStack ().size (); 170 return original.getChildrenCount (node); 171 } 172 173 191 public Object [] getChildren ( 192 TreeModel original, 193 Object parent, 194 int from, 195 int to 196 ) throws UnknownTypeException { 197 if (parent.equals (original.getRoot ())) { 198 Object [] originalCh = original.getChildren ( 199 parent, 200 0, 201 original.getChildrenCount (parent) 202 ); 203 int i, k = originalCh.length; 204 ArrayList newCh = new ArrayList (); 205 HiddenFrames hiddenFrames = null; 206 for (i = 0; i < k; i++) { 207 if (! (originalCh [i] instanceof CallStackFrame)) { 208 newCh.add (originalCh [i]); 209 continue; 210 } 211 CallStackFrame f = (CallStackFrame) originalCh [i]; 212 if (!isOnSourcePath(f)) { 213 if (hiddenFrames == null) { 214 hiddenFrames = new HiddenFrames (); 215 newCh.add (hiddenFrames); 216 } 217 hiddenFrames.addFrame (f); 218 } else { 219 hiddenFrames = null; 220 newCh.add (f); 221 } 222 } 223 int lto = Math.min(newCh.size(), to); 227 return newCh.subList (from, lto).toArray (); 228 } 229 if (parent instanceof HiddenFrames) 230 return ((HiddenFrames) parent).getStack ().toArray (); 231 return original.getChildren (parent, from, to); 232 } 233 234 243 public boolean isLeaf (TreeModel original, Object node) 244 throws UnknownTypeException { 245 if (node instanceof HiddenFrames) return false; 246 return original.isLeaf (node); 247 } 248 249 public void addModelListener (ModelListener l) { 250 } 251 public void removeModelListener (ModelListener l) { 252 } 253 254 public String getDisplayName (Object node) throws UnknownTypeException { 255 if (node instanceof HiddenFrames) 256 return NbBundle.getMessage(CallStackFilter.class, "LBL_HIDDEN_FRAMES"); 257 throw new UnknownTypeException (node); 258 } 259 260 public String getIconBase (Object node) throws UnknownTypeException { 261 if (node instanceof HiddenFrames) 262 return NbBundle.getMessage(CallStackFilter.class, "RES_FRAME_GROUP"); 263 throw new UnknownTypeException (node); 264 } 265 266 public String getShortDescription (Object node) throws UnknownTypeException { 267 if (node instanceof HiddenFrames) 268 return NbBundle.getMessage(CallStackFilter.class, "TLT_HIDDEN_FRAMES"); 269 throw new UnknownTypeException (node); 270 } 271 272 274 public void setValueAt(TableModel original, Object node, String columnID, Object value) throws UnknownTypeException { 275 276 original.setValueAt(node, columnID, value); 277 } 278 279 public boolean isReadOnly(TableModel original, Object node, String columnID) throws UnknownTypeException { 280 281 if (node instanceof HiddenFrames) 282 return true; 283 284 return original.isReadOnly(node, columnID); 285 } 286 287 public Object getValueAt(TableModel original, Object node, String columnID) throws UnknownTypeException { 288 289 if (node instanceof HiddenFrames) 290 return ""; 291 292 return original.getValueAt(node, columnID); 293 } 294 295 296 298 public static class HiddenFrames { 299 private List frames = new ArrayList (); 300 301 void addFrame (CallStackFrame frame) { 302 frames.add (frame); 303 } 304 305 List getStack () { 306 return frames; 307 } 308 309 public boolean equals (Object o) { 310 if (!(o instanceof HiddenFrames)) return false; 311 if (frames.size () != ((HiddenFrames) o).frames.size ()) return false; 312 if (frames.size () == 0) return o == this; 313 return frames.get (0).equals ( 314 ((HiddenFrames) o).frames.get (0) 315 ); 316 } 317 318 public int hashCode () { 319 if (frames.size () == 0) return super.hashCode (); 320 return frames.get (0).hashCode (); 321 } 322 } 323 324 private static class CompoundSourcePathProvider extends SourcePathProvider { 325 326 private SourcePathProvider cp1, cp2; 327 328 CompoundSourcePathProvider ( 329 SourcePathProvider cp1, 330 SourcePathProvider cp2 331 ) { 332 this.cp1 = cp1; 333 this.cp2 = cp2; 334 } 335 336 public String getURL (String relativePath, boolean global) { 337 String p1 = cp1.getURL (relativePath, global); 338 if (p1 != null) return p1; 339 return cp2.getURL (relativePath, global); 340 } 341 342 public String getRelativePath ( 343 String url, 344 char directorySeparator, 345 boolean includeExtension 346 ) { 347 String p1 = cp1.getRelativePath ( 348 url, 349 directorySeparator, 350 includeExtension 351 ); 352 if (p1 != null) return p1; 353 return cp2.getRelativePath ( 354 url, 355 directorySeparator, 356 includeExtension 357 ); 358 } 359 360 public String [] getSourceRoots () { 361 String [] fs1 = cp1.getSourceRoots (); 362 String [] fs2 = cp2.getSourceRoots (); 363 String [] fs = new String [fs1.length + fs2.length]; 364 System.arraycopy (fs1, 0, fs, 0, fs1.length); 365 System.arraycopy (fs2, 0, fs, fs1.length, fs2.length); 366 return fs; 367 } 368 369 public String [] getOriginalSourceRoots () { 370 String [] fs1 = cp1.getOriginalSourceRoots (); 371 String [] fs2 = cp2.getOriginalSourceRoots (); 372 String [] fs = new String [fs1.length + fs2.length]; 373 System.arraycopy (fs1, 0, fs, 0, fs1.length); 374 System.arraycopy (fs2, 0, fs, fs1.length, fs2.length); 375 return fs; 376 } 377 378 public void setSourceRoots (String [] sourceRoots) { 379 cp1.setSourceRoots (sourceRoots); 380 cp2.setSourceRoots (sourceRoots); 381 } 382 383 public void addPropertyChangeListener (PropertyChangeListener l) { 384 cp1.addPropertyChangeListener (l); 385 cp2.addPropertyChangeListener (l); 386 } 387 388 public void removePropertyChangeListener (PropertyChangeListener l) { 389 cp1.removePropertyChangeListener (l); 390 cp2.removePropertyChangeListener (l); 391 } 392 } 393 394 } 395 | Popular Tags |