Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: da755d2eb3a165d726372ce03c1ad74e1fc1aa12 (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
/*******************************************************************************
 * Copyright (c) 2004, 2014 QNX Software Systems 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:
 * QNX Software Systems - Initial API and implementation
 * Alvaro Sanchez-Leon (Ericsson) - preserve selection changes (needed by Bug 235747)
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.actions;

import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IDebugEventSetListener;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.IDebugView;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.INullSelectionListener;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionDelegate;

/**
 * The superclass for action delegates of views different than the Debug view and
 * driven by the selection in the Debug view.
 */
public abstract class AbstractViewActionDelegate extends ActionDelegate
		implements IViewActionDelegate, INullSelectionListener, IDebugEventSetListener {

	private IAction fAction;

	private IDebugView fView;

	private IStructuredSelection fSelection = StructuredSelection.EMPTY;

	@Override
	public void init(IViewPart view) {
		setView(view);
		DebugPlugin.getDefault().addDebugEventListener(this);
		IWorkbenchWindow window = getWindow();
		if (window != null) {
			window.getSelectionService().addSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
		}
		IAdaptable context = DebugUITools.getDebugContext();
		IStructuredSelection ss = (context != null) ? new StructuredSelection(context) : StructuredSelection.EMPTY;
		selectionChanged((IWorkbenchPart) null, ss);
	}

	@Override
	public void dispose() {
		IWorkbenchWindow window = getWindow();
		if (window != null) {
			window.getSelectionService().removeSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
		}
		DebugPlugin.getDefault().removeDebugEventListener(this);
		super.dispose();
	}

	@Override
	public void init(IAction action) {
		setAction(action);
		action.setEnabled(false);
		super.init(action);
	}

	protected IDebugView getView() {
		return fView;
	}

	private void setView(IViewPart view) {
		fView = (view instanceof IDebugView) ? (IDebugView) view : null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	@Override
	public void run(IAction action) {
		final MultiStatus ms = new MultiStatus(CDebugUIPlugin.getUniqueIdentifier(), DebugException.REQUEST_FAILED, "", //$NON-NLS-1$
				null);
		BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {

			@Override
			public void run() {
				try {
					doAction();
				} catch (DebugException e) {
					ms.merge(e.getStatus());
				}
			}
		});
		if (!ms.isOK()) {
			IWorkbenchWindow window = CDebugUIPlugin.getActiveWorkbenchWindow();
			if (window != null) {
				CDebugUIPlugin.errorDialog(getErrorDialogMessage(), ms.getChildren()[0]);
			} else {
				CDebugUIPlugin.log(ms);
			}
		}
	}

	protected IAction getAction() {
		return fAction;
	}

	private void setAction(IAction action) {
		fAction = action;
	}

	private IWorkbenchWindow getWindow() {
		if (getView() != null) {
			return getView().getViewSite().getWorkbenchWindow();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		setSelection(selection);
		update();
	}

	protected IStructuredSelection getSelection() {
		return fSelection;
	}

	protected void setSelection(ISelection selection) {
		fSelection = (selection instanceof IStructuredSelection) ? (IStructuredSelection) selection
				: StructuredSelection.EMPTY;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
	 */
	@Override
	public void handleDebugEvents(final DebugEvent[] events) {
		if (getWindow() == null || getAction() == null) {
			return;
		}
		Shell shell = getWindow().getShell();
		if (shell == null || shell.isDisposed()) {
			return;
		}
		Runnable r = new Runnable() {

			@Override
			public void run() {
				for (int i = 0; i < events.length; i++) {
					if (events[i].getSource() != null) {
						doHandleDebugEvent(events[i]);
					}
				}
			}
		};
		shell.getDisplay().asyncExec(r);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.actions.ActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		setSelection(selection);
		update();
	}

	protected abstract String getErrorDialogTitle();

	protected abstract String getErrorDialogMessage();

	protected abstract void doAction() throws DebugException;

	protected abstract void update();

	protected abstract void doHandleDebugEvent(DebugEvent event);
}

Back to the top