KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > runtime > registry > RegistryBrowserContentProvider


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.runtime.registry;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.core.runtime.IConfigurationElement;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.jface.viewers.ITreeContentProvider;
18 import org.eclipse.jface.viewers.TreeViewer;
19 import org.eclipse.jface.viewers.Viewer;
20 import org.eclipse.osgi.util.ManifestElement;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.pde.internal.runtime.PDERuntimeMessages;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.BundleException;
25 import org.osgi.framework.Constants;
26
27 public class RegistryBrowserContentProvider implements ITreeContentProvider {
28     private Hashtable JavaDoc fPluginMap = new Hashtable JavaDoc();
29     private boolean fShowRunning;
30     public boolean isInExtensionSet;
31     private TreeViewer fViewer;
32     private int fPluginsTotal;
33     
34     class BundleFolder implements IBundleFolder {
35         private int id;
36         private Bundle bundle;
37         public BundleFolder(Bundle pd, int id) {
38             this.bundle = pd;
39             this.id = id;
40         }
41         public Bundle getBundle() {
42             return bundle;
43         }
44         public Object JavaDoc[] getChildren() {
45             return getFolderChildren(bundle, id);
46         }
47         public int getFolderId() {
48             return id;
49         }
50         public Object JavaDoc getAdapter(Class JavaDoc key) {
51             return null;
52         }
53     }
54     
55     class BundlePrerequisite implements IBundlePrerequisite {
56         private ManifestElement underlyingElement;
57         public BundlePrerequisite(ManifestElement element) {
58             underlyingElement = element;
59         }
60         public ManifestElement getPrerequisite() {
61             return underlyingElement;
62         }
63         public boolean isExported() {
64             String JavaDoc visibility = underlyingElement.getDirective(Constants.VISIBILITY_DIRECTIVE);
65             return Constants.VISIBILITY_REEXPORT.equals(visibility);
66         }
67         public String JavaDoc getLabel() {
68             String JavaDoc version = underlyingElement.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE);
69             String JavaDoc value = underlyingElement.getValue();
70             if (version == null)
71                 return value;
72             if (Character.isDigit(version.charAt(0)))
73                 version = '(' + version + ')';
74             return value + ' ' + version;
75         }
76     }
77     
78     class BundleLibrary implements IBundleLibrary {
79         private ManifestElement underlyingElement;
80         public BundleLibrary(ManifestElement element) {
81             underlyingElement = element;
82         }
83         public String JavaDoc getLibrary() {
84             return underlyingElement.getValue();
85         }
86     }
87     
88     public RegistryBrowserContentProvider(TreeViewer viewer, boolean showRunning){
89         super();
90         this.fViewer = viewer;
91         this.fShowRunning = showRunning;
92         this.fPluginsTotal = 0;
93     }
94     
95     protected PluginObjectAdapter createAdapter(Object JavaDoc object, int id) {
96         if (id == IBundleFolder.F_EXTENSIONS)
97             return new ExtensionAdapter(object);
98         if (id == IBundleFolder.F_EXTENSION_POINTS)
99             return new ExtensionPointAdapter(object);
100         return new PluginObjectAdapter(object);
101     }
102     protected Object JavaDoc[] createPluginFolders(Bundle bundle) {
103         Object JavaDoc[] array = new Object JavaDoc[5];
104         array[0] = new BundleFolder(bundle, IBundleFolder.F_LOCATION);
105         array[1] = new BundleFolder(bundle, IBundleFolder.F_IMPORTS);
106         array[2] = new BundleFolder(bundle, IBundleFolder.F_LIBRARIES);
107         array[3] = new BundleFolder(bundle, IBundleFolder.F_EXTENSION_POINTS);
108         array[4] = new BundleFolder(bundle, IBundleFolder.F_EXTENSIONS);
109         return array;
110     }
111     
112     public void dispose() { }
113     
114     public Object JavaDoc[] getElements(Object JavaDoc element) {
115         return getChildren(element);
116     }
117     
118     public Object JavaDoc[] getChildren(Object JavaDoc element) {
119         if (element == null)
120             return null;
121         
122         if (element instanceof ExtensionAdapter)
123             return ((ExtensionAdapter) element).getChildren();
124         
125         isInExtensionSet = false;
126         if (element instanceof ExtensionPointAdapter)
127             return ((ExtensionPointAdapter) element).getChildren();
128         
129         if (element instanceof ConfigurationElementAdapter)
130             return ((ConfigurationElementAdapter) element).getChildren();
131         
132         if (element instanceof PluginObjectAdapter)
133             element = ((PluginObjectAdapter) element).getObject();
134         
135         if (element instanceof Bundle) {
136             Bundle bundle = (Bundle) element;
137             Object JavaDoc[] folders = (Object JavaDoc[]) fPluginMap.get(bundle.getSymbolicName());
138             if (folders == null) {
139                 folders = createPluginFolders(bundle);
140                 fPluginMap.put(bundle.getSymbolicName(), folders);
141             } else {
142                 ArrayList JavaDoc folderList = new ArrayList JavaDoc();
143                 for (int i = 0; i < folders.length; i++) {
144                     if (folders[i] != null
145                             && ((IBundleFolder)folders[i]).getChildren() != null
146                             || ((IBundleFolder)folders[i]).getFolderId() == IBundleFolder.F_LOCATION)
147                         folderList.add(folders[i]);
148                 }
149                 folders = folderList.toArray(new Object JavaDoc[folderList.size()]);
150             }
151             return folders;
152         }
153         if (element instanceof IBundleFolder) {
154             IBundleFolder folder = (IBundleFolder) element;
155             isInExtensionSet = folder.getFolderId() == IBundleFolder.F_EXTENSIONS;
156             return ((IBundleFolder) element).getChildren();
157         }
158         if (element instanceof IConfigurationElement) {
159             return ((IConfigurationElement) element).getChildren();
160         }
161         if (element instanceof Bundle[]) {
162             PluginObjectAdapter[] bundles = getPlugins((Bundle[])element);
163             fPluginsTotal = bundles.length;
164             
165             if (fShowRunning){
166                 ArrayList JavaDoc resultList = new ArrayList JavaDoc();
167                 for (int i = 0; i < bundles.length; i++)
168                     if (bundles[i].getObject() instanceof Bundle &&
169                             ((Bundle)bundles[i].getObject()).getState() == Bundle.ACTIVE)
170                         resultList.add(bundles[i]);
171                 return resultList.toArray(new Object JavaDoc[resultList.size()]);
172             }
173             return bundles;
174         }
175         return null;
176     }
177
178     public PluginObjectAdapter[] getPlugins(Bundle[] bundles) {
179         ArrayList JavaDoc list = new ArrayList JavaDoc();
180         for (int i = 0; i < bundles.length; i++)
181             if (bundles[i].getHeaders().get(Constants.FRAGMENT_HOST) == null)
182                 list.add(new PluginObjectAdapter(bundles[i]));
183         return (PluginObjectAdapter[]) list.toArray(new PluginObjectAdapter[list.size()]);
184     }
185     private Object JavaDoc[] getFolderChildren(Bundle bundle, int id) {
186         Object JavaDoc[] array = null;
187         String JavaDoc bundleId = bundle.getSymbolicName();
188         switch (id) {
189             case IBundleFolder.F_EXTENSIONS :
190                 array = Platform.getExtensionRegistry().getExtensions(bundleId);
191                 break;
192             case IBundleFolder.F_EXTENSION_POINTS :
193                 array = Platform.getExtensionRegistry().getExtensionPoints(bundleId);
194                 break;
195             case IBundleFolder.F_IMPORTS :
196                 array = getManifestHeaderArray(bundle, Constants.REQUIRE_BUNDLE);
197                 break;
198             case IBundleFolder.F_LIBRARIES :
199                 array = getManifestHeaderArray(bundle, Constants.BUNDLE_CLASSPATH);
200                 break;
201         }
202         Object JavaDoc[] result = null;
203         if (array != null && array.length > 0) {
204             result = new Object JavaDoc[array.length];
205             for (int i = 0; i < array.length; i++) {
206                 result[i] = createAdapter(array[i], id);
207             }
208         }
209         return result;
210     }
211     public Object JavaDoc getParent(Object JavaDoc element) {
212         return null;
213     }
214     public boolean hasChildren(Object JavaDoc element) {
215         Object JavaDoc[] children = getChildren(element);
216         return children != null && children.length > 0;
217     }
218     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
219     }
220     public void setShowRunning(boolean showRunning){
221         this.fShowRunning = showRunning;
222     }
223     public boolean isShowRunning(){
224         return fShowRunning;
225     }
226     
227     public String JavaDoc getTitleSummary(){
228         if (fViewer == null || fViewer.getTree() == null)
229             return NLS.bind(PDERuntimeMessages.RegistryView_titleSummary, (new String JavaDoc[] {"0", "0"})); //$NON-NLS-1$ //$NON-NLS-2$
230

231         return NLS.bind(PDERuntimeMessages.RegistryView_titleSummary, (new String JavaDoc[] {new Integer JavaDoc(fViewer.getTree().getItemCount()).toString(), new Integer JavaDoc(fPluginsTotal).toString()}));
232     }
233     
234     private Object JavaDoc[] getManifestHeaderArray(Bundle bundle, String JavaDoc headerKey) {
235         String JavaDoc libraries = (String JavaDoc)bundle.getHeaders().get(headerKey);
236         try {
237             ManifestElement[] elements = ManifestElement.parseHeader(headerKey, libraries);
238             if (elements == null)
239                 return null;
240             if (headerKey.equals(Constants.BUNDLE_CLASSPATH)) {
241                 IBundleLibrary[] array = new IBundleLibrary[elements.length];
242                 for (int i = 0; i < elements.length; i++)
243                     array[i] = new BundleLibrary(elements[i]);
244                 return array;
245             } else if (headerKey.equals(Constants.REQUIRE_BUNDLE)) {
246                 IBundlePrerequisite[] array = new IBundlePrerequisite[elements.length];
247                 for (int i = 0; i < elements.length; i++)
248                     array[i] = new BundlePrerequisite(elements[i]);
249                 return array;
250             }
251         } catch (BundleException e) {
252         }
253         return null;
254     }
255     
256 }
257
Popular Tags