david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2001, 2004 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 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
| 13 | package org.eclipse.wst.sse.ui.internal.editor; |
| 14 | |
| 15 | import java.util.HashMap; |
| 16 | |
| 17 | import org.eclipse.jface.resource.ImageDescriptor; |
| 18 | import org.eclipse.jface.resource.ImageRegistry; |
| 19 | import org.eclipse.jface.resource.JFaceResources; |
| 20 | import org.eclipse.swt.graphics.Image; |
| 21 | import org.eclipse.ui.plugin.AbstractUIPlugin; |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 22 | import org.eclipse.wst.sse.ui.internal.SSEUIPlugin; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | /** |
| 26 | * Helper class to handle images provided by this plug-in. |
| 27 | * |
| 28 | * NOTE: For internal use only. For images used externally, please use the |
| 29 | * Shared***ImageHelper class instead. |
| 30 | * |
| 31 | * @author amywu |
| 32 | */ |
| 33 | public class EditorPluginImageHelper { |
| 34 | private static EditorPluginImageHelper instance = null; |
| 35 | |
| 36 | /** |
| 37 | * Gets the instance. |
| 38 | * |
| 39 | * @return Returns a EditorPluginImageHelper |
| 40 | */ |
| 41 | public synchronized static EditorPluginImageHelper getInstance() { |
| 42 | if (instance == null) |
| 43 | instance = new EditorPluginImageHelper(); |
| 44 | return instance; |
| 45 | } |
| 46 | |
| 47 | // save a descriptor for each image |
| 48 | private HashMap fImageDescRegistry = null; |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 49 | private final String PLUGINID = SSEUIPlugin.ID; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Creates an image from the given resource and adds the image to the |
| 53 | * image registry. |
| 54 | * |
| 55 | * @param resource |
| 56 | * @return Image |
| 57 | */ |
| 58 | private Image createImage(String resource) { |
| 59 | ImageDescriptor desc = getImageDescriptor(resource); |
| 60 | Image image = null; |
| 61 | |
| 62 | if (desc != null) { |
| 63 | image = desc.createImage(); |
| 64 | // dont add the missing image descriptor image to the image |
| 65 | // registry |
| 66 | if (!desc.equals(ImageDescriptor.getMissingImageDescriptor())) |
| 67 | getImageRegistry().put(resource, image); |
| 68 | } |
| 69 | return image; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Creates an image descriptor from the given imageFilePath and adds the |
| 74 | * image descriptor to the image descriptor registry. If an image |
| 75 | * descriptor could not be created, the default "missing" image descriptor |
| 76 | * is returned but not added to the image descriptor registry. |
| 77 | * |
| 78 | * @param imageFilePath |
| 79 | * @return ImageDescriptor image descriptor for imageFilePath or default |
| 80 | * "missing" image descriptor if resource could not be found |
| 81 | */ |
| 82 | private ImageDescriptor createImageDescriptor(String imageFilePath) { |
| 83 | ImageDescriptor imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(PLUGINID, imageFilePath); |
| 84 | if (imageDescriptor != null) { |
| 85 | getImageDescriptorRegistry().put(imageFilePath, imageDescriptor); |
| 86 | } else { |
| 87 | imageDescriptor = ImageDescriptor.getMissingImageDescriptor(); |
| 88 | } |
| 89 | |
| 90 | return imageDescriptor; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Retrieves the image associated with resource from the image registry. |
| 95 | * If the image cannot be retrieved, attempt to find and load the image at |
| 96 | * the location specified in resource. |
| 97 | * |
| 98 | * @param resource |
| 99 | * the image to retrieve |
| 100 | * @return Image the image associated with resource or null if one could |
| 101 | * not be found |
| 102 | */ |
| 103 | public Image getImage(String resource) { |
| 104 | Image image = getImageRegistry().get(resource); |
| 105 | if (image == null) { |
| 106 | // create an image |
| 107 | image = createImage(resource); |
| 108 | } |
| 109 | return image; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Retrieves the image descriptor associated with resource from the image |
| 114 | * descriptor registry. If the image descriptor cannot be retrieved, |
| 115 | * attempt to find and load the image descriptor at the location specified |
| 116 | * in resource. |
| 117 | * |
| 118 | * @param resource |
| 119 | * the image descriptor to retrieve |
| 120 | * @return ImageDescriptor the image descriptor assocated with resource or |
| 121 | * the default "missing" image descriptor if one could not be |
| 122 | * found |
| 123 | */ |
| 124 | public ImageDescriptor getImageDescriptor(String resource) { |
| 125 | ImageDescriptor imageDescriptor = null; |
| 126 | Object o = getImageDescriptorRegistry().get(resource); |
| 127 | if (o == null) { |
| 128 | //create a descriptor |
| 129 | imageDescriptor = createImageDescriptor(resource); |
| 130 | } else { |
| 131 | imageDescriptor = (ImageDescriptor) o; |
| 132 | } |
| 133 | return imageDescriptor; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns the image descriptor registry for this plugin. |
| 138 | * |
| 139 | * @return HashMap - image descriptor registry for this plugin |
| 140 | */ |
| 141 | private HashMap getImageDescriptorRegistry() { |
| 142 | if (fImageDescRegistry == null) |
| 143 | fImageDescRegistry = new HashMap(); |
| 144 | return fImageDescRegistry; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns the image registry for this plugin. |
| 149 | * |
| 150 | * @return ImageRegistry - image registry for this plugin |
| 151 | */ |
| 152 | private ImageRegistry getImageRegistry() { |
| 153 | return JFaceResources.getImageRegistry(); |
| 154 | } |
| 155 | } |