Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 43388f5e22cebe5b4b8dfddd8542ed6ef9778360 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package org.eclipse.debug.internal.ui;

/*
 * Licensed Materials - Property of IBM,
 * WebSphere Studio Workbench
 * (c) Copyright IBM Corp 2000
 */

import org.eclipse.debug.core.*;
import org.eclipse.debug.core.model.*;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.*;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import java.util.HashMap;
import java.util.Iterator;

/**
 * A model presentation that delegates to the appropriate extension. This
 * presentation contains a table of specialized presentations that are defined
 * as <code>org.eclipse.debug.ui.debugModelPresentations</code> extensions. When
 * asked to render an object from a debug model, this presentation delegates
 * to the extension registered for that debug model. 
 */
public class DelegatingModelPresentation implements IDebugModelPresentation {

	private final static String TOP_PREFIX= "presentation.";
	private final static String BREAKPOINT_LABEL= TOP_PREFIX + "breakpoint_label";
	
	/**
	 * A mapping of attribute ids to their values
	 * @see IDebugModelPresentation#setAttribute
	 */
	protected HashMap fAttributes= new HashMap(3);
	/**
	 * A table of label providers keyed by debug model identifiers.
	 */
	protected HashMap fLabelProviders= new HashMap(5);

	// Resource String keys
	private static final String PREFIX= "label_provider.";
	private static final String TERMINATED= PREFIX + "terminated";
	private static final String UNKNOWN= PREFIX + "unknown";

	/**
	 * Constructs a new DelegatingLabelProvider that delegates to extensions
	 * of kind <code>org.eclipse.debug.ui.debugLabelProvider</code>
	 */
	public DelegatingModelPresentation() {
		IPluginDescriptor descriptor= DebugUIPlugin.getDefault().getDescriptor();
		IExtensionPoint point= descriptor.getExtensionPoint(IDebugUIConstants.ID_DEBUG_MODEL_PRESENTATION);
		if (point != null) {
			IExtension[] extensions= point.getExtensions();
			for (int i= 0; i < extensions.length; i++) {
				IExtension extension= extensions[i];
				IConfigurationElement[] configElements= extension.getConfigurationElements();
				for (int j= 0; j < configElements.length; j++) {
					IConfigurationElement elt= configElements[j];
					String id= elt.getAttribute("id");
					if (id != null) {
						IDebugModelPresentation lp= new LazyModelPresentation(elt);
						fLabelProviders.put(id, lp);
					}
				}
			}
		}
	}

	/**
	 * Delegate to all extensions.
	 *
	 * @see ILabelProvider
	 */
	public void addListener(ILabelProviderListener listener) {
		Iterator i= fLabelProviders.values().iterator();
		while (i.hasNext()) {
			((ILabelProvider) i.next()).addListener(listener);
		}
	}

	/**
	 * Delegate to all extensions.
	 *
	 * @see ILabelProvider
	 */
	public void dispose() {
		Iterator i= fLabelProviders.values().iterator();
		while (i.hasNext()) {
			((ILabelProvider) i.next()).dispose();
		}
	}

	/**
	 * Returns an image for the item
	 * Can return <code>null</code>
	 */
	public Image getImage(Object item) {
		if (item instanceof IDebugElement || item instanceof IMarker) {
			IDebugModelPresentation lp= getConfiguredPresentation(item);
			if (lp != null) {
				Image image= lp.getImage(item);
				if (image != null) {
					return image;
				}
			}
			// default to show the simple element name
			return getDefaultImage(item);
		} else {
			ImageRegistry iRegistry= DebugUIPlugin.getDefault().getImageRegistry();
			if (item instanceof IProcess) {
				if (((IProcess) item).isTerminated()) {
					return iRegistry.get(IDebugUIConstants.IMG_OBJS_OS_PROCESS_TERMINATED);
				} else {
					return iRegistry.get(IDebugUIConstants.IMG_OBJS_OS_PROCESS);
				}
			} else
				if (item instanceof ILaunch) {
					String mode= ((ILaunch) item).getLaunchMode();
					if (mode.equals(ILaunchManager.DEBUG_MODE)) {
						return iRegistry.get(IDebugUIConstants.IMG_ACT_DEBUG);
					} else {
						return iRegistry.get(IDebugUIConstants.IMG_ACT_RUN);
					}
				} else
					if (item instanceof InspectItem) {
						return iRegistry.get(IDebugUIConstants.IMG_OBJS_EXPRESSION);
					} else
						if (item instanceof IAdaptable) {
							IWorkbenchAdapter de= (IWorkbenchAdapter) ((IAdaptable) item).getAdapter(IWorkbenchAdapter.class);
							if (de != null) {
								ImageDescriptor descriptor= de.getImageDescriptor(item);
								if( descriptor != null) {
									return descriptor.createImage();
								}
							}
						}

			return null;

		}
	}

