c33a7d003675d05238f1db95f9836219952e2fe0
[phpeclipse.git] /
1 package net.sourceforge.phpdt.internal.debug.ui.launcher;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.eclipse.core.resources.IFile;
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.core.runtime.Status;
9 import org.eclipse.debug.core.DebugPlugin;
10 import org.eclipse.debug.core.ILaunchConfiguration;
11 import org.eclipse.debug.core.ILaunchConfigurationType;
12 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
13 import org.eclipse.debug.core.ILaunchManager;
14 import org.eclipse.debug.ui.ILaunchShortcut;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.viewers.StructuredSelection;
18 import org.eclipse.ui.IEditorInput;
19 import org.eclipse.ui.IEditorPart;
20 import net.sourceforge.phpdt.debug.ui.PHPDebugUiConstants;
21 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
22 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
23 import net.sourceforge.phpdt.internal.launching.PHPLaunchConfigurationAttribute;
24
25 public class PHPLaunchShortcut implements ILaunchShortcut {
26         public PHPLaunchShortcut() {
27         }
28
29         public void launch(ISelection selection, String mode)  {
30                 if (selection instanceof IStructuredSelection) {
31                         Object firstSelection = ((IStructuredSelection)selection).getFirstElement();
32                         if (firstSelection instanceof IFile) {
33                                 if (
34              ((IFile) firstSelection).getFileExtension().equals("php") ||
35              ((IFile) firstSelection).getFileExtension().equals("php3") ||
36              ((IFile) firstSelection).getFileExtension().equals("php4")
37             ) {
38                                         ILaunchConfiguration config = findLaunchConfiguration((IFile)firstSelection, mode);
39                                         try {
40                                                 if (config != null)
41                                                         config.launch(mode, null);
42                                         } catch (CoreException e) {
43                                                 log(e);
44                                         }
45                                         return;
46                                 }
47                         }
48                 }
49
50                 log("The resource selected is not a PHP file.");
51         }
52
53         public void launch(IEditorPart editor, String mode)  {
54                 IEditorInput input = editor.getEditorInput();
55                 ISelection selection = new StructuredSelection(input.getAdapter(IFile.class));
56                 launch(selection, mode);
57         }
58
59         protected ILaunchConfiguration findLaunchConfiguration(IFile phpFile, String mode) {
60                 ILaunchConfigurationType configType = getPHPLaunchConfigType();
61                 List candidateConfigs = null;
62                 try {
63                         ILaunchConfiguration[] configs = getLaunchManager().getLaunchConfigurations(configType);
64                         candidateConfigs = new ArrayList(configs.length);
65                         for (int i = 0; i < configs.length; i++) {
66                                 ILaunchConfiguration config = configs[i];
67                                 if (config.getAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, "").equals(phpFile.getFullPath().toString())) {
68                                                 candidateConfigs.add(config);
69                                 }
70                         }
71                 } catch (CoreException e) {
72                         log(e);
73                 }
74                 
75                 switch (candidateConfigs.size()) {
76                         case 0 :
77                                 return createConfiguration(phpFile);
78                         case 1 :
79                                 return (ILaunchConfiguration) candidateConfigs.get(0);
80                         default :
81                                 log(new RuntimeException(PHPDebugUiMessages.getString("LaunchConfigurationShortcut.PHP.multipleConfigurationsError")));
82                                 return null;
83                 }
84         }
85
86         protected ILaunchConfiguration createConfiguration(IFile phpFile) {
87                 ILaunchConfiguration config = null;
88                 try {
89                         ILaunchConfigurationType configType = getPHPLaunchConfigType();
90                         ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(phpFile.getName()));
91                         wc.setAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, phpFile.getProject().getName());
92                         wc.setAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, phpFile.getProjectRelativePath().toString());
93                         wc.setAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, PHPDebugUiConstants.DEFAULT_WORKING_DIRECTORY);
94                         config = wc.doSave();           
95                 } catch (CoreException ce) {
96                         log(ce);                        
97                 }
98                 return config;
99         }
100
101         protected ILaunchConfigurationType getPHPLaunchConfigType() {
102                 return getLaunchManager().getLaunchConfigurationType(PHPLaunchConfigurationAttribute.PHP_LAUNCH_CONFIGURATION_TYPE);            
103         }
104         
105         protected ILaunchManager getLaunchManager() {
106                 return DebugPlugin.getDefault().getLaunchManager();
107         }
108         
109         protected void log(String message) {
110                 PHPDebugUiPlugin.getDefault().log(new Status(Status.INFO, PHPDebugUiPlugin.PLUGIN_ID, Status.INFO, message, null));
111         }
112         
113         protected void log(Throwable t) {
114                 PHPDebugUiPlugin.getDefault().log(t);
115         }
116 }