Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b303ff8884bb62799e11b883891fd86a4f6bcc40 (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
/*******************************************************************************
 * Copyright (c) 2005, 2011 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
 *     Pawel Piech - Bug 210023: Another NPE in DefaultWatchExpressionModelProxy
 *******************************************************************************/
package org.eclipse.debug.internal.ui.viewers.update;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IWatchExpression;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
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.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.UIJob;

/**
 * Model proxy for the expressions view
 *
 * @see org.eclipse.debug.internal.ui.views.expression.ExpressionView
 * @see org.eclipse.debug.internal.ui.model.elements.ExpressionContentProvider
 * @see org.eclipse.debug.internal.ui.model.elements.ExpressionsViewMementoProvider
 *
 * @since 3.2
 */
public class DefaultWatchExpressionModelProxy extends DefaultExpressionModelProxy implements IDebugContextListener {

	private IWorkbenchWindow fWindow;

	public DefaultWatchExpressionModelProxy(IWatchExpression expression) {
		super(expression);
	}

	@Override
	public void installed(final Viewer viewer) {
		super.installed(viewer);
		UIJob job = new UIJob("install watch expression model proxy") { //$NON-NLS-1$
			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
			    if (!isDisposed()) {
    				IWorkbenchWindow[] workbenchWindows = PlatformUI.getWorkbench().getWorkbenchWindows();
    				for (int i = 0; i < workbenchWindows.length; i++) {
    					IWorkbenchWindow window = workbenchWindows[i];
    					// Virtual viewer may have a null control.
    					Control control = viewer.getControl();
    					if (control != null && control.getShell().equals(window.getShell())) {
    						fWindow = window;
    						break;
    					}
    				}
    				if (fWindow == null) {
    					fWindow = DebugUIPlugin.getActiveWorkbenchWindow();
    				}
    				IDebugContextService contextService = DebugUITools.getDebugContextManager().getContextService(fWindow);
    				contextService.addDebugContextListener(DefaultWatchExpressionModelProxy.this);
    				ISelection activeContext = contextService.getActiveContext();
    				if (activeContext != null) {
    					contextActivated(activeContext);
    				}
			    }
				return Status.OK_STATUS;
			}

		};
		job.setSystem(true);
		job.schedule();
	}

	@Override
	public synchronized void dispose() {
		super.dispose();
		if (fWindow != null) {
            DebugUITools.getDebugContextManager().getContextService(fWindow).removeDebugContextListener(this);
    		fWindow = null;
		}
	}

	@Override
	protected DebugEventHandler[] createEventHandlers() {
		return new DebugEventHandler[]{new ExpressionEventHandler(this)};
	}

	/**
	 * Handles the activation of the specified debug context (i.e. the selection)
	 * @param selection the specified context to 'activate'
	 */
	protected void contextActivated(ISelection selection) {
		if (fWindow != null) {
			if (selection instanceof IStructuredSelection) {
				IDebugElement context = null;
				IStructuredSelection ss = (IStructuredSelection)selection;
				if (ss.size() < 2) {
					Object object = ss.getFirstElement();
					if (object instanceof IDebugElement) {
						context= (IDebugElement) object;
					} else if (object instanceof ILaunch) {
						context= ((ILaunch) object).getDebugTarget();
					}
				}
				IWatchExpression expression = (IWatchExpression)getExpression();
				if (expression != null){
					expression.setExpressionContext(context);
				}
			}
		}
	}

	@Override
	public void debugContextChanged(DebugContextEvent event) {
		if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) {
			contextActivated(event.getContext());
		}
	}

}

Back to the top