KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.util.ArrayList JavaDoc;
14 import java.util.TreeMap JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.Preferences;
20 import org.eclipse.debug.core.DebugPlugin;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22 import org.eclipse.debug.core.ILaunchConfigurationType;
23 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
24 import org.eclipse.debug.core.ILaunchManager;
25 import org.eclipse.debug.ui.DebugUITools;
26 import org.eclipse.debug.ui.IDebugModelPresentation;
27 import org.eclipse.debug.ui.ILaunchShortcut;
28 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.pde.core.plugin.IFragmentModel;
33 import org.eclipse.pde.core.plugin.IPluginAttribute;
34 import org.eclipse.pde.core.plugin.IPluginBase;
35 import org.eclipse.pde.core.plugin.IPluginElement;
36 import org.eclipse.pde.core.plugin.IPluginExtension;
37 import org.eclipse.pde.core.plugin.IPluginImport;
38 import org.eclipse.pde.core.plugin.IPluginModelBase;
39 import org.eclipse.pde.internal.core.ICoreConstants;
40 import org.eclipse.pde.internal.core.ModelEntry;
41 import org.eclipse.pde.internal.core.PDECore;
42 import org.eclipse.pde.internal.core.PluginModelManager;
43 import org.eclipse.pde.internal.core.TargetPlatform;
44 import org.eclipse.pde.internal.ui.PDEPlugin;
45 import org.eclipse.pde.internal.ui.PDEUIMessages;
46 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
47 import org.eclipse.swt.widgets.Shell;
48 import org.eclipse.ui.IEditorPart;
49 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
50  
51 /**
52  * A launch short cut for the run-time workspace.
53  */

