1 package net.sourceforge.phpdt.internal.debug.ui.launcher;
3 import java.util.ArrayList;
6 import org.eclipse.core.resources.IFile;
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.core.runtime.Status;
9 import org.eclipse.debug.core.DebugPlugin;
10 import org.eclipse.debug.core.ILaunchConfiguration;
11 import org.eclipse.debug.core.ILaunchConfigurationType;
12 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
13 import org.eclipse.debug.core.ILaunchManager;
14 import org.eclipse.debug.ui.ILaunchShortcut;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.viewers.StructuredSelection;
18 import org.eclipse.ui.IEditorInput;
19 import org.eclipse.ui.IEditorPart;
20 import net.sourceforge.phpdt.debug.ui.PHPDebugUiConstants;
21 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
22 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
23 import net.sourceforge.phpdt.internal.launching.PHPLaunchConfigurationAttribute;
25 public class PHPLaunchShortcut implements ILaunchShortcut {
26 public PHPLaunchShortcut() {
29 public void launch(ISelection selection, String mode) {
30 if (selection instanceof IStructuredSelection) {
31 Object firstSelection = ((IStructuredSelection)selection).getFirstElement();
32 if (firstSelection instanceof IFile) {
34 ((IFile) firstSelection).getFileExtension().equals("php") ||
35 ((IFile) firstSelection).getFileExtension().equals("php3") ||
36 ((IFile) firstSelection).getFileExtension().equals("php4")
38 ILaunchConfiguration config = findLaunchConfiguration((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.getAdapter(IFile.class));
56 launch(selection, mode);
59 protected ILaunchConfiguration findLaunchConfiguration(IFile phpFile, String mode) {
60 ILaunchConfigurationType configType = getPHPLaunchConfigType();
61 List candidateConfigs = null;
63 ILaunchConfiguration[] configs = getLaunchManager().getLaunchConfigurations(configType);
64 candidateConfigs = new ArrayList(configs.length);
65 for (int i = 0; i < configs.length; i++) {
66 ILaunchConfiguration config = configs[i];
67 if (config.getAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, "").equals(phpFile.getFullPath().toString())) {
68 candidateConfigs.add(config);
71 } catch (CoreException e) {
75 switch (candidateConfigs.size()) {
77 return createConfiguration(phpFile);
79 return (ILaunchConfiguration) candidateConfigs.get(0);
81 log(new RuntimeException(PHPDebugUiMessages.getString("LaunchConfigurationShortcut.PHP.multipleConfigurationsError")));
86 protected ILaunchConfiguration createConfiguration(IFile phpFile) {
87 ILaunchConfiguration config = null;
89 ILaunchConfigurationType configType = getPHPLaunchConfigType();
90 ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(phpFile.getName()));
91 wc.setAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, phpFile.getProject().getName());
92 wc.setAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, phpFile.getProjectRelativePath().toString());
93 wc.setAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, PHPDebugUiConstants.DEFAULT_WORKING_DIRECTORY);
95 } catch (CoreException ce) {
101 protected ILaunchConfigurationType getPHPLaunchConfigType() {
102 return getLaunchManager().getLaunchConfigurationType(PHPLaunchConfigurationAttribute.PHP_LAUNCH_CONFIGURATION_TYPE);
105 protected ILaunchManager getLaunchManager() {
106 return DebugPlugin.getDefault().getLaunchManager();
109 protected void log(String message) {
110 PHPDebugUiPlugin.getDefault().log(new Status(Status.INFO, PHPDebugUiPlugin.PLUGIN_ID, Status.INFO, message, null));
113 protected void log(Throwable t) {
114 PHPDebugUiPlugin.getDefault().log(t);