KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ProductProperties


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16 import com.ibm.icu.text.MessageFormat;
17 import java.util.ArrayList JavaDoc;
18 import java.util.MissingResourceException JavaDoc;
19 import java.util.PropertyResourceBundle JavaDoc;
20
21 import org.eclipse.core.runtime.IProduct;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.ui.branding.IProductConstants;
26
27 /**
28  * A class that converts the strings returned by
29  * <code>org.eclipse.core.runtime.IProduct.getProperty</code> to the
30  * appropriate class. This implementation is tightly bound to the properties
31  * provided in IProductConstants. Clients adding their own properties could
32  * choose to subclass this.
33  *
34  * @see org.eclipse.ui.branding.IProductConstants
35  * @since 3.0
36  */

37 public class ProductProperties extends BrandingProperties implements
38         IProductConstants {
39
40     private final IProduct product;
41
42     private String JavaDoc appName;
43
44     private String JavaDoc aboutText;
45
46     private ImageDescriptor aboutImageDescriptor;
47
48     private ImageDescriptor[] windowImageDescriptors;
49
50     private URL JavaDoc welcomePageUrl;
51
52     private String JavaDoc productName;
53
54     private String JavaDoc productId;
55
56     private static final String JavaDoc ABOUT_MAPPINGS = "$nl$/about.mappings"; //$NON-NLS-1$
57

58     private static String JavaDoc[] systemPropertiesKeys = new String JavaDoc[0];
59     private static String JavaDoc[] mappings = loadMappings();
60
61     private static String JavaDoc[] loadMappings() {
62         IProduct product = Platform.getProduct();
63         if (product == null) {
64             return new String JavaDoc[0];
65         }
66         URL JavaDoc location = Platform.find(product.getDefiningBundle(), new Path(
67                 ABOUT_MAPPINGS));
68         PropertyResourceBundle JavaDoc bundle = null;
69         InputStream JavaDoc is;
70         if (location != null) {
71             is = null;
72             try {
73                 is = location.openStream();
74                 bundle = new PropertyResourceBundle JavaDoc(is);
75             } catch (IOException JavaDoc e) {
76                 bundle = null;
77             } finally {
78                 try {
79                     if (is != null) {
80                         is.close();
81                     }
82                 } catch (IOException JavaDoc e) {
83                     // do nothing if we fail to close
84
}
85             }
86         }
87
88         ArrayList JavaDoc mappingsList = new ArrayList JavaDoc();
89         if (bundle != null) {
90             boolean found = true;
91             int i = 0;
92             ArrayList JavaDoc systemPropertiesKeysList = new ArrayList JavaDoc();
93             while (found) {
94                 try {
95                     String JavaDoc nextString = bundle.getString(Integer.toString(i));
96                     int length = nextString.length();
97                     /*
98                      * Check if the mapping value is a system property, specified
99                      * by '$' at the beginning and end of the string. If so, add
100                      * the key to the systemPropertiesKeys array and insert array
101                      * indices "{i}" to allow the string to be formatted again with
102                      * the new system property values.
103                      */

104                     if (length > 2 && nextString.indexOf('$') == 0 && nextString.lastIndexOf('$') == length - 1) {
105                         int newIndex = systemPropertiesKeysList.size();
106                         systemPropertiesKeysList.add(nextString.substring(1, length-1));
107                         nextString = "{" + newIndex + "}"; //$NON-NLS-1$ //$NON-NLS-2$
108
}
109                     mappingsList.add(nextString);
110                 } catch (MissingResourceException JavaDoc e) {
111                     found = false;
112                 }
113                 i++;
114             }
115             systemPropertiesKeys = (String JavaDoc[]) systemPropertiesKeysList.toArray(new String JavaDoc[systemPropertiesKeysList.size()]) ;
116         }
117         return (String JavaDoc[]) mappingsList.toArray(new String JavaDoc[mappingsList.size()]);
118     }
119
120     /**
121      * This instance will return properties from the given product. The properties are
122      * retrieved in a lazy fashion and cached for later retrieval.
123      * @param product must not be null
124      */

125     public ProductProperties(IProduct product) {
126         if (product == null) {
127             throw new IllegalArgumentException JavaDoc();
128         }
129         this.product = product;
130     }
131
132     /**
133      * The application name, used to initialize the SWT Display. This
134      * value is distinct from the string displayed in the application
135      * title bar.
136      * <p>
137      * E.g., On motif, this can be used to set the name used for
138      * resource lookup.
139      * </p>
140      * @see org.eclipse.swt.widgets.Display#setAppName
141      */

142     public String JavaDoc getAppName() {
143         if (appName == null) {
144             appName = getAppName(product);
145         }
146         return appName;
147     }
148
149     /**
150      * The text to show in an "about" dialog for this product.
151      * Products designed to run "headless" typically would not
152      * have such text.
153      */

154     public String JavaDoc getAboutText() {
155         if (aboutText == null) {
156             aboutText = getAboutText(product);
157         }
158         return aboutText;
159     }
160
161     /**
162      * An image which can be shown in an "about" dialog for this
163      * product. Products designed to run "headless" typically would not
164      * have such an image.
165      * <p>
166      * A full-sized product image (no larger than 500x330 pixels) is
167      * shown without the "aboutText" blurb. A half-sized product image
168      * (no larger than 250x330 pixels) is shown with the "aboutText"
169      * blurb beside it.
170      */

171     public ImageDescriptor getAboutImage() {
172         if (aboutImageDescriptor == null) {
173             aboutImageDescriptor = getAboutImage(product);
174         }
175         return aboutImageDescriptor;
176     }
177
178     /**
179      * An array of one or more images to be used for this product. The
180      * expectation is that the array will contain the same image rendered
181      * at different sizes (16x16 and 32x32).
182      * Products designed to run "headless" typically would not have such images.
183      * <p>
184      * If this property is given, then it supercedes <code>WINDOW_IMAGE</code>.
185      * </p>
186      */

187     public ImageDescriptor[] getWindowImages() {
188         if (windowImageDescriptors == null) {
189             windowImageDescriptors = getWindowImages(product);
190         }
191         return windowImageDescriptors;
192     }
193
194     /**
195      * Location of the product's welcome page (special XML-based format), either
196      * a fully qualified valid URL or a path relative to the product's defining
197      * bundle. Products designed to run "headless" typically would not have such
198      * a page. Use of this property is discouraged in 3.0, the new
199      * org.eclipse.ui.intro extension point should be used instead.
200      */

201     public URL JavaDoc getWelcomePageUrl() {
202         if (welcomePageUrl == null) {
203             welcomePageUrl = getWelcomePageUrl(product);
204         }
205         return welcomePageUrl;
206     }
207
208     /**
209      * Returns the product name or <code>null</code>.
210      * This is shown in the window title and the About action.
211      */

212     public String JavaDoc getProductName() {
213         if (productName == null) {
214             productName = getProductName(product);
215         }
216         return productName;
217     }
218
219     /**
220      * Returns the id for the product or <code>null</code> if none.
221      */

222     public String JavaDoc getProductId() {
223         if (productId == null) {
224             productId = getProductId(product);
225         }
226         return productId;
227     }
228
229     /**
230      * The application name, used to initialize the SWT Display. This
231      * value is distinct from the string displayed in the application
232      * title bar.
233      * <p>
234      * E.g., On motif, this can be used to set the name used for
235      * resource lookup.
236      * </p>
237      * <p>
238      * The returned value will have {n} values substituted based on the
239      * current product's mappings regardless of the given product argument.
240      * </p>
241      * @see org.eclipse.swt.widgets.Display#setAppName
242      */

243     public static String JavaDoc getAppName(IProduct product) {
244         String JavaDoc property = product.getProperty(APP_NAME);
245         if (property == null) {
246             return ""; //$NON-NLS-1$
247
}
248         if (property.indexOf('{') == -1) {
249             return property;
250         }
251         return MessageFormat.format(property, mappings);
252     }
253
254     /**
255      * The text to show in an "about" dialog for this product.
256      * Products designed to run "headless" typically would not
257      * have such text.
258      * <p>
259      * The returned value will have {n} values substituted based on the
260      * current product's mappings regardless of the given product argument.
261      * </p>
262      */

263     public static String JavaDoc getAboutText(IProduct product) {
264         String JavaDoc property = product.getProperty(ABOUT_TEXT);
265         if (property == null) {
266             return ""; //$NON-NLS-1$
267
}
268         if (property.indexOf('{') == -1) {
269             return property;
270         }
271         property = MessageFormat.format(property, mappings);
272         
273         /*
274          * If there is still a "{" character, check if there are
275          * System properties in the systemPropertiesKeys array that
276          * need to be loaded.
277          */

278         if (property.indexOf('{') == -1) {
279             return property;
280         }
281         
282         if (systemPropertiesKeys.length == 0)
283             return property;
284         
285         /*
286          * Create a String array of the actual values to be mapped
287          * to the system properties keys.
288          */

289         String JavaDoc[] systemPropertiesMappings = new String JavaDoc[systemPropertiesKeys.length];
290         for (int i=0; i < systemPropertiesKeys.length; i++){
291             String JavaDoc systemProperty = systemPropertiesKeys[i];
292             // If system property is not set, insert an empty String
293
systemPropertiesMappings[i] = System.getProperty(systemProperty, ""); //$NON-NLS-1$
294
}
295         /*
296          * Format string with the system properties values.
297          */

298         return MessageFormat.format(property, systemPropertiesMappings);
299     }
300
301     /**
302      * An image which can be shown in an "about" dialog for this
303      * product. Products designed to run "headless" typically would not
304      * have such an image.
305      * <p>
306      * A full-sized product image (no larger than 500x330 pixels) is
307      * shown without the "aboutText" blurb. A half-sized product image
308      * (no larger than 250x330 pixels) is shown with the "aboutText"
309      * blurb beside it.
310      */

311     public static ImageDescriptor getAboutImage(IProduct product) {
312         return getImage(product.getProperty(ABOUT_IMAGE), product
313                 .getDefiningBundle());
314     }
315
316     /**
317      * An array of one or more images to be used for this product. The
318      * expectation is that the array will contain the same image rendered
319      * at different sizes (16x16 and 32x32).
320      * Products designed to run "headless" typically would not have such images.
321      * <p>
322      * If this property is given, then it supercedes <code>WINDOW_IMAGE</code>.
323      * </p>
324      */

325     public static ImageDescriptor[] getWindowImages(IProduct product) {
326         String JavaDoc property = product.getProperty(WINDOW_IMAGES);
327
328         // for compatibility with pre-3.0 plugins that may still use WINDOW_IMAGE
329
if (property == null) {
330             property = product.getProperty(WINDOW_IMAGE);
331         }
332
333         return getImages(property, product.getDefiningBundle());
334     }
335
336     /**
337      * Location of the product's welcome page (special XML-based format), either
338      * a fully qualified valid URL or a path relative to the product's defining
339      * bundle. Products designed to run "headless" typically would not have such
340      * a page. Use of this property is discouraged in 3.0, the new
341      * org.eclipse.ui.intro extension point should be used instead.
342      */

343     public static URL JavaDoc getWelcomePageUrl(IProduct product) {
344         return getUrl(product.getProperty(WELCOME_PAGE), product
345                 .getDefiningBundle());
346     }
347
348     /**
349      * Returns the product name or <code>null</code>.
350      * This is shown in the window title and the About action.
351      */

352     public static String JavaDoc getProductName(IProduct product) {
353         return product.getName();
354     }
355
356     /**
357      * Returns the id for the product.
358      */

359     public static String JavaDoc getProductId(IProduct product) {
360         return product.getId();
361     }
362 }
363
Popular Tags