Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 46aa62bd335bfb3d25282c02c1d7e0ba66af412f (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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*******************************************************************************
 * Copyright (c) 2004, 2011 IBM Corporation 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:
 *     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.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IMemoryBlockListener;
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.internal.ui.DebugUIPlugin;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.memory.IMemoryRendering;
import org.eclipse.debug.ui.memory.IMemoryRenderingBindingsListener;
import org.eclipse.debug.ui.memory.IMemoryRenderingSite;
import org.eclipse.debug.ui.memory.IMemoryRenderingType;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.SelectionDialog;
import org.eclipse.ui.progress.UIJob;
import org.eclipse.ui.progress.WorkbenchJob;

/**
 * Dialog allowing user to add a memory rendering
 */
public class AddMemoryRenderingDialog extends SelectionDialog {

	private IMemoryBlock[] fMemoryBlocks;
	private Combo memoryBlock;
	private ListViewer fViewer;
	private IMemoryBlock fSelectedMemoryBlock;
	private Button addNew;

	private ISelectionChangedListener fSelectionChangedListener;
	private SelectionListener fSelectionListener;
	private SelectionAdapter fAddNewSelectionAdapter;
	private IMemoryRenderingSite fSite;

	private IMemoryBlockListener fMemoryBlockListener = new IMemoryBlockListener() {

		@Override
		public void memoryBlocksAdded(final IMemoryBlock[] memory) {
			if (memory.length > 0) {
				IMemoryBlock currentBlock = getMemoryBlockToSelect(memory[0]);
				if (currentBlock == null) {
					addNew();
				} else {
					populateDialog(currentBlock);
				}
			}
		}

		@Override
		public void memoryBlocksRemoved(IMemoryBlock[] memory) {
		}
	};

	private IMemoryRenderingBindingsListener fBindingListener = new IMemoryRenderingBindingsListener() {
		@Override
		public void memoryRenderingBindingsChanged() {
			UIJob job = new UIJob("refresh") { //$NON-NLS-1$

				@Override
				public IStatus runInUIThread(IProgressMonitor monitor) {
					fViewer.refresh();
					return Status.OK_STATUS;
				}
			};
			job.setSystem(true);
			job.schedule();
		}

	};

	class MemoryRenderingLabelProvider implements ILabelProvider {

		/*
		 * (non-Javadoc)
		 * @see
		 * org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
		 */
		@Override
		public Image getImage(Object element) {
			return null;
		}

		/*
		 * (non-Javadoc)
		 * @see
		 * org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
		 */
		@Override
		public String getText(Object element) {
			if (element instanceof IMemoryRenderingType) {
				String label = ((IMemoryRenderingType) element).getLabel();
				return label;
			}
			return element.toString();
		}

		/*
		 * (non-Javadoc)
		 * @see
		 * org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse
		 * .jface.viewers.ILabelProviderListener)
		 */
		@Override
		public void addListener(ILabelProviderListener listener) {
		}

		/*
		 * (non-Javadoc)
		 * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
		 */
		@Override
		public void dispose() {
		}

		/*
		 * (non-Javadoc)
		 * @see
		 * org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java
		 * .lang.Object, java.lang.String)
		 */
		@Override
		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		/*
		 * (non-Javadoc)
		 * @see
		 * org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse
		 * .jface.viewers.ILabelProviderListener)
		 */
		@Override
		public void removeListener(ILabelProviderListener listener) {
		}

	}

	class MemoryRenderingContentProvider implements IStructuredContentProvider {

		/*
		 * (non-Javadoc)
		 * @see
		 * org.eclipse.jface.viewers.IStructuredContentProvider#getElements(
		 * java.lang.Object)
		 */
		@Override
		public Object[] getElements(Object inputElement) {
			IMemoryRenderingType[] renderings = DebugUITools.getMemoryRenderingManager().getRenderingTypes((IMemoryBlock) inputElement);
			return renderings;
		}

		/*
		 * (non-Javadoc)
		 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
		 */
		@Override
		public void dispose() {
		}

		/*
		 * (non-Javadoc)
		 * @see
		 * org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse
		 * .jface.viewers.Viewer, java.lang.Object, java.lang.Object)
		 */
		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}

	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.window.Window#close()
	 */
	@Override
	public boolean close() {

		fViewer.removeSelectionChangedListener(fSelectionChangedListener);
		memoryBlock.removeSelectionListener(fSelectionListener);
		addNew.removeSelectionListener(fAddNewSelectionAdapter);
		DebugPlugin.getDefault().getMemoryBlockManager().removeListener(fMemoryBlockListener);
		DebugUITools.getMemoryRenderingManager().removeListener(fBindingListener);

		return super.close();
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse
	 * .swt.widgets.Composite)
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(false);
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.SelectionDialog#getResult()
	 */
	@Override
	public Object[] getResult() {

		Object[] results = super.getResult();

		if (results != null) {
			Object[] renderings = ((IStructuredSelection) results[0]).toArray();
			return renderings;
		}
		return new Object[0];
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
	 */
	@Override
	protected void cancelPressed() {

		setResult(null);

		super.cancelPressed();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
	 */
	@Override
	protected void okPressed() {

		ISelection select = fViewer.getSelection();
		setSelectionResult(new Object[] { select });

		super.okPressed();
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets
	 * .Composite)
	 */
	@Override
	protected Control createDialogArea(Composite parent) {

		PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, DebugUIPlugin.getUniqueIdentifier() + ".AddMemoryRenderingDialog_context"); //$NON-NLS-1$
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout compositeLayout = new GridLayout();
		compositeLayout.numColumns = 3;
		compositeLayout.makeColumnsEqualWidth = true;
		composite.setLayout(compositeLayout);

		GridData comositeSpec = new GridData();
		comositeSpec.grabExcessVerticalSpace = true;
		comositeSpec.grabExcessHorizontalSpace = true;
		comositeSpec.horizontalAlignment = GridData.FILL;
		comositeSpec.verticalAlignment = GridData.CENTER;
		composite.setLayoutData(comositeSpec);

		Label textLabel = new Label(composite, SWT.NONE);
		textLabel.setText(DebugUIMessages.AddMemoryRenderingDialog_Memory_Monitor);
		GridData textLayout = new GridData();
		textLayout.verticalAlignment = GridData.CENTER;
		textLayout.horizontalAlignment = GridData.BEGINNING;
		textLabel.setLayoutData(textLayout);

		memoryBlock = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
		GridData spec = new GridData(GridData.FILL_HORIZONTAL);
		spec.grabExcessVerticalSpace = false;
		spec.grabExcessHorizontalSpace = false;
		spec.horizontalAlignment = GridData.FILL;
		spec.verticalAlignment = GridData.FILL;
		spec.horizontalSpan = 4;
		memoryBlock.setLayoutData(spec);

		Label filler = new Label(composite, SWT.NONE);
		filler.setText(" "); //$NON-NLS-1$
		GridData fillerData = new GridData(GridData.FILL_HORIZONTAL);
		fillerData.horizontalSpan = 2;
		filler.setLayoutData(fillerData);

		addNew = new Button(composite, SWT.NONE);
		addNew.setText(DebugUIMessages.AddMemoryRenderingDialog_Add_New);
		GridData specButton = new GridData();
		specButton.horizontalAlignment = GridData.END;
		specButton.verticalAlignment = GridData.CENTER;
		addNew.setLayoutData(specButton);

		fAddNewSelectionAdapter = new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				RetargetAddMemoryBlockAction action = new RetargetAddMemoryBlockAction(fSite, false);
				action.run();
				action.dispose();
			}
		};

		addNew.addSelectionListener(fAddNewSelectionAdapter);

		fSelectionListener = new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				int idx = memoryBlock.getSelectionIndex();

				// avoid null pointer exception
				if (fMemoryBlocks == null)
					return;

				fSelectedMemoryBlock = fMemoryBlocks[idx];

				fViewer.setInput(fSelectedMemoryBlock);

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		};

		memoryBlock.addSelectionListener(fSelectionListener);

		Label renderingLabel = new Label(composite, SWT.NONE);
		renderingLabel.setText(DebugUIMessages.AddMemoryRenderingDialog_Memory_renderings);
		GridData renderingLayout = new GridData();
		renderingLayout.horizontalAlignment = GridData.BEGINNING;
		renderingLayout.verticalAlignment = GridData.CENTER;
		renderingLayout.horizontalSpan = 3;
		renderingLabel.setLayoutData(renderingLayout);

		fViewer = new ListViewer(composite);
		fViewer.setContentProvider(new MemoryRenderingContentProvider());
		fViewer.setLabelProvider(new MemoryRenderingLabelProvider());

		GridData listLayout = new GridData(GridData.FILL_BOTH);
		listLayout.horizontalSpan = 3;
		listLayout.heightHint = 140;
		fViewer.getControl().setLayoutData(listLayout);

		fViewer.addDoubleClickListener(new IDoubleClickListener() {

			@Override
			public void doubleClick(DoubleClickEvent event) {
				okPressed();
			}
		});

		IMemoryBlock currentBlock = getMemoryBlockToSelect(null);
		if (currentBlock == null) {
			addNew();
		} else {
			populateDialog(currentBlock);
		}

		fSelectionChangedListener = new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				ISelection selection = fViewer.getSelection();

				if (selection.isEmpty()) {
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				} else {
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		};

		fViewer.addSelectionChangedListener(fSelectionChangedListener);

		DebugPlugin.getDefault().getMemoryBlockManager().addListener(fMemoryBlockListener);
		DebugUITools.getMemoryRenderingManager().addListener(fBindingListener);

		return composite;
	}

	public AddMemoryRenderingDialog(Shell parent, IMemoryRenderingSite site) {
		super(parent);
		super.setTitle(DebugUIMessages.AddMemoryRenderingDialog_Add_memory_rendering);
		setShellStyle(getShellStyle() | SWT.RESIZE);
		fSite = site;
	}

	private void doPopulateDialog(Combo combo, ListViewer viewer, String[] labels, int selectionIdx, IMemoryBlock currentBlock) {
		// clean up
		combo.removeAll();

		for (int i = 0; i < labels.length; i++) {
			combo.add(labels[i]);
		}

		combo.select(selectionIdx);
		fSelectedMemoryBlock = currentBlock;

		viewer.setInput(currentBlock);
	}

	private IMemoryBlock getMemoryBlockToSelect(IMemoryBlock lastAdded) {
		IMemoryBlock currentBlock = null;

		if (lastAdded != null)
			currentBlock = lastAdded;
		else {
			// take Memory View's selection if possible
			ISelectionProvider selectionProvider = fSite.getSite().getSelectionProvider();
			ISelection selection = null;

			if (selectionProvider != null)
				selection = selectionProvider.getSelection();
			else
				// otherwise, take selection from selection service
				selection = DebugUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(IDebugUIConstants.ID_MEMORY_VIEW);

			IMemoryBlock element = getMemoryBlock(selection);

			if (element == null) {
				IAdaptable context = DebugUITools.getPartDebugContext(fSite.getSite());

				if (context != null) {
					IMemoryBlockRetrieval retrieval = MemoryViewUtil.getMemoryBlockRetrieval(context);

					if (retrieval == null)
						return currentBlock;

					IMemoryBlock[] blocks = new IMemoryBlock[0];

					if (retrieval != null)
						blocks = MemoryViewUtil.getMemoryBlockManager().getMemoryBlocks(retrieval);

					if (blocks.length > 0)
						currentBlock = blocks[0];
				}
			} else {
				currentBlock = element;
			}
		}
		return currentBlock;
	}

	private String[] getLabels(IMemoryBlock[] memoryBlocks) {
		String[] labels = new String[memoryBlocks.length];
		for (int i = 0; i < memoryBlocks.length; i++) {
			String text = IInternalDebugCoreConstants.EMPTY_STRING;
			if (memoryBlocks[i] instanceof IMemoryBlockExtension) {
				try {
					text = ((IMemoryBlockExtension) memoryBlocks[i]).getExpression();

					if (text == null)
						text = DebugUIMessages.AddMemoryRenderingDialog_Unknown;

					if (((IMemoryBlockExtension) memoryBlocks[i]).getBigBaseAddress() != null) {
						text += " : 0x"; //$NON-NLS-1$
						text += ((IMemoryBlockExtension) memoryBlocks[i]).getBigBaseAddress().toString(16);
					}
				} catch (DebugException e) {
					long address = memoryBlocks[i].getStartAddress();
					text = Long.toHexString(address);
				}
			} else {
				long address = memoryBlocks[i].getStartAddress();
				text = Long.toHexString(address);
			}

			// ask decorator to decorate to ensure consistent label
			ILabelDecorator decorator = fMemoryBlocks[i].getAdapter(ILabelDecorator.class);
			if (decorator != null)
				text = decorator.decorateText(text, fMemoryBlocks[i]);

			labels[i] = text;
		}
		return labels;
	}

	private IMemoryBlock getMemoryBlock(ISelection selection) {
		if (!(selection instanceof IStructuredSelection))
			return null;

		// only single selection of debug element is allowed for this action
		if (selection.isEmpty() || ((IStructuredSelection) selection).size() > 1) {
			return null;
		}

		Object elem = ((IStructuredSelection) selection).getFirstElement();

		if (elem instanceof IMemoryBlock)
			return (IMemoryBlock) elem;
		else if (elem instanceof IMemoryRendering)
			return ((IMemoryRendering) elem).getMemoryBlock();
		else
			return null;
	}

	public IMemoryBlock getMemoryBlock() {
		return fSelectedMemoryBlock;
	}

	/**
	 * @param currentBlock the current memory block context
	 */
	private void populateDialog(IMemoryBlock currentBlock) {
		final IMemoryBlock selectMB = currentBlock;
		Job job = new Job("Populate dialog") //$NON-NLS-1$
		{
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				IMemoryBlockRetrieval mbRetrieval = MemoryViewUtil.getMemoryBlockRetrieval(selectMB);

				if (mbRetrieval != null) {
					fMemoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(mbRetrieval);
					int selectionIdx = 0;
					for (int i = 0; i < fMemoryBlocks.length; i++) {
						if (fMemoryBlocks[i] == selectMB) {
							selectionIdx = i;
							break;
						}
					}

					final String[] labels = getLabels(fMemoryBlocks);
					final int idx = selectionIdx;
					final IMemoryBlock selectedBlk = selectMB;
					WorkbenchJob wbJob = new WorkbenchJob("populate dialog") { //$NON-NLS-1$

						@Override
						public IStatus runInUIThread(IProgressMonitor wbMonitor) {
							doPopulateDialog(memoryBlock, fViewer, labels, idx, selectedBlk);
							return Status.OK_STATUS;
						}
					};
					wbJob.setSystem(true);
					wbJob.schedule();
				} else {
					DebugUIPlugin.logErrorMessage("Unable to obtain memory block retrieval."); //$NON-NLS-1$
				}
				return Status.OK_STATUS;
			}
		};
		job.setSystem(true);
		job.schedule();
	}

	/**
	 *
	 */
	private void addNew() {
		WorkbenchJob job = new WorkbenchJob("populate dialog") { //$NON-NLS-1$

			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
				memoryBlock.add(DebugUIMessages.AddMemoryRenderingDialog_Add_New);
				memoryBlock.select(0);
				return Status.OK_STATUS;
			}
		};
		job.setSystem(true);
		job.schedule();
	}
}

Back to the top