Wednesday, 9 December 2015

Creating Image Registry in Eclipse RCP


Advantage of Creating Image Registry in Eclipse RCP
  1. Share the same image between different plugin.
  2. Don't dispose the image and keep it for another plugin.

How to create Image Registry?

Override the initializeImageRegistry method inside the Activator and you are ready to use t

 public class Activator  {
      public static final String MY_PLUGIN_ID = "org.harikrushnav.myplugin.name";
      public static final String MY_IMAGE_ID = "myimage.save";
   

....
     @override
      protected void initializeImageRegistry(ImageRegistry registry) {
     
         Bundle bundle = Platform.getBundle(MY_PLUGIN_ID);
         IPath path = new Path("icons/save_image.gif");
         URL url = FileLocator.find(bundle, path, null);
         ImageDescriptor desc = ImageDescriptor.createFromURL(url);
         registry.put(MY_IMAGE_ID , desc);
      }
   }

How to use in Code :

In same plugin :
ProfileAction.setImageDescriptor(
            imageRegistry.getDescriptor( Activator.MY_IMAGE_ID) );

In Different plugin :

AbstractUIPlugin.imageDescriptorFromPlugin( "org.harikrushnav.myplugin.name", "myimage.save" );

Best Practice for Maintaining Images in Eclipse Plugin

If we maintain the same image in multiple Plugins the each plugin will be of large size. Instead of maintaining the copy of Image at multiple locations, reusing the images is the best practice.
By doing this, we can avoid loading of the same image multiple times and as a result, RCP application will perform faster.
So I would suggest reusing the images as much as possible in the Eclipse Plugin Development.