Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5337c5ff531a017906d9e1a1e4791011ca3becbf (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
/*******************************************************************************
  * Copyright (c) 2005, 2015 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
 *     Wind River - Pawel Piech - Fixed debug context service usage (Bug 258189)
 *******************************************************************************/
package org.eclipse.debug.internal.ui.sourcelookup;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.debug.internal.ui.views.launch.DebugElementAdapterFactory;
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.contexts.IDebugContextService;
import org.eclipse.debug.ui.sourcelookup.ISourceDisplay;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;

/**
 * Performs source lookup in a window.
 *
 * @since 3.2
 */
public class SourceLookupService implements IDebugContextListener, ISourceDisplay {

	private IWorkbenchWindow fWindow;
	private IDebugContextService fDebugContextService;

	public SourceLookupService(IWorkbenchWindow window) {
		fWindow = window;
		fDebugContextService = DebugUITools.getDebugContextManager().getContextService(window);
		fDebugContextService.addDebugContextListener(this);
	}

	public void dispose() {
		fDebugContextService.removeDebugContextListener(this);
		fWindow = null;
	}

	@Override
	public synchronized void debugContextChanged(DebugContextEvent event) {
		if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) {
			displaySource(event.getContext(), event.getDebugContextProvider().getPart(), false);
		}
	}

	/**
	 * Displays source for the given selection and part, optionally forcing
	 * a source lookup.
	 *
	 * @param selection
	 * @param part
	 * @param force
	 */
	protected synchronized void displaySource(ISelection selection, IWorkbenchPart part, boolean force) {
	    if (fWindow == null) return; // disposed

		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection)selection;
			if (structuredSelection.size() == 1) {
				Object context = (structuredSelection).getFirstElement();
				IWorkbenchPage page = null;
				if (part == null) {
					page = fWindow.getActivePage();
				} else {
					page = part.getSite().getPage();
				}
				displaySource(context, page, force);
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.ui.contexts.ISourceDisplayAdapter#displaySource(java.lang.Object, org.eclipse.ui.IWorkbenchPage, boolean)
	 */
	@Override
	public void displaySource(Object context, IWorkbenchPage page, boolean forceSourceLookup) {
		if (context instanceof IAdaptable) {
			IAdaptable adaptable = (IAdaptable) context;
			ISourceDisplay adapter = adaptable.getAdapter(ISourceDisplay.class);
			if (adapter == null && !(context instanceof PlatformObject)) {
	        	// for objects that don't properly subclass PlatformObject to inherit default
	        	// adapters, just delegate to the adapter factory
	        	adapter = new DebugElementAdapterFactory().getAdapter(context, ISourceDisplay.class);
	        }
			if (adapter != null) {
				adapter.displaySource(context, page, forceSourceLookup);
			}
		}
	}
}

Back to the top