1 package net.sourceforge.phpdt.internal.debug.ui.launcher;
3 import java.util.ArrayList;
6 import net.sourceforge.phpdt.debug.ui.PHPDebugUiConstants;
7 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
8 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
9 import net.sourceforge.phpdt.internal.launching.PHPLaunchConfigurationAttribute;
10 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.Status;
15 import org.eclipse.debug.core.DebugPlugin;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.debug.core.ILaunchConfigurationType;
18 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
19 import org.eclipse.debug.core.ILaunchManager;
20 import org.eclipse.debug.ui.ILaunchShortcut;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.StructuredSelection;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IEditorPart;
27 public class PHPLaunchShortcut implements ILaunchShortcut {
28 public PHPLaunchShortcut() {
31 public void launch(ISelection selection, String mode) {
32 if (selection instanceof IStructuredSelection) {
33 Object firstSelection = ((IStructuredSelection) selection)
35 if (firstSelection instanceof IFile) {
36 if (PHPFileUtil.isPHPFile((IFile) firstSelection)) {
37 ILaunchConfiguration config = findLaunchConfiguration(
38 (IFile) firstSelection, mode);
41 config.launch(mode, null);
42 } catch (CoreException e) {
50 log("The resource selected is not a PHP file.");
53 public void launch(IEditorPart editor, String mode) {
54 IEditorInput input = editor.getEditorInput();
55 ISelection selection = new StructuredSelection(input
56 .getAdapter(IFile.class));
57 launch(selection, mode);
60 protected ILaunchConfiguration findLaunchConfiguration(IFile phpFile,
62 ILaunchConfigurationType configType = getPHPLaunchConfigType();
63 List candidateConfigs = null;
65 ILaunchConfiguration[] configs = getLaunchManager()
66 .getLaunchConfigurations(configType);
67 candidateConfigs = new ArrayList(configs.length);
68 for (int i = 0; i < configs.length; i++) {
69 ILaunchConfiguration config = configs[i];
70 if (config.getAttribute(
71 PHPLaunchConfigurationAttribute.FILE_NAME, "").equals(
72 phpFile.getFullPath().toString())) {
73 candidateConfigs.add(config);
76 } catch (CoreException e) {
80 switch (candidateConfigs.size()) {
82 return createConfiguration(phpFile);
84 return (ILaunchConfiguration) candidateConfigs.get(0);
86 log(new RuntimeException(
88 .getString("LaunchConfigurationShortcut.PHP.multipleConfigurationsError")));
93 protected ILaunchConfiguration createConfiguration(IFile phpFile) {
94 ILaunchConfiguration config = null;
96 ILaunchConfigurationType configType = getPHPLaunchConfigType();
97 ILaunchConfigurationWorkingCopy wc = configType.newInstance(null,
99 .generateUniqueLaunchConfigurationNameFrom(
101 wc.setAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME,
102 phpFile.getProject().getName());
103 wc.setAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, phpFile
104 .getProjectRelativePath().toString());
105 wc.setAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY,
106 PHPDebugUiConstants.DEFAULT_WORKING_DIRECTORY);
107 config = wc.doSave();
108 } catch (CoreException ce) {
114 protected ILaunchConfigurationType getPHPLaunchConfigType() {
115 return getLaunchManager().getLaunchConfigurationType(
116 PHPLaunchConfigurationAttribute.PHP_LAUNCH_CONFIGURATION_TYPE);
119 protected ILaunchManager getLaunchManager() {
120 return DebugPlugin.getDefault().getLaunchManager();
123 protected void log(String message) {
124 PHPDebugUiPlugin.log(new Status(Status.INFO,
125 PHPDebugUiPlugin.PLUGIN_ID, Status.INFO, message, null));
128 protected void log(Throwable t) {
129 PHPDebugUiPlugin.log(t);