added chached image, to avoid multiple download of the same image

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/widgets/image-previewer-widget@134913 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-11-26 15:10:04 +00:00
parent 8497c44374
commit d0dfa453a4
2 changed files with 57 additions and 28 deletions

View File

@ -2,6 +2,8 @@ package org.gcube.portlets.widgets.imagepreviewerwidget.client;
import org.gcube.portlets.widgets.imagepreviewerwidget.shared.Orientation; import org.gcube.portlets.widgets.imagepreviewerwidget.shared.Orientation;
import com.github.gwtbootstrap.client.ui.Image;
/** /**
* This class allows to build the image to show within the carousel. It contains: * This class allows to build the image to show within the carousel. It contains:
* <ul> * <ul>
@ -33,6 +35,11 @@ public class EnhancedImage {
* The orientation * The orientation
*/ */
private Orientation orientation = Orientation.UNDEFINED; private Orientation orientation = Orientation.UNDEFINED;
/**
* The cached image (once downloaded)
*/
private Image cachedImage;
/** /**
* Build an enhanced image from a url. * Build an enhanced image from a url.
@ -128,6 +135,14 @@ public class EnhancedImage {
public void setOrientation(Orientation orientation) { public void setOrientation(Orientation orientation) {
this.orientation = orientation; this.orientation = orientation;
} }
public Image getCachedImage() {
return cachedImage;
}
public void setCachedImage(Image cachedImage) {
this.cachedImage = cachedImage;
}
@Override @Override
public String toString() { public String toString() {

View File

@ -245,7 +245,7 @@ public class Carousel extends Composite{
currentPreviewPosition = currentPreviewPosition =
currentPreviewPosition == listOfAttachmentsToShow.size() -1 ? currentPreviewPosition == listOfAttachmentsToShow.size() -1 ?
0 : currentPreviewPosition + 1; 0 : currentPreviewPosition + 1;
// show the image // show the image
showImage(currentPreviewPosition); showImage(currentPreviewPosition);
@ -332,40 +332,54 @@ public class Carousel extends Composite{
showLoader(); showLoader();
final EnhancedImage imageToShow = listOfAttachmentsToShow.get(index); final EnhancedImage imageToShow = listOfAttachmentsToShow.get(index);
final String url = imageToShow.getImageUrl();
// when image is downloaded ... // check if it was already shown before and cached...
shownImage.addLoadHandler(new LoadHandler() { if(imageToShow.getCachedImage() != null){
@Override shownImage = imageToShow.getCachedImage();
public void onLoad(LoadEvent event) { setOrientation(imageToShow, imageToShow.getOrientation());
// call only if undefined }else{
if(imageToShow.getOrientation().equals(Orientation.UNDEFINED)){
imageServices.getImageOrientation(url, new AsyncCallback<Orientation>() { final String url = imageToShow.getImageUrl();
@Override // when image is downloaded ...
public void onSuccess(Orientation result) { shownImage.addLoadHandler(new LoadHandler() {
imageToShow.setOrientation(result);
setOrientation(imageToShow, result);
}
@Override @Override
public void onFailure(Throwable caught) { public void onLoad(LoadEvent event) {
imageToShow.setOrientation(Orientation.DO_NOT_ROTATE);
setOrientation(imageToShow, Orientation.DO_NOT_ROTATE);
}
});
}else{ // call only if undefined
setOrientation(imageToShow, imageToShow.getOrientation()); if(imageToShow.getOrientation().equals(Orientation.UNDEFINED)){
imageServices.getImageOrientation(url, new AsyncCallback<Orientation>() {
@Override
public void onSuccess(Orientation result) {
imageToShow.setOrientation(result);
setOrientation(imageToShow, result);
}
@Override
public void onFailure(Throwable caught) {
imageToShow.setOrientation(Orientation.DO_NOT_ROTATE);
setOrientation(imageToShow, Orientation.DO_NOT_ROTATE);
}
});
}else{
setOrientation(imageToShow, imageToShow.getOrientation());
}
// cache it
imageToShow.setCachedImage(shownImage);
} }
} });
});
// fetch the image from the url // fetch the image from the url
shownImage.setUrl(url); shownImage.setUrl(url);
}
// change image tooltip // change image tooltip
shownImage.setTitle(imageToShow.getToolTipToShow()); shownImage.setTitle(imageToShow.getToolTipToShow());
@ -427,10 +441,10 @@ public class Carousel extends Composite{
default: shownImage.addStyleName("rotate-0"); default: shownImage.addStyleName("rotate-0");
} }
// few ms are needed to remove/add the style, after that we remove the loading image // few ms are needed to remove/add the style, after that we remove the loading image
Timer t = new Timer() { Timer t = new Timer() {
@Override @Override
public void run() { public void run() {
removeLoader(); removeLoader();