Imagepreloader not working properly - GWT project
I have written this custom widget to show a list of photos on a browser:
public class PhotosSlideShow extends DialogBox {
private List<Image> photos;
private int index;
private DockLayoutPanel mainPnl;
private SimpleLayoutPanel centerPnl;
private Image n;
private Image p;
private final int MAX_WIDTH = 800;
private final int MAX_HEIGHT = 600;
public PhotosSlideShow(final List<String> result) {
super();
this.setAutoHideEnabled(true);
this.setGlassEnabled(true);
index = 0;
photos = new ArrayList<Image>();
centerPnl = new SimpleLayoutPanel();
mainPnl = new DockLayoutPanel(Unit.PCT);
this.setStyleName("slideShow");
n = new Image("images/next.png");
n.setPixelSize(40, 50);
n.getElement().getStyle().setZIndex(0);
n.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
showNext();
}
});
p = new Image("images/prev.png");
p.setPixelSize(40, 50);
p.getElement().getStyle().setZIndex(0);
p.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
showPrev();
}
});
mainPnl.addWest(p, 5);
mainPnl.addEast(n, 5);
mainPnl.add(centerPnl);
mainPnl.setSize("900px", "600px");
this.add(mainPnl);
String path = GWT.getModuleBaseURL() + "imageUpload?src=";
for (String foto : result) {
String url = path.concat(foto);
// Image.prefetch(url);
ImagePreloader.load(url, new ImageLoadHandler() {
@Override
public void imageLoaded(ImageLoadEvent event) {
final Image curr = event.takeImage();
setSize(curr);
photos.add(curr);
if (photos.size() == 1)
showNext();
}
});
}
this.center();
}
private void showNext() {
if (index == photos.size() - 1) {
index = 0;
} else {
index++;
}
Image i = photos.get(index);
setSize(i);
centerPnl.setWidget(i);
i.getElement()
.getStyle()
.setLeft(centerPnl.getOffsetWidth() / 2 - i.getWidth() / 2,
Unit.PX);
}
private void showPrev() {
if (index == 0) {
index = photos.size() - 1;
} else {
index--;
}
Image i = photos.get(index);
setSize(i);
centerPnl.setWidget(i);
i.getElement()
.getStyle()
.setLeft(centerPnl.getOffsetWidth() / 2 - i.getWidth() / 2,
Unit.PX);
}
private double getRatio(Image i) {
double res = (double) i.getHeight() / i.getWidth();
return res;
}
private void setSize(Image i) {
double ratio = getRatio(i);
if (i.getHeight() > i.getWidth())
i.setPixelSize((int) (MAX_HEIGHT / ratio), MAX_HEIGHT);
else
i.setPixelSize(MAX_WIDTH, (int) (ratio * MAX_WIDTH));
}
}
The problem is, the first time I open this widget, images are properly
shown. But if I close and reopen it, no image is loaded (as if they've
been deleted from the browser cache). I'm using this library (GWT
imageloader). How can I solve this issue?
No comments:
Post a Comment