KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > AboutInfo


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.ui.internal.ide;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.zip.CRC32 JavaDoc;
17 import java.util.zip.CheckedInputStream JavaDoc;
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 /**
30  * The information within this object is obtained from the about INI file.
31  * This file resides within an install configurations directory and must be a
32  * standard java property file.
33  * <p>
34  * This class is not intended to be instantiated or subclassed by clients.
35  * </p>
36  */

37 public final class AboutInfo {
38     private ProductProperties productProperties;
39
40     private BundleGroupProperties bundleGroupProperties;
41
42     private Long JavaDoc featureImageCRC;
43
44     private boolean calculatedImageCRC = false;
45
46     /**
47      * The information contained in this info will apply to only the argument product.
48      */

49     public AboutInfo(IProduct product) {
50         this.productProperties = new ProductProperties(product);
51     }
52
53     /**
54      * This info object will apply to the argument bundle group.
55      */

56     public AboutInfo(IBundleGroup bundleGroup) {
57         this.bundleGroupProperties = new BundleGroupProperties(bundleGroup);
58     }
59
60     /**
61      * Returns the configuration information for the feature with the given id.
62      *
63      * @param featureId
64      * the feature id
65      * @param versionId
66      * the version id (of the feature)
67      * @return the configuration information for the feature
68      */

69     public static AboutInfo readFeatureInfo(String JavaDoc featureId, String JavaDoc versionId) {
70         Assert.isNotNull(featureId);
71         Assert.isNotNull(versionId);
72
73         // first see if the id matches the product
74
IProduct product = Platform.getProduct();
75         if (product != null
76                 && featureId.equals(ProductProperties.getProductId(product))) {
77             return new AboutInfo(product);
78         }
79
80         // then check the bundle groups
81
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 JavaDoc id, String JavaDoc 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     /**
109      * Returns the descriptor for an image which can be shown in an "about" dialog
110      * for this product. Products designed to run "headless" typically would not
111      * have such an image.
112      *
113      * @return the descriptor for an about image, or <code>null</code> if none
114      */

115     public ImageDescriptor getAboutImage() {
116         return productProperties == null ? null : productProperties
117                 .getAboutImage();
118     }
119
120     /**
121      * Returns the descriptor for an image which can be shown in an "about features"
122      * dialog. Products designed to run "headless" typically would not have such an image.
123      *
124      * @return the descriptor for a feature image, or <code>null</code> if none
125      */

126     public ImageDescriptor getFeatureImage() {
127         return bundleGroupProperties == null ? null : bundleGroupProperties
128                 .getFeatureImage();
129     }
130
131     /**
132      * Returns the simple name of the feature image file.
133      *
134      * @return the simple name of the feature image file,
135      * or <code>null</code> if none
136      */

137     public String JavaDoc getFeatureImageName() {
138         if (bundleGroupProperties == null) {
139             return null;
140         }
141
142         URL JavaDoc url = bundleGroupProperties.getFeatureImageUrl();
143         return url == null ? null : new Path(url.getPath()).lastSegment();
144     }
145
146     /**
147      * Returns the CRC of the feature image as supplied in the properties file.
148      *
149      * @return the CRC of the feature image, or <code>null</code> if none
150      */

151     public Long JavaDoc 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     /**
166      * Calculate a CRC for the feature image
167      */

168     private static Long JavaDoc calculateImageCRC(URL JavaDoc url) {
169         if (url == null) {
170             return null;
171         }
172
173         InputStream JavaDoc in = null;
174         try {
175             CRC32 JavaDoc checksum = new CRC32 JavaDoc();
176             in = new CheckedInputStream JavaDoc(url.openStream(), checksum);
177
178             // the contents don't matter, the read just needs a place to go
179
byte[] sink = new byte[2048];
180             while (true) {
181                 if (in.read(sink) <= 0) {
182                     break;
183                 }
184             }
185
186             return new Long JavaDoc(checksum.getValue());
187         } catch (IOException JavaDoc e) {
188             return null;
189         } finally {
190             if (in != null) {
191                 try {
192                     in.close();
193                 } catch (IOException JavaDoc e) {
194                     // do nothing
195
}
196             }
197         }
198     }
199
200     /**
201      * Returns a label for the feature plugn, or <code>null</code>.
202      */

203     public String JavaDoc 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     /**
214      * Returns the id for this feature.
215      *
216      * @return the feature id
217      */

218     public String JavaDoc getFeatureId() {
219         String JavaDoc 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 : ""; //$NON-NLS-1$
226
}
227
228     /**
229      * Returns the text to show in an "about" dialog for this product.
230      * Products designed to run "headless" typically would not have such text.
231      *
232      * @return the about text, or <code>null</code> if none
233      */

234     public String JavaDoc getAboutText() {
235         return productProperties == null ? null : productProperties
236                 .getAboutText();
237     }
238
239     /**
240      * Returns the application name or <code>null</code>.
241      * Note this is never shown to the user.
242      * It is used to initialize the SWT Display.
243      * <p>
244      * On Motif, for example, this can be used
245      * to set the name used for resource lookup.
246      * </p>
247      *
248      * @return the application name, or <code>null</code>
249      *
250      * @see org.eclipse.swt.widgets.Display#setAppName
251      */

252     public String JavaDoc getAppName() {
253         return productProperties == null ? null : productProperties
254                 .getAppName();
255     }
256
257     /**
258      * Returns the product name or <code>null</code>.
259      * This is shown in the window title and the About action.
260      *
261      * @return the product name, or <code>null</code>
262      */

263     public String JavaDoc getProductName() {
264         return productProperties == null ? null : productProperties
265                 .getProductName();
266     }
267
268     /**
269      * Returns the provider name or <code>null</code>.
270      *
271      * @return the provider name, or <code>null</code>
272      */

273     public String JavaDoc getProviderName() {
274         return bundleGroupProperties == null ? null : bundleGroupProperties
275                 .getProviderName();
276     }
277
278     /**
279      * Returns the feature version id.
280      *
281      * @return the version id of the feature
282      */

283     public String JavaDoc getVersionId() {
284         return bundleGroupProperties == null ? "" : bundleGroupProperties.getFeatureVersion(); //$NON-NLS-1$
285
}
286
287     /**
288      * Returns a <code>URL</code> for the welcome page.
289      * Products designed to run "headless" typically would not have such an page.
290      *
291      * @return the welcome page, or <code>null</code> if none
292      */

293     public URL JavaDoc 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     /**
304      * Returns the ID of a perspective in which to show the welcome page.
305      * May be <code>null</code>.
306      *
307      * @return the welcome page perspective id, or <code>null</code> if none
308      */

309     public String JavaDoc getWelcomePerspectiveId() {
310         return bundleGroupProperties == null ? null : bundleGroupProperties
311                 .getWelcomePerspective();
312     }
313
314     /**
315      * Returns a <code>String</code> for the tips and trick href.
316      *
317      * @return the tips and tricks href, or <code>null</code> if none
318      */

319     public String JavaDoc getTipsAndTricksHref() {
320         return bundleGroupProperties == null ? null : bundleGroupProperties
321                 .getTipsAndTricksHref();
322     }
323
324     /**
325      * Return an array of image descriptors for the window images to use for
326      * this product. The expectations is that the elements will be the same
327      * image rendered at different sizes. Products designed to run "headless"
328      * typically would not have such images.
329      *
330      * @return an array of the image descriptors for the window images, or
331      * <code>null</code> if none
332      * @since 3.0
333      */

334     public ImageDescriptor[] getWindowImages() {
335         return productProperties == null ? null : productProperties
336                 .getWindowImages();
337     }
338 }
339
Popular Tags