Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a2604aef1bd94d4d2ac37120ab90b7b50bc630b5 (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
/*******************************************************************************
 * Copyright (c) 2005, 2010 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     WindRiver - Bug 192028 [Memory View] Memory view does not
 *                 display memory blocks that do not reference IDebugTarget
 *******************************************************************************/

package org.eclipse.debug.internal.ui.views.memory;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IMemoryBlockListener;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IMemoryBlockExtension;
import org.eclipse.debug.core.model.IMemoryBlockRetrieval;
import org.eclipse.debug.internal.core.IInternalDebugCoreConstants;
import org.eclipse.debug.internal.ui.DebugUIMessages;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.contexts.DebugContextEvent;
import org.eclipse.debug.ui.contexts.IDebugContextListener;
import org.eclipse.debug.ui.memory.IMemoryRendering;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IActionDelegate2;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.progress.UIJob;
import org.eclipse.ui.progress.WorkbenchJob;

/**
 * The switch memory block action, used
 */
public class SwitchMemoryBlockAction extends Action implements IViewActionDelegate, IActionDelegate2 {

	/**
	 * A job that updates the enablement of the of the backing action delegate
	 * in the UI thread
	 *
	 * @since 3.3.0
	 */
	class UpdateActionEnablementJob extends UIJob {

		/**
		 * Constructor
		 */
		public UpdateActionEnablementJob() {
			super("Update Action Enablement"); //$NON-NLS-1$
			setSystem(true);
		}

		@Override
		public IStatus runInUIThread(IProgressMonitor monitor) {
			if (fAction != null) {
				IAdaptable context = getDebugContext();
				if (context != null) {
					IMemoryBlockRetrieval retrieval = MemoryViewUtil.getMemoryBlockRetrieval(context);

					if (retrieval != null) {
						IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval);
						fAction.setEnabled(memoryBlocks.length > 0);
						return Status.OK_STATUS;
					} else if (getViewer() != null) {
						Object input = getViewer().getInput();
						if (input instanceof IMemoryBlockRetrieval) {
							retrieval = (IMemoryBlockRetrieval) input;
							IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval);
							fAction.setEnabled(memoryBlocks.length > 0);
							return Status.OK_STATUS;
						}
					}
				}
				fAction.setEnabled(false);
			}
			return Status.CANCEL_STATUS;
		}
	}

	private IViewPart fView;
	private MenuCreator fMenuCreator;
	private IAction fAction;
	private UpdateActionEnablementJob fUpdateJob = new UpdateActionEnablementJob();

	/**
	 * Memoryblock listener to update action delegate enablement
	 */
	private IMemoryBlockListener fListener = new IMemoryBlockListener() {
		@Override
		public void memoryBlocksAdded(IMemoryBlock[] memory) {
			if (fAction != null) {
				fUpdateJob.schedule();
			}
		}

		@Override
		public void memoryBlocksRemoved(IMemoryBlock[] memory) {
			if (fAction != null) {
				fUpdateJob.schedule();
			}
		}
	};

	/**
	 * Listens for debug context changes and updates action delegate enablement
	 */
	private IDebugContextListener fDebugContextListener = new IDebugContextListener() {
		@Override
		public void debugContextChanged(DebugContextEvent event) {
			if (fAction != null) {
				fUpdateJob.schedule();
			}
		}
	};

	/**
	 * Switch tab folder for fMemoryBlock to the top in Memory Rendering View
	 */
	class SwitchToAction extends Action {
		private IMemoryBlock fMemoryblock;

		@Override
		public void run() {
			if (fView == null) {
				return;
			}
			// tell the view to switch memory block
			fView.getSite().getSelectionProvider().setSelection(new StructuredSelection(fMemoryblock));
		}

		public SwitchToAction(final IMemoryBlock memBlk, boolean buildLabel) {
			super();
			if (buildLabel) {
				setText(DebugUIMessages.SwitchMemoryBlockAction_4);
				Job job = new Job("SwtichToAction") { //$NON-NLS-1$
					@Override
					protected IStatus run(IProgressMonitor monitor) {
						getLabels(memBlk);
						return Status.OK_STATUS;
					}
				};
				job.setSystem(true);
				job.schedule();
			}
			fMemoryblock = memBlk;
		}

		public SwitchToAction(final IMemoryBlock memBlk, String label) {
			super(label);
			fMemoryblock = memBlk;
		}

		private void getLabels(final IMemoryBlock memBlk) {
			StringBuffer text = new StringBuffer(IInternalDebugCoreConstants.EMPTY_STRING);
			String label = new String(IInternalDebugCoreConstants.EMPTY_STRING);
			if (memBlk instanceof IMemoryBlockExtension) {
				String expression = ((IMemoryBlockExtension) memBlk).getExpression();
				if (expression == null) {
					expression = DebugUIMessages.SwitchMemoryBlockAction_0;
				}
				text.append(expression);
			} else {
				long address = memBlk.getStartAddress();
				text.append(Long.toHexString(address));
			}

			label = text.toString();
			label = decorateLabel(memBlk, label);

			final String finalLabel = label;
			WorkbenchJob job = new WorkbenchJob("SwtichToAction Update Label") { //$NON-NLS-1$
				@Override
				public IStatus runInUIThread(IProgressMonitor monitor) {
					SwitchToAction.super.setText(finalLabel);
					return Status.OK_STATUS;
				}
			};
			job.setSystem(true);
			job.schedule();
		}
	}

	/**
	 * Menu creator for the action
	 */
	class MenuCreator implements IMenuCreator {
		Menu dropdown;

		@Override
		public void dispose() {
			if (dropdown != null) {
				dropdown.dispose();
			}
		}

		@Override
		public Menu getMenu(Control parent) {
			if (dropdown != null) {
				dropdown.dispose();
				dropdown = null;
			}
			if (dropdown == null) {
				dropdown = new Menu(parent);

				// get all memory blocks from tree viewer
				IMemoryBlock[] allMemoryBlocks = null;

				// get selection from memory view
				IMemoryBlock memoryBlock = getCurrentMemoryBlock();

				Object context = getDebugContext();
				IMemoryBlockRetrieval retrieval = MemoryViewUtil.getMemoryBlockRetrieval(context);
				if (retrieval != null) {
					allMemoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval);
				}
				if (allMemoryBlocks != null) {
					for (int i = 0; i < allMemoryBlocks.length; i++) {
						SwitchToAction action = new SwitchToAction(allMemoryBlocks[i], true);
						if (allMemoryBlocks[i] == memoryBlock) {
							action.setChecked(true);
						}
						ActionContributionItem item = new ActionContributionItem(action);
						item.fill(dropdown, -1);
						item.getAction().setChecked(true);
					}
				}
			}
			return dropdown;
		}

		@Override
		public Menu getMenu(Menu parent) {
			return null;
		}

	}

	@Override
	public void init(IViewPart view) {
		fView = view;
		DebugUITools.getDebugContextManager().getContextService(fView.getViewSite().getWorkbenchWindow()).addDebugContextListener(fDebugContextListener);
		DebugPlugin.getDefault().getMemoryBlockManager().addListener(fListener);
		fUpdateJob.runInUIThread(new NullProgressMonitor());
	}

	/**
	 * Returns the current memory blocks tree viewer, or <code>null</code>
	 *
	 * @return the memory blocks tree viewer or <code>null</code>
	 */
	private StructuredViewer getViewer() {
		if (fView instanceof MemoryView) {
			MemoryView memView = (MemoryView) fView;
			IMemoryViewPane pane = memView.getViewPane(MemoryBlocksTreeViewPane.PANE_ID);
			if (pane instanceof MemoryBlocksTreeViewPane) {
				StructuredViewer viewer = ((MemoryBlocksTreeViewPane) pane).getViewer();
				return viewer;
			}
		}
		return null;
	}

	@Override
	public void run(IAction action) {
		switchToNext();
	}

	@Override
	public void run() {
		switchToNext();
	}

	private void switchToNext() {
		IAdaptable context = getDebugContext();
		if (context instanceof IDebugElement) {
			IDebugElement debugContext = (IDebugElement) context;
			IMemoryBlockRetrieval retrieval = MemoryViewUtil.getMemoryBlockRetrieval(debugContext);

			if (retrieval != null) {
				IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval);
				doSwitchToNext(memoryBlocks);
			}
		}
	}

	/**
	 * @param memoryBlocks
	 */
	private void doSwitchToNext(IMemoryBlock[] memoryBlocks) {
		// only run if there is more than one memory block
		if (memoryBlocks.length > 1) {
			IMemoryBlock current = getCurrentMemoryBlock();
			int next = 0;
			if (current != null) {
				for (int i = 0; i < memoryBlocks.length; i++) {
					if (memoryBlocks[i] == current) {
						next = i + 1;
					}
				}
			}
			if (next > memoryBlocks.length - 1) {
				next = 0;
			}
			SwitchToAction switchAction = new SwitchToAction(memoryBlocks[next], false);
			switchAction.run();
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
	}

	@Override
	public void init(IAction action) {
		fAction = action;
		fUpdateJob.runInUIThread(new NullProgressMonitor());
		fMenuCreator = new MenuCreator();
		action.setMenuCreator(fMenuCreator);
	}

	@Override
	public void dispose() {
		fAction = null;
		DebugPlugin.getDefault().getMemoryBlockManager().removeListener(fListener);
		DebugUITools.getDebugContextManager().getContextService(fView.getViewSite().getWorkbenchWindow()).removeDebugContextListener(fDebugContextListener);
		if (fMenuCreator != null) {
			fMenuCreator.dispose();
		}
	}

	@Override
	public void runWithEvent(IAction action, Event event) {
		switchToNext();
	}

	/**
	 * Returns the current memory block
	 *
	 * @return the current memory block or <code>null</code>
	 */
	private IMemoryBlock getCurrentMemoryBlock() {
		if (fView == null) {
			return null;
		}
		ISelection memBlkSelection = fView.getSite().getSelectionProvider().getSelection();
		IMemoryBlock memoryBlock = null;

		if (memBlkSelection != null) {
			if (!memBlkSelection.isEmpty() && memBlkSelection instanceof IStructuredSelection) {
				Object obj = ((IStructuredSelection) memBlkSelection).getFirstElement();
				if (obj instanceof IMemoryBlock) {
					memoryBlock = (IMemoryBlock) obj;
				} else if (obj instanceof IMemoryRendering) {
					memoryBlock = ((IMemoryRendering) obj).getMemoryBlock();
				}
			}
		}
		return memoryBlock;
	}

	/**
	 * Decorate the label for the specified <code>IMemoryBlock</code>
	 *
	 * @param memBlk
	 * @param label
	 * @return the decorated label for the specified <code>IMemoryBlock</code>
	 */
	private String decorateLabel(final IMemoryBlock memBlk, String label) {
		ILabelDecorator decorator = memBlk.getAdapter(ILabelDecorator.class);
		if (decorator != null) {
			label = decorator.decorateText(label, memBlk);
		}
		return label;
	}

	private IAdaptable getDebugContext() {
		if (fView != null) {
			return DebugUITools.getPartDebugContext(fView.getSite());
		} else {
			return DebugUITools.getDebugContext();
		}
	}
}

Back to the top