2016-01-21 17:50:48 +01:00
|
|
|
package org.gcube.portlets.widgets.imagepreviewerwidget.client.ui;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.gcube.portlets.widgets.imagepreviewerwidget.client.EnhancedImage;
|
|
|
|
|
|
|
|
import com.github.gwtbootstrap.client.ui.Button;
|
|
|
|
import com.github.gwtbootstrap.client.ui.Image;
|
|
|
|
import com.github.gwtbootstrap.client.ui.Modal;
|
|
|
|
import com.google.gwt.core.client.GWT;
|
|
|
|
import com.google.gwt.event.dom.client.ClickEvent;
|
|
|
|
import com.google.gwt.uibinder.client.UiBinder;
|
|
|
|
import com.google.gwt.uibinder.client.UiField;
|
|
|
|
import com.google.gwt.uibinder.client.UiHandler;
|
2016-01-22 15:40:49 +01:00
|
|
|
import com.google.gwt.user.client.Window;
|
2016-01-21 17:50:48 +01:00
|
|
|
import com.google.gwt.user.client.ui.Composite;
|
2016-01-22 15:40:49 +01:00
|
|
|
import com.google.gwt.user.client.ui.HorizontalPanel;
|
|
|
|
import com.google.gwt.user.client.ui.VerticalPanel;
|
2016-01-21 17:50:48 +01:00
|
|
|
import com.google.gwt.user.client.ui.Widget;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A carousel for image/file previews.
|
|
|
|
* @author Costantino Perciante at ISTI-CNR
|
|
|
|
*/
|
|
|
|
public class Carousel extends Composite{
|
|
|
|
|
|
|
|
private static CarouselUiBinder uiBinder = GWT
|
|
|
|
.create(CarouselUiBinder.class);
|
|
|
|
|
|
|
|
interface CarouselUiBinder extends UiBinder<Widget, Carousel> {
|
|
|
|
}
|
2016-01-22 15:40:49 +01:00
|
|
|
|
2016-01-21 17:50:48 +01:00
|
|
|
@UiField
|
|
|
|
Modal mainModalPanel;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
Button showButton;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
Button prevButton;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
Button nextButton;
|
|
|
|
|
2016-01-22 15:40:49 +01:00
|
|
|
@UiField
|
|
|
|
Button downloadButton;
|
|
|
|
|
|
|
|
@UiField
|
|
|
|
Button closeButton;
|
|
|
|
|
2016-01-21 17:50:48 +01:00
|
|
|
@UiField
|
|
|
|
Image shownImage;
|
|
|
|
|
2016-01-22 15:40:49 +01:00
|
|
|
@UiField
|
|
|
|
HorizontalPanel horizontaFooterPanel;
|
|
|
|
|
2016-01-21 17:50:48 +01:00
|
|
|
private List<EnhancedImage> listOfAttachmentsToShow;
|
|
|
|
|
|
|
|
private int currentPreviewPosition;
|
2016-01-21 18:50:30 +01:00
|
|
|
|
2016-01-22 15:40:49 +01:00
|
|
|
/**
|
|
|
|
* Build a carousel that shows imagesToShow images.
|
|
|
|
* @param imagesToShow
|
|
|
|
*/
|
|
|
|
public Carousel(List<EnhancedImage> imagesToShow) {
|
2016-01-21 17:50:48 +01:00
|
|
|
|
|
|
|
initWidget(uiBinder.createAndBindUi(this));
|
2016-01-22 17:05:41 +01:00
|
|
|
|
|
|
|
// hide show button (not needed when the widget is used)
|
|
|
|
showButton.setVisible(false);
|
2016-01-21 17:50:48 +01:00
|
|
|
|
2016-01-22 15:40:49 +01:00
|
|
|
// set alignment of the horizontal panel's children
|
|
|
|
horizontaFooterPanel.setCellHorizontalAlignment(closeButton, HorizontalPanel.ALIGN_CENTER);
|
|
|
|
horizontaFooterPanel.setCellHorizontalAlignment(downloadButton, HorizontalPanel.ALIGN_CENTER);
|
|
|
|
horizontaFooterPanel.setCellHorizontalAlignment(prevButton, HorizontalPanel.ALIGN_LEFT);
|
|
|
|
horizontaFooterPanel.setCellHorizontalAlignment(nextButton, HorizontalPanel.ALIGN_RIGHT);
|
|
|
|
|
|
|
|
// set central buttons' cell widths to be equal
|
|
|
|
horizontaFooterPanel.setCellWidth(downloadButton, "200px");
|
|
|
|
horizontaFooterPanel.setCellWidth(closeButton, "200px");
|
|
|
|
|
|
|
|
// set vertical alignment
|
|
|
|
horizontaFooterPanel.setCellVerticalAlignment((Widget)nextButton, VerticalPanel.ALIGN_MIDDLE);
|
|
|
|
horizontaFooterPanel.setCellVerticalAlignment(prevButton, VerticalPanel.ALIGN_MIDDLE);
|
|
|
|
|
2016-01-21 17:50:48 +01:00
|
|
|
// prev button
|
|
|
|
prevButton.setText("<<");
|
|
|
|
|
|
|
|
// next button
|
|
|
|
nextButton.setText(">>");
|
2016-01-21 18:50:30 +01:00
|
|
|
|
2016-01-21 17:50:48 +01:00
|
|
|
// save the attachments list
|
|
|
|
listOfAttachmentsToShow = imagesToShow;
|
|
|
|
|
|
|
|
// take the first object
|
|
|
|
currentPreviewPosition = 0;
|
|
|
|
|
|
|
|
// show it
|
|
|
|
shownImage.setUrl(listOfAttachmentsToShow.get(currentPreviewPosition).getImageUrl());
|
|
|
|
|
|
|
|
// change image tooltip
|
|
|
|
shownImage.setTitle(listOfAttachmentsToShow.get(currentPreviewPosition).getToolTipToShow());
|
|
|
|
|
|
|
|
// change the title to the modal
|
|
|
|
mainModalPanel.setTitle(listOfAttachmentsToShow.get(currentPreviewPosition).getTitleToShow());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@UiHandler("showButton")
|
|
|
|
public void onClick(ClickEvent e){
|
|
|
|
|
|
|
|
mainModalPanel.show();
|
|
|
|
|
|
|
|
}
|
2016-01-22 17:05:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to show this carousel.
|
|
|
|
*/
|
|
|
|
public void show(){
|
|
|
|
|
|
|
|
mainModalPanel.show();
|
|
|
|
}
|
2016-01-21 17:50:48 +01:00
|
|
|
|
2016-01-22 15:40:49 +01:00
|
|
|
@UiHandler("closeButton")
|
|
|
|
public void closeOnClick(ClickEvent e){
|
|
|
|
|
|
|
|
mainModalPanel.hide();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@UiHandler("downloadButton")
|
|
|
|
public void downloadOnClick(ClickEvent e){
|
|
|
|
|
|
|
|
Window.open(listOfAttachmentsToShow.get(currentPreviewPosition).getDownloadUrl(), "_blank", "");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:50:48 +01:00
|
|
|
@UiHandler("prevButton")
|
|
|
|
public void onClickPrev(ClickEvent e){
|
|
|
|
|
|
|
|
int prevPreviewPosition = currentPreviewPosition == 0 ? listOfAttachmentsToShow.size() - 1 : currentPreviewPosition - 1;
|
|
|
|
currentPreviewPosition = prevPreviewPosition;
|
|
|
|
|
|
|
|
// show it
|
|
|
|
shownImage.setUrl(listOfAttachmentsToShow.get(currentPreviewPosition).getImageUrl());
|
|
|
|
|
|
|
|
// change image tooltip
|
|
|
|
shownImage.setTitle(listOfAttachmentsToShow.get(currentPreviewPosition).getToolTipToShow());
|
|
|
|
|
|
|
|
// change the title to the modal
|
|
|
|
mainModalPanel.setTitle(listOfAttachmentsToShow.get(currentPreviewPosition).getTitleToShow());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@UiHandler("nextButton")
|
|
|
|
public void onClickNext(ClickEvent e){
|
|
|
|
|
|
|
|
int nextPreviewPosition = currentPreviewPosition == listOfAttachmentsToShow.size() -1 ?
|
|
|
|
0 : currentPreviewPosition + 1;
|
|
|
|
currentPreviewPosition = nextPreviewPosition;
|
|
|
|
|
|
|
|
// show it
|
|
|
|
shownImage.setUrl(listOfAttachmentsToShow.get(currentPreviewPosition).getImageUrl());
|
|
|
|
|
|
|
|
// change image tooltip
|
|
|
|
shownImage.setTitle(listOfAttachmentsToShow.get(currentPreviewPosition).getToolTipToShow());
|
|
|
|
|
|
|
|
// change the title to the modal
|
|
|
|
mainModalPanel.setTitle(listOfAttachmentsToShow.get(currentPreviewPosition).getTitleToShow());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the current image.
|
|
|
|
* @param newPosition index of the new image to show
|
|
|
|
*/
|
2016-01-22 15:40:49 +01:00
|
|
|
public void changeIndexImageShown(int newPosition){
|
2016-01-21 17:50:48 +01:00
|
|
|
|
|
|
|
if(newPosition < 0 || newPosition >= listOfAttachmentsToShow.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
currentPreviewPosition = newPosition;
|
|
|
|
|
|
|
|
// show it
|
|
|
|
shownImage.setUrl(listOfAttachmentsToShow.get(currentPreviewPosition).getImageUrl());
|
|
|
|
|
|
|
|
// change image tooltip
|
|
|
|
shownImage.setTitle(listOfAttachmentsToShow.get(currentPreviewPosition).getToolTipToShow());
|
|
|
|
|
|
|
|
// change the title to the modal
|
|
|
|
mainModalPanel.setTitle(listOfAttachmentsToShow.get(currentPreviewPosition).getTitleToShow());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|