KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > ui > launcher > EclipseLaunchShortcut


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.ui.launcher;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IAdaptable;
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.jdt.launching.IJavaLaunchConfigurationConstants;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.pde.core.plugin.IPluginAttribute;
29 import org.eclipse.pde.core.plugin.IPluginBase;
30 import org.eclipse.pde.core.plugin.IPluginElement;
31 import org.eclipse.pde.core.plugin.IPluginExtension;
32 import org.eclipse.pde.core.plugin.IPluginModelBase;
33 import org.eclipse.pde.core.plugin.PluginRegistry;
34 import org.eclipse.pde.core.plugin.TargetPlatform;
35 import org.eclipse.pde.internal.core.DependencyManager;
36 import org.eclipse.pde.internal.core.TargetPlatformHelper;
37 import org.eclipse.pde.internal.core.product.WorkspaceProductModel;
38 import org.eclipse.pde.internal.core.util.IdUtil;
39 import org.eclipse.pde.internal.ui.IPDEUIConstants;
40 import org.eclipse.pde.internal.ui.PDEPlugin;
41 import org.eclipse.pde.internal.ui.launcher.ApplicationSelectionDialog;
42 import org.eclipse.pde.internal.ui.launcher.LaunchAction;
43 import org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper;
44 import org.eclipse.ui.IEditorPart;
45
46 /**
47  * A launch shortcut capable of launching an Eclipse application.
48  * Given the current selection, either a new Eclipse Application launch configuration is created with default settings, or the user is presented
49  * with a list of suitable existing Eclipse Application launch configurations to choose from.
50  * <p>
51  * This class may be substantiated or subclassed by clients.
52  * </p>
53  * @since 3.3
54  */