54 public class RuntimeWorkbenchShortcut implements ILaunchShortcut {
55     
56     public static final String JavaDoc CLASSPATH_PROVIDER = "org.eclipse.pde.ui.workbenchClasspathProvider"; //$NON-NLS-1$
57
public static final String JavaDoc CONFIGURATION_TYPE = "org.eclipse.pde.ui.RuntimeWorkbench"; //$NON-NLS-1$
58

59     private IPluginModelBase fModel = null;
60     
61     public void run(IProject project) {
62         launch(PDECore.getDefault().getModelManager().findModel(project), ILaunchManager.RUN_MODE);
63     }
64     
65     public void debug(IProject project) {
66         launch(PDECore.getDefault().getModelManager().findModel(project), ILaunchManager.DEBUG_MODE);
67     }
68     
69     /*
70      * @see ILaunchShortcut#launch(IEditorPart, String)
71      */

72     public void launch(IEditorPart editor, String JavaDoc mode) {
73         launch(mode, null);
74     }
75
76     /*
77      * @see ILaunchShortcut#launch(ISelection, String)
78      */

79     public void launch(ISelection selection, String JavaDoc mode) {
80         launch(getSelectedModel(selection), mode);
81     }
82     
83     private void launch(IPluginModelBase model, String JavaDoc mode) {
84         fModel = model;
85         if (fModel != null) {
86             String JavaDoc[] applicationNames = getAvailableApplications();
87             if (applicationNames.length == 0) {
88                 launch(mode, null);
89             } else if (applicationNames.length == 1) {
90                 launch(mode, applicationNames[0]);
91             } else {
92                 ApplicationSelectionDialog dialog = new ApplicationSelectionDialog(
93                         PDEPlugin.getActiveWorkbenchShell().getShell(), applicationNames,
94                         mode);
95                 if (dialog.open() == Window.OK) {
96                     launch(mode, dialog.getSelectedApplication());
97                 }
98             }
99         } else {
100             launch(mode, null);
101         }
102     }
103     
104     private IPluginModelBase getSelectedModel(ISelection selection) {
105         if (selection instanceof IStructuredSelection) {
106             IStructuredSelection ssel = (IStructuredSelection)selection;
107             if (!ssel.isEmpty()) {
108                 Object JavaDoc object = ssel.getFirstElement();
109                 if (object instanceof IAdaptable) {
110                     IProject project = (IProject)((IAdaptable)object).getAdapter(IProject.class);
111                     if (project != null && project.isOpen())
112                         return PDECore.getDefault().getModelManager().findModel(project);
113                 }
114             }
115         }
116         return null;
117     }
118     
119     private String JavaDoc[] getAvailableApplications() {
120         IPluginBase plugin = fModel.getPluginBase();
121         String JavaDoc id = plugin.getId();
122         if (id == null || id.trim().length() == 0)
123             return new String JavaDoc[0];
124         
125         IPluginExtension[] extensions = plugin.getExtensions();
126         ArrayList JavaDoc result = new ArrayList JavaDoc();
127         for (int i = 0; i < extensions.length; i++) {
128             IPluginExtension extension = extensions[i];
129             if ("org.eclipse.core.runtime.applications".equals(extension.getPoint())) { //$NON-NLS-1$
130
String JavaDoc extensionID = extension.getId();
131                 if (extensionID != null && extensionID.trim().length() > 0) {
132                     result.add(id.trim() + "." + extensionID.trim()); //$NON-NLS-1$
133
}
134             }
135         }
136         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
137     }
138         
139     /**
140      * Launches a configuration in the given mode
141      */

142     protected void launch(String JavaDoc mode, String JavaDoc applicationName) {
143         ILaunchConfiguration config = findLaunchConfiguration(mode, applicationName);
144         if (config != null) {
145             DebugUITools.launch(config, mode);
146         }
147     }
148     
149     /**
150      * Locate a configuration to relaunch. If one cannot be found, create one.
151      *
152      * @return a re-useable config or <code>null</code> if none
153      */

154     protected ILaunchConfiguration findLaunchConfiguration(String JavaDoc mode, String JavaDoc applicationName) {
155         ILaunchConfiguration[] configs = getLaunchConfigurations(getWorkbenchLaunchConfigType(), applicationName);
156             
157         if (configs.length == 0)
158             return createConfiguration(applicationName);
159
160         if (configs.length == 1)
161             return configs[0];
162
163         // Prompt the user to choose a config.
164
return chooseConfiguration(configs, mode);
165     }
166     
167     private ILaunchConfiguration[] getLaunchConfigurations(ILaunchConfigurationType configType, String JavaDoc applicationName) {
168         ArrayList JavaDoc result = new ArrayList JavaDoc();
169         try {
170             ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
171             ILaunchConfiguration[] configs = manager.getLaunchConfigurations(configType);
172             for (int i = 0; i < configs.length; i++) {
173                 if (!DebugUITools.isPrivate(configs[i])) {
174                     if (!configs[i].getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) {
175                         String JavaDoc configApp = configs[i].getAttribute(IPDELauncherConstants.APPLICATION, (String JavaDoc)null);
176                         if ((configApp == null && applicationName == null)
177                             || (configApp != null && applicationName != null && configApp.equals(applicationName))) {
178                             result.add(configs[i]);
179                         }
180                     } else {
181                         String JavaDoc thisProduct = configs[i].getAttribute(IPDELauncherConstants.PRODUCT, (String JavaDoc)null);
182                         if (thisProduct != null && thisProduct.equals(getProduct(applicationName))) {
183                             result.add(configs[i]);
184                         }
185                     }
186                     
187                 }
188             }
189         } catch (CoreException e) {
190         }
191         return (ILaunchConfiguration[]) result.toArray(new ILaunchConfiguration[result.size()]);
192     }
193     
194     /**
195      * Shows a selection dialog that allows the user to choose one of the specified
196      * launch configurations. Return the chosen config, or <code>null</code> if the
197      * user cancelled the dialog.
198      */

199     protected ILaunchConfiguration chooseConfiguration(ILaunchConfiguration[] configs, String JavaDoc mode) {
200         IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation();
201         ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), labelProvider);
202         dialog.setElements(configs);
203         dialog.setTitle(PDEUIMessages.RuntimeWorkbenchShortcut_title);
204         if (mode.equals(ILaunchManager.DEBUG_MODE)) {
205             dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_debug);
206         } else {
207             dialog.setMessage(PDEUIMessages.RuntimeWorkbenchShortcut_select_run);
208         }
209         dialog.setMultipleSelection(false);
210         int result= dialog.open();
211         labelProvider.dispose();
212         if (result == Window.OK) {
213             return (ILaunchConfiguration)dialog.getFirstResult();
214         }
215         return null;
216     }
217     
218     /**
219      * Creates a new configuration with default values.
220      */

