1 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 37 public class StackFrameSourceDisplayAdapter implements ISourceDisplay { 38 39 private IStackFrame fPrevFrame; 40 private SourceLookupResult fPrevResult; 41 42 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 case DebugEvent.RESUME: 55 if (!event.isEvaluation()) { 56 Job uijob = new UIJob("clear source selection"){ 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 clearCachedModel(event.getSource()); 73 } 74 } 75 break; 76 } 77 } 78 } 79 }); 80 } 81 82 83 private SourceLookupJob fSourceLookupJob = new SourceLookupJob(); 84 87 class SourceLookupJob extends Job { 88 89 private IStackFrame fTarget; 90 private ISourceLocator fLocator; 91 private IWorkbenchPage fPage; 92 93 96 public SourceLookupJob() { 97 super("Debug Source Lookup"); 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 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"); setSystem(true); 144 setPriority(Job.INTERACTIVE); 145 } 146 147 150 public synchronized void setDisplayInfo(ISourceLookupResult result, IWorkbenchPage page) { 151 fResult = result; 152 fPage = page; 153 } 154 155 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 if (monitor.isCanceled()) { 170 Object 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 185 public synchronized void displaySource(Object 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 203 private void clearSourceSelection(Object 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 220 private synchronized void clearCachedModel(Object 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 |