55 public class EclipseLaunchShortcut extends AbstractLaunchShortcut {
56     
57     public static final String JavaDoc CONFIGURATION_TYPE = "org.eclipse.pde.ui.RuntimeWorkbench"; //$NON-NLS-1$
58

59     private IPluginModelBase fModel = null;
60     
61     private String JavaDoc fApplicationName = null;
62     
63     /*
64      * (non-Javadoc)
65      * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.ui.IEditorPart, java.lang.String)
66      */

67     public void launch(IEditorPart editor, String JavaDoc mode) {
68         fApplicationName = null;
69         fModel = null;
70         launch(mode);
71     }
72
73     /*
74      * (non-Javadoc)
75      * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.jface.viewers.ISelection, java.lang.String)
76      */

77     public void launch(ISelection selection, String JavaDoc mode) {
78         IPluginModelBase model = null;
79         if (selection instanceof IStructuredSelection) {
80             IStructuredSelection ssel = (IStructuredSelection)selection;
81             if (!ssel.isEmpty()) {
82                 Object JavaDoc object = ssel.getFirstElement();
83                 IProject project = null;
84                 if (object instanceof IFile) {
85                     // if instanceof Product model, we are launching from Product Editor. Launch as Product
86
if ("product".equals(((IFile)object).getFileExtension())) { //$NON-NLS-1$
87
WorkspaceProductModel productModel = new WorkspaceProductModel((IFile)object, false);
88                         try {
89                             productModel.load();
90                             new LaunchAction(productModel.getProduct(), ((IFile)object).getFullPath().toOSString(), mode).run();
91                         } catch (CoreException e) {
92                             PDEPlugin.log(e);
93                         }
94                         return;
95                     }
96                     // if it isn't a .product file, then find the project of the file inorder to launch using that project's corresponding plug-in
97
// bug 180043
98
project = ((IFile)object).getProject();
99                 }
100                 else if (object instanceof IAdaptable) {
101                     project = (IProject)((IAdaptable)object).getAdapter(IProject.class);
102                 }
103                 if (project != null && project.isOpen())
104                     model = PluginRegistry.findModel(project);
105             }
106         }
107         launch(model, mode);
108     }
109     
110     private void launch(IPluginModelBase model, String JavaDoc mode) {
111         fModel = model;
112         fApplicationName = null;
113         if (fModel != null) {
114             String JavaDoc[] applicationNames = getAvailableApplications();
115             if (applicationNames.length == 1) {
116                 fApplicationName = applicationNames[0];
117             } else if (applicationNames.length > 1){
118                 ApplicationSelectionDialog dialog = new ApplicationSelectionDialog(
119                         PDEPlugin.getActiveWorkbenchShell().getShell(), applicationNames,
120                         mode);
121                 if (dialog.open() == Window.OK) {
122                     fApplicationName = dialog.getSelectedApplication();
123                 }
124             }
125         }
126         launch(mode);
127     }
128     
129     private String JavaDoc[] getAvailableApplications() {
130         IPluginBase plugin = fModel.getPluginBase();
131         String JavaDoc id = plugin.getId();
132         if (id == null || id.trim().length() == 0)
133             return new String JavaDoc[0];
134         
135         IPluginExtension[] extensions = plugin.getExtensions();
136         ArrayList JavaDoc result = new ArrayList JavaDoc();
137         for (int i = 0; i < extensions.length; i++) {
138             IPluginExtension extension = extensions[i];
139             if ("org.eclipse.core.runtime.applications".equals(extension.getPoint())) { //$NON-NLS-1$
140
String JavaDoc extensionID = extension.getId();
141                 if (extensionID != null) {
142                     result.add(IdUtil.getFullId(extensions[i]));
143                 }
144             }
145         }
146         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
147     }
148
149     private String JavaDoc getProduct(String JavaDoc appName) {
150         if (appName == null)
151             return TargetPlatform.getDefaultProduct();
152         if (fModel != null && appName != null) {
153             IPluginExtension[] extensions = fModel.getPluginBase().getExtensions();
154             for (int i = 0; i < extensions.length; i++) {
155                 IPluginExtension ext = extensions[i];
156                 String JavaDoc point = ext.getPoint();
157                 if ("org.eclipse.core.runtime.products".equals(point)) { //$NON-NLS-1$
158
if (ext.getChildCount() == 1) {
159                         IPluginElement prod = (IPluginElement)ext.getChildren()[0];
160                         if (prod.getName().equals("product")) { //$NON-NLS-1$
161
IPluginAttribute attr = prod.getAttribute("application"); //$NON-NLS-1$
162
if (attr != null && appName.equals(attr.getValue())) {
163                                 return IdUtil.getFullId(ext);
164                             }
165                         }
166                     }
167                 }
168             }
169         }
170         return null;
171     }
172
173     /**
174      * Returns a boolean value indicating whether the launch configuration is a good match for
175      * the application or product to launch.
176      *
177      * @param configuration
178      * the launch configuration being evaluated
179      *
180      * @return <code>true</coded> if the launch configuration is suitable for the application
181      * or product to launch with, <code>false</code> otherwise.
182      */

183     protected boolean isGoodMatch(ILaunchConfiguration configuration) {
184         try {
185             if (!configuration.getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) {
186                 String JavaDoc configApp = configuration.getAttribute(IPDELauncherConstants.APPLICATION, (String JavaDoc)null);
187                 return (configApp == null && fApplicationName == null)
188                        || (fApplicationName != null && fApplicationName.equals(configApp));
189             }
190             String JavaDoc thisProduct = configuration.getAttribute(IPDELauncherConstants.PRODUCT, (String JavaDoc)null);
191             return thisProduct != null && thisProduct.equals(getProduct(fApplicationName));
192             
193         } catch (CoreException e) {
194         }
195         return false;
196     }
197     
198     /**
199      * Initializes a new Eclipse Application launch configuration with defaults based
200      * on the current selection:
201      * <ul>
202      * <li>If there is no selection or the selected project is a plug-in that does not declare an application,
203      * the default product is launched.</li>
204      * <li>If the selected project is a plug-in that declares an application, then that application is launched.</li>
205      * <li>If the selected project is a plug-in that declares more than one application, then the user is presented
206      * with a list of choices to choose from.</li>
207      * </ul>
208      * Once an application is chosen, the plug-in is searched to see if there is a product
209      * bound to this application. If a product is found, the product is launched instead, since
210      * a product provides a richer branded experience.
211      *
212      * @since 3.3
213      */

214     protected void initializeConfiguration(ILaunchConfigurationWorkingCopy wc) {
215         if (TargetPlatformHelper.usesNewApplicationModel())
216             wc.setAttribute(IPDEUIConstants.LAUNCHER_PDE_VERSION, "3.3"); //$NON-NLS-1$
217
else if (TargetPlatformHelper.getTargetVersion() >= 3.2)
218             wc.setAttribute(IPDEUIConstants.LAUNCHER_PDE_VERSION, "3.2a"); //$NON-NLS-1$
219
wc.setAttribute(IPDELauncherConstants.LOCATION, LaunchArgumentsHelper.getDefaultWorkspaceLocation(wc.getName())); //$NON-NLS-1$
220
initializeProgramArguments(wc);
221         initializeVMArguments(wc);
222         wc.setAttribute(IPDELauncherConstants.USEFEATURES, false);
223         wc.setAttribute(IPDELauncherConstants.DOCLEAR, false);
224         wc.setAttribute(IPDELauncherConstants.ASKCLEAR, true);
225         wc.setAttribute(IPDEUIConstants.APPEND_ARGS_EXPLICITLY, true);
226         wc.setAttribute(IPDELauncherConstants.TRACING_CHECKED, IPDELauncherConstants.TRACING_NONE);
227         wc.setAttribute(IPDELauncherConstants.USE_DEFAULT, fApplicationName == null);
228         if (fApplicationName != null) {
229             String JavaDoc product = getProduct(fApplicationName);
230             if (product == null) {
231                 wc.setAttribute(IPDELauncherConstants.APPLICATION, fApplicationName);
232             } else {
233                 wc.setAttribute(IPDELauncherConstants.USE_PRODUCT, true);
234                 wc.setAttribute(IPDELauncherConstants.PRODUCT, product);
235             }
236             wc.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, false);
237             
238             StringBuffer JavaDoc wsplugins = new StringBuffer JavaDoc();
239             StringBuffer JavaDoc explugins = new StringBuffer JavaDoc();
240             Set JavaDoc plugins = DependencyManager.getSelfAndDependencies(fModel);
241             Iterator JavaDoc iter = plugins.iterator();
242             while (iter.hasNext()) {
243                 String JavaDoc id = iter.next().toString();
244                 IPluginModelBase model = PluginRegistry.findModel(id);
245                 if (model == null || !model.isEnabled())
246                     continue;
247                 if (model.getUnderlyingResource() == null) {
248                     if (explugins.length() > 0)
249                         explugins.append(","); //$NON-NLS-1$
250
explugins.append(id);
251                 } else {
252                     if (wsplugins.length() > 0)
253                         wsplugins.append(","); //$NON-NLS-1$
254
wsplugins.append(id);
255                 }
256             }
257             wc.setAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, wsplugins.toString());
258             wc.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, explugins.toString());
259         } else {
260             String JavaDoc defaultProduct = TargetPlatform.getDefaultProduct();
261             if (defaultProduct != null) {
262                 wc.setAttribute(IPDELauncherConstants.USE_DEFAULT, true);
263                 wc.setAttribute(IPDELauncherConstants.USE_PRODUCT, true);
264                 wc.setAttribute(IPDELauncherConstants.PRODUCT, defaultProduct);
265             }
266         }
267         wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, PDESourcePathProvider.ID);
268     }
269     
270     private void initializeProgramArguments(ILaunchConfigurationWorkingCopy wc) {
271         String JavaDoc programArgs = LaunchArgumentsHelper.getInitialProgramArguments();
272         if (programArgs.length() > 0)
273             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, programArgs);
274     }
275     
276     private void initializeVMArguments(ILaunchConfigurationWorkingCopy wc) {
277         String JavaDoc vmArgs = LaunchArgumentsHelper.getInitialVMArguments();
278         if (vmArgs.length() > 0)
279             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs);
280     }
281     
282     /**
283      * Returns the Eclipse application configuration type ID as declared in the plugin.xml
284      *
285      * @return the Eclipse application configuration type ID
286      */

287     protected String JavaDoc getLaunchConfigurationTypeName() {
288         return CONFIGURATION_TYPE;
289     }
290
291     /* (non-Javadoc)
292      * @see org.eclipse.pde.ui.launcher.AbstractLaunchShortcut#getName(org.eclipse.debug.core.ILaunchConfigurationType)
293      */

294     protected String JavaDoc getName(ILaunchConfigurationType type) {
295         // if launching default product, use default naming convention
296
if (fApplicationName == null)
297             return super.getName(type);
298         String JavaDoc product = getProduct(fApplicationName);
299         return (product == null) ? fApplicationName : product;
300     }
301 }
302
Popular Tags