1 11 package org.eclipse.debug.internal.ui; 12 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 import org.eclipse.debug.core.DebugException; 20 import org.eclipse.debug.core.model.IDebugTarget; 21 import org.eclipse.debug.core.model.IStackFrame; 22 import org.eclipse.debug.core.model.IThread; 23 import org.eclipse.jface.text.BadLocationException; 24 import org.eclipse.jface.text.IDocument; 25 import org.eclipse.jface.text.IRegion; 26 import org.eclipse.jface.text.Position; 27 import org.eclipse.jface.text.source.Annotation; 28 import org.eclipse.jface.text.source.IAnnotationModel; 29 import org.eclipse.ui.IEditorInput; 30 import org.eclipse.ui.IPageListener; 31 import org.eclipse.ui.IPartListener2; 32 import org.eclipse.ui.IWorkbenchPage; 33 import org.eclipse.ui.IWorkbenchPart; 34 import org.eclipse.ui.IWorkbenchPartReference; 35 import org.eclipse.ui.texteditor.IDocumentProvider; 36 import org.eclipse.ui.texteditor.ITextEditor; 37 38 43 public class InstructionPointerManager{ 44 45 48 private static InstructionPointerManager fgDefault; 49 50 53 private Set fIPCSet = new HashSet (); 54 55 58 private Map fEditorMap = new HashMap (); 59 60 64 private IPartListener2 fPartListener; 65 66 69 private IPageListener fPageListener; 70 71 74 private InstructionPointerManager() { 75 } 76 77 80 public static InstructionPointerManager getDefault() { 81 if (fgDefault == null) { 82 fgDefault = new InstructionPointerManager(); 83 } 84 return fgDefault; 85 } 86 87 91 public void addAnnotation(ITextEditor textEditor, IStackFrame frame, Annotation annotation) { 92 93 IDocumentProvider docProvider = textEditor.getDocumentProvider(); 94 IEditorInput editorInput = textEditor.getEditorInput(); 95 IAnnotationModel annModel = docProvider.getAnnotationModel(editorInput); 97 if (annModel == null) { 98 return; 99 } 100 101 Position position = null; 103 int charStart = -1; 104 int length = -1; 105 try { 106 charStart = frame.getCharStart(); 107 length = frame.getCharEnd() - charStart; 108 } catch (DebugException de) { 109 } 110 if (charStart < 0) { 111 IDocument doc = docProvider.getDocument(editorInput); 112 if (doc == null) { 113 return; 114 } 115 try { 116 int lineNumber = frame.getLineNumber() - 1; 117 IRegion region = doc.getLineInformation(lineNumber); 118 charStart = region.getOffset(); 119 length = region.getLength(); 120 } catch (BadLocationException ble) { 121 return; 122 } catch (DebugException de) { 123 return; 124 } 125 } 126 if (charStart < 0) { 127 return; 128 } 129 position = new Position(charStart, length); 130 131 if (frame.isTerminated()) { 132 return; 133 } 134 135 synchronized (fIPCSet) { 136 137 annModel.removeAnnotation(annotation); 139 annModel.addAnnotation(annotation, position); 140 141 InstructionPointerContext ipc = new InstructionPointerContext(frame.getDebugTarget(), frame.getThread(), textEditor, annotation); 143 144 Set editorIPCs = (Set )fEditorMap.get(textEditor); 146 if (editorIPCs == null){ 147 editorIPCs = new HashSet (); 148 fEditorMap.put(textEditor, editorIPCs); 149 } else { 150 editorIPCs.remove(ipc); 151 } 152 editorIPCs.add(ipc); 153 fIPCSet.remove(ipc); 154 fIPCSet.add(ipc); 155 156 textEditor.getSite().getPage().addPartListener(getPartListener()); 158 textEditor.getSite().getPage().getWorkbenchWindow().addPageListener(getPageListener()); 159 } 160 } 161 162 166 public void removeAnnotations(IDebugTarget debugTarget) { 167 synchronized (fIPCSet) { 168 Iterator ipcIter = fIPCSet.iterator(); 169 while (ipcIter.hasNext()) { 170 InstructionPointerContext currentIPC = (InstructionPointerContext) ipcIter.next(); 171 if (currentIPC.getDebugTarget().equals(debugTarget)){ 172 removeAnnotationFromModel(currentIPC); 173 ipcIter.remove(); 174 removeAnnotationFromEditorMapping(currentIPC); 175 } 176 } 177 } 178 } 179 180 184 public void removeAnnotations(IThread thread) { 185 synchronized (fIPCSet) { 186 Iterator ipcIter = fIPCSet.iterator(); 187 while (ipcIter.hasNext()) { 188 InstructionPointerContext currentIPC = (InstructionPointerContext) ipcIter.next(); 189 if (currentIPC.getThread().equals(thread)){ 190 removeAnnotationFromModel(currentIPC); 191 ipcIter.remove(); 192 removeAnnotationFromEditorMapping(currentIPC); 193 } 194 } 195 } 196 } 197 198 202 public void removeAnnotations(ITextEditor editor) { 203 synchronized (fIPCSet) { 204 Set editorIPCs = (Set )fEditorMap.get(editor); 205 if (editorIPCs != null){ 206 Iterator ipcIter = editorIPCs.iterator(); 207 while (ipcIter.hasNext()) { 208 InstructionPointerContext currentIPC = (InstructionPointerContext) ipcIter.next(); 209 removeAnnotationFromModel(currentIPC); 210 fIPCSet.remove(currentIPC); 211 } 212 fEditorMap.remove(editor); 213 } 214 } 215 } 216 217 220 private void removeAnnotationFromEditorMapping(InstructionPointerContext ipc) { 221 Set editorIPCs = (Set )fEditorMap.get(ipc.getEditor()); 222 if (editorIPCs != null){ 223 editorIPCs.remove(ipc); 224 if (editorIPCs.isEmpty()){ 225 fEditorMap.remove(ipc.getEditor()); 226 } 227 } 228 229 } 230 231 234 private void removeAnnotationFromModel(InstructionPointerContext ipc){ 235 IDocumentProvider docProvider = ipc.getEditor().getDocumentProvider(); 236 if (docProvider != null) { 237 IAnnotationModel annotationModel = docProvider.getAnnotationModel(ipc.getEditor().getEditorInput()); 238 if (annotationModel != null) { 239 annotationModel.removeAnnotation(ipc.getAnnotation()); 240 } 241 } 242 } 243 244 251 public int getInstructionPointerCount() { 252 return fIPCSet.size(); 253 } 254 255 262 public int getEditorMappingCount() { 263 return fEditorMap.size(); 264 } 265 266 269 private IPageListener getPageListener(){ 270 if (fPageListener == null){ 271 fPageListener = new PageListener(); 272 } 273 return fPageListener; 274 } 275 276 279 private IPartListener2 getPartListener(){ 280 if (fPartListener == null){ 281 fPartListener = new PartListener(); 282 } 283 return fPartListener; 284 } 285 286 290 class PartListener implements IPartListener2{ 291 public void partActivated(IWorkbenchPartReference partRef) {} 292 public void partDeactivated(IWorkbenchPartReference partRef) {} 293 public void partHidden(IWorkbenchPartReference partRef) {} 294 public void partOpened(IWorkbenchPartReference partRef) {} 295 public void partVisible(IWorkbenchPartReference partRef) {} 296 public void partBroughtToTop(IWorkbenchPartReference partRef) {} 297 298 301 public void partClosed(IWorkbenchPartReference partRef) { 302 IWorkbenchPart part = partRef.getPart(false); 303 if (part instanceof ITextEditor){ 304 removeAnnotations((ITextEditor)part); 305 } 306 307 } 308 309 312 public void partInputChanged(IWorkbenchPartReference partRef) { 313 IWorkbenchPart part = partRef.getPart(false); 314 if (part instanceof ITextEditor){ 315 removeAnnotations((ITextEditor)part); 316 } 317 } 318 } 319 320 323 class PageListener implements IPageListener{ 324 325 public void pageActivated(IWorkbenchPage page) {} 326 public void pageOpened(IWorkbenchPage page) {} 327 328 331 public void pageClosed(IWorkbenchPage page) { 332 page.removePartListener(getPartListener()); 333 page.getWorkbenchWindow().removePageListener(getPageListener()); 334 } 335 336 } 337 338 } 339 | Popular Tags |