001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.runtime.registry;
011:
012: import java.util.ArrayList;
013: import java.util.Hashtable;
014:
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.Platform;
017: import org.eclipse.jface.viewers.ITreeContentProvider;
018: import org.eclipse.jface.viewers.TreeViewer;
019: import org.eclipse.jface.viewers.Viewer;
020: import org.eclipse.osgi.util.ManifestElement;
021: import org.eclipse.osgi.util.NLS;
022: import org.eclipse.pde.internal.runtime.PDERuntimeMessages;
023: import org.osgi.framework.Bundle;
024: import org.osgi.framework.BundleException;
025: import org.osgi.framework.Constants;
026:
027: public class RegistryBrowserContentProvider implements
028: ITreeContentProvider {
029: private Hashtable fPluginMap = new Hashtable();
030: public boolean isInExtensionSet;
031: private TreeViewer fViewer;
032:
033: class BundleFolder implements IBundleFolder {
034: private int id;
035: private Bundle bundle;
036:
037: public BundleFolder(Bundle pd, int id) {
038: this .bundle = pd;
039: this .id = id;
040: }
041:
042: public Bundle getBundle() {
043: return bundle;
044: }
045:
046: public Object[] getChildren() {
047: return getFolderChildren(bundle, id);
048: }
049:
050: public int getFolderId() {
051: return id;
052: }
053:
054: public Object getAdapter(Class key) {
055: return null;
056: }
057: }
058:
059: class BundlePrerequisite implements IBundlePrerequisite {
060: private ManifestElement underlyingElement;
061:
062: public BundlePrerequisite(ManifestElement element) {
063: underlyingElement = element;
064: }
065:
066: public ManifestElement getPrerequisite() {
067: return underlyingElement;
068: }
069:
070: public boolean isExported() {
071: String visibility = underlyingElement
072: .getDirective(Constants.VISIBILITY_DIRECTIVE);
073: return Constants.VISIBILITY_REEXPORT.equals(visibility);
074: }
075:
076: public String getLabel() {
077: String version = underlyingElement
078: .getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE);
079: String value = underlyingElement.getValue();
080: if (version == null)
081: return value;
082: if (Character.isDigit(version.charAt(0)))
083: version = '(' + version + ')';
084: return value + ' ' + version;
085: }
086: }
087:
088: class BundleLibrary implements IBundleLibrary {
089: private ManifestElement underlyingElement;
090:
091: public BundleLibrary(ManifestElement element) {
092: underlyingElement = element;
093: }
094:
095: public String getLibrary() {
096: return underlyingElement.getValue();
097: }
098: }
099:
100: public RegistryBrowserContentProvider(TreeViewer viewer) {
101: super ();
102: this .fViewer = viewer;
103: }
104:
105: protected PluginObjectAdapter createAdapter(Object object, int id) {
106: if (id == IBundleFolder.F_EXTENSIONS)
107: return new ExtensionAdapter(object);
108: if (id == IBundleFolder.F_EXTENSION_POINTS)
109: return new ExtensionPointAdapter(object);
110: return new PluginObjectAdapter(object);
111: }
112:
113: protected Object[] createPluginFolders(Bundle bundle) {
114: Object[] array = new Object[5];
115: array[0] = new BundleFolder(bundle, IBundleFolder.F_LOCATION);
116: array[1] = new BundleFolder(bundle, IBundleFolder.F_IMPORTS);
117: array[2] = new BundleFolder(bundle, IBundleFolder.F_LIBRARIES);
118: array[3] = new BundleFolder(bundle,
119: IBundleFolder.F_EXTENSION_POINTS);
120: array[4] = new BundleFolder(bundle, IBundleFolder.F_EXTENSIONS);
121: return array;
122: }
123:
124: public void dispose() {
125: }
126:
127: public Object[] getElements(Object element) {
128: return getChildren(element);
129: }
130:
131: public Object[] getChildren(Object element) {
132: if (element == null)
133: return null;
134:
135: if (element instanceof ExtensionAdapter)
136: return ((ExtensionAdapter) element).getChildren();
137:
138: isInExtensionSet = false;
139: if (element instanceof ExtensionPointAdapter)
140: return ((ExtensionPointAdapter) element).getChildren();
141:
142: if (element instanceof ConfigurationElementAdapter)
143: return ((ConfigurationElementAdapter) element)
144: .getChildren();
145:
146: if (element instanceof PluginObjectAdapter)
147: element = ((PluginObjectAdapter) element).getObject();
148:
149: if (element instanceof Bundle) {
150: Bundle bundle = (Bundle) element;
151: String bundleID = new Long(bundle.getBundleId()).toString();
152: Object[] folders = (Object[]) fPluginMap.get(bundleID);
153: if (folders == null) {
154: folders = createPluginFolders(bundle);
155: fPluginMap.put(bundleID, folders);
156: } else {
157: ArrayList folderList = new ArrayList();
158: for (int i = 0; i < folders.length; i++) {
159: if (folders[i] != null
160: && ((IBundleFolder) folders[i])
161: .getChildren() != null
162: || ((IBundleFolder) folders[i])
163: .getFolderId() == IBundleFolder.F_LOCATION)
164: folderList.add(folders[i]);
165: }
166: folders = folderList.toArray(new Object[folderList
167: .size()]);
168: }
169: return folders;
170: }
171: if (element instanceof IBundleFolder) {
172: IBundleFolder folder = (IBundleFolder) element;
173: isInExtensionSet = folder.getFolderId() == IBundleFolder.F_EXTENSIONS;
174: return ((IBundleFolder) element).getChildren();
175: }
176: if (element instanceof IConfigurationElement) {
177: return ((IConfigurationElement) element).getChildren();
178: }
179: if (element instanceof PluginObjectAdapter[]) {
180: return (PluginObjectAdapter[]) element;
181: }
182: return null;
183: }
184:
185: private Object[] getFolderChildren(Bundle bundle, int id) {
186: Object[] array = null;
187: String bundleId = bundle.getSymbolicName();
188: switch (id) {
189: case IBundleFolder.F_EXTENSIONS:
190: array = Platform.getExtensionRegistry().getExtensions(
191: bundleId);
192: break;
193: case IBundleFolder.F_EXTENSION_POINTS:
194: array = Platform.getExtensionRegistry().getExtensionPoints(
195: bundleId);
196: break;
197: case IBundleFolder.F_IMPORTS:
198: array = getManifestHeaderArray(bundle,
199: Constants.REQUIRE_BUNDLE);
200: break;
201: case IBundleFolder.F_LIBRARIES:
202: array = getManifestHeaderArray(bundle,
203: Constants.BUNDLE_CLASSPATH);
204: break;
205: }
206: Object[] result = null;
207: if (array != null && array.length > 0) {
208: result = new Object[array.length];
209: for (int i = 0; i < array.length; i++) {
210: result[i] = createAdapter(array[i], id);
211: }
212: }
213: return result;
214: }
215:
216: public Object getParent(Object element) {
217: return null;
218: }
219:
220: public boolean hasChildren(Object element) {
221: Object[] children = getChildren(element);
222: return children != null && children.length > 0;
223: }
224:
225: public void inputChanged(Viewer viewer, Object oldInput,
226: Object newInput) {
227: }
228:
229: public String getTitleSummary(int bundleCount) {
230: if (fViewer == null || fViewer.getTree() == null)
231: return NLS.bind(
232: PDERuntimeMessages.RegistryView_titleSummary,
233: (new String[] { "0", "0" })); //$NON-NLS-1$ //$NON-NLS-2$
234: return NLS.bind(PDERuntimeMessages.RegistryView_titleSummary,
235: (new String[] {
236: Integer.toString(fViewer.getTree()
237: .getItemCount()),
238: Integer.toString(bundleCount) }));
239: }
240:
241: private Object[] getManifestHeaderArray(Bundle bundle,
242: String headerKey) {
243: String libraries = (String) bundle.getHeaders().get(headerKey);
244: try {
245: ManifestElement[] elements = ManifestElement.parseHeader(
246: headerKey, libraries);
247: if (elements == null)
248: return null;
249: if (headerKey.equals(Constants.BUNDLE_CLASSPATH)) {
250: IBundleLibrary[] array = new IBundleLibrary[elements.length];
251: for (int i = 0; i < elements.length; i++)
252: array[i] = new BundleLibrary(elements[i]);
253: return array;
254: } else if (headerKey.equals(Constants.REQUIRE_BUNDLE)) {
255: IBundlePrerequisite[] array = new IBundlePrerequisite[elements.length];
256: for (int i = 0; i < elements.length; i++)
257: array[i] = new BundlePrerequisite(elements[i]);
258: return array;
259: }
260: } catch (BundleException e) {
261: }
262: return null;
263: }
264:
265: }
|