221     protected ILaunchConfiguration createConfiguration(String JavaDoc applicationName) {
222         ILaunchConfiguration config = null;
223         try {
224             ILaunchConfigurationType configType = getWorkbenchLaunchConfigType();
225             String JavaDoc computedName = getComputedName(configType.getName());
226             ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, computedName);
227             if (TargetPlatform.isRuntimeRefactored2())
228                 wc.setAttribute("pde.version", "3.2a"); //$NON-NLS-1$ //$NON-NLS-2$
229
else if (TargetPlatform.isRuntimeRefactored1())
230                 wc.setAttribute("pde.version", "3.2"); //$NON-NLS-1$ //$NON-NLS-2$
231
wc.setAttribute(IPDELauncherConstants.LOCATION, LaunchArgumentsHelper.getDefaultWorkspaceLocation(computedName)); //$NON-NLS-1$
232
setJavaArguments(wc);
233             wc.setAttribute(IPDELauncherConstants.USEFEATURES, false);
234             wc.setAttribute(IPDELauncherConstants.DOCLEAR, false);
235             wc.setAttribute(IPDELauncherConstants.ASKCLEAR, true);
236             wc.setAttribute(IPDELauncherConstants.TRACING_CHECKED, IPDELauncherConstants.TRACING_NONE);
237             wc.setAttribute(IPDELauncherConstants.USE_DEFAULT, applicationName == null);
238             if (applicationName != null) {
239                 String JavaDoc product = getProduct(applicationName);
240                 if (product == null) {
241                     wc.setAttribute(IPDELauncherConstants.APPLICATION, applicationName);
242                 } else {
243                     wc.setAttribute(IPDELauncherConstants.USE_PRODUCT, true);
244                     wc.setAttribute(IPDELauncherConstants.PRODUCT, product);
245                 }
246                 wc.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, false);
247                 TreeMap JavaDoc map = new TreeMap JavaDoc();
248                 addPluginAndDependencies(fModel, map);
249                 Object JavaDoc[] models = map.values().toArray();
250                 StringBuffer JavaDoc wsplugins = new StringBuffer JavaDoc();
251                 StringBuffer JavaDoc explugins = new StringBuffer JavaDoc();
252                 for (int i = 0; i < models.length; i++) {
253                     IPluginModelBase model = (IPluginModelBase)models[i];
254                     String JavaDoc id = model.getPluginBase().getId();
255                     if (model.getUnderlyingResource() == null) {
256                         if (explugins.length() > 0)
257                             explugins.append(","); //$NON-NLS-1$
258
explugins.append(id);
259                     } else {
260                         if (wsplugins.length() > 0)
261                             wsplugins.append(","); //$NON-NLS-1$
262
wsplugins.append(id);
263                     }
264                 }
265                 wc.setAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, wsplugins.toString());
266                 wc.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, explugins.toString());
267             } else {
268                 String JavaDoc defaultProduct = TargetPlatform.getDefaultProduct();
269                 if (defaultProduct != null) {
270                     wc.setAttribute(IPDELauncherConstants.USE_DEFAULT, true);
271                     wc.setAttribute(IPDELauncherConstants.USE_PRODUCT, true);
272                     wc.setAttribute(IPDELauncherConstants.PRODUCT, defaultProduct);
273                 }
274             }
275
276             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, CLASSPATH_PROVIDER);
277             config= wc.doSave();
278         } catch (CoreException ce) {
279             PDEPlugin.logException(ce);
280         }
281         return config;
282     }
283     
284     private void setJavaArguments(ILaunchConfigurationWorkingCopy wc) {
285         Preferences preferences = PDECore.getDefault().getPluginPreferences();
286         String JavaDoc programArgs = preferences.getString(ICoreConstants.PROGRAM_ARGS);
287         if (programArgs.length() > 0)
288             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, programArgs);
289         String JavaDoc vmArgs = preferences.getString(ICoreConstants.VM_ARGS);
290         if (vmArgs.length() > 0)
291             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs);
292     }
293     
294     private String JavaDoc getProduct(String JavaDoc appName) {
295         if (appName == null)
296             return TargetPlatform.getDefaultProduct();
297         if (fModel != null && appName != null) {
298             IPluginExtension[] extensions = fModel.getPluginBase().getExtensions();
299             for (int i = 0; i < extensions.length; i++) {
300                 IPluginExtension ext = extensions[i];
301                 String JavaDoc point = ext.getPoint();
302                 if ("org.eclipse.core.runtime.products".equals(point)) { //$NON-NLS-1$
303
if (ext.getChildCount() == 1) {
304                         IPluginElement prod = (IPluginElement)ext.getChildren()[0];
305                         if (prod.getName().equals("product")) { //$NON-NLS-1$
306
IPluginAttribute attr = prod.getAttribute("application"); //$NON-NLS-1$
307
if (attr != null && appName.equals(attr.getValue())) {
308                                 return fModel.getPluginBase().getId() + "." + ext.getId(); //$NON-NLS-1$
309
}
310                         }
311                     }
312                 }
313             }
314         }
315         return null;
316     }
317     
318     /**
319      * Returns the workbench config type
320      */

