fixing compile errors
[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             ) {
39                                         ILaunchConfiguration config = findLaunchConfiguration((IFile)firstSelection, mode);
40                                         try {
41                                                 if (config != null)
42                                                         config.launch(mode, null);
43                                         } catch (CoreException e) {
44                                                 log(e);
45                                         }
46                                         return;
47                                 }
48                         }
49                 }
50
51                 log("The resource selected is not a PHP file.");
52         }
53
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);
58         }
59
60         protected ILaunchConfiguration findLaunchConfiguration(IFile phpFile, String mode) {
61                 ILaunchConfigurationType configType = getPHPLaunchConfigType();
62                 List candidateConfigs = null;
63                 try {
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);
70                                 }
71                         }
72                 } catch (CoreException e) {
73                         log(e);
74                 }
75                 
76                 switch (candidateConfigs.size()) {
77                         case 0 :
78                                 return createConfiguration(phpFile);
79                         case 1 :
80                                 return (ILaunchConfiguration) candidateConfigs.get(0);
81                         default :
82                                 log(new RuntimeException(PHPDebugUiMessages.getString("LaunchConfigurationShortcut.PHP.multipleConfigurationsError")));
83                                 return null;
84                 }
85         }
86
87         protected ILaunchConfiguration createConfiguration(IFile phpFile) {
88                 ILaunchConfiguration config = null;
89                 try {
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);
95                         config = wc.doSave();           
96                 } catch (CoreException ce) {
97                         log(ce);                        
98                 }
99                 return config;
100         }
101
102         protected ILaunchConfigurationType getPHPLaunchConfigType() {
103                 return getLaunchManager().getLaunchConfigurationType(PHPLaunchConfigurationAttribute.PHP_LAUNCH_CONFIGURATION_TYPE);            
104         }
105         
106         protected ILaunchManager getLaunchManager() {
107                 return DebugPlugin.getDefault().getLaunchManager();
108         }
109         
110         protected void log(String message) {
111                 PHPDebugUiPlugin.log(new Status(Status.INFO, PHPDebugUiPlugin.PLUGIN_ID, Status.INFO, message, null));
112         }
113         
114         protected void log(Throwable t) {
115                 PHPDebugUiPlugin.log(t);
116         }
117 }