share-updates/src/main/java/org/gcube/portlets/user/shareupdates/client/view/AttachmentPreviewer.java

127 lines
3.3 KiB
Java

package org.gcube.portlets.user.shareupdates.client.view;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
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.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
public class AttachmentPreviewer extends Composite {
private static AttachmentPreviewerUiBinder uiBinder = GWT
.create(AttachmentPreviewerUiBinder.class);
interface AttachmentPreviewerUiBinder extends
UiBinder<Widget, AttachmentPreviewer> {
}
public AttachmentPreviewer() {
initWidget(uiBinder.createAndBindUi(this));
}
private static final String DELETE_ATTACHMENT = "The attachment won't be saved. Would you like to continue?";
@UiField
HTML deleteAttachment;
@UiField
Image imagePreview;
@UiField
Label fileName;
@UiField
Label resultLabel;
@UiField
Image resultImage;
// Parent of this AttachmentPreviewer object
private Placeholder parent;
// the ShareUpdateForm
private ShareUpdateForm shareUpdateForm;
public AttachmentPreviewer(String fileName, String urlImagePreview, Placeholder parent, ShareUpdateForm shareUpdateForm) {
initWidget(uiBinder.createAndBindUi(this));
// set filename and temp attachment url
this.fileName.setText(fileName);
this.imagePreview.setUrl(urlImagePreview);
// style the delete button
this.deleteAttachment.setStyleName("su-deleteAttachment");
this.deleteAttachment.setTitle("Cancel");
// save parent
this.parent = parent;
// save the shareUpdateForm object, since it maintains the list of attached files
this.shareUpdateForm = shareUpdateForm;
}
@UiHandler("deleteAttachment")
void onClick(ClickEvent e) {
// alert the user
boolean confirm = Window.confirm(DELETE_ATTACHMENT);
if(!confirm)
return;
// we have to remove the AttachmentPreview object (that is, this object) and
// remove the file from the List of AttachedFiles
parent.remove(this);
shareUpdateForm.removeAttachedFile(this);
}
/**
* set the label and the that shows if the file has been saved or not
* @param result
* @param urlImageResult
*/
public void setResultAttachment(String result, String urlImageResult){
this.resultLabel.setText(result);
this.resultImage.setUrl(urlImageResult);
}
/**
* Change the image preview of the attachment from the default one
* @param urlImagePreview
*/
public void setImagePreview(String urlImagePreview){
this.imagePreview.setUrl(urlImagePreview);
}
/**
* Change style of part of this object to allow the user to retry to upload the file
* @param tooltip
* @param retryToAttachImageUrl
*/
public void setImagePreviewToRetry(String tooltip, String retryToAttachImageUrl) {
this.imagePreview.setUrl(retryToAttachImageUrl);
this.imagePreview.setTitle(tooltip);
// add the handler on the icon
this.imagePreview.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("Retry to attach handler to be implemented...");
}
});
}
}