1 11 package org.eclipse.pde.internal.runtime.registry; 12 import java.util.ArrayList ; 13 import java.util.Hashtable ; 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 fPluginMap = new Hashtable (); 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 [] getChildren() { 45 return getFolderChildren(bundle, id); 46 } 47 public int getFolderId() { 48 return id; 49 } 50 public Object getAdapter(Class 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 visibility = underlyingElement.getDirective(Constants.VISIBILITY_DIRECTIVE); 65 return Constants.VISIBILITY_REEXPORT.equals(visibility); 66 } 67 public String getLabel() { 68 String version = underlyingElement.getAttribute(Constants.BUNDLE_VERSION_ATTRIBUTE); 69 String 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 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 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 [] createPluginFolders(Bundle bundle) { 103 Object [] array = new Object [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 [] getElements(Object element) { 115 return getChildren(element); 116 } 117 118 public Object [] getChildren(Object 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 [] folders = (Object []) fPluginMap.get(bundle.getSymbolicName()); 138 if (folders == null) { 139 folders = createPluginFolders(bundle); 140 fPluginMap.put(bundle.getSymbolicName(), folders); 141 } else { 142 ArrayList folderList = new ArrayList (); 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 [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 resultList = new ArrayList (); 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 [resultList.size()]); 172 } 173 return bundles; 174 } 175 return null; 176 } 177 178 public PluginObjectAdapter[] getPlugins(Bundle[] bundles) { 179 ArrayList list = new ArrayList (); 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 [] 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(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 [] result = null; 203 if (array != null && array.length > 0) { 204 result = new Object [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 getParent(Object element) { 212 return null; 213 } 214 public boolean hasChildren(Object element) { 215 Object [] children = getChildren(element); 216 return children != null && children.length > 0; 217 } 218 public void inputChanged(Viewer viewer, Object oldInput, Object 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 getTitleSummary(){ 228 if (fViewer == null || fViewer.getTree() == null) 229 return NLS.bind(PDERuntimeMessages.RegistryView_titleSummary, (new String [] {"0", "0"})); 231 return NLS.bind(PDERuntimeMessages.RegistryView_titleSummary, (new String [] {new Integer (fViewer.getTree().getItemCount()).toString(), new Integer (fPluginsTotal).toString()})); 232 } 233 234 private Object [] getManifestHeaderArray(Bundle bundle, String headerKey) { 235 String libraries = (String )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 |