321     protected ILaunchConfigurationType getWorkbenchLaunchConfigType() {
322         ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
323         return lm.getLaunchConfigurationType(CONFIGURATION_TYPE);
324     }
325     
326     private String JavaDoc getComputedName(String JavaDoc prefix) {
327         ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
328         return lm.generateUniqueLaunchConfigurationNameFrom(prefix);
329     }
330     
331     /**
332      * Convenience method to get the window that owns this action's Shell.
333      */

334     protected Shell getShell() {
335         return PDEPlugin.getActiveWorkbenchShell();
336     }
337     
338     public static void addPluginAndDependencies(IPluginModelBase model, TreeMap JavaDoc map) {
339         if (model == null)
340             return;
341
342         String JavaDoc id = model.getPluginBase().getId();
343         if (id == null || map.containsKey(id))
344             return;
345
346         map.put(id, model);
347
348         if (model instanceof IFragmentModel) {
349             IPluginModelBase parent =
350                 findPlugin(((IFragmentModel) model).getFragment().getPluginId());
351             addPluginAndDependencies(parent, map);
352         } else {
353             IFragmentModel[] fragments = findFragments(model.getPluginBase());
354             for (int i = 0; i < fragments.length; i++) {
355                 String JavaDoc fragmentID = fragments[i].getPluginBase().getId();
356                 if (!"org.eclipse.ui.workbench.compatibility".equals(fragmentID)) //$NON-NLS-1$
357
addPluginAndDependencies(fragments[i], map);
358             }
359         }
360
361         IPluginImport[] imports = model.getPluginBase().getImports();
362         for (int i = 0; i < imports.length; i++) {
363             addPluginAndDependencies(findPlugin(imports[i].getId()), map);
364         }
365     }
366     
367     private static IPluginModelBase findPlugin(String JavaDoc id) {
368         PluginModelManager manager = PDECore.getDefault().getModelManager();
369         ModelEntry entry = manager.findEntry(id);
370         return (entry != null) ? entry.getActiveModel() : null;
371     }
372     
373     private static IFragmentModel[] findFragments(IPluginBase plugin) {
374         ModelEntry[] entries = PDECore.getDefault().getModelManager().getEntries();
375         ArrayList JavaDoc result = new ArrayList JavaDoc();
376         for (int i = 0; i < entries.length; i++) {
377             ModelEntry entry = entries[i];
378             IPluginModelBase model = entry.getActiveModel();
379             if (model instanceof IFragmentModel && TargetPlatform.matchesCurrentEnvironment(model)) {
380                 String JavaDoc id = ((IFragmentModel) model).getFragment().getPluginId();
381                 if (id.equals(plugin.getId())) {
382                     result.add(model);
383                 }
384             }
385         }
386         return (IFragmentModel[]) result.toArray(new IFragmentModel[result.size()]);
387     }
388 }
389
Popular Tags