Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fb84812ae4cf51137e856b301dfb6b18f650229f (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
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui;

 
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IStackFrame;
import org.eclipse.debug.core.model.IThread;
import org.eclipse.debug.ui.IDebugEditorPresentation;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;

/**
 * An annotation for the vertical ruler in text editors that shows one of two
 * images for the current instruction pointer when debugging (one for the top
 * stack frame, one for all others).
 */
public class InstructionPointerAnnotation extends Annotation {

	/**
	 * The frame for this instruction pointer annotation.  This is necessary only so that
	 * instances of this class can be distinguished by equals().
	 */
	private IStackFrame fStackFrame;
	
	/**
	 * Flag indicating if this annotation represents a top stack frame.  Top stack frames
	 * have different images from all other stack frames.
	 */
	private boolean fTopStackFrame;

	/**
	 * The layer at which to draw the instruction pointer annotation.  The instruction pointer
	 * should be rendered on top of any other type of annotation or marker.
	 * 
	 * @see org.eclipse.jface.text.source.Annotation
	 */
	private static final int INSTRUCTION_POINTER_ANNOTATION_LAYER = 6;

	/**
	 * Construct an instruction pointer annotation for the given stack frame.
	 * 
	 * @param stackFrame frame to create an instruction pointer annotation for
	 */
	public InstructionPointerAnnotation(IStackFrame stackFrame) {
		setLayer(INSTRUCTION_POINTER_ANNOTATION_LAYER);
		setStackFrame(stackFrame);
	}

	/**
	 * @see org.eclipse.jface.text.source.Annotation#paint(org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
	 */
	public void paint(GC gc, Canvas canvas, Rectangle bounds) {
		Image image = getInstructionPointerImage(isTopStackFrame());
		drawImage(image, gc, canvas, bounds, SWT.CENTER);
	}
	
	/**
	 * Returns the image associated with this instruction pointer.
	 * 
	 * @return image associated with this instruction pointer
	 */
	private Image getInstructionPointerImage(boolean topStackFrame) {
		IDebugEditorPresentation presentation = (IDebugEditorPresentation)DebugUIPlugin.getModelPresentation();
		Image image = presentation.getInstructionPointerImage(getStackFrame());
		if (image == null) {
			if (topStackFrame) {
				image = DebugPluginImages.getImage(IInternalDebugUIConstants.IMG_OBJS_INSTRUCTION_POINTER_TOP);
			} else {
				image = DebugPluginImages.getImage(IInternalDebugUIConstants.IMG_OBJS_INSTRUCTION_POINTER);			
			}
		}
		return image;
	}
	
	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object other) {
		if (other instanceof InstructionPointerAnnotation) {
			return getStackFrame().equals(((InstructionPointerAnnotation)other).getStackFrame());			
		}
		return false;
	}
	
	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return getStackFrame().hashCode();
	}

	/**
	 * Sets the stack frame associated with this annotation and determines
	 * if the frame is on top of the stack.
	 * 
	 * @param stackFrame frame associated with this annotation
	 */
	private void setStackFrame(IStackFrame stackFrame) {
		fStackFrame = stackFrame;
		IThread thread = stackFrame.getThread();
		try {
			fTopStackFrame = stackFrame.equals(thread.getTopStackFrame());
		} catch (DebugException de) {
			fTopStackFrame = false;
		}		
	}

	/**
	 * Returns the stack frame associated with this annotation
	 * 
	 * @return the stack frame associated with this annotation
	 */
	private IStackFrame getStackFrame() {
		return fStackFrame;
	}
		
	/**
	 * Returns whether the stack frame associated with this annotation is the
	 * top stack frame in its thread.
	 * 
	 * @return whether the stack frame associated with this annotation is the
	 * top stack frame in its thread
	 */
	private boolean isTopStackFrame() {
		return fTopStackFrame;
	}

}

Back to the top