Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5f2302adbefe0bc3fb59196e06a26c733d994ba6 (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
/*******************************************************************************
 *  Copyright (c) 2006, 2016 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
 *     QNX Software Systems - Mikhail Khodjaiants - Registers View (Bug 53640)
 *     Wind River Systems -  adopted to use with Modules view
 *     Ericsson AB		  -  Modules view for DSF implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.debug.ui.viewmodel.modules.detail;

import java.util.concurrent.ExecutionException;

import org.eclipse.cdt.debug.ui.ICDebugUIConstants;
import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.IDsfStatusConstants;
import org.eclipse.cdt.dsf.concurrent.Query;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.datamodel.IDMContext;
import org.eclipse.cdt.dsf.debug.internal.ui.IDsfDebugHelpContextIds;
import org.eclipse.cdt.dsf.debug.internal.ui.IInternalDsfDebugUIConstants;
import org.eclipse.cdt.dsf.debug.internal.ui.viewmodel.detailsupport.MessagesForDetailPane;
import org.eclipse.cdt.dsf.debug.internal.ui.viewmodel.detailsupport.TextViewerAction;
import org.eclipse.cdt.dsf.debug.service.IModules;
import org.eclipse.cdt.dsf.debug.service.IModules.IModuleDMContext;
import org.eclipse.cdt.dsf.debug.service.IModules.IModuleDMData;
import org.eclipse.cdt.dsf.internal.ui.DsfUIPlugin;
import org.eclipse.cdt.dsf.service.DsfServicesTracker;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.dsf.ui.viewmodel.datamodel.IDMVMContext;
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.ui.IDebugUIConstants;
import org.eclipse.debug.ui.IDebugView;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.ITextOperationTarget;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.progress.WorkbenchJob;

/**
 *
 */
public class ModuleDetailPane extends ModulesAbstractDetailPane implements IAdaptable, IPropertyChangeListener {

	/**
	 * These are the IDs for the actions in the context menu
	 */
	protected static final String DETAIL_COPY_ACTION = ActionFactory.COPY.getId() + ".SourceDetailPane"; //$NON-NLS-1$
	protected static final String DETAIL_SELECT_ALL_ACTION = IDebugView.SELECT_ALL_ACTION + ".SourceDetailPane"; //$NON-NLS-1$

	/**
	 * The ID, name and description of this pane are stored in constants so that the class
	 * does not have to be instantiated to access them.
	 */
	public static final String ID = "ModuleDetailPane"; //$NON-NLS-1$
	public static final String NAME = "Module Viewer"; //$NON-NLS-1$
	public static final String DESCRIPTION = "A detail pane that is based on a source viewer.  Displays as text and has actions for assigning values, content assist and text modifications."; //$NON-NLS-1$

	/**
	 * The source viewer in which the computed string detail
	 * of selected modules will be displayed.
	 */
	private SourceViewer fSourceViewer;

	@Override
	public Control createControl(Composite parent) {
		createSourceViewer(parent);

		if (isInView()) {
			createViewSpecificComponents();
			createActions();
			DsfUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this);
			JFaceResources.getFontRegistry().addListener(this);
		}
		return fSourceViewer.getControl();
	}

	private DetailJob fDetailJob = null;

	@Override
	public void display(IStructuredSelection selection) {
		if (selection == null) {
			clearSourceViewer();
			return;
		}

		if (isInView()) {
			fSourceViewer.setEditable(true);
		}

		if (selection.isEmpty()) {
			clearSourceViewer();
			return;
		}

		synchronized (this) {
			if (fDetailJob != null) {
				fDetailJob.cancel();
			}
			fDetailJob = new DetailJob(selection.getFirstElement());
			fDetailJob.schedule();
		}

	}

	/**
	 * Clears the source viewer, removes all text.
	 */
	protected void clearSourceViewer() {
		if (fDetailJob != null) {
			fDetailJob.cancel();
		}
		fDetailDocument.set(""); //$NON-NLS-1$
		fSourceViewer.setEditable(false);
	}

	@Override
	public void dispose() {
		super.dispose();
		if (fDetailJob != null)
			fDetailJob.cancel();
		if (fSourceViewer != null && fSourceViewer.getControl() != null)
			fSourceViewer.getControl().dispose();

		if (isInView()) {
			DsfUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this);
			JFaceResources.getFontRegistry().removeListener(this);
		}

	}

	@Override
	public String getDescription() {
		return DESCRIPTION;
	}

	@Override
	public String getID() {
		return ID;
	}

	@Override
	public String getName() {
		return NAME;
	}

	@Override
	public boolean setFocus() {
		if (fSourceViewer != null) {
			fSourceViewer.getTextWidget().setFocus();
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getAdapter(Class<T> adapter) {
		if (ITextViewer.class.equals(adapter)) {
			return (T) fSourceViewer;
		}
		return null;
	}

	@Override
	public void propertyChange(PropertyChangeEvent event) {
		String propertyName = event.getProperty();
		if (propertyName.equals(IDebugUIConstants.PREF_DETAIL_PANE_FONT)) {
			fSourceViewer.getTextWidget().setFont(JFaceResources.getFont(IDebugUIConstants.PREF_DETAIL_PANE_FONT));
		}
	}

	/**
	 * Creates the source viewer in the given parent composite
	 *
	 * @param parent Parent composite to create the source viewer in
	 */
	private void createSourceViewer(Composite parent) {

		// Create & configure a SourceViewer
		fSourceViewer = new SourceViewer(parent, null, SWT.V_SCROLL | SWT.H_SCROLL);
		fSourceViewer.setDocument(getDetailDocument());
		fSourceViewer.getTextWidget().setFont(JFaceResources.getFont(IDebugUIConstants.PREF_DETAIL_PANE_FONT));
		fSourceViewer.getTextWidget().setWordWrap(DsfUIPlugin.getDefault().getPreferenceStore()
				.getBoolean(IInternalDsfDebugUIConstants.PREF_DETAIL_PANE_WORD_WRAP));
		fSourceViewer.setEditable(false);
		PlatformUI.getWorkbench().getHelpSystem().setHelp(fSourceViewer.getTextWidget(),
				IDsfDebugHelpContextIds.DETAIL_PANE);
		Control control = fSourceViewer.getControl();
		GridData gd = new GridData(GridData.FILL_BOTH);
		control.setLayoutData(gd);
	}

	/**
	 * Variables used to create the detailed information for a selection
	 */
	private IDocument fDetailDocument;

	/**
	 * Lazily instantiate and return a Document for the detail pane text viewer.
	 */
	protected IDocument getDetailDocument() {
		if (fDetailDocument == null) {
			fDetailDocument = new Document();
		}
		return fDetailDocument;
	}

	/**
	 * Creates listeners and other components that should only be added to the
	 * source viewer when this detail pane is inside a view.
	 */
	private void createViewSpecificComponents() {

		// Add a document listener so actions get updated when the document changes
		getDetailDocument().addDocumentListener(new IDocumentListener() {
			@Override
			public void documentAboutToBeChanged(DocumentEvent event) {
			}

			@Override
			public void documentChanged(DocumentEvent event) {
				updateSelectionDependentActions();
			}
		});

		// Add the selection listener so selection dependent actions get updated.
		fSourceViewer.getSelectionProvider().addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				updateSelectionDependentActions();
			}
		});

		// Add a focus listener to update actions when details area gains focus
		fSourceViewer.getControl().addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {

				getViewSite().setSelectionProvider(fSourceViewer.getSelectionProvider());

				setGlobalAction(IDebugView.SELECT_ALL_ACTION, getAction(DETAIL_SELECT_ALL_ACTION));
				setGlobalAction(IDebugView.COPY_ACTION, getAction(DETAIL_COPY_ACTION));

				getViewSite().getActionBars().updateActionBars();
			}

			@Override
			public void focusLost(FocusEvent e) {

				getViewSite().setSelectionProvider(null);

				setGlobalAction(IDebugView.SELECT_ALL_ACTION, null);
				setGlobalAction(IDebugView.COPY_ACTION, null);
				getViewSite().getActionBars().updateActionBars();

			}
		});

		// Add a context menu to the detail area
		createDetailContextMenu(fSourceViewer.getTextWidget());
	}

	/**
	 * Create the context menu particular to the detail pane.  Note that anyone
	 * wishing to contribute an action to this menu must use
	 * <code>ICDebugUIConstants.MODULES_VIEW_DETAIL_ID</code> as the
	 * <code>targetID</code> in the extension XML.
	 */
	protected void createDetailContextMenu(Control menuControl) {
		MenuManager menuMgr = new MenuManager();
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			@Override
			public void menuAboutToShow(IMenuManager mgr) {
				fillDetailContextMenu(mgr);
			}
		});
		Menu menu = menuMgr.createContextMenu(menuControl);
		menuControl.setMenu(menu);

		getViewSite().registerContextMenu(ICDebugUIConstants.MODULES_VIEW_DETAIL_ID, menuMgr,
				fSourceViewer.getSelectionProvider());

	}

	/**
	 * Adds items to the detail pane's context menu including any extension defined
	 * actions.
	 *
	 * @param menu The menu to add the item to.
	 */
	protected void fillDetailContextMenu(IMenuManager menu) {

		menu.add(new Separator(ICDebugUIConstants.MODULES_GROUP));
		menu.add(new Separator());
		menu.add(getAction(DETAIL_COPY_ACTION));
		menu.add(getAction(DETAIL_SELECT_ALL_ACTION));
		menu.add(new Separator());
		menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));

	}

	/**
	 * Creates the actions to add to the context menu
	 */
	private void createActions() {
		TextViewerAction textAction = new TextViewerAction(fSourceViewer, ITextOperationTarget.SELECT_ALL);
		textAction.configureAction(MessagesForDetailPane.DetailPane_Select_All, "", ""); //$NON-NLS-1$ //$NON-NLS-2$
		textAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_SELECT_ALL);
		PlatformUI.getWorkbench().getHelpSystem().setHelp(textAction,
				IDsfDebugHelpContextIds.DETAIL_PANE_SELECT_ALL_ACTION);
		setAction(DETAIL_SELECT_ALL_ACTION, textAction);

		textAction = new TextViewerAction(fSourceViewer, ITextOperationTarget.COPY);
		textAction.configureAction(MessagesForDetailPane.DetailPane_Copy, "", ""); //$NON-NLS-1$ //$NON-NLS-2$
		textAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_COPY);
		PlatformUI.getWorkbench().getHelpSystem().setHelp(textAction, IDsfDebugHelpContextIds.DETAIL_PANE_COPY_ACTION);
		setAction(DETAIL_COPY_ACTION, textAction);

		setSelectionDependantAction(DETAIL_COPY_ACTION);

		updateSelectionDependentActions();
	}

	/**
	 * Job to compute the details for a selection
	 */
	class DetailJob extends Job {

		private Object fElement;
		// whether a result was collected
		private IProgressMonitor fMonitor;

		public DetailJob(Object element) {
			super("compute module details"); //$NON-NLS-1$
			setSystem(true);
			fElement = element;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
		 */
		@Override
		protected IStatus run(IProgressMonitor monitor) {
			fMonitor = monitor;
			/*
			 *  Make sure this is an element we want to deal with.
			 */
			IModuleDMContext dmc = null;
			if (fElement instanceof IDMVMContext) {
				IDMContext vmcdmc = ((IDMVMContext) fElement).getDMContext();
				dmc = DMContexts.getAncestorOfType(vmcdmc, IModuleDMContext.class);
			}

			if (dmc == null)
				return Status.OK_STATUS;
			DsfSession session = DsfSession.getSession(dmc.getSessionId());
			if (session == null)
				return Status.OK_STATUS;

			/*
			 * Create the query to write the value to the service. Note: no need to
			 * guard against RejectedExecutionException, because
			 * DsfSession.getSession() above would only return an active session.
			 */
			GetModuleDetailsQuery query = new GetModuleDetailsQuery(dmc);
			session.getExecutor().execute(query);

			/*
			 * Now we have the data, go and get it. Since the call is completed now
			 * the ".get()" will not suspend it will immediately return with the
			 * data.
			 */
			try {
				detailComputed(getModuleDetail((IModuleDMData) query.get()));
			} catch (InterruptedException e) {
				assert false;
				return Status.OK_STATUS;
			} catch (ExecutionException e) {
				return Status.OK_STATUS;
			}
			return Status.OK_STATUS;
		}

		/**
		 * Set the module details in the detail pane view
		 * @param result
		 */
		private void detailComputed(final String result) {
			if (!fMonitor.isCanceled()) {
				WorkbenchJob setDetail = new WorkbenchJob("set details") { //$NON-NLS-1$
					@Override
					public IStatus runInUIThread(IProgressMonitor monitor) {
						if (!fMonitor.isCanceled()) {
							getDetailDocument().set(result);
						}
						return Status.OK_STATUS;
					}
				};
				setDetail.setSystem(true);
				setDetail.schedule();
			}
		}

	}

	/**
	 * To get the details of the given module selected in Modules View
	 * @param module
	 * @return
	 */
	private String getModuleDetail(IModuleDMData module) {
		StringBuilder sb = new StringBuilder();

		// Type
		String type = null;
		//        switch( module.getType() ) {
		//            case ICModule.EXECUTABLE:
		//                type = ModulesMessages.getString( "ModulesView.Executable" ); //$NON-NLS-1$
		//                break;
		//            case ICModule.SHARED_LIBRARY:
		//                type = ModulesMessages.getString( "ModulesView.SharedLibrary" ); //$NON-NLS-1$
		//                break;
		//        }
		type = ModulesMessages.getString("ModulesView.SharedLibrary"); //$NON-NLS-1$
		if (type != null) {
			sb.append(ModulesMessages.getString("ModulesView.Type")); //$NON-NLS-1$
			sb.append(type);
			sb.append('\n');
		}

		// Symbols flag
		sb.append(ModulesMessages.getString("ModulesView.Symbols")); //$NON-NLS-1$
		sb.append((module.isSymbolsLoaded()) ? ModulesMessages.getString("ModulesView.Loaded") //$NON-NLS-1$
				: ModulesMessages.getString("ModulesView.NotLoaded")); //$NON-NLS-1$
		sb.append('\n');

		// Symbols file
		sb.append(ModulesMessages.getString("ModulesView.SymbolsFile")); //$NON-NLS-1$
		sb.append(module.getFile());
		sb.append('\n');

		// Base address
		String baseAddress = module.getBaseAddress();
		sb.append(ModulesMessages.getString("ModulesView.BaseAddress")); //$NON-NLS-1$
		sb.append(baseAddress);
		sb.append('\n');

		// Size
		long size = module.getSize();
		if (size > 0) {
			sb.append(ModulesMessages.getString("ModulesView.Size")); //$NON-NLS-1$
			sb.append(size);
			sb.append('\n');
		}

		return sb.toString();
	}

	public class GetModuleDetailsQuery extends Query<Object> {

		private IModuleDMContext fDmc;

		public GetModuleDetailsQuery(IModuleDMContext dmc) {
			super();
			fDmc = dmc;
		}

		@Override
		protected void execute(final DataRequestMonitor<Object> rm) {
			/*
			 * We're in another dispatch, so we must guard against executor
			 * shutdown again.
			 */
			final DsfSession session = DsfSession.getSession(fDmc.getSessionId());
			if (session == null) {
				rm.setStatus(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID, IDsfStatusConstants.INVALID_STATE,
						"Debug session already shut down.", null)); //$NON-NLS-1$
				rm.done();
				return;
			}

			/*
			 * Guard against a disposed service
			 */
			DsfServicesTracker tracker = new DsfServicesTracker(DsfUIPlugin.getBundleContext(), fDmc.getSessionId());
			IModules service = tracker.getService(IModules.class);
			tracker.dispose();
			if (service == null) {
				rm.setStatus(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID, IDsfStatusConstants.INVALID_STATE,
						"Service unavailable", null)); //$NON-NLS-1$
				rm.done();
				return;
			}

			service.getModuleData(fDmc, new DataRequestMonitor<IModuleDMData>(session.getExecutor(), rm) {
				@Override
				protected void handleCompleted() {
					/*
					 * We're in another dispatch, so we must guard against executor shutdown again.
					 */
					if (!DsfSession.isSessionActive(session.getId())) {
						rm.setStatus(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID, IDsfStatusConstants.INVALID_STATE,
								"Debug session already shut down.", null)); //$NON-NLS-1$
						rm.done();
						return;
					}
					super.handleCompleted();
				}

				@Override
				protected void handleSuccess() {
					/*
					 * All good set return value.
					 */
					rm.setData(getData());
					rm.done();
				}
			});
		}
	}
}

Back to the top