blob: aefc562c4106c7b4ed7ed76340e3bba7397e6581 [file] [log] [blame]
david_williamscfdb2cd2004-11-11 08:37:49 +00001/*******************************************************************************
amywuecebb042007-04-10 20:07:35 +00002 * Copyright (c) 2001, 2005 IBM Corporation and others.
david_williamscfdb2cd2004-11-11 08:37:49 +00003 * 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
amywuecebb042007-04-10 20:07:35 +00007 *
david_williamscfdb2cd2004-11-11 08:37:49 +00008 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Jens Lukowski/Innoopract - initial renaming/restructuring
11 *
12 *******************************************************************************/
13package org.eclipse.wst.sse.ui.internal.extension;
14
15
16
17import java.net.MalformedURLException;
18import java.net.URL;
19
20import org.eclipse.core.runtime.IExtension;
david_williams425ffe72004-12-07 21:46:39 +000021import org.eclipse.core.runtime.Platform;
david_williamscfdb2cd2004-11-11 08:37:49 +000022import org.eclipse.jface.resource.ImageDescriptor;
david_williams425ffe72004-12-07 21:46:39 +000023import org.osgi.framework.Bundle;
david_williamscfdb2cd2004-11-11 08:37:49 +000024
25public class ImageUtil {
26
27
28 /**
29 * Convenience Method. Returns an ImageDescriptor whose path, relative to
30 * the plugin containing the <code>extension</code> is
31 * <code>subdirectoryAndFilename</code>. If there isn't any value
32 * associated with the name then <code>null
33 * </code> is returned.
34 *
35 * This method is convenience and only intended for use by the workbench
36 * because it explicitly uses the workbench's registry for
37 * caching/retrieving images from other extensions -- other plugins must
38 * user their own registry. This convenience method is subject to removal.
39 *
40 * Note: subdirectoryAndFilename must not have any leading "." or path
41 * separators / or \ ISV's should use icons/mysample.gif and not
42 * ./icons/mysample.gif
43 *
44 * Note: This consults the plugin for extension and obtains its
45 * installation location. all requested images are assumed to be in a
46 * directory below and relative to that plugins installation directory.
47 */
48 public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
david_williams425ffe72004-12-07 21:46:39 +000049 String pluginId = extension.getNamespace();
50 Bundle bundle = Platform.getBundle(pluginId);
51 return getImageDescriptorFromBundle(bundle, subdirectoryAndFilename);
david_williamscfdb2cd2004-11-11 08:37:49 +000052 }
53
54 /**
55 * Convenience Method. Return an ImageDescriptor whose path relative to
david_williams425ffe72004-12-07 21:46:39 +000056 * the plugin described by <code>bundle</code> is
david_williamscfdb2cd2004-11-11 08:37:49 +000057 * <code>subdirectoryAndFilename</code>. Returns <code>null</code> if
58 * no image could be found.
59 *
60 * This method is convenience and only intended for use by the workbench
61 * because it explicitly uses the workbench's registry for
62 * caching/retrieving images from other extensions -- other plugins must
63 * user their own registry. This convenience method is subject to removal.
64 *
65 * Note: subdirectoryAndFilename must not have any leading "." or path
66 * separators / or \ ISV's should use icons/mysample.gif and not
67 * ./icons/mysample.gif
68 *
69 * Note: This consults the plugin for extension and obtains its
70 * installation location. all requested images are assumed to be in a
71 * directory below and relative to that plugins installation directory.
72 */
david_williams425ffe72004-12-07 21:46:39 +000073 public static ImageDescriptor getImageDescriptorFromBundle(Bundle bundle, String subdirectoryAndFilename) {
david_williamscfdb2cd2004-11-11 08:37:49 +000074
david_williamsb9da93e2005-04-13 02:38:05 +000075 URL path = bundle.getEntry("/"); //$NON-NLS-1$
david_williamscfdb2cd2004-11-11 08:37:49 +000076 URL fullPathString = null;
77 try {
78 fullPathString = new URL(path, subdirectoryAndFilename);
79 return ImageDescriptor.createFromURL(fullPathString);
80 } catch (MalformedURLException e) {
81 }
82 return null;
83 }
84}