Search code examples
eclipseimageosgieclipse-rap

Eclipse RAP with OSGi Images of Labels won´t show


I have a RAP Application running in OSGI. And I want to display some images.

Unfortunately my browser won´t show the image for the file I want. I tested it with an very small gif, which showed. But just one. When I exchanged it with an other it still won´t work. The exact problem is, that, if I place a border around the pictures, i can see the border. But there is nothing in the border.

I have tried a number of different things for days now but I think I´m stuck at this point.

I tried doing it like this:

Image image3 = loadImage( parent.getDisplay(), "icons/test-100x50.png" );
Label l3 = new Label( parent, SWT.BORDER ); 
l3.setImage( image3 );

and like this:

public static Image loadImage( Display display, String name ) {
    Image result = null;
    InputStream stream = BasicEntryPoint.class.getClassLoader().getResourceAsStream( name );
    if( stream != null ) {
        try {
            result = new Image( display, stream );
        } finally {
            try {
                stream.close();
            } catch( IOException unexpected ) {
                throw new RuntimeException( "Failed to close image input stream", unexpected );
            }
        }
    }
    return result;
}

I even tried it with MarkUps

but i can´t find what i´m missing.

the images are in the plug-in in a folder called icons and i have selected the icons folder in the build.properties file.

FYI: I saw the RAP Demo with the Blue Header and Implemented my picture like this. At first it worked, but as soon as I started it as a OSGI App I could only see the border for the command SWT.BORDER


Solution

  • My guess is that stream in loadImage() is null. Which means that the 'icons' folder is not part of the class-path. Soley adding a folder to the build.properties file does not make it part of the class-path.

    You have two options here: Put the images on your class-path or load the image from the plug-in.

    Put Image on Class-path

    Place the image alongside (i.e. in the same package as) the BasicEntryPoint class. Then you can open the input stream with

    BasicEntryPoint.class.getResourceAsStream( "test-100x50.png" );
    

    Note that the class is used here to get the resource stream instead of the class-loader. Second, note that the resource name is just the name of the image (without the path to its package).

    Alternatively you can use the class-loader and specify the full path to the image to obtain the input stream

    BasicEntryPoint.class.getClassLoader().getResourceAsStream( "com/example/myapp/test-100x50.png" );
    

    I recommend the class-path solution because it works the same whether you run on OSGi or not.

    Load Image from Plug-in

    If you prefer to hold the image file outside the class-path, you have to specify the 'icons' folder in the build.properties as you already did:

    bin.includes = META-INF/,icons/,...
    

    The bundle entry can then be access like this:

    Bundle bundle = FrameworkUtil.getBundle( BasicEntryPoint.class );
    bundle.getEntry( "icons/test-100x50.png" ).openStream();
    

    See also this article for a detailed comparison of both approaches and possible pitfalls.