A massive organize imports and formatting of the sources using default Eclipse code...
[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)
34                                         .getFirstElement();
35                         if (firstSelection instanceof IFile) {
36                                 if (PHPFileUtil.isPHPFile((IFile) firstSelection)) {
37                                         ILaunchConfiguration config = findLaunchConfiguration(
38                                                         (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
56                                 .getAdapter(IFile.class));
57                 launch(selection, mode);
58         }
59
60         protected ILaunchConfiguration findLaunchConfiguration(IFile phpFile,
61                         String mode) {
62                 ILaunchConfigurationType configType = getPHPLaunchConfigType();
63                 List candidateConfigs = null;
64                 try {
65                         ILaunchConfiguration[] configs = getLaunchManager()
66                                         .getLaunchConfigurations(configType);
67                         candidateConfigs = new ArrayList(configs.length);
68                         for (int i = 0; i < configs.length; i++) {
69                                 ILaunchConfiguration config = configs[i];
70                                 if (config.getAttribute(
71                                                 PHPLaunchConfigurationAttribute.FILE_NAME, "").equals(
72                                                 phpFile.getFullPath().toString())) {
73                                         candidateConfigs.add(config);
74                                 }
75                         }
76                 } catch (CoreException e) {
77                         log(e);
78                 }
79
80                 switch (candidateConfigs.size()) {
81                 case 0:
82                         return createConfiguration(phpFile);
83                 case 1:
84                         return (ILaunchConfiguration) candidateConfigs.get(0);
85                 default:
86                         log(new RuntimeException(
87                                         PHPDebugUiMessages
88                                                         .getString("LaunchConfigurationShortcut.PHP.multipleConfigurationsError")));
89                         return null;
90                 }
91         }
92
93         protected ILaunchConfiguration createConfiguration(IFile phpFile) {
94                 ILaunchConfiguration config = null;
95                 try {
96                         ILaunchConfigurationType configType = getPHPLaunchConfigType();
97                         ILaunchConfigurationWorkingCopy wc = configType.newInstance(null,
98                                         getLaunchManager()
99                                                         .generateUniqueLaunchConfigurationNameFrom(
100                                                                         phpFile.getName()));
101                         wc.setAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME,
102                                         phpFile.getProject().getName());
103                         wc.setAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, phpFile
104                                         .getProjectRelativePath().toString());
105                         wc.setAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY,
106                                         PHPDebugUiConstants.DEFAULT_WORKING_DIRECTORY);
107                         config = wc.doSave();
108                 } catch (CoreException ce) {
109                         log(ce);
110                 }
111                 return config;
112         }
113
114         protected ILaunchConfigurationType getPHPLaunchConfigType() {
115                 return getLaunchManager().getLaunchConfigurationType(
116                                 PHPLaunchConfigurationAttribute.PHP_LAUNCH_CONFIGURATION_TYPE);
117         }
118
119         protected ILaunchManager getLaunchManager() {
120                 return DebugPlugin.getDefault().getLaunchManager();
121         }
122
123         protected void log(String message) {
124                 PHPDebugUiPlugin.log(new Status(Status.INFO,
125                                 PHPDebugUiPlugin.PLUGIN_ID, Status.INFO, message, null));
126         }
127
128         protected void log(Throwable t) {
129                 PHPDebugUiPlugin.log(t);
130         }
131 }