	/**
	 * @see IDebugModelPresentation
	 */
	public IEditorInput getEditorInput(Object item) {
		IDebugModelPresentation lp= getConfiguredPresentation(item);
		if (lp != null) {
			return lp.getEditorInput(item);
		}
		return null;
	}
	
	/**
	 * @see IDebugModelPresentation
	 */
	public String getEditorId(IEditorInput input, Object objectInput) {
		IDebugModelPresentation lp= getConfiguredPresentation(objectInput);
		if (lp != null) {
			return lp.getEditorId(input, objectInput);
		}
		return null;
	}


	/**
	 * Returns a default image for the debug element
	 */
	public String getDefaultText(Object element) {
		if (element instanceof IDebugElement) {
			try {
				return ((IDebugElement) element).getName();
			} catch (DebugException de) {
			}
		} else
			if (element instanceof IMarker) {
				IMarker m= (IMarker) element;
				try {
					if (m.exists() && m.isSubtypeOf(IDebugConstants.BREAKPOINT_MARKER)) {
						return DebugUIUtils.getResourceString(BREAKPOINT_LABEL);
					}
				} catch (CoreException e) {
				}
			}
		return DebugUIUtils.getResourceString(UNKNOWN);
	}

	/**
	 * Returns a default image for the debug element
	 */
	public Image getDefaultImage(Object element) {
		ImageRegistry iRegistry= DebugUIPlugin.getDefault().getImageRegistry();
		if (element instanceof IThread) {
			IThread thread = (IThread)element;
			if (thread.isSuspended()) {
				return iRegistry.get(IDebugUIConstants.IMG_OBJS_THREAD_SUSPENDED);
			} else if (thread.isTerminated()) {
				return iRegistry.get(IDebugUIConstants.IMG_OBJS_THREAD_TERMINATED);
			} else {
				return iRegistry.get(IDebugUIConstants.IMG_OBJS_THREAD_RUNNING);
			}
		} else
			if (element instanceof IStackFrame) {
				return iRegistry.get(IDebugUIConstants.IMG_OBJS_STACKFRAME);
			} else
				if (element instanceof IProcess) {
					if (((IProcess) element).isTerminated()) {
						return iRegistry.get(IDebugUIConstants.IMG_OBJS_OS_PROCESS_TERMINATED);
					} else {
						return iRegistry.get(IDebugUIConstants.IMG_OBJS_OS_PROCESS);
					}
				} else
					if (element instanceof IDebugTarget) {
						IDebugTarget target= (IDebugTarget) element;
						if (target.isTerminated() || target.isDisconnected()) {
							return iRegistry.get(IDebugUIConstants.IMG_OBJS_DEBUG_TARGET_TERMINATED);
						} else {
							return iRegistry.get(IDebugUIConstants.IMG_OBJS_DEBUG_TARGET);
						}
					} else
						if (element instanceof IMarker) {
							try {
								IMarker m= (IMarker) element;
								if (m.exists() && m.isSubtypeOf(IDebugConstants.BREAKPOINT_MARKER)) {
									if (DebugPlugin.getDefault().getBreakpointManager().isEnabled(m)) {
										return DebugPluginImages.getImage(IDebugUIConstants.IMG_OBJS_BREAKPOINT);
									} else {
										return DebugPluginImages.getImage(IDebugUIConstants.IMG_OBJS_BREAKPOINT_DISABLED);
									}

								}
							} catch (CoreException e) {
								DebugUIUtils.logError(e);
							}
						}
		return null;
	}

	/**
	 * Returns a label for the item
	 */
	public String getText(Object item) {
		boolean displayVariableTypes= showVariableTypeNames();
		boolean displayQualifiedNames= showQualifiedNames();
		if (item instanceof InspectItem) {
			return getInspectItemText((InspectItem)item);
		} else if (item instanceof IDebugElement || item instanceof IMarker) { 
			IDebugModelPresentation lp= getConfiguredPresentation(item);
			if (lp != null) {
				String label= lp.getText(item);
				if (label != null) {
					return label;
				}
			}
			if (item instanceof IVariable) {
				IVariable var= (IVariable) item;
				StringBuffer buf= new StringBuffer();
				try {
					IValue value = var.getValue();
					
					if (displayVariableTypes) {
						buf.append(value.getReferenceTypeName());
						buf.append(' ');
					}
					buf.append(var.getName());
					buf.append(" = ");
					buf.append(value.getValueString());
					return buf.toString();
				} catch (DebugException de) {
				}
			}
			// default to show the simple element name
			return getDefaultText(item);
		} else {

			String label= null;
			if (item instanceof IProcess) {
				label= ((IProcess) item).getLabel();
			} else
				if (item instanceof ILauncher) {
					label = ((ILauncher)item).getLabel();
				} else
					if (item instanceof ILaunch) {
						label= getLaunchText((ILaunch) item);
					} else if (item instanceof InspectItem) {
						try {
							InspectItem var= (InspectItem) item;
							StringBuffer buf= new StringBuffer();
							buf.append(var.getLabel());
							buf.append(" = ");
							IValue value = var.getValue();
							if (displayVariableTypes) {
								buf.append(value.getReferenceTypeName());
								buf.append(' ');
							}
							buf.append(value.getValueString());
							return buf.toString();
						} catch (DebugException de) {
							return getDefaultText(item);
						}
					} else {
						label= getDesktopLabel(item);
					}

			if ((item instanceof ITerminate) && ((ITerminate) item).isTerminated()) {
				label= DebugUIUtils.getResourceString(TERMINATED) + label;
			}
			return label;
		}
	}

