Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ffbb2be29f953d4cf443ea52d31ea8af6f7cbd31 (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
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.jface.viewers.IBasicPropertyConstants;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StructuredSelection;

/**
 * Provides the contents for a debug mode of the Launches viewer.
 */
public class DebugContentProvider extends BasicContentProvider implements IDebugEventListener, ILaunchListener, ITreeContentProvider {
	
	/**
	 * Constructs a new content provider.
	 */
	public DebugContentProvider() {
		DebugPlugin plugin= DebugPlugin.getDefault();
		plugin.addDebugEventListener(this);
		plugin.getLaunchManager().addLaunchListener(this);
	}

	/**
	 * @see BasicContentProvider#doGetChildren(Object)
	 */
	protected Object[] doGetChildren(Object parent) {
		if (parent instanceof IDebugElement) {
			IDebugElement de= (IDebugElement) parent;
			if (de.getElementType() < IDebugElement.STACK_FRAME) {
				try {
					return de.getChildren();
				} catch (DebugException e) {
					DebugUIUtils.logError(e);
				}
			}
		} else
			if (parent instanceof ILaunch) {
				return ((ILaunch)parent).getChildren();
			} else
				if (parent instanceof ILaunchManager) {
					return ((ILaunchManager) parent).getLaunches();
				}
		return new Object[0];
	}

	/**
	 * Returns the <code>ILaunch</code>es for the <code>ILaunchManager</code>
	 */
	public Object[] getElements(Object element) {
		if (element instanceof ILaunchManager) {
			return ((ILaunchManager) element).getLaunches();
		}
		return new Object[0];
	}

	/**
	 * @see org.eclipse.jface.viewer.ITreeContentProvider
	 */
	public Object getParent(Object item) {
		Object parent= null;
		if (item instanceof IDebugElement) {
			IDebugElement de= (IDebugElement) item;
			parent= de.getParent();
			if (parent == null) {
				parent= de.getLaunch();
			}
		} else
			if (item instanceof IProcess) {
				parent= ((IProcess) item).getLaunch();
			} else
				if (item instanceof ILaunch) {
					parent= DebugPlugin.getDefault().getLaunchManager();
				}
		return parent;
	}

	/**
	 * @see BasicContentProvider#doHandleDebug(Event)
	 */
	public void doHandleDebugEvent(DebugEvent event) {
		Object element= event.getSource();
		if (element instanceof IDebugElement && ((IDebugElement) element).getElementType() == IDebugElement.VARIABLE) {
			// the debug view does not show variables
			return;
		}
		switch (event.getKind()) {
			case DebugEvent.CREATE :
				insert(element);
				break;
			case DebugEvent.TERMINATE :
				clearSourceSelection();
				Object parent= getParent(element);
				refresh(parent);
				updateButtons();
				break;
			case DebugEvent.RESUME :
				if (element instanceof ISuspendResume) {
					if (((ISuspendResume)element).isSuspended()) {
						return;
					}
				}
				clearSourceSelection();
				if (event.getDetail() != DebugEvent.STEP_START) {
					refresh(element);
					if (element instanceof IThread) {
						//select and reveal will update buttons
						//via selection changed callback
						selectAndReveal(element);
						break;
					}
				}
				labelChanged(element);
				updateButtons();
				break;
			case DebugEvent.SUSPEND :
				refresh(element);
				if (!DebugUIPlugin.getDefault().userPreferenceToSwitchPerspective(true)) {
					Object tos= null;
					if (element instanceof IThread) {
						// select the top stack frame
						try {
							tos= ((IThread) element).getTopStackFrame();	
						} catch (DebugException de) {
						}
					}
					if (tos != null) {
						selectAndReveal(tos);
					}
				}
				updateButtons();
				break;
			case DebugEvent.CHANGE :
				refresh(element);
				updateButtons();
				break;
		}
	}

	/**
	 * Helper method for inserting the given element - must be called in UI thread
	 */
	protected void insert(Object element) {
		final Object parent= getParent(element);
		// a parent can be null for a debug target or process that has not yet been associated
		// with a launch
		if (parent != null) {
			((LaunchesViewer) fViewer).add(parent, element);
		}
	}

	/**
	 * Helper method to remove the given element - must be called in UI thread
	 */
	private void remove(Object element) {
		((LaunchesViewer) fViewer).remove(element);
	}

	/**
	 * Helper method to update the label of the given element - must be called in UI thread
	 */
	protected void labelChanged(Object element) {
		((LaunchesViewer) fViewer).update(element, new String[] {IBasicPropertyConstants.P_TEXT});
	}

	/**
	 * Helper method to update the buttons of the viewer - must be called in UI thread
	 */
	protected void updateButtons() {
		((LaunchesViewer)fViewer).updateButtons();
	}

	/**
	 * Helper method to update the selection of the viewer - must be called in UI thread
	 */
	protected void updateMarkerForSelection() {
		((LaunchesViewer)fViewer).updateMarkerForSelection();
	}

	/**
	 * Helper method to select and reveal the given element - must be called in UI thread
	 */
	protected void selectAndReveal(Object element) {
		((LaunchesViewer) fViewer).setSelection(new StructuredSelection(element), true);
	}

	/**
	 * @see ITreeContentProvider
	 */
	public boolean hasChildren(Object item) {
		if (item instanceof IStackFrame) {
			return false;
			// No children shown for IStackFrames in this view (see the variables view instead).
		} else
			if (item instanceof IDebugElement) {
				try {
					return ((IDebugElement) item).hasChildren();
				} catch (DebugException de) {
				}
			} else
				if (item instanceof ILaunch) {
					return true;
				} else
					if (item instanceof ILaunchManager) {
						return ((ILaunchManager) item).getLaunches().length > 0;
					}
		return false;
	}

	/**
	 * @see ILaunchListener
	 */
	public void launchDeregistered(final ILaunch launch) {
		Runnable r= new Runnable() {
			public void run() {
				remove(launch);
				updateButtons();
			}
		};

		asyncExec(r);
	}

	/**
	 * @see ILaunchListener
	 */
	public void launchRegistered(final ILaunch launch) {
		Runnable r= new Runnable() {
			public void run() {
				insert(launch);
				if (!DebugUIPlugin.getDefault().userPreferenceToSwitchPerspective(true)) {
					Object dt = launch.getDebugTarget();
					if (dt != null) {
						selectAndReveal(dt);
					}
				}
			}
		};

		asyncExec(r);
	}

	/**
	 * Unregisters this content provider from the debug model so that
	 * this object can be garbage-collected.
	 */
	public void dispose() {
		DebugPlugin plugin= DebugPlugin.getDefault();
		plugin.removeDebugEventListener(this);
		plugin.getLaunchManager().removeLaunchListener(this);
	}

	/**
	 * Clear the selection in the editor - must be called in UI thread
	 */
	private void clearSourceSelection() {
		if (fViewer != null) {
			((LaunchesViewer)fViewer).clearSourceSelection();
		}
	}
}

Back to the top