
I’ve been working on demos for JavaZone and built something small that has been asked a few times. Some developers were looking for
URLImage
support in image viewer but that’s not a very good approach since URLImage expects images to be at a specific resolution and is thus very bad for images with unknown dimensions. However, ImageViewer thrives on showing and zooming full sized images.
There was a bit of a community hack with ImageDownloadService a while back but it was a bit in-elegant and wasn’t
what I was looking for, so I created a simple list model that just streams images, you can use it with image viewer like this:
ImageViewer viewer = new ImageViewer(imageList.getItemAt(offset));
viewer.setImageList(imageList);
URLImage
support in image viewer but that’s not a very good approach since URLImage expects images to be at a specific resolution and is thus very bad for images with unknown dimensions. However, ImageViewer thrives on showing and zooming full sized images.
There was a bit of a community hack with ImageDownloadService a while back but it was a bit in-elegant and wasn’t
what I was looking for, so I created a simple list model that just streams images, you can use it with image viewer like this:
ImageViewer viewer = new ImageViewer(imageList.getItemAt(offset));
viewer.setImageList(imageList);
The list model is just a plain old list model that expects the placeholder image to be an encoded image you have already loaded. This implementation expects images to have an ID which we use in the createImageURL method. Notice we could just use the image index but since that might change (user deletes images etc.) we needed to have ID’s generated by the server. We’ll have the full demo up with the server side code for JavaZone:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImageList implements ListModel<Image> { | |
private int selection; | |
private long[] imageIds; | |
private EncodedImage[] images; | |
private EventDispatcher listeners = new EventDispatcher(); | |
public ImageList(long[] images) { | |
this.imageIds = images; | |
this.images = new EncodedImage[images.length]; | |
} | |
public Image getItemAt(final int index) { | |
if(images[index] == null) { | |
images[index] = placeholder; | |
Util.downloadUrlToStorageInBackground(createImageURL(imageIds[index]), new ActionListener() { | |
public void actionPerformed(ActionEvent evt) { | |
try { | |
images[index] = EncodedImage.create(Storage.getInstance().createInputStream("FullImage_" + imageIds[index])); | |
listeners.fireDataChangeEvent(index, DataChangedListener.CHANGED); | |
} catch(IOException err) { | |
err.printStackTrace(); | |
} | |
} | |
}); | |
} | |
return images[index]; | |
} | |
public int getSize() { | |
return imageIds.length; | |
} | |
public int getSelectedIndex() { | |
return selection; | |
} | |
public void setSelectedIndex(int index) { | |
selection = index; | |
} | |
public void addDataChangedListener(DataChangedListener l) { | |
listeners.addListener(l); | |
} | |
public void removeDataChangedListener(DataChangedListener l) { | |
listeners.removeListener(l); | |
} | |
public void addSelectionListener(SelectionListener l) { | |
} | |
public void removeSelectionListener(SelectionListener l) { | |
} | |
public void addItem(Image item) { | |
} | |
public void removeItem(int index) { | |
} | |
} |
Notice: This post was automatically converted using a script from an older blogging system. Some elements might not have come out as intended…. If that is the case please let us know via the comments section below.