001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.launcher;
011:
012: import java.io.File;
013: import java.util.ArrayList;
014: import java.util.HashMap;
015: import java.util.List;
016:
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.Path;
021: import org.eclipse.core.runtime.Platform;
022: import org.eclipse.debug.core.DebugPlugin;
023: import org.eclipse.debug.core.ILaunchConfiguration;
024: import org.eclipse.debug.core.ILaunchConfigurationType;
025: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
026: import org.eclipse.debug.core.ILaunchManager;
027: import org.eclipse.debug.ui.DebugUITools;
028: import org.eclipse.debug.ui.IDebugModelPresentation;
029: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
030: import org.eclipse.jface.action.Action;
031: import org.eclipse.jface.window.Window;
032: import org.eclipse.pde.core.plugin.IPluginModelBase;
033: import org.eclipse.pde.core.plugin.PluginRegistry;
034: import org.eclipse.pde.internal.core.FeatureModelManager;
035: import org.eclipse.pde.internal.core.PDECore;
036: import org.eclipse.pde.internal.core.TargetPlatformHelper;
037: import org.eclipse.pde.internal.core.ifeature.IFeature;
038: import org.eclipse.pde.internal.core.ifeature.IFeatureChild;
039: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
040: import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
041: import org.eclipse.pde.internal.core.iproduct.IArgumentsInfo;
042: import org.eclipse.pde.internal.core.iproduct.IConfigurationFileInfo;
043: import org.eclipse.pde.internal.core.iproduct.IProduct;
044: import org.eclipse.pde.internal.core.iproduct.IProductFeature;
045: import org.eclipse.pde.internal.core.iproduct.IProductPlugin;
046: import org.eclipse.pde.internal.core.util.CoreUtility;
047: import org.eclipse.pde.internal.ui.IPDEUIConstants;
048: import org.eclipse.pde.internal.ui.PDEPlugin;
049: import org.eclipse.pde.internal.ui.PDEUIMessages;
050: import org.eclipse.pde.ui.launcher.EclipseLaunchShortcut;
051: import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
052: import org.eclipse.pde.ui.launcher.PDESourcePathProvider;
053: import org.eclipse.ui.dialogs.ElementListSelectionDialog;
054:
055: public class LaunchAction extends Action {
056:
057: private IProduct fProduct;
058: private String fMode;
059: private String fPath;
060:
061: public LaunchAction(IProduct product, String path, String mode) {
062: fProduct = product;
063: fMode = mode;
064: fPath = path;
065: }
066:
067: public void run() {
068: try {
069: ILaunchConfiguration config = findLaunchConfiguration();
070: if (config != null)
071: DebugUITools.launch(config, fMode);
072: } catch (CoreException e) {
073: }
074: }
075:
076: private ILaunchConfiguration findLaunchConfiguration()
077: throws CoreException {
078: ILaunchConfiguration[] configs = getLaunchConfigurations();
079:
080: if (configs.length == 0)
081: return createConfiguration();
082:
083: ILaunchConfiguration config = null;
084: if (configs.length == 1) {
085: config = configs[0];
086: } else {
087: // Prompt the user to choose a config.
088: config = chooseConfiguration(configs);
089: }
090:
091: if (config != null) {
092: config = refreshConfiguration(config.getWorkingCopy());
093: }
094: return config;
095: }
096:
097: private ILaunchConfiguration refreshConfiguration(
098: ILaunchConfigurationWorkingCopy wc) throws CoreException {
099: wc
100: .setAttribute(IPDELauncherConstants.PRODUCT, fProduct
101: .getId());
102: String os = Platform.getOS();
103: wc.setAttribute(
104: IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,
105: getVMArguments(os));
106: wc
107: .setAttribute(
108: IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
109: getProgramArguments(os));
110: StringBuffer wsplugins = new StringBuffer();
111: StringBuffer explugins = new StringBuffer();
112: IPluginModelBase[] models = getModels();
113: for (int i = 0; i < models.length; i++) {
114: IPluginModelBase model = models[i];
115: String id = model.getPluginBase().getId();
116: if (model.getUnderlyingResource() == null) {
117: explugins.append(id);
118: explugins.append(","); //$NON-NLS-1$
119: } else {
120: wsplugins.append(id);
121: wsplugins.append(","); //$NON-NLS-1$
122: }
123: }
124: wc.setAttribute(
125: IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS,
126: wsplugins.toString());
127: wc.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS,
128: explugins.toString());
129: String configIni = getTemplateConfigIni();
130: wc.setAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT,
131: configIni == null);
132: if (configIni != null)
133: wc.setAttribute(
134: IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION,
135: configIni);
136: return wc.doSave();
137: }
138:
139: private String getProgramArguments(String os) {
140: StringBuffer buffer = new StringBuffer(LaunchArgumentsHelper
141: .getInitialProgramArguments());
142: IArgumentsInfo info = fProduct.getLauncherArguments();
143: String userArgs = (info != null) ? CoreUtility.normalize(info
144: .getCompleteProgramArguments(os)) : ""; //$NON-NLS-1$
145: if (userArgs.length() > 0) {
146: buffer.append(" "); //$NON-NLS-1$
147: buffer.append(userArgs);
148: }
149: return buffer.toString();
150: }
151:
152: private String getVMArguments(String os) {
153: IArgumentsInfo info = fProduct.getLauncherArguments();
154: return (info != null) ? CoreUtility.normalize(info
155: .getCompleteVMArguments(os)) : ""; //$NON-NLS-1$
156: }
157:
158: private IPluginModelBase[] getModels() {
159: HashMap map = new HashMap();
160: if (fProduct.useFeatures()) {
161: IFeatureModel[] features = getUniqueFeatures();
162: for (int i = 0; i < features.length; i++) {
163: addFeaturePlugins(features[i].getFeature(), map);
164: }
165: } else {
166: IProductPlugin[] plugins = fProduct.getPlugins();
167: for (int i = 0; i < plugins.length; i++) {
168: String id = plugins[i].getId();
169: if (id == null || map.containsKey(id))
170: continue;
171: IPluginModelBase model = PluginRegistry.findModel(id);
172: if (model != null
173: && TargetPlatformHelper
174: .matchesCurrentEnvironment(model))
175: map.put(id, model);
176: }
177: }
178: return (IPluginModelBase[]) map.values().toArray(
179: new IPluginModelBase[map.size()]);
180: }
181:
182: private IFeatureModel[] getUniqueFeatures() {
183: ArrayList list = new ArrayList();
184: IProductFeature[] features = fProduct.getFeatures();
185: for (int i = 0; i < features.length; i++) {
186: String id = features[i].getId();
187: String version = features[i].getVersion();
188: addFeatureAndChildren(id, version, list);
189: }
190: return (IFeatureModel[]) list.toArray(new IFeatureModel[list
191: .size()]);
192: }
193:
194: private void addFeatureAndChildren(String id, String version,
195: List list) {
196: FeatureModelManager manager = PDECore.getDefault()
197: .getFeatureModelManager();
198: IFeatureModel model = manager.findFeatureModel(id, version);
199: if (model == null || list.contains(model))
200: return;
201:
202: list.add(model);
203:
204: IFeatureChild[] children = model.getFeature()
205: .getIncludedFeatures();
206: for (int i = 0; i < children.length; i++) {
207: addFeatureAndChildren(children[i].getId(), children[i]
208: .getVersion(), list);
209: }
210: }
211:
212: private void addFeaturePlugins(IFeature feature, HashMap map) {
213: IFeaturePlugin[] plugins = feature.getPlugins();
214: for (int i = 0; i < plugins.length; i++) {
215: String id = plugins[i].getId();
216: if (id == null || map.containsKey(id))
217: continue;
218: IPluginModelBase model = PluginRegistry.findModel(id);
219: if (model != null
220: && TargetPlatformHelper
221: .matchesCurrentEnvironment(model))
222: map.put(id, model);
223: }
224: }
225:
226: private String getTemplateConfigIni() {
227: IConfigurationFileInfo info = fProduct
228: .getConfigurationFileInfo();
229: if (info != null && info.getUse().equals("custom")) { //$NON-NLS-1$
230: String path = getExpandedPath(info.getPath());
231: if (path != null) {
232: File file = new File(path);
233: if (file.exists() && file.isFile())
234: return file.getAbsolutePath();
235: }
236: }
237: return null;
238: }
239:
240: private String getExpandedPath(String path) {
241: if (path == null || path.length() == 0)
242: return null;
243: IResource resource = PDEPlugin.getWorkspace().getRoot()
244: .findMember(new Path(path));
245: if (resource != null) {
246: IPath fullPath = resource.getLocation();
247: return fullPath == null ? null : fullPath.toOSString();
248: }
249: return null;
250: }
251:
252: private ILaunchConfiguration chooseConfiguration(
253: ILaunchConfiguration[] configs) {
254: IDebugModelPresentation labelProvider = DebugUITools
255: .newDebugModelPresentation();
256: ElementListSelectionDialog dialog = new ElementListSelectionDialog(
257: PDEPlugin.getActiveWorkbenchShell(), labelProvider);
258: dialog.setElements(configs);
259: dialog.setTitle(PDEUIMessages.RuntimeWorkbenchShortcut_title);
260: if (fMode.equals(ILaunchManager.DEBUG_MODE)) {
261: dialog
262: .setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_debug);
263: } else {
264: dialog
265: .setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_run);
266: }
267: dialog.setMultipleSelection(false);
268: int result = dialog.open();
269: labelProvider.dispose();
270: if (result == Window.OK) {
271: return (ILaunchConfiguration) dialog.getFirstResult();
272: }
273: return null;
274: }
275:
276: private ILaunchConfiguration createConfiguration()
277: throws CoreException {
278: ILaunchConfigurationType configType = getWorkbenchLaunchConfigType();
279: String computedName = getComputedName(new Path(fPath)
280: .lastSegment());
281: ILaunchConfigurationWorkingCopy wc = configType.newInstance(
282: null, computedName);
283: wc.setAttribute(IPDELauncherConstants.LOCATION,
284: LaunchArgumentsHelper
285: .getDefaultWorkspaceLocation(computedName)); //$NON-NLS-1$
286: wc.setAttribute(IPDELauncherConstants.USEFEATURES, false);
287: wc.setAttribute(IPDELauncherConstants.USE_DEFAULT, false);
288: wc.setAttribute(IPDELauncherConstants.DOCLEAR, false);
289: wc.setAttribute(IPDEUIConstants.DOCLEARLOG, false);
290: wc.setAttribute(IPDEUIConstants.APPEND_ARGS_EXPLICITLY, true);
291: wc.setAttribute(IPDELauncherConstants.ASKCLEAR, true);
292: wc.setAttribute(IPDELauncherConstants.USE_PRODUCT, true);
293: wc.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, false);
294: wc.setAttribute(IPDELauncherConstants.PRODUCT_FILE, fPath);
295: wc
296: .setAttribute(
297: IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER,
298: PDESourcePathProvider.ID);
299: return refreshConfiguration(wc);
300: }
301:
302: private String getComputedName(String prefix) {
303: ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
304: return lm.generateUniqueLaunchConfigurationNameFrom(prefix);
305: }
306:
307: private ILaunchConfiguration[] getLaunchConfigurations()
308: throws CoreException {
309: ArrayList result = new ArrayList();
310: ILaunchManager manager = DebugPlugin.getDefault()
311: .getLaunchManager();
312: ILaunchConfigurationType type = manager
313: .getLaunchConfigurationType(EclipseLaunchShortcut.CONFIGURATION_TYPE);
314: ILaunchConfiguration[] configs = manager
315: .getLaunchConfigurations(type);
316: for (int i = 0; i < configs.length; i++) {
317: if (!DebugUITools.isPrivate(configs[i])) {
318: String path = configs[i].getAttribute(
319: IPDELauncherConstants.PRODUCT_FILE, ""); //$NON-NLS-1$
320: if (new Path(fPath).equals(new Path(path))) {
321: result.add(configs[i]);
322: }
323: }
324: }
325: return (ILaunchConfiguration[]) result
326: .toArray(new ILaunchConfiguration[result.size()]);
327: }
328:
329: protected ILaunchConfigurationType getWorkbenchLaunchConfigType() {
330: ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
331: return lm
332: .getLaunchConfigurationType(EclipseLaunchShortcut.CONFIGURATION_TYPE);
333: }
334:
335: }
|