KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.debug.core.ILaunchConfiguration;
19 import org.eclipse.osgi.service.resolver.BundleDescription;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.pde.core.plugin.IPluginAttribute;
22 import org.eclipse.pde.core.plugin.IPluginElement;
23 import org.eclipse.pde.core.plugin.IPluginExtension;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.core.plugin.PluginRegistry;
26 import org.eclipse.pde.core.plugin.TargetPlatform;
27 import org.eclipse.pde.internal.core.util.IdUtil;
28 import org.eclipse.pde.internal.ui.IPDEUIConstants;
29 import org.eclipse.pde.internal.ui.PDELabelProvider;
30 import org.eclipse.pde.internal.ui.PDEPlugin;
31 import org.eclipse.pde.internal.ui.PDEPluginImages;
32 import org.eclipse.pde.internal.ui.PDEUIMessages;
33 import org.eclipse.pde.internal.ui.elements.NamedElement;
34 import org.eclipse.pde.ui.launcher.EclipseLaunchShortcut;
35 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
36 import org.eclipse.swt.graphics.Image;
37
38 public class EclipsePluginValidationOperation extends LaunchValidationOperation {
39     
40     private Map JavaDoc fExtensionErrors = new HashMap JavaDoc(2);
41     private static Object JavaDoc[] EMPTY = new Object JavaDoc[0];
42     
43
44     public EclipsePluginValidationOperation(ILaunchConfiguration configuration) {
45         super(configuration);
46     }
47
48     protected IPluginModelBase[] getModels() throws CoreException{
49         return LaunchPluginValidator.getPluginList(fLaunchConfiguration);
50     }
51     
52     public void run(IProgressMonitor monitor) throws CoreException {
53         super.run(monitor);
54         if (fExtensionErrors.size() > 0)
55             fExtensionErrors.clear();
56         validateExtensions();
57     }
58     
59     private void validateExtensions() {
60         try {
61             if (fLaunchConfiguration.getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) {
62                 String JavaDoc product = fLaunchConfiguration.getAttribute(IPDELauncherConstants.PRODUCT, (String JavaDoc)null);
63                 if (product != null) {
64                     validateExtension(product);
65                     String JavaDoc application = getApplication(product);
66                     if (application != null)
67                         validateExtension(application);
68                 }
69             } else {
70                 String JavaDoc configType = fLaunchConfiguration.getType().getIdentifier();
71                 String JavaDoc attribute = configType.equals(EclipseLaunchShortcut.CONFIGURATION_TYPE)
72                                     ? IPDELauncherConstants.APPLICATION : IPDELauncherConstants.APP_TO_TEST;
73                 String JavaDoc application = fLaunchConfiguration.getAttribute(attribute, TargetPlatform.getDefaultApplication());
74                 if (!IPDEUIConstants.CORE_TEST_APPLICATION.equals(application)) {
75                     validateExtension(application);
76                 }
77             }
78         } catch (CoreException e) {
79             PDEPlugin.log(e);
80         }
81     }
82     
83     private String JavaDoc getApplication(String JavaDoc product) {
84         String JavaDoc bundleID = product.substring(0, product.lastIndexOf('.'));
85         BundleDescription bundle = getState().getBundle(bundleID, null);
86         if (bundle != null) {
87             IPluginModelBase model = PluginRegistry.findModel(bundle);
88             if (model != null) {
89                 IPluginExtension[] extensions = model.getPluginBase().getExtensions();
90                 for (int i = 0; i < extensions.length; i++) {
91                     IPluginExtension ext = extensions[i];
92                     String JavaDoc point = ext.getPoint();
93                     if ("org.eclipse.core.runtime.products".equals(point) //$NON-NLS-1$
94
&& product.equals(IdUtil.getFullId(ext))) {
95                         if (ext.getChildCount() == 1) {
96                             IPluginElement prod = (IPluginElement)ext.getChildren()[0];
97                             if (prod.getName().equals("product")) { //$NON-NLS-1$
98
IPluginAttribute attr = prod.getAttribute("application"); //$NON-NLS-1$
99
return attr != null ? attr.getValue() : null;
100                             }
101                         }
102                     }
103                 }
104             }
105         }
106         return null;
107     }
108         
109     private void validateExtension(String JavaDoc id) {
110         String JavaDoc bundleID = id.substring(0, id.lastIndexOf('.'));
111         BundleDescription bundle = getState().getBundle(bundleID, null);
112         if (bundle == null) {
113             String JavaDoc name = NLS.bind(PDEUIMessages.EclipsePluginValidationOperation_pluginMissing, bundleID);
114             PDELabelProvider provider = PDEPlugin.getDefault().getLabelProvider();
115             Image image = provider.get(PDEPluginImages.DESC_PLUGIN_OBJ);
116             fExtensionErrors.put(new NamedElement(name, image), EMPTY);
117         }
118     }
119     
120     public boolean hasErrors() {
121         return super.hasErrors() || fExtensionErrors.size() >= 1;
122     }
123     
124     public Map JavaDoc getInput() {
125         Map JavaDoc map = super.getInput();
126         map.putAll(fExtensionErrors);
127         return map;
128     }
129
130 }
131
Popular Tags