5a3cafb902e8005192d76f0bfd05d8b757a3a509
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / launcher / PHPLaunchShortcut.java
1 package net.sourceforge.phpdt.internal.debug.ui.launcher;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
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
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;
25
26 public class PHPLaunchShortcut implements ILaunchShortcut {
27         public PHPLaunchShortcut() {
28         }
29
30         public void launch(ISelection selection, String mode)  {
31                 if (selection instanceof IStructuredSelection) {
32                         Object firstSelection = ((IStructuredSelection)selection).getFirstElement();
33                         if (firstSelection instanceof IFile) {
34                                 if (
35              ((IFile) firstSelection).getFileExtension().equals("php") ||
36              ((IFile) firstSelection).getFileExtension().equals("php3") ||
37              ((IFile) firstSelection).getFileExtension().equals("php4") ||
38              ((IFile) firstSelection).getFileExtension().equals("php5")
39             ) {
40                                         ILaunchConfiguration config = findLaunchConfiguration((IFile)firstSelection, mode);
41                                         try {
42                                                 if (config != null)
43                                                         config.launch(mode, null);
44                                         } catch (CoreException e) {
45                                                 log(e);
46                                         }
47                                         return;
48                                 }
49                         }
50                 }
51
52                 log("The resource selected is not a PHP file.");
53         }
54
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);
59         }
60
61         protected ILaunchConfiguration findLaunchConfiguration(IFile phpFile, String mode) {
62                 ILaunchConfigurationType configType = getPHPLaunchConfigType();
63                 List candidateConfigs = null;
64                 try {
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);
71                                 }
72                         }
73                 } catch (CoreException e) {
74                         log(e);
75                 }
76                 
77                 switch (candidateConfigs.size()) {
78                         case 0 :
79                                 return createConfiguration(phpFile);
80                         case 1 :
81                                 return (ILaunchConfiguration) candidateConfigs.get(0);
82                         default :
83                                 log(new RuntimeException(PHPDebugUiMessages.getString("LaunchConfigurationShortcut.PHP.multipleConfigurationsError")));
84                                 return null;
85                 }
86         }
87
88         protected ILaunchConfiguration createConfiguration(IFile phpFile) {
89                 ILaunchConfiguration config = null;
90                 try {
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);
96                         config = wc.doSave();           
97                 } catch (CoreException ce) {
98                         log(ce);                        
99                 }
100                 return config;
101         }
102
103         protected ILaunchConfigurationType getPHPLaunchConfigType() {
104                 return getLaunchManager().getLaunchConfigurationType(PHPLaunchConfigurationAttribute.PHP_LAUNCH_CONFIGURATION_TYPE);            
105         }
106         
107         protected ILaunchManager getLaunchManager() {
108                 return DebugPlugin.getDefault().getLaunchManager();
109         }
110         
111         protected void log(String message) {
112                 PHPDebugUiPlugin.log(new Status(Status.INFO, PHPDebugUiPlugin.PLUGIN_ID, Status.INFO, message, null));
113         }
114         
115         protected void log(Throwable t) {
116                 PHPDebugUiPlugin.log(t);
117         }
118 }