Delete association of htmledit.gif for the PHPUnitEditor
[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 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
11
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;
26
27 public class PHPLaunchShortcut implements ILaunchShortcut {
28         public PHPLaunchShortcut() {
29         }
30  
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);
37                                         try {
38                                                 if (config != null)
39                                                         config.launch(mode, null);
40                                         } catch (CoreException e) {
41                                                 log(e);
42                                         }
43                                         return;
44                                 }
45                         }
46                 }
47
48                 log("The resource selected is not a PHP file.");
49         }
50
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);
55         }
56
57         protected ILaunchConfiguration findLaunchConfiguration(IFile phpFile, String mode) {
58                 ILaunchConfigurationType configType = getPHPLaunchConfigType();
59                 List candidateConfigs = null;
60                 try {
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);
67                                 }
68                         }
69                 } catch (CoreException e) {
70                         log(e);
71                 }
72                 
73                 switch (candidateConfigs.size()) {
74                         case 0 :
75                                 return createConfiguration(phpFile);
76                         case 1 :
77                                 return (ILaunchConfiguration) candidateConfigs.get(0);
78                         default :
79                                 log(new RuntimeException(PHPDebugUiMessages.getString("LaunchConfigurationShortcut.PHP.multipleConfigurationsError")));
80                                 return null;
81                 }
82         }
83
84         protected ILaunchConfiguration createConfiguration(IFile phpFile) {
85                 ILaunchConfiguration config = null;
86                 try {
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);
92                         config = wc.doSave();           
93                 } catch (CoreException ce) {
94                         log(ce);                        
95                 }
96                 return config;
97         }
98
99         protected ILaunchConfigurationType getPHPLaunchConfigType() {
100                 return getLaunchManager().getLaunchConfigurationType(PHPLaunchConfigurationAttribute.PHP_LAUNCH_CONFIGURATION_TYPE);            
101         }
102         
103         protected ILaunchManager getLaunchManager() {
104                 return DebugPlugin.getDefault().getLaunchManager();
105         }
106         
107         protected void log(String message) {
108                 PHPDebugUiPlugin.log(new Status(Status.INFO, PHPDebugUiPlugin.PLUGIN_ID, Status.INFO, message, null));
109         }
110         
111         protected void log(Throwable t) {
112                 PHPDebugUiPlugin.log(t);
113         }
114 }