Seb
Seb

Reputation: 100

Eclipse RCP ListProvider tweaking

I have problem with changing this image list provider in to thumbnail provider. In case of need I will post View for it too.

public Object[] getElements(Object inputElement) {
        if (iDirname == null)
            return null;
        File dir = new File(iDirname);
        FilenameFilter filter = new FilenameFilter() {
            public boolean accept(File directory, String filename) {
                if (filename.endsWith("jpg") || (filename.endsWith("bmp")) || (filename.endsWith("png") || (filename.endsWith("JPG") || (filename.endsWith("BMP")) || (filename.endsWith("PNG")))))
                    return true;
                else
                    return false;
            }
        };
        String[] dirList = null;

        if (dir.isDirectory()) {
            dirList = dir.list(filter);
            for (int i=0; i<dirList.length;++i){

                //dirList2[i] = new Image(device, dirList2[i]); added this to try passing array of Images - failed.
                dirList[i] = iDirname + File.separatorChar + dirList[i];

            }
        }
        return dirList;
    }

And the view

public void createPartControl(Composite parent) {
        iViewer = new ListViewer(parent);
        iViewer.setContentProvider(new DirListProvider());


        getSite().setSelectionProvider(iViewer);
        makeActions();
        hookContextMenu();
        contributeToActionBars();

    }

I don't know how to change provided path lists to the thumbnail displaying. Should I get the provided content in to Array and iterate through it creating Images? If so how?

Thanks in advance for your help.

EDIT:

I added

ImageDescriptor[] dirList = null;
        if (dir.isDirectory()) {
            String[] dirList2 = dir.list(filter);
            for (int i=0; i<dirList2.length;++i){

                dirList[i] = ImageDescriptor.createFromImageData(new ImageData(iDirname + File.separatorChar + dirList2[i]));
                //dirList[i] = iDirname + File.separatorChar + dirList[i];

            }
        }
        return dirList;

but this is not showing anything at all.

When you are telling me to use Composite, is it my parent variable? I still don't know how to display the images from paths passed by ListProvider. I am really green in this :/

Upvotes: 0

Views: 158

Answers (2)

Malcolm Smith
Malcolm Smith

Reputation: 3580

What you are missing here is a LabelProvider. You can use a LabelProvider to provide an image for each element in your viewer's input.

However, Francis Upton is right, I don't think ListViewer will really suit your needs as you will end up with a single column of images. Although you won't be able to add the images directly to your Composite, you will need to set them as the background image of a label.

There are a couple of other things to consider:

  • You need to dispose() of your Images once you're done with them as they use up System handles. Therefore you need to keep track of the Images you create in your getElements(Object) method.
  • If the directories you are reading the images from do not already contain thumbnails, you will need to scale the images before presenting them on your UI.

Remember, the array type you return from your ContentProvider's getElements(Object) method defines the type that will get passed into your LabelProvider's methods. So you started off returning an array of strings representing paths to the images. Your LabelProvider would need to load these into images to be returned from the provider's getImage method - but bear in mind what I said about disposing of these images! Then you switched to returning an Array of image descriptors, in this case you would need to cast your incoming Object to an ImageDescriptor and use that to create the Image in the getImage method. Maybe once you have this working you can think about whether this meets your needs, and then possibly look at doing a different implementation, such as the composite/gridlayout/label approach.

Upvotes: 1

Francis Upton IV
Francis Upton IV

Reputation: 19443

I would not use a ListViewer for this. I would just create a Composite and then using GridLayout set up the number of columns you want and margins and so forth, and then just add the images directly to the composite. As far as I know you cannot put arbitrary things like imagines in an SWT List, so the ListViewer is not going to help you. You can do all of this in the createPartControl method.

Upvotes: 0

Related Questions