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).getFirstElement();
34 if (firstSelection instanceof IFile) {
35 if (PHPFileUtil.isPHPFile((IFile) firstSelection)) {
36 ILaunchConfiguration config = findLaunchConfiguration((IFile)firstSelection, mode);
39 config.launch(mode, null);
40 } catch (CoreException e) {
48 log("The resource selected is not a PHP file.");
51 public void launch(IEditorPart editor, String mode) {
52 IEditorInput input = editor.getEditorInput();
53 ISelection selection = new StructuredSelection(input.getAdapter(IFile.class));
54 launch(selection, mode);
57 protected ILaunchConfiguration findLaunchConfiguration(IFile phpFile, String mode) {
58 ILaunchConfigurationType configType = getPHPLaunchConfigType();
59 List candidateConfigs = null;
61 ILaunchConfiguration[] configs = getLaunchManager().getLaunchConfigurations(configType);
62 candidateConfigs = new ArrayList(configs.length);
63 for (int i = 0; i < configs.length; i++) {
64 ILaunchConfiguration config = configs[i];
65 if (config.getAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, "").equals(phpFile.getFullPath().toString())) {
66 candidateConfigs.add(config);
69 } catch (CoreException e) {
73 switch (candidateConfigs.size()) {
75 return createConfiguration(phpFile);
77 return (ILaunchConfiguration) candidateConfigs.get(0);
79 log(new RuntimeException(PHPDebugUiMessages.getString("LaunchConfigurationShortcut.PHP.multipleConfigurationsError")));
84 protected ILaunchConfiguration createConfiguration(IFile phpFile) {
85 ILaunchConfiguration config = null;
87 ILaunchConfigurationType configType = getPHPLaunchConfigType();
88 ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(phpFile.getName()));
89 wc.setAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, phpFile.getProject().getName());
90 wc.setAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, phpFile.getProjectRelativePath().toString());
91 wc.setAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, PHPDebugUiConstants.DEFAULT_WORKING_DIRECTORY);
93 } catch (CoreException ce) {
99 protected ILaunchConfigurationType getPHPLaunchConfigType() {
100 return getLaunchManager().getLaunchConfigurationType(PHPLaunchConfigurationAttribute.PHP_LAUNCH_CONFIGURATION_TYPE);
103 protected ILaunchManager getLaunchManager() {
104 return DebugPlugin.getDefault().getLaunchManager();
107 protected void log(String message) {
108 PHPDebugUiPlugin.log(new Status(Status.INFO, PHPDebugUiPlugin.PLUGIN_ID, Status.INFO, message, null));
111 protected void log(Throwable t) {
112 PHPDebugUiPlugin.log(t);