KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > LaunchAction


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.launcher;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.debug.core.DebugPlugin;
24 import org.eclipse.debug.core.ILaunchConfiguration;
25 import org.eclipse.debug.core.ILaunchConfigurationType;
26 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
27 import org.eclipse.debug.core.ILaunchManager;
28 import org.eclipse.debug.ui.DebugUITools;
29 import org.eclipse.debug.ui.IDebugModelPresentation;
30 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
31 import org.eclipse.jface.action.Action;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.pde.core.plugin.IPluginModelBase;
34 import org.eclipse.pde.core.plugin.PluginRegistry;
35 import org.eclipse.pde.internal.core.FeatureModelManager;
36 import org.eclipse.pde.internal.core.PDECore;
37 import org.eclipse.pde.internal.core.TargetPlatformHelper;
38 import org.eclipse.pde.internal.core.ifeature.IFeature;
39 import org.eclipse.pde.internal.core.ifeature.IFeatureChild;
40 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
41 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
42 import org.eclipse.pde.internal.core.iproduct.IArgumentsInfo;
43 import org.eclipse.pde.internal.core.iproduct.IConfigurationFileInfo;
44 import org.eclipse.pde.internal.core.iproduct.IProduct;
45 import org.eclipse.pde.internal.core.iproduct.IProductFeature;
46 import org.eclipse.pde.internal.core.iproduct.IProductPlugin;
47 import org.eclipse.pde.internal.core.util.CoreUtility;
48 import org.eclipse.pde.internal.ui.IPDEUIConstants;
49 import org.eclipse.pde.internal.ui.PDEPlugin;
50 import org.eclipse.pde.internal.ui.PDEUIMessages;
51 import org.eclipse.pde.ui.launcher.EclipseLaunchShortcut;
52 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
53 import org.eclipse.pde.ui.launcher.PDESourcePathProvider;
54 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
55
56 public class LaunchAction extends Action {
57
58     private IProduct fProduct;
59     private String JavaDoc fMode;
60     private String JavaDoc fPath;
61
62     public LaunchAction(IProduct product, String JavaDoc path, String JavaDoc mode) {
63         fProduct = product;
64         fMode = mode;
65         fPath = path;
66     }
67     
68     public void run() {
69         try {
70             ILaunchConfiguration config = findLaunchConfiguration();
71             if (config != null)
72                 DebugUITools.launch(config, fMode);
73         } catch (CoreException e) {
74         }
75     }
76     
77     private ILaunchConfiguration findLaunchConfiguration() throws CoreException {
78         ILaunchConfiguration[] configs = getLaunchConfigurations();
79         
80         if (configs.length == 0)
81             return createConfiguration();
82
83         ILaunchConfiguration config = null;
84         if (configs.length == 1) {
85             config = configs[0];
86         } else {
87             // Prompt the user to choose a config.
88
config = chooseConfiguration(configs);
89         }
90         
91         if (config != null) {
92             config = refreshConfiguration(config.getWorkingCopy());
93         }
94         return config;
95     }
96
97     private ILaunchConfiguration refreshConfiguration(ILaunchConfigurationWorkingCopy wc) throws CoreException {
98         wc.setAttribute(IPDELauncherConstants.PRODUCT, fProduct.getId());
99         String JavaDoc os = Platform.getOS();
100         wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, getVMArguments(os));
101         wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, getProgramArguments(os));
102         StringBuffer JavaDoc wsplugins = new StringBuffer JavaDoc();
103         StringBuffer JavaDoc explugins = new StringBuffer JavaDoc();
104         IPluginModelBase[] models = getModels();
105         for (int i = 0; i < models.length; i++) {
106             IPluginModelBase model = models[i];
107             String JavaDoc id = model.getPluginBase().getId();
108             if (model.getUnderlyingResource() == null) {
109                 explugins.append(id);
110                 explugins.append(","); //$NON-NLS-1$
111
} else {
112                 wsplugins.append(id);
113                 wsplugins.append(","); //$NON-NLS-1$
114
}
115         }
116         wc.setAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, wsplugins.toString());
117         wc.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, explugins.toString());
118         String JavaDoc configIni = getTemplateConfigIni();
119         wc.setAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, configIni == null);
120         if (configIni != null)
121             wc.setAttribute(IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION, configIni);
122         return wc.doSave();
123     }
124     
125     private String JavaDoc getProgramArguments(String JavaDoc os) {
126         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(LaunchArgumentsHelper.getInitialProgramArguments());
127         IArgumentsInfo info = fProduct.getLauncherArguments();
128         String JavaDoc userArgs = (info != null) ? CoreUtility.normalize(info.getCompleteProgramArguments(os)) : ""; //$NON-NLS-1$
129
if (userArgs.length() > 0) {
130             buffer.append(" "); //$NON-NLS-1$
131
buffer.append(userArgs);
132         }
133         return buffer.toString();
134     }
135     
136     private String JavaDoc getVMArguments(String JavaDoc os) {
137         IArgumentsInfo info = fProduct.getLauncherArguments();
138         return (info != null) ? CoreUtility.normalize(info.getCompleteVMArguments(os)) : ""; //$NON-NLS-1$
139
}
140     
141     private IPluginModelBase[] getModels() {
142         HashMap JavaDoc map = new HashMap JavaDoc();
143         if (fProduct.useFeatures()) {
144             IFeatureModel[] features = getUniqueFeatures();
145             for (int i = 0; i < features.length; i++) {
146                 addFeaturePlugins(features[i].getFeature(), map);
147             }
148         } else {
149             IProductPlugin[] plugins = fProduct.getPlugins();
150             for (int i = 0; i < plugins.length; i++) {
151                 String JavaDoc id = plugins[i].getId();
152                 if (id == null || map.containsKey(id))
153                     continue;
154                 IPluginModelBase model = PluginRegistry.findModel(id);
155                 if (model != null && TargetPlatformHelper.matchesCurrentEnvironment(model))
156                     map.put(id, model);
157             }
158         }
159         return (IPluginModelBase[])map.values().toArray(new IPluginModelBase[map.size()]);
160     }
161     
162     private IFeatureModel[] getUniqueFeatures() {
163         ArrayList JavaDoc list = new ArrayList JavaDoc();
164         IProductFeature[] features = fProduct.getFeatures();
165         for (int i = 0; i < features.length; i++) {
166             String JavaDoc id = features[i].getId();
167             String JavaDoc version = features[i].getVersion();
168             addFeatureAndChildren(id, version, list);
169         }
170         return (IFeatureModel[])list.toArray(new IFeatureModel[list.size()]);
171     }
172     
173     private void addFeatureAndChildren(String JavaDoc id, String JavaDoc version, List JavaDoc list) {
174         FeatureModelManager manager = PDECore.getDefault().getFeatureModelManager();
175         IFeatureModel model = manager.findFeatureModel(id, version);
176         if (model == null || list.contains(model))
177             return;
178         
179         list.add(model);
180         
181         IFeatureChild[] children = model.getFeature().getIncludedFeatures();
182         for (int i = 0; i < children.length; i++) {
183             addFeatureAndChildren(children[i].getId(), children[i].getVersion(), list);
184         }
185     }
186     
187     private void addFeaturePlugins(IFeature feature, HashMap JavaDoc map) {
188         IFeaturePlugin[] plugins = feature.getPlugins();
189         for (int i = 0; i < plugins.length; i++) {
190             String JavaDoc id = plugins[i].getId();
191             if (id == null || map.containsKey(id))
192                 continue;
193             IPluginModelBase model = PluginRegistry.findModel(id);
194             if (model != null && TargetPlatformHelper.matchesCurrentEnvironment(model))
195                 map.put(id, model);
196         }
197     }
198     
199     private String JavaDoc getTemplateConfigIni() {
200         IConfigurationFileInfo info = fProduct.getConfigurationFileInfo();
201         if (info != null && info.getUse().equals("custom")) { //$NON-NLS-1$
202
String JavaDoc path = getExpandedPath(info.getPath());
203             if (path != null) {
204                 File JavaDoc file = new File JavaDoc(path);
205                 if (file.exists() && file.isFile())
206                     return file.getAbsolutePath();
207             }
208         }
209         return null;
210     }
211     
212     private String JavaDoc getExpandedPath(String JavaDoc path) {
213         if (path == null || path.length() == 0)
214             return null;
215         IResource resource = PDEPlugin.getWorkspace().getRoot().findMember(new Path(path));
216         if (resource != null) {
217             IPath fullPath = resource.getLocation();
218             return fullPath == null ? null : fullPath.toOSString();
219         }
220         return null;
221     }
222
223
224     private ILaunchConfiguration chooseConfiguration(ILaunchConfiguration[] configs) {
225         IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation();
226         ElementListSelectionDialog dialog= new ElementListSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), labelProvider);
227         dialog.setElements(configs);
228         dialog.setTitle(PDEUIMessages.RuntimeWorkbenchShortcut_title);
229         if (fMode.equals(ILaunchManager.DEBUG_MODE)) {
230             dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_debug);
231         } else {
232             dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_run);
233         }
234         dialog.setMultipleSelection(false);
235         int result= dialog.open();
236         labelProvider.dispose();
237         if (result == Window.OK) {
238             return (ILaunchConfiguration)dialog.getFirstResult();
239         }
240         return null;
241     }
242
243     private ILaunchConfiguration createConfiguration() throws CoreException {
244         ILaunchConfigurationType configType = getWorkbenchLaunchConfigType();
245         String JavaDoc computedName = getComputedName(new Path(fPath).lastSegment());
246         ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, computedName);
247         wc.setAttribute(IPDELauncherConstants.LOCATION, LaunchArgumentsHelper.getDefaultWorkspaceLocation(computedName)); //$NON-NLS-1$
248
wc.setAttribute(IPDELauncherConstants.USEFEATURES, false);
249         wc.setAttribute(IPDELauncherConstants.USE_DEFAULT, false);
250         wc.setAttribute(IPDELauncherConstants.DOCLEAR, false);
251         wc.setAttribute(IPDEUIConstants.DOCLEARLOG, false);
252         wc.setAttribute(IPDEUIConstants.APPEND_ARGS_EXPLICITLY, true);
253         wc.setAttribute(IPDELauncherConstants.ASKCLEAR, true);
254         wc.setAttribute(IPDELauncherConstants.USE_PRODUCT, true);
255         wc.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, false);
256         wc.setAttribute(IPDELauncherConstants.PRODUCT_FILE, fPath);
257         wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, PDESourcePathProvider.ID);
258         return refreshConfiguration(wc);
259     }
260     
261     private String JavaDoc getComputedName(String JavaDoc prefix) {
262         ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
263         return lm.generateUniqueLaunchConfigurationNameFrom(prefix);
264     }
265     
266     private ILaunchConfiguration[] getLaunchConfigurations() throws CoreException {
267         ArrayList JavaDoc result = new ArrayList JavaDoc();
268         ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
269         ILaunchConfigurationType type = manager.getLaunchConfigurationType(EclipseLaunchShortcut.CONFIGURATION_TYPE);
270         ILaunchConfiguration[] configs = manager.getLaunchConfigurations(type);
271         for (int i = 0; i < configs.length; i++) {
272             if (!DebugUITools.isPrivate(configs[i])) {
273                 String JavaDoc path = configs[i].getAttribute(IPDELauncherConstants.PRODUCT_FILE, ""); //$NON-NLS-1$
274
if (new Path(fPath).equals(new Path(path))) {
275                     result.add(configs[i]);
276                 }
277             }
278         }
279         return (ILaunchConfiguration[]) result.toArray(new ILaunchConfiguration[result.size()]);
280     }
281     
282     protected ILaunchConfigurationType getWorkbenchLaunchConfigType() {
283         ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
284         return lm.getLaunchConfigurationType(EclipseLaunchShortcut.CONFIGURATION_TYPE);
285     }
286     
287 }
288
Popular Tags