Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6b6dd25d8c1d257010a368dbb71320aa892e38e6 (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
/*******************************************************************************
 * Copyright (c) 2004, 2005 QNX Software Systems 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:
 * QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.views.disassembly;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.cdt.debug.core.model.ICLineBreakpoint;
import org.eclipse.core.resources.IMarker;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.texteditor.MarkerAnnotation;

/**
 * Annotation model for Disassembly view.
 */
public class DisassemblyAnnotationModel extends AnnotationModel {

	private DisassemblyEditorInput fInput;

//	private IDocument fDisassemblyDocument;

	/**
	 * Constructor for DisassemblyAnnotationModel.
	 */
	public DisassemblyAnnotationModel() {
		super();
	}

	protected void breakpointsAdded( final IBreakpoint[] breakpoints, final IDocument document ) {
		DisassemblyEditorInput input = getInput();
		if ( DisassemblyEditorInput.EMPTY_EDITOR_INPUT.equals( input ) ||
			 DisassemblyEditorInput.PENDING_EDITOR_INPUT.equals( input ) )
			 return;
		asyncExec( new Runnable() {		
			public void run() {
				breakpointsAdded0( breakpoints, document );
			}
		} ); 			
	}

	protected void breakpointsRemoved( final IBreakpoint[] breakpoints, final IDocument document ) {
		DisassemblyEditorInput input = getInput();
		if ( DisassemblyEditorInput.EMPTY_EDITOR_INPUT.equals( input ) ||
			 DisassemblyEditorInput.PENDING_EDITOR_INPUT.equals( input ) )
			 return;
		asyncExec( new Runnable() {		
			public void run() {
				breakpointsRemoved0( breakpoints, document );
			}
		} );
	}

	protected void breakpointsChanged( final IBreakpoint[] breakpoints, final IDocument document ) {
		DisassemblyEditorInput input = getInput();
		if ( DisassemblyEditorInput.EMPTY_EDITOR_INPUT.equals( input ) ||
			 DisassemblyEditorInput.PENDING_EDITOR_INPUT.equals( input ) )
			 return;
		asyncExec( new Runnable() {		
			public void run() {
				breakpointsChanged0( breakpoints, document );
			}
		} );
	}

	protected void breakpointsAdded0( IBreakpoint[] breakpoints, IDocument document ) {
		for ( int i = 0; i < breakpoints.length; ++i ) {
			if ( breakpoints[i] instanceof ICLineBreakpoint && isApplicable( breakpoints[i] ) ) {
				addBreakpointAnnotation( (ICLineBreakpoint)breakpoints[i], document );
			}
		}
		fireModelChanged();
	}

	protected void breakpointsRemoved0( IBreakpoint[] breakpoints, IDocument document ) {
		removeAnnotations( findAnnotationsforBreakpoints( breakpoints ), true, false );
	}

	protected void breakpointsChanged0( IBreakpoint[] breakpoints, IDocument document ) {
		List annotations = findAnnotationsforBreakpoints( breakpoints );
		List markers = new ArrayList( annotations.size() );
		Iterator it = annotations.iterator();
		while( it.hasNext() ) {
			MarkerAnnotation ma = (MarkerAnnotation)it.next();
			markers.add( ma.getMarker() );
			modifyAnnotationPosition( ma, getPosition( ma ), false );
		}
		for ( int i = 0; i < breakpoints.length; ++i ) {
			if ( breakpoints[i] instanceof ICLineBreakpoint && !markers.contains( breakpoints[i].getMarker() ) ) {
				addBreakpointAnnotation( (ICLineBreakpoint)breakpoints[i], document );
			}
		}
		fireModelChanged();
	}

	protected DisassemblyEditorInput getInput() {
		return this.fInput;
	}

	protected void setInput( DisassemblyEditorInput input, IDocument document ) {
		DisassemblyEditorInput oldInput = this.fInput;
		this.fInput = input;
		if ( this.fInput != null && !this.fInput.equals( oldInput ) )
			updateAnnotations( document );
	}

	private boolean isApplicable( IBreakpoint breakpoint ) {
		return true;
	}

	private void addBreakpointAnnotation( ICLineBreakpoint breakpoint, IDocument document ) {
		Position position = createBreakpointPosition( breakpoint, document );
		if ( position != null ) {
			try {
				addAnnotation( createMarkerAnnotation( breakpoint ), position, false );
			}
			catch( BadLocationException e ) {
			}
		}
	}

	private Position createBreakpointPosition( ICLineBreakpoint breakpoint, IDocument document ) {
		Position position = null;
		DisassemblyEditorInput input = getInput();
		if ( input != null ) {
			int start = -1;
			if ( document != null ) {
				int instrNumber = input.getInstructionLine( breakpoint );
				if ( instrNumber > 0 ) {
					try {
						start = fDocument.getLineOffset( instrNumber - 1 );
						if ( start > -1 ) {
							// Avoid the document boundaries; see bugzilla 178485
							int lineLen = document.getLineLength(instrNumber - 1);							
							if (start == 0) {
								start++;
								lineLen--;
							}
							if (start + lineLen == document.getLength()) {
								lineLen--;
							}
							return new Position( start, lineLen );
						}
					}
					catch( BadLocationException e ) {
					}
				}
			}
		}
		return position;
	}

	private MarkerAnnotation createMarkerAnnotation( IBreakpoint breakpoint ) {
		return new MarkerAnnotation( breakpoint.getMarker() );
	}

	protected void dispose() {
	}

	private List findAnnotationsforBreakpoints( IBreakpoint[] breakpoints ) {
		List annotations = new LinkedList();
		Iterator it = getAnnotationIterator();
		while ( it.hasNext() ) {
			Annotation ann = (Annotation)it.next();
			if ( ann instanceof MarkerAnnotation ) {
				IMarker marker = ((MarkerAnnotation)ann).getMarker();
				if ( marker != null ) {
					for ( int i = 0; i < breakpoints.length; ++i ) {
						if ( marker.equals( breakpoints[i].getMarker() ) ) {
							annotations.add( ann );
						}
					}
				}
			}
		}
		return annotations;
	}

	private void asyncExec( Runnable r ) {
		Display display = Display.getDefault();
		if ( display != null )
			display.asyncExec( r );
	}

	private void updateAnnotations( final IDocument document ) {
		asyncExec( new Runnable() {		
			public void run() {
				doUpdateAnnotations( document );
			}
		} );
	}

	protected void doUpdateAnnotations( IDocument document ) {
		breakpointsAdded0( DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(), document );
	}
}

Back to the top