Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui;
  
 import java.net.URL;
 import java.util.Map;
 
 import  org.eclipse.jface.resource.ImageDescriptor;
 import  org.eclipse.swt.graphics.Image;
 import  org.eclipse.ui.plugin.AbstractUIPlugin;
 import  org.osgi.framework.BundleEvent;
 import  org.osgi.framework.SynchronousBundleListener;
 

Author(s):
Sebastian Zarnekow
 
 public class PluginImageHelper implements IImageHelperIImageDescriptorHelper, SynchronousBundleListener {
 	private Map<ImageDescriptor, Image> registry = Maps.newHashMapWithExpectedSize(10);
 
 	private AbstractUIPlugin plugin;
 
 	@Inject(optional = true)
 	@Named("org.eclipse.xtext.ui.PluginImageHelper.pathSuffix")
 	private String pathSuffix = "icons/"//$NON-NLS-1$
 
 	@Inject(optional = true)
 	@Named("org.eclipse.xtext.ui.PluginImageHelper.defaultImage")
 	private String defaultImage = "default.gif"//$NON-NLS-1$
 
 	@Inject(optional = true)
 	@Named("org.eclipse.xtext.ui.PluginImageHelper.notFound")
 	private String notFound = "notFound.gif"//$NON-NLS-1$
 
Returns the image associated with the given image descriptor.

Parameters:
descriptor the image descriptor for which the helper manages an image, or null for a missing image descriptor
Returns:
the image associated with the image descriptor or null if the image descriptor can't create the requested image.
 
 	public Image getImage(ImageDescriptor descriptor) {
 		if (descriptor == null) {
 			descriptor = ImageDescriptor.getMissingImageDescriptor();
 		}
 
 		Image result = .get(descriptor);
 		if (result != null) {
 			return result;
 		}
 		result = descriptor.createImage();
 		if (result != null) {
 			.put(descriptorresult);
 		}
 		return result;
 	}

Disposes all images managed by this image helper.
 
 	public void dispose() {
 		for (Iterator<Image> iter = .values().iterator(); iter.hasNext();) {
 			Image image = iter.next();
 			image.dispose();
 		}
 		.clear();
 	}
 
 	public void hookBundleListener(AbstractUIPlugin plugin) {
 		plugin.getBundle().getBundleContext().addBundleListener(this);
 	}
 
 	public Image getImage(String imageName) {
 		String imgname = imageName == null ?  : imageName;
 		if (imgname != null) {
 			Image result = null;
 			URL imgUrl = getPlugin().getBundle().getEntry(getPathSuffix() + imgname);
 			if (imgUrl != null) {
 				ImageDescriptor id = null;
 				result = getPlugin().getImageRegistry().get(imgUrl.toExternalForm());
 				if (result == null) {
					id = ImageDescriptor.createFromURL(imgUrl);
					if (id != null) {
						result = id.createImage();
						getPlugin().getImageRegistry().put(imgUrl.toExternalForm(), result);
					}
				}
				return result;
			}
			if (!imgname.equals()) {
			}
		}
		return null;
	}

Since:
2.4
	public ImageDescriptor getImageDescriptor(String imageName) {
		String imgname = imageName == null ?  : imageName;
		if (imgname != null) {
			URL imgUrl = getPlugin().getBundle().getEntry(getPathSuffix() + imgname);
			if (imgUrl != null) {
				return ImageDescriptor.createFromURL(imgUrl);
			}
			if (!imgname.equals()) {
			}
		}
		return null;
	}

Since:
2.4
	public ImageDescriptor getImageDescriptor(Image image) {
		for(Map.Entry<ImageDescriptor, Image> entry : .entrySet()) {
			if(entry.getValue().equals(image))
				return entry.getKey();
		}
		ImageDescriptor newDescriptor = ImageDescriptor.createFromImage(image);
		.put(newDescriptorimage);
		return newDescriptor;
	}
	public void setPathSuffix(String pathSuffix) {
		this. = pathSuffix;
	}
	public String getPathSuffix() {
		return ;
	}
	public void setPlugin(AbstractUIPlugin plugin) {
		this. = plugin;
	}
	public AbstractUIPlugin getPlugin() {
		return ;
	}
	public void setNotFound(String notFound) {
		this. = notFound;
	}
	public String getNotFound() {
		return ;
	}
	public String getDefaultImage() {
		return ;
	}
	public void setDefaultImage(String defaultImage) {
		this. = defaultImage;
	}
	public void bundleChanged(BundleEvent event) {
		if (event.getType() == BundleEvent.STOPPING
				&& event.getBundle().getBundleId() == getPlugin().getBundle().getBundleId()) {
		}
	}
New to GrepCode? Check out our FAQ X