image-previewer-widget/src/main/java/org/gcube/portlets/widgets/imagepreviewerwidget/client/ui/Carousel.java

196 lines
5.3 KiB
Java
Raw Normal View History

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;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
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> {
}
@UiField
Modal mainModalPanel;
@UiField
Button showButton;
@UiField
Button prevButton;
@UiField
Button nextButton;
@UiField
Button downloadButton;
@UiField
Button closeButton;
@UiField
Image shownImage;
@UiField
HorizontalPanel horizontaFooterPanel;
private List<EnhancedImage> listOfAttachmentsToShow;
private int currentPreviewPosition;
/**
* Build a carousel that shows imagesToShow images.
* @param imagesToShow
*/
public Carousel(List<EnhancedImage> imagesToShow) {
initWidget(uiBinder.createAndBindUi(this));
// hide show button (not needed when the widget is used)
showButton.setVisible(false);
// 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);
// prev button
prevButton.setText("<<");
// next button
nextButton.setText(">>");
// 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();
}
/**
* Used to show this carousel.
*/
public void show(){
mainModalPanel.show();
}
@UiHandler("closeButton")
public void closeOnClick(ClickEvent e){
mainModalPanel.hide();
}
@UiHandler("downloadButton")
public void downloadOnClick(ClickEvent e){
Window.open(listOfAttachmentsToShow.get(currentPreviewPosition).getDownloadUrl(), "_blank", "");
}
@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
*/
public void changeIndexImageShown(int newPosition){
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());
}
}