How to load image to view in RCP?
Asked Answered
H

3

2

I am developing an RCP plugin project that includes certain views.First view take employee details like name ,address etc.There is an option to upload employee image using browse button.Second view shows the details that have entered in the first view.All details except photo is displaying fine.

It shows a red square in the place of photo label. My code for setting photo is shown like this :

 Label photoLabel = new Label(parent, SWT.NONE);
 photoLabel.setBounds(420, 233, 100, 106);           

 photoLabel.setImage(SWTResourceManager.getImage(FormDataViewClass.class,photoUploadPath));

where photoUploadPath is string variable that contains the path of uploaded photo. How can I solve this issue?

Homeless answered 20/5, 2014 at 6:27 Comment(4)
Is the path an absolute path? Does SWTResourceManager.getImage(...) return an image or null?Plot
yes. SWTResourceManager.getImage(...) returns image like Image {140719108178432}Homeless
the CLabel class may help you. it can set text and image. several years ago, i used it in my project perfectly.Mcclish
SWTResourceManager.getImage returns the red square image if it cannot find the image. The image path must be relative to the FormDataViewClass for this form of getImage call.Skywriting
H
2

Following code segment helped me to resolve the above problem.

byte[] uploadedImg = null;
try {
    File f1 = new File(photoUploadPath);
    double fileLen = f1.length();
    uploadedImg = new byte[(int) fileLen];
    FileInputStream inputStream = new FileInputStream(photoUploadPath);
    int nRead = 0;
    while ((nRead = inputStream.read(uploadedImg)) != -1) {
    System.out.println("!!!!!!!!!!!!!!!!!" + new String(uploadedImg));
    }
    inputStream.close();

} catch (Exception e2) {
    // TODO: handle exception
}

BufferedInputStream inputStreamReader = new BufferedInputStream(new ByteArrayInputStream(uploadedImg));
ImageData imageData = new ImageData(inputStreamReader);
Image image = new Image(Display.getCurrent(), imageData);
photoLabel.setImage(image);
Homeless answered 20/5, 2014 at 10:13 Comment(1)
Remember to dispose() the Image when you don't need it anymore. Otherwise you'll leak system handles.Plot
M
0

If it's an RCP App, I would go with a scalable solution.

Create an ImageCache object, which you instantiate at the beginning of the app lifecycle (preferably in the Activator class of the app).

This ImageCache can get images (and cache them, of course) from a path relative to the plugin (e.g. plugin has a folder icons; then, when you need an icon, you just call Activator.getDefault().getImage("icons/random.png"); - where getDefault() is the Activator singleton instance).

Have two of these in the ImageCache:

public ImageDescriptor getImageDescriptor(final String path)
{
    ImageDescriptor imgD = AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID, path);

    if (imgD == null)
    {
        return null; // OR a "missing icon", e.g. a red flag
    }
}

and

public Image getImage(final String path)
{
    Image image = imageCacheMap.get(path);

    if (image == null)
    {
        image = getImageDescripto(path).createImage();
        imageCacheMap.put(path, image);
    }

    return image;
}

Since these images need to be disposed, have a method dispose() in the ImageCache which is called in the stop() method of the Activator.

There are many approaches to this. In my opinion, this is the best one for RCP apps.

Mieshamiett answered 20/5, 2014 at 9:57 Comment(0)
B
0

Java SWT Load and Resize Image to View or Editor at Dynamically

![enter image description here


Button click to open FileDialog Box and selected any image to display on specific label.

ImageLoader class are used to load images from, and save images to, a file or stream

ImageData class are device-independent descriptions of images

SWT's Image class can be used to display images in a GUI

package rcp_demo.Editor;

import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;


public class ProductEditor extends EditorPart {

        public static final  String ID="rcp_demo.Editor.product";
        private Text text;
        private CLabel lbl_image_text;

        private static final String[] FILTER_NAMES = {
        "Images(*.jpg)","Images(*.jpeg)","Images(*.png)","All Files (*.*)"};

        // These filter extensions are used to filter which files are displayed.
        private static final String[] FILTER_EXTS = { "*.jpg", "*.jpeg", "*.png", "*.*"};

    public void createPartControl(final Composite parent) {

        parent.setLayout(null);
        //Layout with absolute positioning components. 

        text = new Text(parent, SWT.BORDER);
        text.setBounds(25, 57, 169, 19);

        Button btnOpen = new Button(parent, SWT.NONE);
        btnOpen.setText("open");
        btnOpen.addSelectionListener(new SelectionAdapter() {
            @Override
        public void widgetSelected(SelectionEvent e) {

            FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN);
            dialog.setFilterNames(FILTER_NAMES);
            dialog.setFilterExtensions(FILTER_EXTS);
            String result = dialog.open();
            if(result!=null)
               {
                   text.setText(result);
                   Image image=SWTResourceManager.getImage(result);
                   ImageData imgData = image.getImageData();
                   imgData=imgData.scaledTo(200, 200);

                   ImageLoader imageLoader = new ImageLoader();
                   imageLoader.data = new ImageData[] {imgData};
                   imageLoader.save(result, SWT.IMAGE_COPY);

                   System.out.println(imgData.width+"....."+imgData.height);
                   lbl_image_text.setBounds(25,88,imgData.width+10,imgData.height+10);
                   //Image size set to Label
                   //lbl_image_text.setBounds(25,88,image.getBounds().width+10,image.getBounds().height+10);
                   lbl_image_text.setImage(SWTResourceManager.getImage(result));
               }
        }
    });
    btnOpen.setText("open");
    lbl_image_text = new CLabel(parent, SWT.Resize);
    }
}

The CLabel class provides some advanced features over the Label class. This class can display its text label and image label at the same time.

    lbl_image_text.setText("Welcome");
    lbl_image_text.setImage(SWTResourceManager.getImage("Image Path"));
Bialystok answered 2/2, 2017 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.