	/**
	 * InspectItems have their left halves rendered here, and their
	 * right halves rendered by the registered model presentation.
	 */
	protected String getInspectItemText(InspectItem inspectItem) {
		StringBuffer buffer= new StringBuffer(inspectItem.getLabel());
		String valueString= null;
		IDebugModelPresentation lp= getConfiguredPresentation(inspectItem);
		IValue value= inspectItem.getValue();
		if (lp != null) {
			valueString= lp.getText(value);
		} 
		if ((valueString == null) || (valueString.length() < 1)) {
			try {
				valueString= value.getValueString();
			} catch (DebugException de) {
			}
		}
		if (valueString != null && valueString.length() > 0) {
			buffer.append("= ");
			buffer.append(valueString);		
		}
		return buffer.toString();
	}

	/**
	 * Delegate to all extensions.
	 *
	 * @see ILabelProvider
	 */
	public void removeListener(ILabelProviderListener listener) {
		Iterator i= fLabelProviders.values().iterator();
		while (i.hasNext()) {
			((ILabelProvider) i.next()).removeListener(listener);
		}
	}

	public String getDesktopLabel(Object object) {
		if (object instanceof IAdaptable) {
			IWorkbenchAdapter de= (IWorkbenchAdapter) ((IAdaptable) object).getAdapter(IWorkbenchAdapter.class);
			if (de != null) {
				return de.getLabel(object);
			}
		}

		return DebugUIUtils.getResourceString(UNKNOWN);
	}

	/**
	 * Delegate to the appropriate label provider.
	 *
	 * @see ILabelProvider
	 */
	public boolean isLabelProperty(Object element, String property) {
		if (element instanceof IDebugElement) {
			IDebugModelPresentation lp= getConfiguredPresentation((IDebugElement) element);
			if (lp != null) {
				return lp.isLabelProperty(element, property);
			}
		}

		return true;
	}

	/**
	 * Returns a configured model presentation for the given object,
	 * or <code>null</code> if one is not registered.
	 */
	protected IDebugModelPresentation getConfiguredPresentation(Object element) {
		String id= null;
		if (element instanceof IDebugElement) {
			IDebugElement de= (IDebugElement) element;
			id= de.getModelIdentifier();
		} else if (element instanceof InspectItem) {
			IValue value= ((InspectItem)element).getValue();
			id= value.getModelIdentifier();
		} else
			if (element instanceof IMarker) {
				IMarker m= (IMarker) element;
				try {
					if (m.exists()) {
						id= (String) m.getAttribute(IDebugConstants.MODEL_IDENTIFIER);
					}
				} catch (CoreException e) {
					DebugUIUtils.logError(e);
				}
			}
		if (id != null) {
			return getPresentation(id);
		}

		return null;
	}
	
	/**
	 * Returns the presentation registered for the given id, or <code>null</code>
	 * of nothing is registered for the id.
	 */
	protected IDebugModelPresentation getPresentation(String id) {
		IDebugModelPresentation lp= (IDebugModelPresentation) fLabelProviders.get(id);
		if (lp != null) {
			Iterator keys= fAttributes.keySet().iterator();
			while (keys.hasNext()) {
				String key= (String)keys.next();
				lp.setAttribute(key, fAttributes.get(key));
			}
			return lp;
		}
		return null;
	}

	/**
	 * Used to render launch history items in the re-launch drop downs
	 */
	protected String getLaunchText(ILaunch launch) {
		return getDesktopLabel(launch.getElement()) + " [" + getText(launch.getLauncher()) + "]";
	}
	
	/**
	 * @see IDebugModelPresentation
	 */
	public void setAttribute(String id, Object value) {
		if (value == null) {
			return;
		}
		fAttributes.put(id, value);
	}

	protected boolean showVariableTypeNames() {
		Boolean show= (Boolean) fAttributes.get(DISPLAY_VARIABLE_TYPE_NAMES);
		show= show == null ? new Boolean(false) : show;
		return show.booleanValue();
	}
	
	protected boolean showQualifiedNames() {
		Boolean show= (Boolean) fAttributes.get(DISPLAY_QUALIFIED_NAMES);
		show= show == null ? new Boolean(false) : show;
		return show.booleanValue();
	}
}

Back to the top