Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 82a00d823a3f9c2aee248733e7ff0500cfc24437 (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
/*******************************************************************************
 * Copyright (c) 2006, 2018 Wind River Systems 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:
 *     Wind River Systems - initial API and implementation
 *     IBM Corporation - bug fixing
 *******************************************************************************/
package org.eclipse.debug.internal.ui.contexts;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.contexts.DebugContextEvent;
import org.eclipse.debug.ui.contexts.IDebugContextListener;
import org.eclipse.debug.ui.contexts.IDebugContextService;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.AbstractSourceProvider;
import org.eclipse.ui.ISources;
import org.eclipse.ui.services.IEvaluationService;

/**
 * A source provider for the active debug context variable.
 *
 * @since 3.5
 */
public class DebugContextSourceProvider extends AbstractSourceProvider implements IDebugContextListener {

    /**
     * The names of the sources supported by this source provider.
     */
    private static final String[] PROVIDED_SOURCE_NAMES = new String[] { IDebugUIConstants.DEBUG_CONTEXT_SOURCE_NAME };

    private final IDebugContextService fDebugContextService;

	private final IEvaluationService fEvaluationService;

	/**
	 * Creates the source provider.  It registers it as a listener to the
	 * given debug context service, and as a provider with the given
	 * evaluation service.
	 */
	public DebugContextSourceProvider(IDebugContextService debugContextService, IEvaluationService evaluationService) {
		fDebugContextService = debugContextService;
		fDebugContextService.addDebugContextListener(this);
		fEvaluationService = evaluationService;
		fEvaluationService.addSourceProvider(this);
	}

	@Override
	public void debugContextChanged(DebugContextEvent event) {
		final Map<String, ISelection> values = new HashMap<>(1);
        values.put(IDebugUIConstants.DEBUG_CONTEXT_SOURCE_NAME, event.getContext());
		// make sure fireSourceChanged is called on the UI thread
		if (Display.getCurrent() == null) {
			DebugUIPlugin.getStandardDisplay().asyncExec(() -> fireSourceChanged(ISources.ACTIVE_CURRENT_SELECTION, values));
		} else {
			fireSourceChanged(ISources.ACTIVE_CURRENT_SELECTION, values);
		}
	}

	@Override
	public void dispose() {
		fDebugContextService.removeDebugContextListener(this);
		fEvaluationService.removeSourceProvider(this);
	}

	@Override
	public String[] getProvidedSourceNames() {
		return PROVIDED_SOURCE_NAMES;
	}

	@Override
	public Map getCurrentState() {
		Map<String, ISelection> currentState = new HashMap<>(1);
	    currentState.put(IDebugUIConstants.DEBUG_CONTEXT_SOURCE_NAME, fDebugContextService.getActiveContext());
	    return currentState;
	}

}

Back to the top