KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > elements > adapters > StackFrameSourceDisplayAdapter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.elements.adapters;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.core.runtime.jobs.Job;
17 import org.eclipse.debug.core.DebugEvent;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.IDebugEventSetListener;
20 import org.eclipse.debug.core.model.IDebugElement;
21 import org.eclipse.debug.core.model.IDebugTarget;
22 import org.eclipse.debug.core.model.ISourceLocator;
23 import org.eclipse.debug.core.model.IStackFrame;
24 import org.eclipse.debug.core.model.IThread;
25 import org.eclipse.debug.internal.ui.InstructionPointerManager;
26 import org.eclipse.debug.internal.ui.sourcelookup.SourceLookupResult;
27 import org.eclipse.debug.internal.ui.views.launch.DecorationManager;
28 import org.eclipse.debug.ui.DebugUITools;
29 import org.eclipse.debug.ui.sourcelookup.ISourceDisplay;
30 import org.eclipse.debug.ui.sourcelookup.ISourceLookupResult;
31 import org.eclipse.ui.IWorkbenchPage;
32 import org.eclipse.ui.progress.UIJob;
33
34 /**
35  * @since 3.2
36  */

37 public class StackFrameSourceDisplayAdapter implements ISourceDisplay {
38
39     private IStackFrame fPrevFrame;
40     private SourceLookupResult fPrevResult;
41     
42     /**
43      * Constructs singleton source display adapter for stack frames.
44      */

45     public StackFrameSourceDisplayAdapter() {
46         DebugPlugin.getDefault().addDebugEventListener(new IDebugEventSetListener() {
47             public void handleDebugEvents(DebugEvent[] events) {
48                 for (int i = 0; i < events.length; i++) {
49                     final DebugEvent event = events[i];
50                     switch (event.getKind()) {
51                         case DebugEvent.TERMINATE:
52                             clearCachedModel(event.getSource());
53                             // fall through
54
case DebugEvent.RESUME:
55                             if (!event.isEvaluation()) {
56                                 Job uijob = new UIJob("clear source selection"){ //$NON-NLS-1$
57
public IStatus runInUIThread(
58                                             IProgressMonitor monitor) {
59                                         clearSourceSelection(event.getSource());
60                                         return Status.OK_STATUS;
61                                     }
62                                     
63                                 };
64                                 uijob.setSystem(true);
65                                 uijob.schedule();
66                             }
67                             break;
68                         case DebugEvent.CHANGE:
69                             if (event.getSource() instanceof IStackFrame) {
70                                 if (event.getDetail() == DebugEvent.CONTENT) {
71                                     // force source lookup if a stack frame fires a content change event
72
clearCachedModel(event.getSource());
73                                 }
74                             }
75                             break;
76                     }
77                 }
78             }
79         });
80     }
81     
82     
83     private SourceLookupJob fSourceLookupJob = new SourceLookupJob();
84     /**
85      * A job to perform source lookup on the currently selected stack frame.
86      */

87     class SourceLookupJob extends Job {
88         
89         private IStackFrame fTarget;
90         private ISourceLocator fLocator;
91         private IWorkbenchPage fPage;
92
93         /**
94          * Constructs a new source lookup job.
95          */

96         public SourceLookupJob() {
97             super("Debug Source Lookup"); //$NON-NLS-1$
98
setPriority(Job.INTERACTIVE);
99             setSystem(true);
100         }
101         
102         public void setLookupInfo(IStackFrame frame, ISourceLocator locator, IWorkbenchPage page) {
103             fTarget = frame;
104             fLocator = locator;
105             fPage = page;
106         }
107
108         /* (non-Javadoc)
109          * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
110          */

111         protected IStatus run(IProgressMonitor monitor) {
112             if (!monitor.isCanceled()) {
113                 IStackFrame lookupFrame = fTarget;
114                 ISourceLocator lookupLocator = fLocator;
115                 
116                 if (lookupFrame != null && lookupLocator != null && !lookupFrame.isTerminated()) {
117                     ISourceLookupResult result = null;
118                     result = DebugUITools.lookupSource(lookupFrame, lookupLocator);
119                     synchronized (StackFrameSourceDisplayAdapter.this) {
120                         fPrevResult = (SourceLookupResult)result;
121                         fPrevFrame = lookupFrame;
122                     }
123                     if (!monitor.isCanceled() && fPage != null && !lookupFrame.isTerminated()) {
124                         fSourceDisplayJob.setDisplayInfo(result, fPage);
125                         fSourceDisplayJob.schedule();
126                     }
127                 }
128                 setLookupInfo(null, null, null);
129             }
130             return Status.OK_STATUS;
131         }
132         
133     }
134     
135     private SourceDisplayJob fSourceDisplayJob = new SourceDisplayJob();
136     class SourceDisplayJob extends UIJob {
137         
138         private ISourceLookupResult fResult;
139         private IWorkbenchPage fPage;
140
141         public SourceDisplayJob() {
142             super("Debug Source Display"); //$NON-NLS-1$
143
setSystem(true);
144             setPriority(Job.INTERACTIVE);
145         }
146         
147         /**
148          * Constructs a new source display job
149          */

150         public synchronized void setDisplayInfo(ISourceLookupResult result, IWorkbenchPage page) {
151             fResult = result;
152             fPage = page;
153         }
154
155         /* (non-Javadoc)
156          * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
157          */

158         public IStatus runInUIThread(IProgressMonitor monitor) {
159             ISourceLookupResult result = null;
160             IWorkbenchPage page = null;
161             synchronized (this) {
162                 result = fResult;
163                 page = fPage;
164                 setDisplayInfo(null, null);
165             }
166             if (!monitor.isCanceled() && result != null && page != null) {
167                 DebugUITools.displaySource(result, page);
168                 // termination may have occurred while displaying source
169
if (monitor.isCanceled()) {
170                     Object JavaDoc artifact = result.getArtifact();
171                     if (artifact instanceof IStackFrame) {
172                         clearSourceSelection(((IStackFrame)artifact).getThread());
173                     }
174                 }
175             }
176             
177             return Status.OK_STATUS;
178         }
179         
180     }
181
182     /* (non-Javadoc)
183      * @see org.eclipse.debug.ui.contexts.ISourceDisplayAdapter#displaySource(java.lang.Object, org.eclipse.ui.IWorkbenchPage, boolean)
184      */

185     public synchronized void displaySource(Object JavaDoc context, IWorkbenchPage page, boolean force) {
186         IStackFrame frame = (IStackFrame)context;
187         if (!force && frame.equals(fPrevFrame)) {
188             fPrevResult.updateArtifact(context);
189             fSourceDisplayJob.setDisplayInfo(fPrevResult, page);
190             fSourceDisplayJob.schedule();
191         } else {
192             fSourceLookupJob.setLookupInfo(frame, frame.getLaunch().getSourceLocator(), page);
193             fSourceLookupJob.schedule();
194         }
195     }
196     
197     /**
198      * Clears any source decorations associated with the given thread or
199      * debug target.
200      *
201      * @param source thread or debug target
202      */

203     private void clearSourceSelection(Object JavaDoc source) {
204         if (source instanceof IThread) {
205             IThread thread = (IThread)source;
206             DecorationManager.removeDecorations(thread);
207             InstructionPointerManager.getDefault().removeAnnotations(thread);
208         } else if (source instanceof IDebugTarget) {
209             IDebugTarget target = (IDebugTarget)source;
210             DecorationManager.removeDecorations(target);
211             InstructionPointerManager.getDefault().removeAnnotations(target);
212         }
213     }
214     
215     /**
216      * Clear any cached results associated with the given object.
217      *
218      * @param source
219      */

220     private synchronized void clearCachedModel(Object JavaDoc source) {
221         if (fPrevFrame != null) {
222             IDebugTarget target = null;
223             if (source instanceof IDebugElement) {
224                 target = ((IDebugElement)source).getDebugTarget();
225             }
226             if (fPrevFrame.getDebugTarget().equals(target)) {
227                 fPrevFrame = null;
228                 fPrevResult = null;
229                 fSourceDisplayJob.cancel();
230             }
231         }
232     }
233     
234 }
235
Popular Tags