1 11 package org.eclipse.debug.internal.ui.sourcelookup; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.debug.core.DebugException; 18 import org.eclipse.debug.core.ILaunch; 19 import org.eclipse.debug.core.model.IDebugElement; 20 import org.eclipse.debug.core.model.ISourceLocator; 21 import org.eclipse.debug.core.model.IStackFrame; 22 import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector; 23 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector; 24 import org.eclipse.debug.internal.ui.DebugUIPlugin; 25 import org.eclipse.debug.internal.ui.DelegatingModelPresentation; 26 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 27 import org.eclipse.debug.internal.ui.InstructionPointerManager; 28 import org.eclipse.debug.internal.ui.views.DebugUIViewsMessages; 29 import org.eclipse.debug.internal.ui.views.launch.Decoration; 30 import org.eclipse.debug.internal.ui.views.launch.DecorationManager; 31 import org.eclipse.debug.internal.ui.views.launch.SourceNotFoundEditorInput; 32 import org.eclipse.debug.internal.ui.views.launch.StandardDecoration; 33 import org.eclipse.debug.ui.DebugUITools; 34 import org.eclipse.debug.ui.IDebugEditorPresentation; 35 import org.eclipse.debug.ui.IDebugModelPresentation; 36 import org.eclipse.debug.ui.IDebugUIConstants; 37 import org.eclipse.debug.ui.IInstructionPointerPresentation; 38 import org.eclipse.debug.ui.ISourcePresentation; 39 import org.eclipse.debug.ui.sourcelookup.CommonSourceNotFoundEditorInput; 40 import org.eclipse.debug.ui.sourcelookup.ISourceLookupResult; 41 import org.eclipse.jface.text.BadLocationException; 42 import org.eclipse.jface.text.IDocument; 43 import org.eclipse.jface.text.IRegion; 44 import org.eclipse.jface.text.source.Annotation; 45 import org.eclipse.jface.util.IPropertyChangeListener; 46 import org.eclipse.jface.util.PropertyChangeEvent; 47 import org.eclipse.swt.custom.BusyIndicator; 48 import org.eclipse.ui.IEditorInput; 49 import org.eclipse.ui.IEditorPart; 50 import org.eclipse.ui.IEditorReference; 51 import org.eclipse.ui.IPageListener; 52 import org.eclipse.ui.IPartListener2; 53 import org.eclipse.ui.IReusableEditor; 54 import org.eclipse.ui.IWorkbenchPage; 55 import org.eclipse.ui.IWorkbenchPart; 56 import org.eclipse.ui.IWorkbenchPartReference; 57 import org.eclipse.ui.PartInitException; 58 import org.eclipse.ui.texteditor.IDocumentProvider; 59 import org.eclipse.ui.texteditor.ITextEditor; 60 61 66 public class SourceLookupFacility implements IPageListener, IPartListener2, IPropertyChangeListener { 67 68 71 private static SourceLookupFacility fgDefault; 72 73 77 private Map fEditorsByPage; 78 79 82 private IInstructionPointerPresentation fPresentation = (IInstructionPointerPresentation) DebugUITools.newDebugModelPresentation(); 83 84 87 private boolean fReuseEditor = DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IDebugUIConstants.PREF_REUSE_EDITOR); 88 89 93 public static SourceLookupFacility getDefault() { 94 if (fgDefault == null) { 95 fgDefault = new SourceLookupFacility(); 96 } 97 return fgDefault; 98 } 99 100 103 public static void shutdown() { 104 if (fgDefault != null) { 105 fgDefault.dispose(); 106 } 107 } 108 109 112 private SourceLookupFacility() { 113 fEditorsByPage = new HashMap (); 114 DebugUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this); 115 } 116 117 126 public SourceLookupResult lookup(Object artifact, ISourceLocator locator) { 127 SourceLookupResult result = new SourceLookupResult(artifact, null, null, null); 128 IDebugElement debugElement = null; 129 if (artifact instanceof IDebugElement) { 130 debugElement = (IDebugElement)artifact; 131 } 132 if (locator == null) { 133 ILaunch launch = null; 134 if (debugElement != null) { 135 launch = debugElement.getLaunch(); 136 } 137 if (launch != null) { 138 locator = launch.getSourceLocator(); 139 } 140 } 141 if (locator != null) { 142 String editorId =null; 143 IEditorInput editorInput = null; 144 Object sourceElement = null; 145 if (locator instanceof ISourceLookupDirector) { 146 ISourceLookupDirector director = (ISourceLookupDirector)locator; 147 sourceElement = director.getSourceElement(artifact); 148 } else { 149 if (artifact instanceof IStackFrame) { 150 sourceElement = locator.getSourceElement((IStackFrame)artifact); 151 } 152 } 153 if (sourceElement == null) { 154 if (locator instanceof AbstractSourceLookupDirector) { 155 editorInput = new CommonSourceNotFoundEditorInput(artifact); 156 editorId = IDebugUIConstants.ID_COMMON_SOURCE_NOT_FOUND_EDITOR; 157 } else { 158 if (artifact instanceof IStackFrame) { 159 IStackFrame frame = (IStackFrame)artifact; 160 editorInput = new SourceNotFoundEditorInput(frame); 161 editorId = IInternalDebugUIConstants.ID_SOURCE_NOT_FOUND_EDITOR; 162 } 163 } 164 } else { 165 ISourcePresentation presentation= null; 166 if (locator instanceof ISourcePresentation) { 167 presentation= (ISourcePresentation) locator; 168 } else { 169 if (debugElement != null) { 170 presentation= getPresentation(debugElement.getModelIdentifier()); 171 } 172 } 173 if (presentation != null) { 174 editorInput= presentation.getEditorInput(sourceElement); 175 } 176 if (editorInput != null) { 177 editorId= presentation.getEditorId(editorInput, sourceElement); 178 } 179 } 180 result.setEditorInput(editorInput); 181 result.setEditorId(editorId); 182 result.setSourceElement(sourceElement); 183 } 184 return result; 185 } 186 187 194 protected IDebugModelPresentation getPresentation(String id) { 195 return ((DelegatingModelPresentation)DebugUIPlugin.getModelPresentation()).getPresentation(id); 196 } 197 198 203 protected IDebugEditorPresentation getEditorPresentation() { 204 return (DelegatingModelPresentation)DebugUIPlugin.getModelPresentation(); 205 } 206 207 217 public void display(ISourceLookupResult result, IWorkbenchPage page) { 218 IEditorPart editor= openEditor(result, page); 219 if (editor == null) { 220 return; 221 } 222 IStackFrame frame = null; 223 if (result.getArtifact() instanceof IStackFrame) { 224 frame = (IStackFrame) result.getArtifact(); 225 } 226 if (frame != null) { 228 IDebugEditorPresentation editorPresentation = getEditorPresentation(); 229 if (editorPresentation.addAnnotations(editor, frame)) { 230 Decoration decoration = new StandardDecoration(editorPresentation, editor, frame.getThread()); 231 DecorationManager.addDecoration(decoration); 232 } else { 233 ITextEditor textEditor = null; 235 if (editor instanceof ITextEditor) { 236 textEditor = (ITextEditor)editor; 237 } else { 238 textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class); 239 } 240 if (textEditor != null) { 241 positionEditor(textEditor, frame); 242 Annotation annotation = fPresentation.getInstructionPointerAnnotation(textEditor, frame); 243 InstructionPointerManager.getDefault().addAnnotation(textEditor, frame, annotation); 244 } 245 } 246 } 247 } 248 249 254 private IEditorPart openEditor(ISourceLookupResult result, IWorkbenchPage page) { 255 IEditorPart editor = null; 256 IEditorInput input= result.getEditorInput(); 257 String id= result.getEditorId(); 258 if (input == null || id == null) { 259 return null; 260 } 261 262 if (fReuseEditor) { 263 IEditorReference[] references = page.findEditors(input, id, IWorkbenchPage.MATCH_ID | IWorkbenchPage.MATCH_INPUT); 264 if (references.length > 0) { 265 IEditorPart refEditor= references[0].getEditor(false); 267 editor = refEditor; 268 page.bringToTop(editor); 269 } 270 if (editor == null) { 271 IEditorPart editorForPage = getEditor(page); 272 if (editorForPage == null || editorForPage.isDirty() || page.isEditorPinned(editorForPage)) { 273 editor = openEditor(page, input, id); 275 editorForPage = editor; 276 } else if (editorForPage instanceof IReusableEditor && editorForPage.getSite().getId().equals(id)) { 277 page.reuseEditor((IReusableEditor)editorForPage, input); 279 editor = editorForPage; 280 if(!page.isPartVisible(editor)) { 281 page.bringToTop(editor); 282 } 283 } else { 284 editor = openEditor(page, input, id); 286 page.closeEditor(editorForPage, false); 287 editorForPage = editor; 288 } 289 setEditor(page, editorForPage); 290 } 291 } else { 292 editor = openEditor(page, input, id); 294 } 295 return editor; 296 } 297 298 301 private void positionEditor(ITextEditor editor, IStackFrame frame) { 302 try { 303 int charStart = frame.getCharStart(); 304 if (charStart > 0) { 305 int length = 0; 306 int charEnd = frame.getCharEnd(); 307 if (charEnd > 0) { 308 length = charEnd - charStart; 309 } 310 editor.selectAndReveal(charStart, length); 311 return; 312 } 313 int lineNumber = frame.getLineNumber(); 314 lineNumber--; IRegion region= getLineInformation(editor, lineNumber); 316 if (region != null) { 317 editor.selectAndReveal(region.getOffset(), 0); 318 } 319 } catch (DebugException e) { 320 } 321 } 322 323 326 private IRegion getLineInformation(ITextEditor editor, int lineNumber) { 327 IDocumentProvider provider= editor.getDocumentProvider(); 328 IEditorInput input= editor.getEditorInput(); 329 try { 330 provider.connect(input); 331 } catch (CoreException e) { 332 return null; 333 } 334 try { 335 IDocument document= provider.getDocument(input); 336 if (document != null) 337 return document.getLineInformation(lineNumber); 338 } catch (BadLocationException e) { 339 } finally { 340 provider.disconnect(input); 341 } 342 return null; 343 } 344 349 private IEditorPart openEditor(final IWorkbenchPage page, final IEditorInput input, final String id) { 350 final IEditorPart[] editor = new IEditorPart[] {null}; 351 Runnable r = new Runnable () { 352 public void run() { 353 if (!page.getWorkbenchWindow().getWorkbench().isClosing()) { 354 try { 355 editor[0] = page.openEditor(input, id, false, IWorkbenchPage.MATCH_ID|IWorkbenchPage.MATCH_INPUT); 356 } catch (PartInitException e) { 357 DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), 358 DebugUIViewsMessages.LaunchView_Error_1, 359 DebugUIViewsMessages.LaunchView_Exception_occurred_opening_editor_for_debugger__2, 360 e); 361 } 362 } 363 } 364 }; 365 BusyIndicator.showWhile(DebugUIPlugin.getStandardDisplay(), r); 366 return editor[0]; 367 } 368 369 372 public void pageActivated(IWorkbenchPage page) { 373 } 374 375 378 public void pageClosed(IWorkbenchPage page) { 379 fEditorsByPage.remove(page); 380 page.removePartListener(this); 381 } 382 383 386 public void pageOpened(IWorkbenchPage page) { 387 page.addPartListener(this); 388 } 389 390 393 public void partActivated(IWorkbenchPartReference partRef) { 394 } 395 396 399 public void partBroughtToTop(IWorkbenchPartReference partRef) { 400 } 401 402 405 public void partClosed(IWorkbenchPartReference partRef) { 406 IWorkbenchPage page = partRef.getPage(); 408 IEditorPart editor = getEditor(page); 409 IWorkbenchPart part = partRef.getPart(false); 410 if (part != null && part.equals(editor)) { 411 fEditorsByPage.remove(page); 412 } 413 } 414 415 418 public void partDeactivated(IWorkbenchPartReference partRef) { 419 } 420 421 424 public void partOpened(IWorkbenchPartReference partRef) { 425 } 426 427 430 public void partHidden(IWorkbenchPartReference partRef) { 431 } 432 433 436 public void partVisible(IWorkbenchPartReference partRef) { 437 } 438 439 442 public void partInputChanged(IWorkbenchPartReference partRef) { 443 } 444 445 448 public void propertyChange(PropertyChangeEvent event) { 449 String property = event.getProperty(); 450 if (property.equals(IDebugUIConstants.PREF_REUSE_EDITOR)) { 451 fReuseEditor = DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IDebugUIConstants.PREF_REUSE_EDITOR); 452 } 453 } 454 455 463 protected IEditorPart getEditor(IWorkbenchPage page) { 464 return (IEditorPart) fEditorsByPage.get(page); 465 } 466 467 475 protected void setEditor(IWorkbenchPage page, IEditorPart editorPart) { 476 if (editorPart == null) { 477 fEditorsByPage.remove(page); 478 } else { 479 fEditorsByPage.put(page, editorPart); 480 } 481 page.addPartListener(this); 482 page.getWorkbenchWindow().addPageListener(this); 483 } 484 485 488 protected void dispose() { 489 DebugUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this); 490 fEditorsByPage.clear(); 491 fPresentation.dispose(); 492 } 493 } 494 | Popular Tags |