david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2005 IBM Corporation and others. |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 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 |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 7 | * |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
| 13 | package org.eclipse.wst.sse.ui.internal.extension; |
| 14 | |
| 15 | |
| 16 | |
| 17 | import java.net.MalformedURLException; |
| 18 | import java.net.URL; |
| 19 | |
| 20 | import org.eclipse.core.runtime.IExtension; |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 21 | import org.eclipse.core.runtime.Platform; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 22 | import org.eclipse.jface.resource.ImageDescriptor; |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 23 | import org.osgi.framework.Bundle; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 24 | |
| 25 | public 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_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 49 | String pluginId = extension.getNamespace(); |
| 50 | Bundle bundle = Platform.getBundle(pluginId); |
| 51 | return getImageDescriptorFromBundle(bundle, subdirectoryAndFilename); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Convenience Method. Return an ImageDescriptor whose path relative to |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 56 | * the plugin described by <code>bundle</code> is |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 57 | * <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_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 73 | public static ImageDescriptor getImageDescriptorFromBundle(Bundle bundle, String subdirectoryAndFilename) { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 74 | |
david_williams | b9da93e | 2005-04-13 02:38:05 +0000 | [diff] [blame] | 75 | URL path = bundle.getEntry("/"); //$NON-NLS-1$ |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 76 | 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 | } |