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")
39 ILaunchConfiguration config = findLaunchConfiguration((IFile)firstSelection, mode);
42 config.launch(mode, null);
43 } catch (CoreException e) {
51 log("The resource selected is not a PHP file.");
54 public void launch(IEditorPart editor, String mode) {
55 IEditorInput input = editor.getEditorInput();
56 ISelection selection = new StructuredSelection(input.getAdapter(IFile.class));
57 launch(selection, mode);
60 protected ILaunchConfiguration findLaunchConfiguration(IFile phpFile, String mode) {
61 ILaunchConfigurationType configType = getPHPLaunchConfigType();
62 List candidateConfigs = null;
64 ILaunchConfiguration[] configs = getLaunchManager().getLaunchConfigurations(configType);
65 candidateConfigs = new ArrayList(configs.length);
66 for (int i = 0; i < configs.length; i++) {
67 ILaunchConfiguration config = configs[i];
68 if (config.getAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, "").equals(phpFile.getFullPath().toString())) {
69 candidateConfigs.add(config);
72 } catch (CoreException e) {
76 switch (candidateConfigs.size()) {
78 return createConfiguration(phpFile);
80 return (ILaunchConfiguration) candidateConfigs.get(0);
82 log(new RuntimeException(PHPDebugUiMessages.getString("LaunchConfigurationShortcut.PHP.multipleConfigurationsError")));
87 protected ILaunchConfiguration createConfiguration(IFile phpFile) {
88 ILaunchConfiguration config = null;
90 ILaunchConfigurationType configType = getPHPLaunchConfigType();
91 ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(phpFile.getName()));
92 wc.setAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, phpFile.getProject().getName());
93 wc.setAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, phpFile.getProjectRelativePath().toString());
94 wc.setAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, PHPDebugUiConstants.DEFAULT_WORKING_DIRECTORY);
96 } catch (CoreException ce) {
102 protected ILaunchConfigurationType getPHPLaunchConfigType() {
103 return getLaunchManager().getLaunchConfigurationType(PHPLaunchConfigurationAttribute.PHP_LAUNCH_CONFIGURATION_TYPE);
106 protected ILaunchManager getLaunchManager() {
107 return DebugPlugin.getDefault().getLaunchManager();
110 protected void log(String message) {
111 PHPDebugUiPlugin.getDefault().log(new Status(Status.INFO, PHPDebugUiPlugin.PLUGIN_ID, Status.INFO, message, null));
114 protected void log(Throwable t) {
115 PHPDebugUiPlugin.getDefault().log(t);