KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > PDEManager


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.core;
12
13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.osgi.service.resolver.BundleDescription;
18 import org.eclipse.osgi.service.resolver.HostSpecification;
19 import org.eclipse.pde.core.plugin.IExtensions;
20 import org.eclipse.pde.core.plugin.IFragmentModel;
21 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
22 import org.eclipse.pde.core.plugin.IPluginModel;
23 import org.eclipse.pde.core.plugin.IPluginModelBase;
24 import org.eclipse.pde.core.plugin.PluginRegistry;
25 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
26 import org.eclipse.pde.internal.core.plugin.ExternalPluginModelBase;
27
28 public class PDEManager {
29
30     public static IFragmentModel[] findFragmentsFor(IPluginModelBase model) {
31         ArrayList JavaDoc result = new ArrayList JavaDoc();
32         BundleDescription desc = getBundleDescription(model);
33         if (desc != null) {
34             BundleDescription[] fragments = desc.getFragments();
35             for (int i = 0; i < fragments.length; i++) {
36                 IPluginModelBase candidate = PluginRegistry.findModel(fragments[i]);
37                 if (candidate instanceof IFragmentModel) {
38                     result.add(candidate);
39                 }
40             }
41         }
42         return (IFragmentModel[])result.toArray(new IFragmentModel[result.size()]);
43     }
44     
45     public static IPluginModel findHostFor(IFragmentModel fragment) {
46         BundleDescription desc = getBundleDescription(fragment);
47         if (desc != null) {
48             HostSpecification spec = desc.getHost();
49             if (spec != null) {
50                 IPluginModelBase host = PluginRegistry.findModel(spec.getName());
51                 if (host instanceof IPluginModel)
52                     return (IPluginModel)host;
53             }
54         }
55         return null;
56     }
57     
58     private static BundleDescription getBundleDescription(IPluginModelBase model) {
59         BundleDescription desc = model.getBundleDescription();
60         
61         if (desc == null && model.getUnderlyingResource() != null) {
62             // the model may be an editor model.
63
// editor models don't carry a bundle description
64
// get the core model counterpart.
65
IProject project = model.getUnderlyingResource().getProject();
66             IPluginModelBase coreModel = PluginRegistry.findModel(project);
67             if (coreModel != null)
68                 desc = coreModel.getBundleDescription();
69         }
70         return desc;
71     }
72     
73     public static URL JavaDoc[] getNLLookupLocations(IPluginModelBase model) {
74         ArrayList JavaDoc urls = new ArrayList JavaDoc();
75         addNLLocation(model, urls);
76         if (model instanceof IPluginModel) {
77             IFragmentModel[] fragments = findFragmentsFor(model);
78             for (int i = 0; i < fragments.length; i++) {
79                 addNLLocation(fragments[i], urls);
80             }
81         } else if (model instanceof IFragmentModel){
82             IPluginModel host = findHostFor((IFragmentModel)model);
83             if (host != null)
84                 addNLLocation(host, urls);
85         }
86         return (URL JavaDoc[])urls.toArray(new URL JavaDoc[urls.size()]);
87     }
88     
89     private static void addNLLocation(IPluginModelBase model, ArrayList JavaDoc urls) {
90         URL JavaDoc location = model.getNLLookupLocation();
91         if (location != null)
92             urls.add(location);
93     }
94     
95     public static String JavaDoc getBundleLocalization(IPluginModelBase model) {
96         if (model instanceof IBundlePluginModelBase && model.getUnderlyingResource() != null)
97                 return((IBundlePluginModelBase)model).getBundleLocalization();
98         
99         if (model instanceof ExternalPluginModelBase)
100             return ((ExternalPluginModelBase)model).getLocalization();
101         
102         return "plugin"; //$NON-NLS-1$
103
}
104     
105     public static IPluginExtensionPoint findExtensionPoint(String JavaDoc fullID) {
106         IPluginModelBase[] bases = PluginRegistry.getActiveModels();
107         for (int i = 0; i < bases.length; i++) {
108             IExtensions extensions = bases[i].getExtensions();
109             if (extensions == null)
110                 continue;
111             IPluginExtensionPoint[] points = extensions.getExtensionPoints();
112             for (int j = 0; j < points.length; j++) {
113                 if (fullID.equals(points[j].getFullId()))
114                     return points[j];
115             }
116         }
117         return null;
118     }
119
120 }
121
Popular Tags