Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Implements ImageRegistry in RAP Application and why using SessionSingletonBase for ImageRegistry?
Implements ImageRegistry in RAP Application and why using SessionSingletonBase for ImageRegistry? [message #721604] Fri, 02 September 2011 09:14 Go to next message
Angelo ZERR is currently offline Angelo ZERRFriend
Messages: 122
Registered: July 2009
Senior Member
Hi RAP Team,

In my RCP/RAP application I create an Action and now I would like set an image to this action. So I must populate an ImageRegistry with my image and I would like know how to do that with single/sourcing mean.

I have noticed that RAP implementation of JFaceResources and org.eclipse.ui.forms.examples.internal.ExamplesPlugin use SessionSingletonBase by implementing an internal class ImageRegistryStore :

public static ImageRegistry getImageRegistry() {
  return ImageRegistryStore.getInstance().getImageRegistry();
}


Where ImageRegistryStore extends SessionSingletonBase :

private final static class ImageRegistryStore extends SessionSingletonBase {
    private final ImageRegistry imageRegistry;
    private ImageRegistryStore() {
      imageRegistry = new ImageRegistry();
      initializeDefaultImages();
    }
    public static ImageRegistryStore getInstance() {
      Class clazz = ImageRegistryStore.class;
      return ( ImageRegistryStore )getInstance( clazz );
    }
    public ImageRegistry getImageRegistry() {
      return imageRegistry;
    }
    ...


I would like know why we cannot use directly new ImageRegistry() in RAP application? Perhaps it's because of the Display.getCurrent()?

If we need do that, I would like know what is the best solution to use ImageRegistry with Single Sourcing. I would like avoid coding MyImagesResources twice (with and without SessionSingletonBase) and I tell me if I can use JFaceResources to populate ImageRegistry.

If you think it's a good idea, I tell me why the Forms sample doesn't use JFaceResources instead of implementing ImageRegistryStore?

Thank a lot for your answer.

Regards Angelo
(no subject) [message #724577 is a reply to message #721604] Mon, 12 September 2011 15:22 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by:

Angelo,

sorry for the late response, we have vacation season here...

Instances of ImageRegistry must live in session scope as they reference
a Display. It is also perfectly valid to directly create an
ImageRegistry with the default constructor.

The ExamplesPlugin however has application scope (there exists just one
instance of the class) but its getImageRegistry() method returns an
ImageRegistry which has session scope. The ImageRegistryStore helper
class is used make sure that the respective instance for the current
session is returned.

HTH
Rüdiger

On 02.09.2011 11:14, Angelo wrote:
> Hi RAP Team,
>
> In my RCP/RAP application I create an Action and now I would like set an
> image to this action. So I must populate an ImageRegistry with my image
> and I would like know how to do that with single/sourcing mean.
>
> I have noticed that RAP implementation of JFaceResources and
> org.eclipse.ui.forms.examples.internal.ExamplesPlugin use
> SessionSingletonBase by implementing an internal class ImageRegistryStore :
> public static ImageRegistry getImageRegistry() {
> return ImageRegistryStore.getInstance().getImageRegistry();
> }
>
> Where ImageRegistryStore extends SessionSingletonBase :
>
> private final static class ImageRegistryStore extends
> SessionSingletonBase {
> private final ImageRegistry imageRegistry;
> private ImageRegistryStore() {
> imageRegistry = new ImageRegistry();
> initializeDefaultImages();
> }
> public static ImageRegistryStore getInstance() {
> Class clazz = ImageRegistryStore.class;
> return ( ImageRegistryStore )getInstance( clazz );
> }
> public ImageRegistry getImageRegistry() {
> return imageRegistry;
> }
> ...
>
>
> I would like know why we cannot use directly new ImageRegistry() in RAP
> application? Perhaps it's because of the Display.getCurrent()?
>
> If we need do that, I would like know what is the best solution to use
> ImageRegistry with Single Sourcing. I would like avoid coding
> MyImagesResources twice (with and without SessionSingletonBase) and I
> tell me if I can use JFaceResources to populate ImageRegistry.
>
> If you think it's a good idea, I tell me why the Forms sample doesn't
> use JFaceResources instead of implementing ImageRegistryStore?
>
> Thank a lot for your answer.
>
> Regards Angelo


--
Rüdiger Herrmann

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Re: (no subject) [message #727486 is a reply to message #724577] Wed, 21 September 2011 12:35 Go to previous message
Angelo ZERR is currently offline Angelo ZERRFriend
Messages: 122
Registered: July 2009
Senior Member
Hi Rüdiger,

Thank a lot for explanation. In my case I have created an ImageResources like this :

package fr.opensagres.xdocreport.eclipse.internal;

import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.osgi.framework.Bundle;

public class ImageResources {
	
	public final static String ICONS_PATH = "icons/"; //$NON-NLS-1$

	/**
	 * Set of predefined Image Descriptors.
	 */
	private static final String PATH_OBJ = ICONS_PATH + "obj16/"; //$NON-NLS-1$

	public static final String IMG_MODULE = "module";
	public static final String IMG_TH_HORIZONTAL = "th_horizontal";
	public static final String IMG_TH_VERTICAL = "th_vertical";

	public static void initialize(ImageRegistry imageRegistry) {
		registerImage(imageRegistry, IMG_MODULE, PATH_OBJ + "module.gif");
		registerImage(imageRegistry, IMG_TH_HORIZONTAL, PATH_OBJ + "th_horizontal.gif");		
		registerImage(imageRegistry, IMG_TH_VERTICAL, PATH_OBJ + "th_vertical.gif");
	}

	private static void registerImage(ImageRegistry registry, String key,
			String fileName) {
		try {
			IPath path = new Path(fileName);
			Bundle bundle = Activator.getDefault().getBundle();
			URL url = FileLocator.find(bundle, path, null);
			if (url != null) {
				ImageDescriptor desc = ImageDescriptor.createFromURL(url);
				registry.put(key, desc);
			}
		} catch (Exception e) {
		}
	}

	public static ImageDescriptor getImageDescriptor(String key) {
		ImageRegistry imageRegistry = Activator.getDefault().getImageRegistry();
		return imageRegistry.getDescriptor(key);
	}

	public static Image getImage(String key) {
		ImageRegistry imageRegistry = Activator.getDefault().getImageRegistry();
		return imageRegistry.get(key);
	}

}


And My Activator set the ImageRegistry from the plugin to my ImageResources:
public class Activator extends AbstractUIPlugin {
...
	@Override
	protected void initializeImageRegistry(ImageRegistry reg) {
		super.initializeImageRegistry(reg);		
		ImageResources.initialize(reg);
	}
...
}


And it works great.

Regards Angelo
Previous Topic:NullPointerException in SessionSingletonBase.getInstanceLock()
Next Topic:How to save and restore workbench state in a RAP App
Goto Forum:
  


Current Time: Thu Sep 19 20:35:03 GMT 2024

Powered by FUDForum. Page generated in 0.03248 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top