Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4dd676405ae5a448c84d7d220b4bea9fcd6ba1fd (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
package org.eclipse.debug.internal.ui;

/*
 * Licensed Materials - Property of IBM,
 * WebSphere Studio Workbench
 * (c) Copyright IBM Corp 2000
 */

import java.util.*;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.debug.core.*;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.jface.action.Action;
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.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;

/**
 * This is the debug action which appears in the desktop menu and toolbar.
 */
public abstract class ExecutionAction extends Action {

	private final static String PREFIX= "execution_action.";
	private final static String ERROR= "error.";
	private final static String STATUS= PREFIX + "status";

	/**
	 * @see Action
	 */
	public void run() {

		final IWorkbenchWindow dwindow= DebugUIPlugin.getActiveWorkbenchWindow();
		final IStructuredSelection selection= resolveSelection(dwindow);

		// if the selection is a debug element, system process, or launch, try to do a relaunch
		if (selection != null && attemptRelaunch(selection)) {
			return;
		}

		// otherwise, resolve a launcher and an element
		final Object[] launchers= resolveLaunchers(selection);
		if (launchers.length == 0) {
			// could not determine any launchers to use to launch
			// very unlikely to happen
			beep();
			return;
		}

		BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
			public void run() {
				// if there are no choices to make, do the launch
				if (launchers.length == 1 && selection != null) {
					ILauncher launcher= (ILauncher)launchers[0];
					Object[] elements= selection.toArray();
					launcher.launch(elements, getMode());
				} else {
					// must choose a launcher
					useWizard(launchers, dwindow.getShell(), selection);
				}
			}
		});
	}

	/**
	 * Returns the mode of a launcher to use for this action
	 */
	protected abstract String getMode();

	/**
	 * Returns the launch manager.
	 */
	protected static ILaunchManager getLaunchManager() {
		return DebugPlugin.getDefault().getLaunchManager();
	}

	/**
	 * Relaunches the launch in the specified mode.
	 */
	public void relaunch(ILaunch launch, String mode) {
		RelaunchActionDelegate.relaunch(launch, mode);
	}

	/**
	 * Determines and returns the selection that provides context for the launch,
	 * or <code>null</code> if there is no selection.
	 */
	protected IStructuredSelection resolveSelection(IWorkbenchWindow window) {
		if (window == null) {
			return null;
		}
		ISelection selection= window.getSelectionService().getSelection();
		if (selection == null || selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
			// there is no obvious selection - go fishing
			selection= null;
			IWorkbenchPage page= window.getActivePage();
			if (page == null) {
				//workspace is closed
				return null;
			}

			// first, see if there is an active editor, and try its input element
			IEditorPart editor= page.getActiveEditor();
			Object element= null;
			if (editor != null) {
				element= editor.getEditorInput();
			}

			if (selection == null && element != null) {
				selection= new StructuredSelection(element);
			}
		}
		return (IStructuredSelection)selection;
	}

	/**
	 * Resolves and returns the applicable launcher(s) to be used to launch the
	 * elements in the specified selection.
	 */
	protected Object[] resolveLaunchers(IStructuredSelection selection) {
		List launchers;
		if (selection == null || selection.isEmpty()) {
			launchers= Arrays.asList(getLaunchManager().getLaunchers(getMode()));
		} else {
			launchers= new ArrayList(2);
			Iterator elements= selection.iterator();
			MultiStatus status= new MultiStatus(DebugUIPlugin.getDefault().getDescriptor().getUniqueIdentifier(), IDebugStatusConstants.REQUEST_FAILED, DebugUIUtils.getResourceString(STATUS), null);
			while (elements.hasNext()) {
				Object element= elements.next();
				ILauncher defaultLauncher= null;
				try {
					IResource resource= null;
					if (element instanceof IAdaptable) {
						IAdaptable el= (IAdaptable)element;
						resource= (IResource)el.getAdapter(IResource.class);
					}
					IProject project= null;
					if (resource != null) {
						project= resource.getProject();
					}
					if (project != null) {
						defaultLauncher= getLaunchManager().getDefaultLauncher(project);
					}
					if (defaultLauncher != null) {
						if (!defaultLauncher.getModes().contains(getMode())) {
							defaultLauncher= null;
						}
					}
				} catch (CoreException e) {
					status.merge(e.getStatus());
				}
				if (defaultLauncher != null) {
					if (!launchers.contains(defaultLauncher)) {
						launchers.add(defaultLauncher);
					}
				}
			}
			if (!status.isOK()) {
				DebugUIUtils.errorDialog(DebugUIPlugin.getActiveWorkbenchWindow().getShell(), PREFIX + ERROR, status);
			}
			if (launchers.isEmpty()) {
				launchers= Arrays.asList(getLaunchManager().getLaunchers(getMode()));
			}
		}
		
		return resolveVisibleLaunchers(launchers);
	}

	protected Object[] resolveVisibleLaunchers(List launchers) {
		List visibleLaunchers= new ArrayList(2);
		Iterator itr= launchers.iterator();
		while (itr.hasNext()) {
			ILauncher launcher= (ILauncher)itr.next();
			if (DebugUIPlugin.getDefault().isVisible(launcher)) {
				//cannot use itr.remove() as the list may be a fixed size list
				visibleLaunchers.add(launcher);
			}
		}
		return visibleLaunchers.toArray();
	}
	
	protected Object[] resolveWizardLaunchers(Object[] launchers) {
		List wizardLaunchers= new ArrayList(2);
		for (int i= 0 ; i < launchers.length; i++) {
			ILauncher launcher= (ILauncher)launchers[i];
			if (DebugUIPlugin.getDefault().hasWizard(launcher)) {
				wizardLaunchers.add(launcher);
			}
		}
		return wizardLaunchers.toArray();
	}

	/**
	 * If the selection contains re-launchables, a relaunch is performed
	 * for each launch and true is returned, otherwise, false is returned.
	 */
	protected boolean attemptRelaunch(IStructuredSelection selection) {
		// if the selection is a debug element, system process, or launch, do a relaunch
		Iterator objects= selection.iterator();
		List relaunchables= null;
		while (objects.hasNext()) {
			Object object= objects.next();
			ILaunch launch= null;
			if (object instanceof IDebugElement) {
				launch= ((IDebugElement)object).getLaunch();
			} else if (object instanceof ILaunch) {
				launch= (ILaunch)object;
			} else if (object instanceof IProcess) {
				launch= ((IProcess)object).getLaunch();
			}
			if (launch != null) {
				if (relaunchables == null) {
					relaunchables= new ArrayList(1);
					relaunchables.add(launch);
				} else if (!relaunchables.contains(launch)) {
					relaunchables.add(launch);
				}
			}
		}
		if (relaunchables == null) {
			return false;
		} else {
			Iterator itr= relaunchables.iterator();
			while (itr.hasNext()) {
				relaunch((ILaunch)itr.next(), getMode());
			}
			return true;
		}
	}

	/**
	 * Use the wizard to do the launch.
	 */
	protected void useWizard(Object[] launchers, Shell shell, IStructuredSelection selection) {
		launchers= resolveWizardLaunchers(launchers);
		LaunchWizard lw= new LaunchWizard(launchers, selection, getMode());
		LaunchWizardDialog dialog= new LaunchWizardDialog(shell, lw);
		dialog.open();
	}

	/**
	 * Ring the bell
	 */
	protected void beep() {
		Display display= Display.getCurrent();
		if (display != null) {
			display.beep();
		}
	}
}

Back to the top