Thursday, May 10, 2007

Plugin your images

Let AbstractUIPlugin handle sharing images with your plugins. It has its own ImageRegistry that handles storing, disposing, and retrieving of images.

A simple override of initializeImageRegistry(ImageRegistry) and you are on your way to Image sharing bliss.

public class Activator extends AbstractUIPlugin {
    public static final String ID = "rcp.eclipse";
    public static final String MY_IMAGE_ID = 
                                  "image.myimage";

    @Override
    protected void initializeImageRegistry(ImageRegistry registry) {
        super.initializeImageRegistry(registry);
        Bundle bundle = Platform.getBundle(ID);

        ImageDescriptor myImage = ImageDescriptor.createFromURL(
              FileLocator.find(bundle,
                               new Path("icons/myImage..gif"),
                                        null));
        registry.put(MY_IMAGE_ID, myImage);
    }
}

Put your image into workspace/rcp.eclipse/icons/myImage.gif and then access it in your code.

AbstractUIPlugin plugin = Activator.getDefault();
ImageRegistry imageRegistry = plugin.getImageRegistry();
Image myImage = imageRegistry.get(Activator.MY_IMAGE_ID);

5 comments:

Fraer9 said...

Great thanks !

I was getting a org.eclipse.jface.resource.DeviceResourceException: Unable to create resource FileImageDescriptor when setting the image of an Action with

final String iconPath = FileLocator.toFileURL(
Activator.getDefault().getBundle().getEntry(
"icons/myicon.gif")).getPath();
super.setImageDescriptor(ImageDescriptor.createFromFile(this
.getClass(), iconPath));


No more problems with :

ImageDescriptor myImage = ImageDescriptor.createFromURL(
FileLocator.find(Activator.getDefault().getBundle(),
new Path("icons/myicon.gif"),null));
super.setImageDescriptor(myImage);


Thanks a lot man :)

jodowd said...

If NOT using an Activator class and resulting imageRegistry...

ImageDescriptor.createFromURL(FileLocator.find(Platform.getBundle(plugin_id), new Path("icons/imageFileName.xxx"),null);

Noah said...

Hey,

Thanks a lot for the post. It helped me resolve my issue of getting a image to be displayed.

I did not have a activator and have a plugin class instead. And i was earlier trying to get the descriptor as below : (but i never worked)

public static ImageDescriptor getImageDescriptor(String path) {
return AbstractUIPlugin.imageDescriptorFromPlugin("MyPlugin", path);
}


After changing it as below, it worked.

public static ImageDescriptor getImageDescriptor(String path) {

ImageDescriptor myImage = ImageDescriptor.createFromURL(
FileLocator.find(getDefault().getBundle(),
new Path("icons/file.gif"),null));


return myImage;

}

Thanks again !

kapitan said...

Hi guys!
I've tried your solution but it throws this bad error. It seems it cannot find the file, but it obviously is inside the jar and in that folder.

org.eclipse.core.runtime - org.eclipse.jface - 0 - /com/views/images/user.gif
java.io.FileNotFoundException: /com/views/images/user.gif
at org.eclipse.osgi.framework.internal.protocol.bundleentry.Handler.findBundleEntry(Handler.java:44)
at org.eclipse.osgi.framework.internal.core.BundleResourceHandler.openConnection(BundleResourceHandler.java:175)

What am I missing?

aneelak said...

Thank you so much! This was great help! I had been struggling to incorporate this feature for a couple of days!