Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2005, 2009 QNX Software Systems and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* QNX Software Systems - Initial API and implementation |
10 |
* Ken Ryall (Nokia) - bug 178731 |
11 |
* Ken Ryall (Nokia) - bug 246201 |
12 |
*******************************************************************************/ |
13 |
package org.eclipse.photran.cdtinterface.launch; |
14 |
|
15 |
import java.lang.reflect.InvocationTargetException; |
16 |
import java.util.ArrayList; |
17 |
import java.util.Collections; |
18 |
import java.util.HashSet; |
19 |
import java.util.Iterator; |
20 |
import java.util.List; |
21 |
|
22 |
import org.eclipse.cdt.core.CCorePlugin; |
23 |
import org.eclipse.cdt.core.model.CModelException; |
24 |
import org.eclipse.cdt.core.model.CoreModel; |
25 |
import org.eclipse.cdt.core.model.IBinary; |
26 |
import org.eclipse.cdt.core.model.ICElement; |
27 |
import org.eclipse.cdt.core.model.ICProject; |
28 |
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription; |
29 |
import org.eclipse.cdt.core.settings.model.ICProjectDescription; |
30 |
import org.eclipse.cdt.debug.core.CDebugCorePlugin; |
31 |
import org.eclipse.cdt.debug.core.CDebugUtils; |
32 |
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants; |
33 |
import org.eclipse.cdt.debug.core.ICDebugConfiguration; |
34 |
import org.eclipse.cdt.debug.core.executables.Executable; |
35 |
import org.eclipse.cdt.debug.ui.CDebugUIPlugin; |
36 |
import org.eclipse.cdt.debug.ui.ICDebuggerPage; |
37 |
import org.eclipse.cdt.ui.CElementLabelProvider; |
38 |
import org.eclipse.core.resources.IFile; |
39 |
import org.eclipse.core.resources.IProject; |
40 |
import org.eclipse.core.resources.IResource; |
41 |
import org.eclipse.core.runtime.CoreException; |
42 |
import org.eclipse.core.runtime.IAdaptable; |
43 |
import org.eclipse.core.runtime.IPath; |
44 |
import org.eclipse.core.runtime.IProgressMonitor; |
45 |
import org.eclipse.core.runtime.Platform; |
46 |
import org.eclipse.core.runtime.SubProgressMonitor; |
47 |
import org.eclipse.debug.core.DebugPlugin; |
48 |
import org.eclipse.debug.core.ILaunchConfiguration; |
49 |
import org.eclipse.debug.core.ILaunchConfigurationType; |
50 |
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
51 |
import org.eclipse.debug.core.ILaunchDelegate; |
52 |
import org.eclipse.debug.core.ILaunchManager; |
53 |
import org.eclipse.debug.ui.DebugUITools; |
54 |
import org.eclipse.debug.ui.IDebugModelPresentation; |
55 |
import org.eclipse.debug.ui.ILaunchShortcut2; |
56 |
import org.eclipse.jface.dialogs.MessageDialog; |
57 |
import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
58 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
59 |
import org.eclipse.jface.text.ITextSelection; |
60 |
import org.eclipse.jface.viewers.ILabelProvider; |
61 |
import org.eclipse.jface.viewers.ISelection; |
62 |
import org.eclipse.jface.viewers.IStructuredSelection; |
63 |
import org.eclipse.jface.viewers.LabelProvider; |
64 |
import org.eclipse.jface.window.Window; |
65 |
import org.eclipse.swt.widgets.Shell; |
66 |
import org.eclipse.ui.IEditorInput; |
67 |
import org.eclipse.ui.IEditorPart; |
68 |
import org.eclipse.ui.IFileEditorInput; |
69 |
import org.eclipse.ui.IWorkbenchPage; |
70 |
import org.eclipse.ui.IWorkbenchWindow; |
71 |
import org.eclipse.ui.dialogs.ElementListSelectionDialog; |
72 |
import org.eclipse.ui.dialogs.TwoPaneElementSelector; |
73 |
|
74 |
public class CApplicationLaunchShortcut implements ILaunchShortcut2 { |
75 |
|
76 |
public void launch(IEditorPart editor, String mode) { |
77 |
searchAndLaunch(new Object[] { editor.getEditorInput()}, mode); |
78 |
} |
79 |
|
80 |
public void launch(ISelection selection, String mode) { |
81 |
if (selection instanceof IStructuredSelection) { |
82 |
searchAndLaunch(((IStructuredSelection) selection).toArray(), mode); |
83 |
} |
84 |
} |
85 |
|
86 |
public void launch(IBinary bin, String mode) { |
87 |
ILaunchConfiguration config = findLaunchConfiguration(bin, mode); |
88 |
if (config != null) { |
89 |
DebugUITools.launch(config, mode); |
90 |
} |
91 |
} |
92 |
|
93 |
/** |
94 |
* Locate a configuration to relaunch for the given type. If one cannot be found, create one. |
95 |
* |
96 |
* @return a re-useable config or <code>null</code> if none |
97 |
*/ |
98 |
protected ILaunchConfiguration findLaunchConfiguration(IBinary bin, String mode) { |
99 |
ILaunchConfiguration configuration = null; |
100 |
ILaunchConfigurationType configType = getCLaunchConfigType(); |
101 |
List candidateConfigs = Collections.EMPTY_LIST; |
102 |
try { |
103 |
ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(configType); |
104 |
candidateConfigs = new ArrayList(configs.length); |
105 |
for (int i = 0; i < configs.length; i++) { |
106 |
ILaunchConfiguration config = configs[i]; |
107 |
IPath programPath = CDebugUtils.getProgramPath(config); |
108 |
String projectName = CDebugUtils.getProjectName(config); |
109 |
IPath name = bin.getResource().getProjectRelativePath(); |
110 |
if (programPath != null && programPath.equals(name)) { |
111 |
if (projectName != null && projectName.equals(bin.getCProject().getProject().getName())) { |
112 |
candidateConfigs.add(config); |
113 |
} |
114 |
} |
115 |
} |
116 |
} catch (CoreException e) { |
117 |
CDebugUIPlugin.log(e); |
118 |
} |
119 |
|
120 |
// If there are no existing configs associated with the IBinary, create one. |
121 |
// If there is exactly one config associated with the IBinary, return it. |
122 |
// Otherwise, if there is more than one config associated with the IBinary, prompt the |
123 |
// user to choose one. |
124 |
int candidateCount = candidateConfigs.size(); |
125 |
if (candidateCount < 1) { |
126 |
// Set the default debugger based on the active toolchain on the project (if possible) |
127 |
ICDebugConfiguration debugConfig = null; |
128 |
IProject project = bin.getResource().getProject(); |
129 |
ICProjectDescription projDesc = CoreModel.getDefault().getProjectDescription(project); |
130 |
ICConfigurationDescription configDesc = projDesc.getActiveConfiguration(); |
131 |
String configId = configDesc.getId(); |
132 |
ICDebugConfiguration[] debugConfigs = CDebugCorePlugin.getDefault().getActiveDebugConfigurations(); |
133 |
int matchLength = 0; |
134 |
for (int i = 0; i < debugConfigs.length; ++i) { |
135 |
ICDebugConfiguration dc = debugConfigs[i]; |
136 |
String[] patterns = dc.getSupportedBuildConfigPatterns(); |
137 |
if (patterns != null) { |
138 |
for (int j = 0; j < patterns.length; ++j) { |
139 |
if (patterns[j].length() > matchLength && configId.matches(patterns[j])) { |
140 |
debugConfig = dc; |
141 |
matchLength = patterns[j].length(); |
142 |
} |
143 |
} |
144 |
} |
145 |
} |
146 |
|
147 |
if ( debugConfig == null ) { |
148 |
// Prompt the user if more then 1 debugger. |
149 |
String programCPU = bin.getCPU(); |
150 |
String os = Platform.getOS(); |
151 |
debugConfigs = CDebugCorePlugin.getDefault().getActiveDebugConfigurations(); |
152 |
List debugList = new ArrayList(debugConfigs.length); |
153 |
for (int i = 0; i < debugConfigs.length; i++) { |
154 |
String platform = debugConfigs[i].getPlatform(); |
155 |
if (debugConfigs[i].supportsMode(ICDTLaunchConfigurationConstants.DEBUGGER_MODE_RUN)) { |
156 |
if (platform.equals("*") || platform.equals(os)) { //$NON-NLS-1$ |
157 |
if (debugConfigs[i].supportsCPU(programCPU)) |
158 |
debugList.add(debugConfigs[i]); |
159 |
} |
160 |
} |
161 |
} |
162 |
debugConfigs = (ICDebugConfiguration[]) debugList.toArray(new ICDebugConfiguration[0]); |
163 |
if (debugConfigs.length == 1) { |
164 |
debugConfig = debugConfigs[0]; |
165 |
} else if (debugConfigs.length > 1) { |
166 |
debugConfig = chooseDebugConfig(debugConfigs, mode); |
167 |
} |
168 |
} |
169 |
|
170 |
if (debugConfig != null) { |
171 |
configuration = createConfiguration(bin, debugConfig, mode); |
172 |
} |
173 |
} else if (candidateCount == 1) { |
174 |
configuration = (ILaunchConfiguration) candidateConfigs.get(0); |
175 |
} else { |
176 |
// Prompt the user to choose a config. A null result means the user |
177 |
// cancelled the dialog, in which case this method returns null, |
178 |
// since cancelling the dialog should also cancel launching anything. |
179 |
configuration = chooseConfiguration(candidateConfigs, mode); |
180 |
} |
181 |
return configuration; |
182 |
} |
183 |
|
184 |
/** |
185 |
* Method createConfiguration. |
186 |
* @param bin |
187 |
* @return ILaunchConfiguration |
188 |
*/ |
189 |
private ILaunchConfiguration createConfiguration(IBinary bin, ICDebugConfiguration debugConfig, String mode) { |
190 |
ILaunchConfiguration config = null; |
191 |
try { |
192 |
String projectName = bin.getResource().getProjectRelativePath().toString(); |
193 |
ILaunchConfigurationType configType = getCLaunchConfigType(); |
194 |
ILaunchConfigurationWorkingCopy wc = |
195 |
configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(bin.getElementName())); |
196 |
wc.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, projectName); |
197 |
wc.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, bin.getCProject().getElementName()); |
198 |
wc.setMappedResources(new IResource[] {bin.getResource().getProject()}); |
199 |
wc.setAttribute(ICDTLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, (String) null); |
200 |
wc.setAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN, true); |
201 |
wc.setAttribute( |
202 |
ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_START_MODE, |
203 |
ICDTLaunchConfigurationConstants.DEBUGGER_MODE_RUN); |
204 |
wc.setAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_ID, debugConfig.getID()); |
205 |
|
206 |
// Workaround for bug 262840: select the standard CDT launcher by default. |
207 |
HashSet<String> set = new HashSet<String>(); |
208 |
set.add(mode); |
209 |
try { |
210 |
ILaunchDelegate preferredDelegate = wc.getPreferredDelegate(set); |
211 |
if (preferredDelegate == null) { |
212 |
wc.setPreferredLaunchDelegate(set, "org.eclipse.cdt.cdi.launch.localCLaunch"); |
213 |
} |
214 |
} catch (CoreException e) {} |
215 |
// End workaround for bug 262840 |
216 |
|
217 |
ICProjectDescription projDes = CCorePlugin.getDefault().getProjectDescription(bin.getCProject().getProject()); |
218 |
if (projDes != null) |
219 |
{ |
220 |
String buildConfigID = projDes.getActiveConfiguration().getId(); |
221 |
wc.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_BUILD_CONFIG_ID, buildConfigID); |
222 |
} |
223 |
|
224 |
// Load up the debugger page to set the defaults. There should probably be a separate |
225 |
// extension point for this. |
226 |
ICDebuggerPage page = CDebugUIPlugin.getDefault().getDebuggerPage(debugConfig.getID()); |
227 |
page.setDefaults(wc); |
228 |
|
229 |
config = wc.doSave(); |
230 |
} catch (CoreException ce) { |
231 |
CDebugUIPlugin.log(ce); |
232 |
} |
233 |
return config; |
234 |
} |
235 |
|
236 |
/** |
237 |
* Method getCLaunchConfigType. |
238 |
* @return ILaunchConfigurationType |
239 |
*/ |
240 |
protected ILaunchConfigurationType getCLaunchConfigType() { |
241 |
return getLaunchManager().getLaunchConfigurationType(ICDTLaunchConfigurationConstants.ID_LAUNCH_C_APP); |
242 |
} |
243 |
|
244 |
protected ILaunchManager getLaunchManager() { |
245 |
return DebugPlugin.getDefault().getLaunchManager(); |
246 |
} |
247 |
|
248 |
/** |
249 |
* Convenience method to get the window that owns this action's Shell. |
250 |
*/ |
251 |
protected Shell getShell() { |
252 |
IWorkbenchWindow w = CDebugUIPlugin.getDefault().getActiveWorkbenchWindow(); |
253 |
if (w != null) { |
254 |
return w.getShell(); |
255 |
} |
256 |
return null; |
257 |
} |
258 |
|
259 |
/** |
260 |
* Method chooseDebugConfig. |
261 |
* @param debugConfigs |
262 |
* @param mode |
263 |
* @return ICDebugConfiguration |
264 |
*/ |
265 |
private ICDebugConfiguration chooseDebugConfig(ICDebugConfiguration[] debugConfigs, String mode) { |
266 |
ILabelProvider provider = new LabelProvider() { |
267 |
/** |
268 |
* The <code>LabelProvider</code> implementation of this |
269 |
* <code>ILabelProvider</code> method returns the element's <code>toString</code> |
270 |
* string. Subclasses may override. |
271 |
*/ |
272 |
public String getText(Object element) { |
273 |
if (element == null) { |
274 |
return ""; //$NON-NLS-1$ |
275 |
} else if (element instanceof ICDebugConfiguration) { |
276 |
return ((ICDebugConfiguration) element).getName(); |
277 |
} |
278 |
return element.toString(); |
279 |
} |
280 |
}; |
281 |
ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), provider); |
282 |
dialog.setElements(debugConfigs); |
283 |
dialog.setTitle(getDebugConfigDialogTitleString(debugConfigs, mode)); |
284 |
dialog.setMessage(getDebugConfigDialogMessageString(debugConfigs, mode)); |
285 |
dialog.setMultipleSelection(false); |
286 |
int result = dialog.open(); |
287 |
provider.dispose(); |
288 |
if (result == Window.OK) { |
289 |
return (ICDebugConfiguration) dialog.getFirstResult(); |
290 |
} |
291 |
return null; |
292 |
} |
293 |
|
294 |
protected String getDebugConfigDialogTitleString(ICDebugConfiguration [] configList, String mode) { |
295 |
return LaunchMessages.getString("CApplicationLaunchShortcut.LaunchDebugConfigSelection"); //$NON-NLS-1$ |
296 |
} |
297 |
|
298 |
protected String getDebugConfigDialogMessageString(ICDebugConfiguration [] configList, String mode) { |
299 |
if (mode.equals(ILaunchManager.DEBUG_MODE)) { |
300 |
return LaunchMessages.getString("CApplicationLaunchShortcut.ChooseConfigToDebug"); //$NON-NLS-1$ |
301 |
} else if (mode.equals(ILaunchManager.RUN_MODE)) { |
302 |
return LaunchMessages.getString("CApplicationLaunchShortcut.ChooseConfigToRun"); //$NON-NLS-1$ |
303 |
} |
304 |
return LaunchMessages.getString("CApplicationLaunchShortcut.Invalid_launch_mode_1"); //$NON-NLS-1$ |
305 |
} |
306 |
|
307 |
|
308 |
/** |
309 |
* Show a selection dialog that allows the user to choose one of the specified |
310 |
* launch configurations. Return the chosen config, or <code>null</code> if the |
311 |
* user cancelled the dialog. |
312 |
*/ |
313 |
protected ILaunchConfiguration chooseConfiguration(List configList, String mode) { |
314 |
IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation(); |
315 |
ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), labelProvider); |
316 |
dialog.setElements(configList.toArray()); |
317 |
dialog.setTitle(getLaunchSelectionDialogTitleString(configList, mode)); |
318 |
dialog.setMessage(getLaunchSelectionDialogMessageString(configList, mode)); |
319 |
dialog.setMultipleSelection(false); |
320 |
int result = dialog.open(); |
321 |
labelProvider.dispose(); |
322 |
if (result == Window.OK) { |
323 |
return (ILaunchConfiguration) dialog.getFirstResult(); |
324 |
} |
325 |
return null; |
326 |
} |
327 |
|
328 |
protected String getLaunchSelectionDialogTitleString(List configList, String mode) { |
329 |
return LaunchMessages.getString("CApplicationLaunchShortcut.LaunchConfigSelection"); //$NON-NLS-1$ |
330 |
} |
331 |
|
332 |
protected String getLaunchSelectionDialogMessageString(List binList, String mode) { |
333 |
if (mode.equals(ILaunchManager.DEBUG_MODE)) { |
334 |
return LaunchMessages.getString("CApplicationLaunchShortcut.ChooseLaunchConfigToDebug"); //$NON-NLS-1$ |
335 |
} else if (mode.equals(ILaunchManager.RUN_MODE)) { |
336 |
return LaunchMessages.getString("CApplicationLaunchShortcut.ChooseLaunchConfigToRun"); //$NON-NLS-1$ |
337 |
} |
338 |
return LaunchMessages.getString("CApplicationLaunchShortcut.Invalid_launch_mode_2"); //$NON-NLS-1$ |
339 |
} |
340 |
|
341 |
/** |
342 |
* Prompts the user to select a binary |
343 |
* |
344 |
* @return the selected binary or <code>null</code> if none. |
345 |
*/ |
346 |
protected IBinary chooseBinary(List binList, String mode) { |
347 |
ILabelProvider programLabelProvider = new CElementLabelProvider() { |
348 |
public String getText(Object element) { |
349 |
if (element instanceof IBinary) { |
350 |
IBinary bin = (IBinary)element; |
351 |
StringBuffer name = new StringBuffer(); |
352 |
name.append(bin.getPath().lastSegment()); |
353 |
return name.toString(); |
354 |
} |
355 |
return super.getText(element); |
356 |
} |
357 |
}; |
358 |
|
359 |
ILabelProvider qualifierLabelProvider = new CElementLabelProvider() { |
360 |
public String getText(Object element) { |
361 |
if (element instanceof IBinary) { |
362 |
IBinary bin = (IBinary)element; |
363 |
StringBuffer name = new StringBuffer(); |
364 |
name.append(bin.getCPU() + (bin.isLittleEndian() ? "le" : "be")); //$NON-NLS-1$ //$NON-NLS-2$ |
365 |
name.append(" - "); //$NON-NLS-1$ |
366 |
name.append(bin.getPath().toString()); |
367 |
return name.toString(); |
368 |
} |
369 |
return super.getText(element); |
370 |
} |
371 |
}; |
372 |
|
373 |
TwoPaneElementSelector dialog = new TwoPaneElementSelector(getShell(), programLabelProvider, qualifierLabelProvider); |
374 |
dialog.setElements(binList.toArray()); |
375 |
dialog.setTitle(getBinarySelectionDialogTitleString(binList, mode)); |
376 |
dialog.setMessage(getBinarySelectionDialogMessageString(binList, mode)); |
377 |
dialog.setUpperListLabel(LaunchMessages.getString("Launch.common.BinariesColon")); //$NON-NLS-1$ |
378 |
dialog.setLowerListLabel(LaunchMessages.getString("Launch.common.QualifierColon")); //$NON-NLS-1$ |
379 |
dialog.setMultipleSelection(false); |
380 |
if (dialog.open() == Window.OK) { |
381 |
return (IBinary) dialog.getFirstResult(); |
382 |
} |
383 |
|
384 |
return null; |
385 |
} |
386 |
|
387 |
protected String getBinarySelectionDialogTitleString(List binList, String mode) { |
388 |
return LaunchMessages.getString("CApplicationLaunchShortcut.CLocalApplication"); //$NON-NLS-1$ |
389 |
} |
390 |
|
391 |
protected String getBinarySelectionDialogMessageString(List binList, String mode) { |
392 |
if (mode.equals(ILaunchManager.DEBUG_MODE)) { |
393 |
return LaunchMessages.getString("CApplicationLaunchShortcut.ChooseLocalAppToDebug"); //$NON-NLS-1$ |
394 |
} else if (mode.equals(ILaunchManager.RUN_MODE)) { |
395 |
return LaunchMessages.getString("CApplicationLaunchShortcut.ChooseLocalAppToRun"); //$NON-NLS-1$ |
396 |
} |
397 |
return LaunchMessages.getString("CApplicationLaunchShortcut.Invalid_launch_mode_3"); //$NON-NLS-1$ |
398 |
} |
399 |
|
400 |
/** |
401 |
* Method searchAndLaunch. |
402 |
* @param objects |
403 |
* @param mode |
404 |
*/ |
405 |
private void searchAndLaunch(final Object[] elements, String mode) { |
406 |
if (elements != null && elements.length > 0) { |
407 |
IBinary bin = null; |
408 |
if (elements.length == 1 && elements[0] instanceof IBinary) { |
409 |
bin = (IBinary)elements[0]; |
410 |
} else { |
411 |
final List results = new ArrayList(); |
412 |
ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell()); |
413 |
IRunnableWithProgress runnable = new IRunnableWithProgress() { |
414 |
public void run(IProgressMonitor pm) throws InterruptedException { |
415 |
int nElements = elements.length; |
416 |
pm.beginTask("Looking for executables", nElements); //$NON-NLS-1$ |
417 |
try { |
418 |
IProgressMonitor sub = new SubProgressMonitor(pm, 1); |
419 |
for (int i = 0; i < nElements; i++) { |
420 |
if (elements[i] instanceof IAdaptable) { |
421 |
IResource r = (IResource) ((IAdaptable) elements[i]).getAdapter(IResource.class); |
422 |
if (r != null) { |
423 |
ICProject cproject = CoreModel.getDefault().create(r.getProject()); |
424 |
if (cproject != null) { |
425 |
try { |
426 |
IBinary[] bins = cproject.getBinaryContainer().getBinaries(); |
427 |
|
428 |
for (int j = 0; j < bins.length; j++) { |
429 |
if (bins[j].isExecutable()) { |
430 |
results.add(bins[j]); |
431 |
} |
432 |
} |
433 |
} catch (CModelException e) { |
434 |
} |
435 |
} |
436 |
} |
437 |
} |
438 |
if (pm.isCanceled()) { |
439 |
throw new InterruptedException(); |
440 |
} |
441 |
sub.done(); |
442 |
} |
443 |
} finally { |
444 |
pm.done(); |
445 |
} |
446 |
} |
447 |
}; |
448 |
try { |
449 |
dialog.run(true, true, runnable); |
450 |
} catch (InterruptedException e) { |
451 |
return; |
452 |
} catch (InvocationTargetException e) { |
453 |
MessageDialog.openError(getShell(), LaunchMessages.getString("CApplicationLaunchShortcut.Application_Launcher"), e.getMessage()); //$NON-NLS-1$ |
454 |
return; |
455 |
} |
456 |
int count = results.size(); |
457 |
if (count == 0) { |
458 |
MessageDialog.openError(getShell(), LaunchMessages.getString("CApplicationLaunchShortcut.Application_Launcher"), LaunchMessages.getString("CApplicationLaunchShortcut.Launch_failed_no_binaries")); //$NON-NLS-1$ //$NON-NLS-2$ |
459 |
} else if (count > 1) { |
460 |
bin = chooseBinary(results, mode); |
461 |
} else { |
462 |
bin = (IBinary)results.get(0); |
463 |
} |
464 |
} |
465 |
if (bin != null) { |
466 |
launch(bin, mode); |
467 |
} |
468 |
} else { |
469 |
MessageDialog.openError(getShell(), LaunchMessages.getString("CApplicationLaunchShortcut.Application_Launcher"), LaunchMessages.getString("CApplicationLaunchShortcut.Launch_failed_no_project_selected")); //$NON-NLS-1$ //$NON-NLS-2$ |
470 |
} |
471 |
} |
472 |
|
473 |
public ILaunchConfiguration[] getLaunchConfigurations(ISelection selection) { |
474 |
// This returns null so the platform will use the ILaunchShortcut behavior |
475 |
// and get the configurations based on the project resource association. |
476 |
return null; |
477 |
} |
478 |
|
479 |
public ILaunchConfiguration[] getLaunchConfigurations(IEditorPart editorpart) { |
480 |
// This returns null so the platform will use the ILaunchShortcut behavior |
481 |
// and get the configurations based on the project resource association. |
482 |
return null; |
483 |
} |
484 |
|
485 |
public IResource getLaunchableResource(ISelection selection) { |
486 |
// Take the selection and determine which project is intended to |
487 |
// be used for the launch. |
488 |
if (selection instanceof IStructuredSelection) { |
489 |
Object firstElement = ((IStructuredSelection) selection).getFirstElement(); |
490 |
if (firstElement != null) |
491 |
{ |
492 |
if (firstElement instanceof IFile) |
493 |
{ |
494 |
IFile file = (IFile) firstElement; |
495 |
return file.getProject(); |
496 |
} |
497 |
if (firstElement instanceof Executable) |
498 |
{ |
499 |
return ((Executable)firstElement).getProject(); |
500 |
} |
501 |
if (firstElement instanceof IBinary) |
502 |
{ |
503 |
return ((IBinary)firstElement).getResource().getProject(); |
504 |
} |
505 |
} |
506 |
} |
507 |
List<IProject> projects = getProjectsFromSelection(selection); |
508 |
if (projects.size() > 0) { |
509 |
return projects.get(0); |
510 |
} |
511 |
return null; |
512 |
} |
513 |
|
514 |
/** |
515 |
* Gets the owning project(s) of the selected object(s) if any |
516 |
* @param selection the current selection |
517 |
* @return a list of projects - may be empty |
518 |
*/ |
519 |
public static List<IProject> getProjectsFromSelection(ISelection selection) { |
520 |
List<IProject> projects = new ArrayList<IProject>(); |
521 |
|
522 |
if (selection != null && !selection.isEmpty()) { |
523 |
if (selection instanceof ITextSelection) { |
524 |
|
525 |
IWorkbenchWindow activeWindow = CDebugUIPlugin.getActiveWorkbenchWindow(); |
526 |
IWorkbenchPage wpage = activeWindow.getActivePage(); |
527 |
if (wpage != null) { |
528 |
IEditorPart ep = wpage.getActiveEditor(); |
529 |
if (ep != null) { |
530 |
IEditorInput editorInput = ep.getEditorInput(); |
531 |
if (editorInput instanceof IFileEditorInput) { |
532 |
IFile file = ((IFileEditorInput)editorInput).getFile(); |
533 |
if (file != null) { |
534 |
projects.add(file.getProject()); |
535 |
} |
536 |
} |
537 |
} |
538 |
} |
539 |
} else if (selection instanceof IStructuredSelection) { |
540 |
IStructuredSelection structuredSelection = (IStructuredSelection) selection; |
541 |
|
542 |
for (Iterator<?> iter = structuredSelection.iterator(); iter.hasNext();) { |
543 |
Object element = (Object) iter.next(); |
544 |
if (element != null) { |
545 |
|
546 |
if (element instanceof ICProject) { |
547 |
projects.add(((ICProject)element).getProject()); |
548 |
} else if (element instanceof IResource) { |
549 |
projects.add(((IResource)element).getProject()); |
550 |
} else if (element instanceof ICElement) { |
551 |
ICElement unit = (ICElement) element; |
552 |
|
553 |
// Get parent of the Element until we reach the owner project. |
554 |
while (unit != null && ! (unit instanceof ICProject)) |
555 |
unit = unit.getParent(); |
556 |
|
557 |
if (unit != null) { |
558 |
projects.add(((ICProject)unit).getProject()); |
559 |
} |
560 |
} else if (element instanceof IAdaptable) { |
561 |
Object adapter = ((IAdaptable)element).getAdapter(IResource.class); |
562 |
if (adapter != null && adapter instanceof IResource) { |
563 |
projects.add(((IResource)adapter).getProject()); |
564 |
} else { |
565 |
adapter = ((IAdaptable)element).getAdapter(ICProject.class); |
566 |
if (adapter != null && adapter instanceof ICProject) { |
567 |
projects.add(((ICProject)adapter).getProject()); |
568 |
} |
569 |
} |
570 |
} |
571 |
} |
572 |
} |
573 |
} |
574 |
} |
575 |
return projects; |
576 |
} |
577 |
|
578 |
public IResource getLaunchableResource(IEditorPart editorpart) { |
579 |
// This handles the case where the selection is text in an editor. |
580 |
IEditorInput editorInput = editorpart.getEditorInput(); |
581 |
if (editorInput instanceof IFileEditorInput) { |
582 |
IFile file = ((IFileEditorInput)editorInput).getFile(); |
583 |
if (file != null) { |
584 |
return file.getProject(); |
585 |
} |
586 |
} |
587 |
return null; |
588 |
} |
589 |
|
590 |
} |