001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.net.MalformedURLException;
013: import java.net.URL;
014: import java.util.ArrayList;
015: import java.util.StringTokenizer;
016:
017: import org.eclipse.core.runtime.Path;
018: import org.eclipse.core.runtime.Platform;
019: import org.eclipse.jface.resource.ImageDescriptor;
020: import org.osgi.framework.Bundle;
021:
022: /**
023: * The branding properties are retrieved as strings, but often used as other
024: * types (e.g., <code>java.net.URL</code>s. This class provides some utility
025: * functions for converting the string values to these well known classes. This
026: * may be subclassed by clients that use more than just these few types.
027: */
028: public abstract class BrandingProperties {
029:
030: /**
031: * Create an url from the argument absolute or relative path. The bundle
032: * parameter is used as the base for relative paths and is allowed to be
033: * null.
034: *
035: * @param value
036: * the absolute or relative path
037: * @param definingBundle
038: * bundle to be used for relative paths (may be null)
039: * @return
040: */
041: public static URL getUrl(String value, Bundle definingBundle) {
042: try {
043: if (value != null) {
044: return new URL(value);
045: }
046: } catch (MalformedURLException e) {
047: if (definingBundle != null) {
048: return Platform.find(definingBundle, new Path(value));
049: }
050: }
051:
052: return null;
053: }
054:
055: /**
056: * Create a descriptor from the argument absolute or relative path to an
057: * image file. bundle parameter is used as the base for relative paths and
058: * is allowed to be null.
059: *
060: * @param value
061: * the absolute or relative path
062: * @param definingBundle
063: * bundle to be used for relative paths (may be null)
064: * @return
065: */
066: public static ImageDescriptor getImage(String value,
067: Bundle definingBundle) {
068: URL url = getUrl(value, definingBundle);
069: return url == null ? null : ImageDescriptor.createFromURL(url);
070: }
071:
072: /**
073: * Returns a array of URL for the given property or <code>null</code>.
074: * The property value should be a comma separated list of urls (either
075: * absolute or relative to the argument bundle). Tokens that do not
076: * represent a valid url will be represented with a null entry in the
077: * returned array.
078: *
079: * @param value
080: * value of a property that contains a comma-separated list of
081: * product relative urls
082: * @param definingBundle
083: * bundle to be used as base for relative paths (may be null)
084: * @return a URL for the given property, or <code>null</code>
085: */
086: public static URL[] getURLs(String value, Bundle definingBundle) {
087: if (value == null) {
088: return null;
089: }
090:
091: StringTokenizer tokens = new StringTokenizer(value, ","); //$NON-NLS-1$
092: ArrayList array = new ArrayList(10);
093: while (tokens.hasMoreTokens()) {
094: array
095: .add(getUrl(tokens.nextToken().trim(),
096: definingBundle));
097: }
098:
099: return (URL[]) array.toArray(new URL[array.size()]);
100: }
101:
102: /**
103: * Returns an array of image descriptors for the given property, or
104: * <code>null</code>. The property value should be a comma separated list
105: * of image paths. Each path should either be absolute or relative to the
106: * optional bundle parameter.
107: *
108: * @param value
109: * value of a property that contains a comma-separated list of
110: * product relative urls describing images
111: * @param definingBundle
112: * bundle to be used for relative paths (may be null)
113: * @return an array of image descriptors for the given property, or
114: * <code>null</code>
115: */
116: public static ImageDescriptor[] getImages(String value,
117: Bundle definingBundle) {
118: URL[] urls = getURLs(value, definingBundle);
119: if (urls == null || urls.length <= 0) {
120: return null;
121: }
122:
123: ImageDescriptor[] images = new ImageDescriptor[urls.length];
124: for (int i = 0; i < images.length; ++i) {
125: images[i] = ImageDescriptor.createFromURL(urls[i]);
126: }
127:
128: return images;
129: }
130: }
|