1
2
3
4
5 package org.apache.commons.jelly.eclipse.ui;
6
7
8 import org.eclipse.core.resources.IFile;
9 import org.eclipse.core.resources.IProject;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.debug.core.DebugPlugin;
12 import org.eclipse.debug.core.ILaunchConfiguration;
13 import org.eclipse.debug.core.ILaunchConfigurationType;
14 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
15 import org.eclipse.debug.core.ILaunchManager;
16 import org.eclipse.debug.ui.ILaunchShortcut;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.ui.IEditorPart;
20
21
22 /***
23 * Launcher shortcut implementation for the Eclipse 3 platform
24 * @author Juergen Mayrbaeurl
25 * @since 0.9
26 */
27 public class JellyLauncherShortcutImpl implements ILaunchShortcut
28 {
29 /***
30 * Default constructor
31 */
32 public JellyLauncherShortcutImpl() {
33 super();
34 }
35
36
37
38
39
40 public void launch(ISelection selection, String mode)
41 {
42
43 if (selection instanceof IStructuredSelection) {
44 searchAndLaunch(((IStructuredSelection)selection).toArray(), mode);
45 }
46 }
47
48
49 /***
50 * Launches the first entry in the <code>search</code> array of IFile entries
51 * @param search array of IFile entries (selected project Jelly script file)
52 * @param mode run or debug (selected by user)
53 */
54 protected void searchAndLaunch(Object[] search, String mode)
55 {
56 if (search != null)
57 {
58
59 if (search.length == 0) {
60 return;
61 }
62
63
64 Object object = search[0];
65 if (object instanceof IFile)
66 {
67 IFile file = (IFile) object;
68 this.launch(file, mode);
69 }
70 }
71 }
72
73
74
75
76 public void launch(IEditorPart arg0, String arg1)
77 {
78
79 JellyLauncherPlugin.getDefault().showNotImplemented();
80 }
81
82 /***
83 * Launches Jelly with the specified Jelly script file and user selected mode
84 * @param script Jelly script file
85 * @param mode run or debug mode, selected by the user
86 */
87 private void launch(IFile script, String mode)
88 {
89 try {
90
91 ILaunchConfiguration config = this.findLaunchConfiguration(script, mode);
92 if(config != null)
93 config.launch(mode, null);
94 } catch (CoreException e) {
95
96 JellyLauncherPlugin.getDefault().showErrorForException(JellyLaunchConfigurationConstants.ERR_LAUNCHING_SHORTCUT, e);
97 }
98 }
99
100 /***
101 * Finds the launch configuration for the Jelly script file. If no launch configuration
102 * exists, a new launch configuration will be created and added to the launch
103 * configuration types list.
104 * @param script Jelly script file
105 * @param mode run or debug
106 * @return the Eclipse launch configuration for the Jelly script
107 * @throws CoreException Eclipse platform exception
108 */
109 private ILaunchConfiguration findLaunchConfiguration(IFile script, String mode)
110 throws CoreException
111 {
112
113 ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
114
115 ILaunchConfigurationType type = manager.getLaunchConfigurationType(JellyLauncherPlugin.getResourceString("jelly.launchConfigurationType"));
116
117
118 String loc = script.getLocation().toOSString();
119
120
121
122 ILaunchConfiguration[] configurations = manager.getLaunchConfigurations(type);
123 for(int i = 0; i < configurations.length; i++)
124 {
125 ILaunchConfiguration config = configurations[i];
126 if(config.getAttribute(JellyLaunchConfigurationConstants.ATTR_JELLYSCRIPT_PATH, "").equals(loc))
127 {
128 return config;
129 }
130 }
131
132
133
134 ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null, this.createLaunchConfigurationname(script, mode));
135
136
137 JellyLaunchUtils.setDefaults(workingCopy, script);
138
139 workingCopy.setAttribute(JellyLaunchConfigurationConstants.ATTR_JELLYSCRIPT_PATH, loc);
140
141
142 return workingCopy.doSave();
143 }
144
145 /***
146 * Creates a name/title for the launch configuration of the Jelly script, that
147 * gets displayed in the launch configuration dialog and various menus. The name
148 * is created by taking the project name and appending the Jelly script file
149 * name, separated by a dot.
150 * @param script Jelly script file
151 * @param mode run or debug
152 * @return name of the launch configuration
153 */
154 private String createLaunchConfigurationname(IFile script, String mode)
155 {
156 String result = script.getFullPath().toString();
157
158
159 IProject proj = script.getProject();
160 if(proj != null)
161 result = proj.getName() + "." + script.getName();
162
163 return result;
164 }
165 }