KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > sourcelookup > SourceLookupFacility


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.sourcelookup;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
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 /**
62  * Utility methods for looking up and displaying source.
63  *
64  * @since 3.1
65  */

66 public class SourceLookupFacility implements IPageListener, IPartListener2, IPropertyChangeListener {
67     
68     /**
69      * Singleton source lookup facility
70      */

71     private static SourceLookupFacility fgDefault;
72     
73     /**
74      * Contains a map of the editor to use for each workbench
75      * page, when the 'reuse editor' preference is on.
76      */

77     private Map JavaDoc fEditorsByPage;
78     
79     /**
80      * Used to generate annotations for stack frames
81      */

82     private IInstructionPointerPresentation fPresentation = (IInstructionPointerPresentation) DebugUITools.newDebugModelPresentation();
83     
84     /**
85      * Whether to re-use editors when displaying source.
86      */

87     private boolean fReuseEditor = DebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IDebugUIConstants.PREF_REUSE_EDITOR);
88     
89     /**
90      * Returns the source lookup facility
91      * @return
92      */

93     public static SourceLookupFacility getDefault() {
94         if (fgDefault == null) {
95             fgDefault = new SourceLookupFacility();
96         }
97         return fgDefault;
98     }
99     
100     /**
101      * Performs cleanup
102      */

103     public static void shutdown() {
104         if (fgDefault != null) {
105             fgDefault.dispose();
106         }
107     }
108     
109     /**
110      * Constructs a source lookup facility.
111      */

112     private SourceLookupFacility() {
113         fEditorsByPage = new HashMap JavaDoc();
114         DebugUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this);
115     }
116     
117     /**
118      * Performs source lookup for the given artifact and returns the result.
119      *
120      * @param artifact object for which source is to be resolved
121      * @param locator the source locator to use, or <code>null</code>. When <code>null</code>
122      * a source locator is determined from the artifact, if possible. If the artifact
123      * is a debug element, the source locator from its associated launch is used.
124      * @return a source lookup result
125      */

126     public SourceLookupResult lookup(Object JavaDoc 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 JavaDoc editorId =null;
143             IEditorInput editorInput = null;
144             Object JavaDoc 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     /**
188      * Returns the model presentation for the given debug model, or <code>null</code>
189      * if none.
190      *
191      * @param id debug model id
192      * @return presentation for the model, or <code>null</code> if none.
193      */

194     protected IDebugModelPresentation getPresentation(String JavaDoc id) {
195         return ((DelegatingModelPresentation)DebugUIPlugin.getModelPresentation()).getPresentation(id);
196     }
197     
198     /**
199      * Returns an editor presentation.
200      *
201      * @return an editor presentation
202      */

203     protected IDebugEditorPresentation getEditorPresentation() {
204         return (DelegatingModelPresentation)DebugUIPlugin.getModelPresentation();
205     }
206     
207     /**
208      * Opens an editor in the given workbench page for the given source lookup
209      * result. Has no effect if the result has an unknown editor id or editor input.
210      * The editor is opened, positioned, and annotated.
211      * <p>
212      * Honor's the user preference of whether to re-use editors when displaying source.
213      * </p>
214      * @param result source lookup result to display
215      * @param page the page to display the result in
216      */

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         // position and annotate editor for stack frame
227
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                 // perform standard positioning and annotations
234
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     /**
250      * Opens the editor used to display the source for an element selected in
251      * this view and returns the editor that was opened or <code>null</code> if
252      * no editor could be opened.
253      */

254     private IEditorPart openEditor(ISourceLookupResult result, IWorkbenchPage page) {
255         IEditorPart editor = null;
256         IEditorInput input= result.getEditorInput();
257         String JavaDoc 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                 // activate the editor we want to reuse
266
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                     // open a new editor
274
editor = openEditor(page, input, id);
275                     editorForPage = editor;
276                 } else if (editorForPage instanceof IReusableEditor && editorForPage.getSite().getId().equals(id)) {
277                     // re-use editor
278
page.reuseEditor((IReusableEditor)editorForPage, input);
279                     editor = editorForPage;
280                     if(!page.isPartVisible(editor)) {
281                         page.bringToTop(editor);
282                     }
283                 } else {
284                     // close editor, open a new one
285
editor = openEditor(page, input, id);
286                     page.closeEditor(editorForPage, false);
287                     editorForPage = editor;
288                 }
289                 setEditor(page, editorForPage);
290             }
291         } else {
292             // Open a new editor
293
editor = openEditor(page, input, id);
294         }
295         return editor;
296     }
297     
298     /**
299      * Positions the text editor for the given stack frame
300      */

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--; // Document line numbers are 0-based. Debug line numbers are 1-based.
315
IRegion region= getLineInformation(editor, lineNumber);
316             if (region != null) {
317                 editor.selectAndReveal(region.getOffset(), 0);
318             }
319         } catch (DebugException e) {
320         }
321     }
322     
323     /**
324      * Returns the line information for the given line in the given editor
325      */

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     /**
345      * Opens an editor in the workbench and returns the editor that was opened
346      * or <code>null</code> if an error occurred while attempting to open the
347      * editor.
348      */

