1 19 package org.netbeans.modules.debugger.jpda.ui; 20 21 import com.sun.jdi.AbsentInformationException; 22 import java.beans.PropertyChangeListener ; 23 import java.io.File ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.List ; 27 28 import org.netbeans.api.debugger.DebuggerManager; 29 import org.netbeans.api.debugger.jpda.LineBreakpoint; 30 import org.netbeans.api.debugger.jpda.CallStackFrame; 31 import org.netbeans.api.debugger.jpda.JPDAThread; 32 import org.netbeans.spi.debugger.jpda.EditorContext; 33 34 35 39 public class EditorContextBridge { 40 41 public static final String FIELD = "field"; 42 public static final String METHOD = "method"; 43 public static final String CLASS = "class"; 44 public static final String LINE = "line"; 45 46 private static EditorContext context; 47 48 private static EditorContext getContext () { 49 if (context == null) { 50 List l = DebuggerManager.getDebuggerManager ().lookup 51 (null, EditorContext.class); 52 context = (EditorContext) l.get (0); 53 int i, k = l.size (); 54 for (i = 1; i < k; i++) 55 context = new CompoundContextProvider ( 56 (EditorContext) l.get (i), 57 context 58 ); 59 } 60 return context; 61 } 62 63 64 66 72 public static boolean showSource ( 73 String url, 74 int lineNumber, 75 Object timeStamp 76 ) { 77 return getContext ().showSource (url, lineNumber, timeStamp); 78 } 79 80 85 public static void createTimeStamp (Object timeStamp) { 86 getContext ().createTimeStamp (timeStamp); 87 } 88 89 94 public static void disposeTimeStamp (Object timeStamp) { 95 getContext ().disposeTimeStamp (timeStamp); 96 } 97 98 107 public static Object annotate ( 108 String url, 109 int lineNumber, 110 String annotationType, 111 Object timeStamp 112 ) { 113 return getContext ().annotate (url, lineNumber, annotationType, timeStamp); 114 } 115 116 126 public static Object annotate ( 127 String url, 128 int startPosition, 129 int endPosition, 130 String annotationType, 131 Object timeStamp 132 ) { 133 return getContext ().annotate (url, startPosition, endPosition, annotationType, timeStamp); 134 } 135 136 139 public static void removeAnnotation ( 140 Object annotation 141 ) { 142 getContext ().removeAnnotation (annotation); 143 } 144 145 150 public static int getLineNumber ( 151 Object annotation, 152 Object timeStamp 153 ) { 154 return getContext ().getLineNumber (annotation, timeStamp); 155 } 156 157 162 public static int getCurrentLineNumber () { 163 return getContext ().getCurrentLineNumber (); 164 } 165 166 171 public static String getCurrentClassName () { 172 return getContext ().getCurrentClassName (); 173 } 174 175 180 public static String getCurrentURL () { 181 return getContext ().getCurrentURL (); 182 } 183 184 189 public static String getCurrentMethodName () { 190 return getContext ().getCurrentMethodName (); 191 } 192 193 198 public static String getCurrentFieldName () { 199 return getContext ().getCurrentFieldName (); 200 } 201 202 207 public static String getSelectedIdentifier () { 208 return getContext ().getSelectedIdentifier (); 209 } 210 211 216 public static String getSelectedMethodName () { 217 return getContext ().getSelectedMethodName (); 218 } 219 220 230 public static int getFieldLineNumber ( 231 String url, 232 String className, 233 String fieldName 234 ) { 235 return getContext ().getFieldLineNumber (url, className, fieldName); 236 } 237 238 246 public static String getClassName ( 247 String url, 248 int lineNumber 249 ) { 250 return getContext ().getClassName (url, lineNumber); 251 } 252 253 260 public static String [] getImports (String url) { 261 return getContext ().getImports (url); 262 } 263 264 public static void addPropertyChangeListener (PropertyChangeListener l) { 265 getContext ().addPropertyChangeListener (l); 266 } 267 268 public static void removePropertyChangeListener (PropertyChangeListener l) { 269 getContext ().removePropertyChangeListener (l); 270 } 271 272 287 288 289 291 public static String getFileName (LineBreakpoint b) { 292 try { 293 return new File (new URL (b.getURL ()).getFile ()).getName (); 294 } catch (MalformedURLException e) { 295 return null; 296 } 297 } 298 299 public static boolean showSource (LineBreakpoint b, Object timeStamp) { 300 if (b.getLineNumber () < 1) 301 return EditorContextBridge.showSource ( 302 b.getURL (), 303 1, 304 timeStamp 305 ); 306 return EditorContextBridge.showSource ( 307 b.getURL (), 308 b.getLineNumber (), 309 timeStamp 310 ); 311 } 312 313 public static String getDefaultType () { 314 String id = getSelectedIdentifier (); 315 if (id != null) { 316 if (id.equals(getCurrentMethodName())) return METHOD; 317 String s = getCurrentClassName(); 318 int i = s.lastIndexOf ('.'); 319 if (i >= 0) 320 s = s.substring (i + 1); 321 if (id.equals (s)) 322 return CLASS; 323 return FIELD; 324 } else { 325 String s = getCurrentFieldName (); 326 if (s != null && s.length () > 0) 327 return FIELD; 328 s = getCurrentMethodName(); 329 if (s != null && s.length () > 0) 330 return METHOD; 331 if (s != null && s.length () < 1) { 332 s = getCurrentClassName (); 333 if (s.length () > 0) 334 return CLASS; 335 } 336 } 337 return CLASS; 338 } 339 340 public static Object annotate ( 341 LineBreakpoint b 342 ) { 343 String url = b.getURL (); 344 int lineNumber = b.getLineNumber (); 345 if (lineNumber < 1) return null; 346 String condition = b.getCondition (); 347 boolean isConditional = (condition != null) && 348 !"".equals (condition.trim ()); String annotationType = b.isEnabled () ? 350 (isConditional ? EditorContext.CONDITIONAL_BREAKPOINT_ANNOTATION_TYPE : 351 EditorContext.BREAKPOINT_ANNOTATION_TYPE) : 352 (isConditional ? EditorContext.DISABLED_CONDITIONAL_BREAKPOINT_ANNOTATION_TYPE : 353 EditorContext.DISABLED_BREAKPOINT_ANNOTATION_TYPE); 354 355 return annotate ( 356 url, 357 lineNumber, 358 annotationType, 359 null 360 ); 361 } 362 363 public static String getRelativePath ( 364 JPDAThread thread, 365 String stratumn 366 ) { 367 try { 368 return convertSlash (thread.getSourcePath (stratumn)); 369 } catch (AbsentInformationException e) { 370 return getRelativePath (thread.getClassName ()); 371 } 372 } 373 374 public static String getRelativePath ( 375 CallStackFrame csf, 376 String stratumn 377 ) { 378 try { 379 return convertSlash (csf.getSourcePath (stratumn)); 380 } catch (AbsentInformationException e) { 381 return getRelativePath (csf.getClassName ()); 382 } 383 } 384 385 public static String getRelativePath ( 386 String className 387 ) { 388 int i = className.indexOf ('$'); 389 if (i > 0) className = className.substring (0, i); 390 String sourceName = className.replace 391 ('.', '/') + ".java"; 392 return sourceName; 393 } 394 395 private static String convertSlash (String original) { 396 return original.replace (File.separatorChar, '/'); 397 } 398 399 400 402 private static class CompoundContextProvider extends EditorContext { 403 404 private EditorContext cp1, cp2; 405 406 CompoundContextProvider ( 407 EditorContext cp1, 408 EditorContext cp2 409 ) { 410 this.cp1 = cp1; 411 this.cp2 = cp2; 412 } 413 414 public void createTimeStamp (Object timeStamp) { 415 cp1.createTimeStamp (timeStamp); 416 cp2.createTimeStamp (timeStamp); 417 } 418 419 public void disposeTimeStamp (Object timeStamp) { 420 cp1.disposeTimeStamp (timeStamp); 421 cp2.disposeTimeStamp (timeStamp); 422 } 423 424 public void updateTimeStamp (Object timeStamp, String url) { 425 cp1.updateTimeStamp (timeStamp, url); 426 cp2.updateTimeStamp (timeStamp, url); 427 } 428 429 public String getCurrentClassName () { 430 String s = cp1.getCurrentClassName (); 431 if (s.trim ().length () < 1) 432 return cp2.getCurrentClassName (); 433 return s; 434 } 435 436 public String getCurrentURL () { 437 String s = cp1.getCurrentURL (); 438 if (s.trim ().length () < 1) 439 return cp2.getCurrentURL (); 440 return s; 441 } 442 443 public String getCurrentFieldName () { 444 String s = cp1.getCurrentFieldName (); 445 if ( (s == null) || (s.trim ().length () < 1)) 446 return cp2.getCurrentFieldName (); 447 return s; 448 } 449 450 public int getCurrentLineNumber () { 451 int i = cp1.getCurrentLineNumber (); 452 if (i < 1) 453 return cp2.getCurrentLineNumber (); 454 return i; 455 } 456 457 public String getCurrentMethodName () { 458 String s = cp1.getCurrentMethodName (); 459 if ( (s == null) || (s.trim ().length () < 1)) 460 return cp2.getCurrentMethodName (); 461 return s; 462 } 463 464 public String getSelectedIdentifier () { 465 String s = cp1.getSelectedIdentifier (); 466 if ( (s == null) || (s.trim ().length () < 1)) 467 return cp2.getSelectedIdentifier (); 468 return s; 469 } 470 471 public String getSelectedMethodName () { 472 String s = cp1.getSelectedMethodName (); 473 if ( (s == null) || (s.trim ().length () < 1)) 474 return cp2.getSelectedMethodName (); 475 return s; 476 } 477 478 public void removeAnnotation (Object annotation) { 479 CompoundAnnotation ca = (CompoundAnnotation) annotation; 480 cp1.removeAnnotation (ca.annotation1); 481 cp2.removeAnnotation (ca.annotation2); 482 } 483 484 public Object annotate ( 485 String sourceName, 486 int lineNumber, 487 String annotationType, 488 Object timeStamp 489 ) { 490 CompoundAnnotation ca = new CompoundAnnotation (); 491 ca.annotation1 = cp1.annotate 492 (sourceName, lineNumber, annotationType, timeStamp); 493 ca.annotation2 = cp2.annotate 494 (sourceName, lineNumber, annotationType, timeStamp); 495 return ca; 496 } 497 498 public int getLineNumber (Object annotation, Object timeStamp) { 499 CompoundAnnotation ca = new CompoundAnnotation (); 500 int ln = cp1.getLineNumber (ca.annotation1, timeStamp); 501 if (ln >= 0) return ln; 502 return cp2.getLineNumber (ca.annotation2, timeStamp); 503 } 504 505 public boolean showSource (String sourceName, int lineNumber, Object timeStamp) { 506 return cp1.showSource (sourceName, lineNumber, timeStamp) | 507 cp2.showSource (sourceName, lineNumber, timeStamp); 508 } 509 510 public int getFieldLineNumber ( 511 String url, 512 String className, 513 String fieldName 514 ) { 515 int ln = cp1.getFieldLineNumber (url, className, fieldName); 516 if (ln != -1) return ln; 517 return cp2.getFieldLineNumber (url, className, fieldName); 518 } 519 520 public String getClassName ( 521 String url, 522 int lineNumber 523 ) { 524 String className = cp1.getClassName (url, lineNumber); 525 if (className != null) return className; 526 return cp2.getClassName (url, lineNumber); 527 } 528 529 public String [] getImports (String url) { 530 String [] r1 = cp1.getImports (url); 531 String [] r2 = cp2.getImports (url); 532 String [] r = new String [r1.length + r2.length]; 533 System.arraycopy (r1, 0, r, 0, r1.length); 534 System.arraycopy (r2, 0, r, r1.length, r2.length); 535 return r; 536 } 537 538 public void addPropertyChangeListener (PropertyChangeListener l) { 539 cp1.addPropertyChangeListener (l); 540 cp2.addPropertyChangeListener (l); 541 } 542 543 public void removePropertyChangeListener (PropertyChangeListener l) { 544 cp1.removePropertyChangeListener (l); 545 cp2.removePropertyChangeListener (l); 546 } 547 548 public void addPropertyChangeListener ( 549 String propertyName, 550 PropertyChangeListener l 551 ) { 552 cp1.addPropertyChangeListener (propertyName, l); 553 cp2.addPropertyChangeListener (propertyName, l); 554 } 555 556 public void removePropertyChangeListener ( 557 String propertyName, 558 PropertyChangeListener l 559 ) { 560 cp1.removePropertyChangeListener (propertyName, l); 561 cp2.removePropertyChangeListener (propertyName, l); 562 } 563 } 564 565 private static class CompoundAnnotation { 566 Object annotation1; 567 Object annotation2; 568 } 569 } 570 571 | Popular Tags |