Skip to main content
summaryrefslogtreecommitdiffstats
blob: 51a6ab472a81242232ed3795aad23d9a9c3c942d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*******************************************************************************
 * Copyright (c) 2000, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.debug.core.model.IStackFrame;
import org.eclipse.debug.core.model.IThread;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPageListener;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * This class tracks instruction pointer contexts for all active debug targets and threads
 * in the current workbench.  There should only ever be one instance of this class, obtained
 * via 'getDefault()'.
 */
public class InstructionPointerManager{

	/**
	 * The singleton instance of this class.
	 */
	private static InstructionPointerManager fgDefault;

	/**
	 * Set containing all instruction pointer contexts this class manages
	 */
	private Set fIPCSet = new HashSet();
	
	/**
	 * Maps ITextEditors to the set of instruction pointer contexts that are displayed in the editor
	 */
	private Map fEditorMap = new HashMap();
	
	/**
	 * Part listener added to editors that contain annotations.  Allows instruction pointer contexts to
	 * be removed when the editor they are displayed in is removed.
	 */
	private IPartListener2 fPartListener;
	
	/**
	 * Page listener added to the workbench window to remove part listeners when the page is closed.  
	 */
	private IPageListener fPageListener;
	
	/**
	 * Clients must not instantiate this class.
	 */
	private InstructionPointerManager() {
	}
	
	/**
	 * Return the singleton instance of this class, creating it if necessary.
	 */
	public static InstructionPointerManager getDefault() {
		if (fgDefault == null) {
			fgDefault = new InstructionPointerManager();
		}
		return fgDefault;
	}
	
	/**
	 * Adds an instruction pointer annotation in the specified editor for the 
	 * specified stack frame.
	 */
	public void addAnnotation(ITextEditor textEditor, IStackFrame frame, Annotation annotation) {
		
		IDocumentProvider docProvider = textEditor.getDocumentProvider();
		IEditorInput editorInput = textEditor.getEditorInput();
        // If there is no annotation model, there's nothing more to do
        IAnnotationModel annModel = docProvider.getAnnotationModel(editorInput);
        if (annModel == null) {
            return;
        }        
		
		// Create the Position object that specifies a location for the annotation
		Position position = null;
		int charStart = -1;
		int length = -1; 
		try {
			charStart = frame.getCharStart();
			length = frame.getCharEnd() - charStart;
		} catch (DebugException de) {
		}
		if (charStart < 0) {
			IDocument doc = docProvider.getDocument(editorInput);
			if (doc == null) {
				return;
			}
			try {
				int lineNumber = frame.getLineNumber() - 1;
				IRegion region = doc.getLineInformation(lineNumber);
				charStart = region.getOffset();
				length = region.getLength();
			} catch (BadLocationException ble) {
				return;
			} catch (DebugException de) {
				return;
			}
		}
		if (charStart < 0) {
			return;
		}
		position = new Position(charStart, length);
		
		if (frame.isTerminated()) {
			return;
		}
		
		synchronized (fIPCSet) {
					
			// Add the annotation at the position to the editor's annotation model.
			annModel.removeAnnotation(annotation);
			annModel.addAnnotation(annotation, position);
			
			// Create the instruction pointer context
			InstructionPointerContext ipc = new InstructionPointerContext(frame.getDebugTarget(), frame.getThread(), textEditor, annotation);
			
			// Add the IPC to the set and map
			Set editorIPCs = (Set)fEditorMap.get(textEditor);
			if (editorIPCs == null){
				editorIPCs = new HashSet();
				fEditorMap.put(textEditor, editorIPCs);
			} else {
				editorIPCs.remove(ipc);
			}
			editorIPCs.add(ipc);
			fIPCSet.remove(ipc);
			fIPCSet.add(ipc);
			
			// Add a listener to the editor so we can remove the IPC when the editor is closed
			textEditor.getSite().getPage().addPartListener(getPartListener());
			textEditor.getSite().getPage().getWorkbenchWindow().addPageListener(getPageListener());
		}
	}
	
	/**
	 * Remove all annotations associated with the specified debug target that this class
	 * is tracking.
	 */
	public void removeAnnotations(IDebugTarget debugTarget) {
		synchronized (fIPCSet) {
			Iterator ipcIter = fIPCSet.iterator();
			while (ipcIter.hasNext()) {
				InstructionPointerContext currentIPC = (InstructionPointerContext) ipcIter.next();
				if (currentIPC.getDebugTarget().equals(debugTarget)){
					removeAnnotationFromModel(currentIPC);
					ipcIter.remove();
					removeAnnotationFromEditorMapping(currentIPC);
				}
			}
		}
	}
	
