Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ee884377840fa80c59640510dc78ba5fcb65ece0 (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Created on 30 avr. 2004
 */
package org.atl.eclipse.adt.debug.ui;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.atl.eclipse.adt.debug.AtlDebugPlugin;
import org.atl.eclipse.adt.debug.Messages;
import org.atl.eclipse.adt.debug.core.AtlBreakpoint;
import org.atl.eclipse.adt.debug.core.AtlDebugTarget;
import org.atl.eclipse.adt.debug.core.AtlStackFrame;
import org.atl.eclipse.adt.debug.core.AtlThread;
import org.atl.eclipse.adt.debug.core.AtlVariable;
import org.atl.eclipse.adt.launching.AtlLauncherTools;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.IValue;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.IValueDetailListener;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;


/**
 * @author Freddy Allilaire
 * A debug model presentation is responsible for providing labels, images,
 * and editors associated with debug elements in a specific debug model.
 *
 * To allow for an extensible configuration, IDebugModelPresentation interface defines
 * a setAttribute method.
 * 
 */

public class AtlDebugModelPresentation extends LabelProvider implements IDebugModelPresentation {

	static final URL BASE_URL = AtlDebugPlugin.getDefault().getBundle().getEntry("/");
	static final String iconPath = "icons/";

	// If you add a constant here, look in the class AtlVariable if the value is not already used
	public final static int BREAKPOINT = 5;
	private Map mapImage = null;
	
	private HashMap fAttributes= new HashMap(3);

	/**
	 * If the button DISPLAY VARIABLE TYPE NAMES in debugUI is pressed
	 * then the status of the variable DISPLAY_VARIABLE_TYPE_NAMES is changed
	 * 
	 * This method returns true if DISPLAY_VARIABLE_TYPE_NAMES is selected in debugUI
	 */
	private boolean isShowVariableTypeNames() {
		Boolean show= (Boolean) fAttributes.get(DISPLAY_VARIABLE_TYPE_NAMES);
		show= show == null ? Boolean.FALSE : show;
		return show.booleanValue();
	}
	
	/**
	 * Clients may define new presentation attributes. For example, a client may wish
	 * to define a "hexadecimal" property to display numeric values in hexadecimal. Implementations
	 * should honor the presentation attributes defined by this interface where possible,
	 * but do not need to honor presentation attributes defined by other clients.
	 * To access the debug model presentation for a debug view, clients should use
	 * DebugView#getPresentation(String).
	 * 
	 * @see org.eclipse.debug.ui.IDebugModelPresentation#setAttribute(java.lang.String, java.lang.Object)
	 */
	public void setAttribute(String attribute, Object value) {
		if (value == null) {
			return;
		}
		fAttributes.put(attribute, value);
	}

	/**
	 * This method returns an image from the path
	 * @param path
	 * @return
	 */
	private Image createImage(String path) {
		try {
			URL url = new URL(BASE_URL, path);
			return ImageDescriptor.createFromURL(url).createImage();
		}
		catch(MalformedURLException e) {}
		return null;
	}

	private void initMapImage() {
		mapImage = new HashMap();
		mapImage.put(new Integer(AtlVariable.ATTRIBUTE), null);
		mapImage.put(new Integer(AtlVariable.ELEMENT), null);
		mapImage.put(new Integer(AtlVariable.LOCALVARIABLE), null);
		mapImage.put(new Integer(AtlVariable.REFERENCE), null);
		mapImage.put(new Integer(AtlVariable.SUPERTYPE), null);
		mapImage.put(new Integer(BREAKPOINT), null);
	}

	
	/**
	 * This method returns the image associate to the type of the parameter
	 * 
	 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
	 */
	public Image getImage(Object item) {
		if (mapImage == null)
			initMapImage();
		
		if (item instanceof AtlVariable) {
			String imageName = null;
			AtlVariable atlVar = (AtlVariable)item;
//			String typeVar = null;
//			try {
//				typeVar = atlVar.getReferenceTypeName();
//			} catch (DebugException e) {
//				e.printStackTrace();
//			}
			switch (atlVar.getDescription()) {
				case AtlVariable.ATTRIBUTE : imageName = "attribute.gif"; break;
				case AtlVariable.ELEMENT : imageName = "element.gif"; break;
				case AtlVariable.LOCALVARIABLE : imageName = "localVariable.gif"; break;
				case AtlVariable.REFERENCE : imageName = "reference.gif"; break;
				case AtlVariable.SUPERTYPE : imageName = "supertype.gif"; break;
				default : return null;
			}				
			if (mapImage.get(new Integer(atlVar.getDescription())) == null) {
				mapImage.put(new Integer(atlVar.getDescription()), createImage(iconPath + imageName));
			}
			return (Image)mapImage.get(new Integer(atlVar.getDescription()));
		}
		else if (item instanceof AtlBreakpoint) {
			if (mapImage.get(new Integer(BREAKPOINT)) == null) {
				mapImage.put(new Integer(BREAKPOINT), createImage(iconPath + "breakpoint.gif"));
			}
			return (Image)mapImage.get(new Integer(BREAKPOINT));
		}
		else if (item instanceof IMarker) {
			if (mapImage.get(new Integer(BREAKPOINT)) == null) {
				mapImage.put(new Integer(BREAKPOINT), createImage(iconPath + "breakpoint.gif"));
			}
			return (Image)mapImage.get(new Integer(BREAKPOINT));
		}
		return null;
	}

	/**
	 * This method returns the text associate to the parameter
	 * 
	 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
	 */
	public String getText(Object item) {
		if (item instanceof AtlDebugTarget) {
			AtlDebugTarget target = (AtlDebugTarget) item;
			String name = "";
			try {
				name = target.getName();
			}
			catch (DebugException e) {
				e.printStackTrace();
			}
			return name + Messages.getString("AtlDebugModelPresentation.CONNECTEDONHOST")+ target.getHost() + Messages.getString("AtlDebugModelPresentation.PORT") + target.getPort(); //$NON-NLS-1$ //$NON-NLS-2$
		}
		else if (item instanceof AtlThread) {
			AtlThread thread = (AtlThread) item;
			try {
				String currentState;
				String message = "";
				switch (((AtlDebugTarget)thread.getDebugTarget()).getState()) {
					case AtlDebugTarget.stateDisconnected: 	currentState = Messages.getString("AtlDebugModelPresentation.DISCONNECTED"); 	break; //$NON-NLS-1$
					case AtlDebugTarget.stateRunning: 		currentState = Messages.getString("AtlDebugModelPresentation.RUNNING"); 		break; //$NON-NLS-1$
					case AtlDebugTarget.stateSuspended: 	currentState = Messages.getString("AtlDebugModelPresentation.SUSPENDED"); //$NON-NLS-1$
															message = ((AtlDebugTarget)thread.getDebugTarget()).getMessageFromDebuggee();
															return thread.getName() + " (" + currentState + " (" + message + "))"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					case AtlDebugTarget.stateTerminated: 	currentState = Messages.getString("AtlDebugModelPresentation.TERMINATED"); 	break; //$NON-NLS-1$
					default : currentState = Messages.getString("AtlDebugModelPresentation.UNKNOWN"); //$NON-NLS-1$
				}
				return thread.getName() + " (" + currentState + ")";
			}
			catch (DebugException e) {
				return null;
			}
		}
		else if (item instanceof AtlStackFrame)
			return null;
		else if (item instanceof AtlVariable) {
			AtlVariable atlVar = (AtlVariable)item;
			String typeVar = null;
			try {
				if (isShowVariableTypeNames())
					typeVar = atlVar.getReferenceTypeName();
				else
					typeVar = "";
				
				String rtn = atlVar.getValue().getReferenceTypeName();
				if (rtn.equals("Boolean") || 
					rtn.equals("Integer") ||
					rtn.equals("Real"))
					return typeVar + " " + atlVar.getName() + " = " + atlVar.getValue().getValueString();
				else if (rtn.equals("String"))
					return typeVar + " " + atlVar.getName() + " = '" + atlVar.getValue().getValueString() + "'";
				else if (rtn.equals("EnumLiteral"))
					return typeVar + " " + atlVar.getName() + " = #" + atlVar.getValue().getValueString();
				else if (rtn.equals("Map Element"))
					return atlVar.getName();
				else
					return typeVar + " " + atlVar.getName() + " = " + atlVar.getReferenceTypeName() + " (id = " + atlVar.getIdVariable() + ")";
			} catch (DebugException e) {
				e.printStackTrace();
			}
		}
		else if (item instanceof AtlBreakpoint)	{
			IMarker marker = ((AtlBreakpoint)item).getMarker();
			String location;
			try {
				location = (String)marker.getResource().getName();
				Integer lineNumber = (Integer)marker.getAttribute(IMarker.LINE_NUMBER);
				Integer charStart = (Integer)marker.getAttribute(IMarker.CHAR_START);
				Integer charEnd = (Integer)marker.getAttribute(IMarker.CHAR_END);
				return location + " [line: " + lineNumber + ", charStart: " + charStart + ", charEnd: " + charEnd + "]";
			}
			catch (CoreException e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	/**
	 * @see org.eclipse.debug.ui.IDebugModelPresentation#computeDetail(org.eclipse.debug.core.model.IValue,
	 *      org.eclipse.debug.ui.IValueDetailListener)
	 */
	public void computeDetail(IValue value, IValueDetailListener listener) {
		try {
			listener.detailComputed(value,value.getValueString());
		}
		catch (DebugException e) {
			e.printStackTrace();
		}
	}
	
	private class DisassemblyEditorInput implements IStorageEditorInput {

		private IStorage contents;
		
		public DisassemblyEditorInput(IStorage contents) {
			this.contents = contents;
		}
		
		/**
		 * @see org.eclipse.ui.IEditorInput#exists()
		 */
		public boolean exists() {
			return true;
		}

		/**
		 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
		 */
		public ImageDescriptor getImageDescriptor() {
			return null;
		}

		/**
		 * @see org.eclipse.ui.IEditorInput#getName()
		 */
		public String getName() {
			return contents.getName();
		}

		/**
		 * @see org.eclipse.ui.IEditorInput#getPersistable()
		 */
		public IPersistableElement getPersistable() {
			return null;
		}

		/**
		 * @see org.eclipse.ui.IEditorInput#getToolTipText()
		 */
		public String getToolTipText() {
			return contents.getName();
		}

		/**
		 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
		 */
		public Object getAdapter(Class adapter) {
			return null;
		}

		/**
		 * @see org.eclipse.ui.IStorageEditorInput#getStorage()
		 */
		public IStorage getStorage() throws CoreException {
			return contents;
		}
	}

//	private IEditorPart dte;
	
	public IEditorInput getDisassemblyEditorInput(AtlStackFrame frame) {
		IEditorInput ret = null;

		ret = new DisassemblyEditorInput(frame.getDisassembled());

		return ret;
	}

	/**
	 * 
	 * @see org.eclipse.debug.ui.ISourcePresentation#getEditorInput(java.lang.Object)
	 */
	public IEditorInput getEditorInput(Object element) {
		IStorageEditorInput i;
		AtlStackFrame frame;
//		String projectName;
		String fileName;
		
		if(element instanceof AtlStackFrame ) {
			frame = (AtlStackFrame) element;
			if(((AtlDebugTarget)frame.getDebugTarget()).isDisassemblyMode()) return getDisassemblyEditorInput(frame);
			ILaunchConfiguration configuration = frame.getDebugTarget().getLaunch().getLaunchConfiguration();
			try {
				// TODO Recuperer le nom du fichier sur la stackframe
				fileName = configuration.getAttribute(AtlLauncherTools.ATLFILENAME, AtlLauncherTools.NULLPARAMETER);

				IWorkspace wks = ResourcesPlugin.getWorkspace();
				IWorkspaceRoot wksroot = wks.getRoot();

				i = new FileEditorInput(wksroot.getFile(new Path(fileName)));
				return i;
			}
			catch (CoreException e) {
				e.printStackTrace();
			}
		}
		else if(element instanceof AtlBreakpoint) {
			IMarker marker = ((AtlBreakpoint)element).getMarker();
			IFile ifile = (IFile)marker.getResource();
			return new FileEditorInput(ifile);
		}
		return null;
	}

	/**
	 * @see org.eclipse.debug.ui.ISourcePresentation#getEditorId(org.eclipse.ui.IEditorInput,
	 *      java.lang.Object)
	 */
	public String getEditorId(IEditorInput input, Object element) {
		if(input instanceof DisassemblyEditorInput)
			return "org.eclipse.ui.DefaultTextEditor";
		IEditorRegistry registry= PlatformUI.getWorkbench().getEditorRegistry();
		IEditorDescriptor descriptor= registry.getDefaultEditor(input.getName());
		if (descriptor != null) {
			return descriptor.getId();
		} else {
			return "org.eclipse.ui.DefaultTextEditor";
		}
	}

}

Back to the top