349     private IEditorPart openEditor(final IWorkbenchPage page, final IEditorInput input, final String JavaDoc id) {
350         final IEditorPart[] editor = new IEditorPart[] {null};
351         Runnable JavaDoc r = new Runnable JavaDoc() {
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     /* (non-Javadoc)
370      * @see org.eclipse.ui.IPageListener#pageActivated(org.eclipse.ui.IWorkbenchPage)
371      */

372     public void pageActivated(IWorkbenchPage page) {
373     }
374
375     /* (non-Javadoc)
376      * @see org.eclipse.ui.IPageListener#pageClosed(org.eclipse.ui.IWorkbenchPage)
377      */

378     public void pageClosed(IWorkbenchPage page) {
379         fEditorsByPage.remove(page);
380         page.removePartListener(this);
381     }
382
383     /* (non-Javadoc)
384      * @see org.eclipse.ui.IPageListener#pageOpened(org.eclipse.ui.IWorkbenchPage)
385      */

386     public void pageOpened(IWorkbenchPage page) {
387         page.addPartListener(this);
388     }
389
390     /* (non-Javadoc)
391      * @see org.eclipse.ui.IPartListener2#partActivated(org.eclipse.ui.IWorkbenchPartReference)
392      */

393     public void partActivated(IWorkbenchPartReference partRef) {
394     }
395
396     /* (non-Javadoc)
397      * @see org.eclipse.ui.IPartListener2#partBroughtToTop(org.eclipse.ui.IWorkbenchPartReference)
398      */

399     public void partBroughtToTop(IWorkbenchPartReference partRef) {
400     }
401
402     /* (non-Javadoc)
403      * @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
404      */

405     public void partClosed(IWorkbenchPartReference partRef) {
406         // clear the cached editor for the page if it has been closed
407
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     /* (non-Javadoc)
416      * @see org.eclipse.ui.IPartListener2#partDeactivated(org.eclipse.ui.IWorkbenchPartReference)
417      */

418     public void partDeactivated(IWorkbenchPartReference partRef) {
419     }
420
421     /* (non-Javadoc)
422      * @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
423      */

424     public void partOpened(IWorkbenchPartReference partRef) {
425     }
426
427     /* (non-Javadoc)
428      * @see org.eclipse.ui.IPartListener2#partHidden(org.eclipse.ui.IWorkbenchPartReference)
429      */

430     public void partHidden(IWorkbenchPartReference partRef) {
431     }
432
433     /* (non-Javadoc)
434      * @see org.eclipse.ui.IPartListener2#partVisible(org.eclipse.ui.IWorkbenchPartReference)
435      */

436     public void partVisible(IWorkbenchPartReference partRef) {
437     }
438
439     /* (non-Javadoc)
440      * @see org.eclipse.ui.IPartListener2#partInputChanged(org.eclipse.ui.IWorkbenchPartReference)
441      */

442     public void partInputChanged(IWorkbenchPartReference partRef) {
443     }
444
445     /* (non-Javadoc)
446      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
447      */

448     public void propertyChange(PropertyChangeEvent event) {
449         String JavaDoc 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     /**
456      * Returns the editor to use to display source in the given page, or
457      * <code>null</code> if a new editor should be opened.
458      *
459      * @param page workbench page
460      * @return the editor to use to display source in the given page, or
461      * <code>null</code> if a new editor should be opened
462      */

463     protected IEditorPart getEditor(IWorkbenchPage page) {
464         return (IEditorPart) fEditorsByPage.get(page);
465     }
466     
467     /**
468      * Sets the editor to use to display source in the given page, or
469      * <code>null</code> if a new editor should be opened.
470      *
471      * @param page workbench page
472      * @return the editor to use to display source in the given page, or
473      * <code>null</code> if a new editor should be opened
474      */

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     /**
486      * Performs cleanup.
487      */

488     protected void dispose() {
489         DebugUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this);
490         fEditorsByPage.clear();
491         fPresentation.dispose();
492     }
493 }
494
Popular Tags