	/**
	 * Remove all annotations associated with the specified thread that this class
	 * is tracking.
	 */
	public void removeAnnotations(IThread thread) {
		synchronized (fIPCSet) {
			Iterator ipcIter = fIPCSet.iterator();
			while (ipcIter.hasNext()) {
				InstructionPointerContext currentIPC = (InstructionPointerContext) ipcIter.next();
				if (currentIPC.getThread().equals(thread)){
					removeAnnotationFromModel(currentIPC);
					ipcIter.remove();
					removeAnnotationFromEditorMapping(currentIPC);
				}
			}
		}
	}
	
	/**
	 * Remove all annotations associated with the specified editor that this class
	 * is tracking.
	 */
	public void removeAnnotations(ITextEditor editor) {
		synchronized (fIPCSet) {
			Set editorIPCs = (Set)fEditorMap.get(editor);
			if (editorIPCs != null){
				Iterator ipcIter = editorIPCs.iterator();
				while (ipcIter.hasNext()) {
					InstructionPointerContext currentIPC = (InstructionPointerContext) ipcIter.next();
					removeAnnotationFromModel(currentIPC);
					fIPCSet.remove(currentIPC);
				}
				fEditorMap.remove(editor);
			}
		}
	}

	/**
	 * Remove the given ipc from the mapping of editors.
	 */
	private void removeAnnotationFromEditorMapping(InstructionPointerContext ipc) {
		Set editorIPCs = (Set)fEditorMap.get(ipc.getEditor());
		if (editorIPCs != null){
			editorIPCs.remove(ipc);
			if (editorIPCs.isEmpty()){
				fEditorMap.remove(ipc.getEditor());
			}
		}
		
	}
	
	/**
	 * Remove the annotation from the document model.
	 */
	private void removeAnnotationFromModel(InstructionPointerContext ipc){
		IDocumentProvider docProvider = ipc.getEditor().getDocumentProvider();
		if (docProvider != null) {
			IAnnotationModel annotationModel = docProvider.getAnnotationModel(ipc.getEditor().getEditorInput());
			if (annotationModel != null) {
				annotationModel.removeAnnotation(ipc.getAnnotation());
			}
		}
	}
	
	/**
	 * Returns the number of instruction pointers.
	 * Used by the test suite.
	 * 
	 * @return the number of instruction pointers
	 * @since 3.2
	 */
	public int getInstructionPointerCount() {
		return fIPCSet.size();
	}
	
	/**
	 * Returns the number of keys in the editor to IPC mapping
	 * Used by the test suite.
	 * 
	 * @return the number of keys in the editor mapping
	 * @since 3.3
	 */
	public int getEditorMappingCount() {
		return fEditorMap.size();
	}
	
	/**
	 * @return the page listener to add to workbench window.
	 */
	private IPageListener getPageListener(){
		if (fPageListener == null){
			fPageListener = new PageListener();
		}
		return fPageListener;
	}
	
	/**
	 * @return the part listener to add to editors.
	 */
	private IPartListener2 getPartListener(){
		if (fPartListener == null){
			fPartListener = new PartListener();
		}
		return fPartListener;
	}

	/**
	 * Part listener that is added to editors to track when the editor is no longer is displaying
	 * the input containing instruction pointer annotations.
	 */
	class PartListener implements IPartListener2{
		public void partActivated(IWorkbenchPartReference partRef) {}
		public void partDeactivated(IWorkbenchPartReference partRef) {}
		public void partHidden(IWorkbenchPartReference partRef) {}
		public void partOpened(IWorkbenchPartReference partRef) {}
		public void partVisible(IWorkbenchPartReference partRef) {}
		public void partBroughtToTop(IWorkbenchPartReference partRef) {}
	
		/* (non-Javadoc)
		 * @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
		 */
		public void partClosed(IWorkbenchPartReference partRef) {
			IWorkbenchPart part = partRef.getPart(false);
			if (part instanceof ITextEditor){
				removeAnnotations((ITextEditor)part);
			}
			
		}
	
		/* (non-Javadoc)
		 * @see org.eclipse.ui.IPartListener2#partInputChanged(org.eclipse.ui.IWorkbenchPartReference)
		 */
		public void partInputChanged(IWorkbenchPartReference partRef) {
			IWorkbenchPart part = partRef.getPart(false);
			if (part instanceof ITextEditor){
				removeAnnotations((ITextEditor)part);
			}
		}
	}
	
	/**
	 * Page listener that is added to the workbench to remove the part listener when the page is closed.
	 */
	class PageListener implements IPageListener{

		public void pageActivated(IWorkbenchPage page) {}
		public void pageOpened(IWorkbenchPage page) {}

		/* (non-Javadoc)
		 * @see org.eclipse.ui.IPageListener#pageClosed(org.eclipse.ui.IWorkbenchPage)
		 */
		public void pageClosed(IWorkbenchPage page) {
			page.removePartListener(getPartListener());
			page.getWorkbenchWindow().removePageListener(getPageListener());
		}
		
	}
	
}

Back to the top