1 11 package org.eclipse.ui.internal.ide; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 import java.util.zip.CRC32 ; 17 import java.util.zip.CheckedInputStream ; 18 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.IBundleGroup; 21 import org.eclipse.core.runtime.IBundleGroupProvider; 22 import org.eclipse.core.runtime.IProduct; 23 import org.eclipse.core.runtime.Path; 24 import org.eclipse.core.runtime.Platform; 25 import org.eclipse.jface.resource.ImageDescriptor; 26 import org.eclipse.ui.internal.BundleGroupProperties; 27 import org.eclipse.ui.internal.ProductProperties; 28 29 37 public final class AboutInfo { 38 private ProductProperties productProperties; 39 40 private BundleGroupProperties bundleGroupProperties; 41 42 private Long featureImageCRC; 43 44 private boolean calculatedImageCRC = false; 45 46 49 public AboutInfo(IProduct product) { 50 this.productProperties = new ProductProperties(product); 51 } 52 53 56 public AboutInfo(IBundleGroup bundleGroup) { 57 this.bundleGroupProperties = new BundleGroupProperties(bundleGroup); 58 } 59 60 69 public static AboutInfo readFeatureInfo(String featureId, String versionId) { 70 Assert.isNotNull(featureId); 71 Assert.isNotNull(versionId); 72 73 IProduct product = Platform.getProduct(); 75 if (product != null 76 && featureId.equals(ProductProperties.getProductId(product))) { 77 return new AboutInfo(product); 78 } 79 80 IBundleGroup bundleGroup = getBundleGroup(featureId, versionId); 82 if (bundleGroup != null) { 83 return new AboutInfo(bundleGroup); 84 } 85 86 return null; 87 } 88 89 private static IBundleGroup getBundleGroup(String id, String versionId) { 90 if (id == null || versionId == null) { 91 return null; 92 } 93 94 IBundleGroupProvider[] providers = Platform.getBundleGroupProviders(); 95 for (int p = 0; p < providers.length; ++p) { 96 IBundleGroup[] groups = providers[p].getBundleGroups(); 97 for (int g = 0; g < groups.length; ++g) { 98 if (id.equals(groups[g].getIdentifier()) 99 && versionId.equals(groups[g].getVersion())) { 100 return groups[g]; 101 } 102 } 103 } 104 105 return null; 106 } 107 108 115 public ImageDescriptor getAboutImage() { 116 return productProperties == null ? null : productProperties 117 .getAboutImage(); 118 } 119 120 126 public ImageDescriptor getFeatureImage() { 127 return bundleGroupProperties == null ? null : bundleGroupProperties 128 .getFeatureImage(); 129 } 130 131 137 public String getFeatureImageName() { 138 if (bundleGroupProperties == null) { 139 return null; 140 } 141 142 URL url = bundleGroupProperties.getFeatureImageUrl(); 143 return url == null ? null : new Path(url.getPath()).lastSegment(); 144 } 145 146 151 public Long getFeatureImageCRC() { 152 if (bundleGroupProperties == null) { 153 return null; 154 } 155 156 if (!calculatedImageCRC) { 157 featureImageCRC = calculateImageCRC(bundleGroupProperties 158 .getFeatureImageUrl()); 159 calculatedImageCRC = featureImageCRC != null; 160 } 161 162 return featureImageCRC; 163 } 164 165 168 private static Long calculateImageCRC(URL url) { 169 if (url == null) { 170 return null; 171 } 172 173 InputStream in = null; 174 try { 175 CRC32 checksum = new CRC32 (); 176 in = new CheckedInputStream (url.openStream(), checksum); 177 178 byte[] sink = new byte[2048]; 180 while (true) { 181 if (in.read(sink) <= 0) { 182 break; 183 } 184 } 185 186 return new Long (checksum.getValue()); 187 } catch (IOException e) { 188 return null; 189 } finally { 190 if (in != null) { 191 try { 192 in.close(); 193 } catch (IOException e) { 194 } 196 } 197 } 198 } 199 200 203 public String getFeatureLabel() { 204 if (productProperties != null) { 205 return productProperties.getProductName(); 206 } 207 if (bundleGroupProperties != null) { 208 return bundleGroupProperties.getFeatureLabel(); 209 } 210 return null; 211 } 212 213 218 public String getFeatureId() { 219 String id = null; 220 if (productProperties != null) { 221 id = productProperties.getProductId(); 222 } else if (bundleGroupProperties != null) { 223 id = bundleGroupProperties.getFeatureId(); 224 } 225 return id != null ? id : ""; } 227 228 234 public String getAboutText() { 235 return productProperties == null ? null : productProperties 236 .getAboutText(); 237 } 238 239 252 public String getAppName() { 253 return productProperties == null ? null : productProperties 254 .getAppName(); 255 } 256 257 263 public String getProductName() { 264 return productProperties == null ? null : productProperties 265 .getProductName(); 266 } 267 268 273 public String getProviderName() { 274 return bundleGroupProperties == null ? null : bundleGroupProperties 275 .getProviderName(); 276 } 277 278 283 public String getVersionId() { 284 return bundleGroupProperties == null ? "" : bundleGroupProperties.getFeatureVersion(); } 286 287 293 public URL getWelcomePageURL() { 294 if (productProperties != null) { 295 return productProperties.getWelcomePageUrl(); 296 } 297 if (bundleGroupProperties != null) { 298 return bundleGroupProperties.getWelcomePageUrl(); 299 } 300 return null; 301 } 302 303 309 public String getWelcomePerspectiveId() { 310 return bundleGroupProperties == null ? null : bundleGroupProperties 311 .getWelcomePerspective(); 312 } 313 314 319 public String getTipsAndTricksHref() { 320 return bundleGroupProperties == null ? null : bundleGroupProperties 321 .getTipsAndTricksHref(); 322 } 323 324 334 public ImageDescriptor[] getWindowImages() { 335 return productProperties == null ? null : productProperties 336 .getWindowImages(); 337 } 338 } 339 | Popular Tags |