KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.osgi.framework.Bundle;
22
23 /**
24  * The branding properties are retrieved as strings, but often used as other
25  * types (e.g., <code>java.net.URL</code>s. This class provides some utility
26  * functions for converting the string values to these well known classes. This
27  * may be subclassed by clients that use more than just these few types.
28  */

29 public abstract class BrandingProperties {
30
31     /**
32      * Create an url from the argument absolute or relative path. The bundle
33      * parameter is used as the base for relative paths and is allowed to be
34      * null.
35      *
36      * @param value
37      * the absolute or relative path
38      * @param definingBundle
39      * bundle to be used for relative paths (may be null)
40      * @return
41      */

42     public static URL JavaDoc getUrl(String JavaDoc value, Bundle definingBundle) {
43         try {
44             if (value != null) {
45                 return new URL JavaDoc(value);
46             }
47         } catch (MalformedURLException JavaDoc e) {
48             if (definingBundle != null) {
49                 return Platform.find(definingBundle, new Path(value));
50             }
51         }
52
53         return null;
54     }
55
56     /**
57      * Create a descriptor from the argument absolute or relative path to an
58      * image file. bundle parameter is used as the base for relative paths and
59      * is allowed to be null.
60      *
61      * @param value
62      * the absolute or relative path
63      * @param definingBundle
64      * bundle to be used for relative paths (may be null)
65      * @return
66      */

67     public static ImageDescriptor getImage(String JavaDoc value, Bundle definingBundle) {
68         URL JavaDoc url = getUrl(value, definingBundle);
69         return url == null ? null : ImageDescriptor.createFromURL(url);
70     }
71
72     /**
73      * Returns a array of URL for the given property or <code>null</code>.
74      * The property value should be a comma separated list of urls (either
75      * absolute or relative to the argument bundle). Tokens that do not
76      * represent a valid url will be represented with a null entry in the
77      * returned array.
78      *
79      * @param value
80      * value of a property that contains a comma-separated list of
81      * product relative urls
82      * @param definingBundle
83      * bundle to be used as base for relative paths (may be null)
84      * @return a URL for the given property, or <code>null</code>
85      */

86     public static URL JavaDoc[] getURLs(String JavaDoc value, Bundle definingBundle) {
87         if (value == null) {
88             return null;
89         }
90
91         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(value, ","); //$NON-NLS-1$
92
ArrayList JavaDoc array = new ArrayList JavaDoc(10);
93         while (tokens.hasMoreTokens()) {
94             array.add(getUrl(tokens.nextToken().trim(), definingBundle));
95         }
96
97         return (URL JavaDoc[]) array.toArray(new URL JavaDoc[array.size()]);
98     }
99
100     /**
101      * Returns an array of image descriptors for the given property, or
102      * <code>null</code>. The property value should be a comma separated list
103      * of image paths. Each path should either be absolute or relative to the
104      * optional bundle parameter.
105      *
106      * @param value
107      * value of a property that contains a comma-separated list of
108      * product relative urls describing images
109      * @param definingBundle
110      * bundle to be used for relative paths (may be null)
111      * @return an array of image descriptors for the given property, or
112      * <code>null</code>
113      */

114     public static ImageDescriptor[] getImages(String JavaDoc value,
115             Bundle definingBundle) {
116         URL JavaDoc[] urls = getURLs(value, definingBundle);
117         if (urls == null || urls.length <= 0) {
118             return null;
119         }
120
121         ImageDescriptor[] images = new ImageDescriptor[urls.length];
122         for (int i = 0; i < images.length; ++i) {
123             images[i] = ImageDescriptor.createFromURL(urls[i]);
124         }
125
126         return images;
127     }
128 }
129
Popular Tags