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;
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.Status;
14 import org.eclipse.debug.core.DebugPlugin;
15 import org.eclipse.debug.core.ILaunchConfiguration;
16 import org.eclipse.debug.core.ILaunchConfigurationType;
17 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
18 import org.eclipse.debug.core.ILaunchManager;
19 import org.eclipse.debug.ui.ILaunchShortcut;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.IEditorPart;
26 public class PHPLaunchShortcut implements ILaunchShortcut {
27 public PHPLaunchShortcut() {
30 public void launch(ISelection selection, String mode) {
31 if (selection instanceof IStructuredSelection) {
32 Object firstSelection = ((IStructuredSelection)selection).getFirstElement();
33 if (firstSelection instanceof IFile) {
35 ((IFile) firstSelection).getFileExtension().equals("php") ||
36 ((IFile) firstSelection).getFileExtension().equals("php3") ||
37 ((IFile) firstSelection).getFileExtension().equals("php4") ||
38 ((IFile) firstSelection).getFileExtension().equals("php5")
40 ILaunchConfiguration config = findLaunchConfiguration((IFile)firstSelection, mode);
43 config.launch(mode, null);
44 } catch (CoreException e) {
52 log("The resource selected is not a PHP file.");
55 public void launch(IEditorPart editor, String mode) {
56 IEditorInput input = editor.getEditorInput();
57 ISelection selection = new StructuredSelection(input.getAdapter(IFile.class));
58 launch(selection, mode);
61 protected ILaunchConfiguration findLaunchConfiguration(IFile phpFile, String mode) {
62 ILaunchConfigurationType configType = getPHPLaunchConfigType();
63 List candidateConfigs = null;
65 ILaunchConfiguration[] configs = getLaunchManager().getLaunchConfigurations(configType);
66 candidateConfigs = new ArrayList(configs.length);
67 for (int i = 0; i < configs.length; i++) {
68 ILaunchConfiguration config = configs[i];
69 if (config.getAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, "").equals(phpFile.getFullPath().toString())) {
70 candidateConfigs.add(config);
73 } catch (CoreException e) {
77 switch (candidateConfigs.size()) {
79 return createConfiguration(phpFile);
81 return (ILaunchConfiguration) candidateConfigs.get(0);
83 log(new RuntimeException(PHPDebugUiMessages.getString("LaunchConfigurationShortcut.PHP.multipleConfigurationsError")));
88 protected ILaunchConfiguration createConfiguration(IFile phpFile) {
89 ILaunchConfiguration config = null;
91 ILaunchConfigurationType configType = getPHPLaunchConfigType();
92 ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(phpFile.getName()));
93 wc.setAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, phpFile.getProject().getName());
94 wc.setAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, phpFile.getProjectRelativePath().toString());
95 wc.setAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, PHPDebugUiConstants.DEFAULT_WORKING_DIRECTORY);
97 } catch (CoreException ce) {
103 protected ILaunchConfigurationType getPHPLaunchConfigType() {
104 return getLaunchManager().getLaunchConfigurationType(PHPLaunchConfigurationAttribute.PHP_LAUNCH_CONFIGURATION_TYPE);
107 protected ILaunchManager getLaunchManager() {
108 return DebugPlugin.getDefault().getLaunchManager();
111 protected void log(String message) {
112 PHPDebugUiPlugin.log(new Status(Status.INFO, PHPDebugUiPlugin.PLUGIN_ID, Status.INFO, message, null));
115 protected void log(Throwable t) {
116 PHPDebugUiPlugin.log(t);