Example usage for com.google.gwt.user.client.ui Label addDragLeaveHandler

List of usage examples for com.google.gwt.user.client.ui Label addDragLeaveHandler

Introduction

In this page you can find the example usage for com.google.gwt.user.client.ui Label addDragLeaveHandler.

Prototype

public HandlerRegistration addDragLeaveHandler(DragLeaveHandler handler) 

Source Link

Usage

From source file:org.wisepersist.gwt.uploader.demo.client.MultiUploadWithProgressBarAndDragAndDrop.java

License:Apache License

@Override
public Widget getUploaderPanel() {
    final VerticalPanel progressBarPanel = new VerticalPanel();
    final Map<String, ProgressBar> progressBars = new LinkedHashMap<String, ProgressBar>();
    final Map<String, Image> cancelButtons = new LinkedHashMap<String, Image>();
    uploader.setUploadURL("/upload").setButtonImageURL(AppResources.INSTANCE.upload().getSafeUri().asString())
            .setButtonWidth(133).setButtonHeight(22).setFileSizeLimit("50 MB")
            .setButtonCursor(Uploader.Cursor.HAND).setButtonAction(Uploader.ButtonAction.SELECT_FILES)
            .setFileQueuedHandler(new FileQueuedHandler() {
                public boolean onFileQueued(final FileQueuedEvent fileQueuedEvent) {
                    // Create a Progress Bar for this file
                    final ProgressBar progressBar = new ProgressBar(0.0, 1.0, 0.0,
                            new CancelProgressBarTextFormatter());
                    progressBar.setTitle(fileQueuedEvent.getFile().getName());
                    progressBar.setHeight("18px");
                    progressBar.setWidth("200px");
                    progressBars.put(fileQueuedEvent.getFile().getId(), progressBar);

                    // Add Cancel Button Image
                    final Image cancelButton = new Image(
                            AppResources.INSTANCE.cancel().getSafeUri().asString());
                    cancelButton.setStyleName("cancelButton");
                    cancelButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            uploader.cancelUpload(fileQueuedEvent.getFile().getId(), false);
                            progressBars.get(fileQueuedEvent.getFile().getId()).setProgress(-1.0d);
                            cancelButton.removeFromParent();
                        }//from   w ww .  j  a v  a  2  s . co m
                    });
                    cancelButtons.put(fileQueuedEvent.getFile().getId(), cancelButton);

                    // Add the Bar and Button to the interface
                    HorizontalPanel progressBarAndButtonPanel = new HorizontalPanel();
                    progressBarAndButtonPanel.add(progressBar);
                    progressBarAndButtonPanel.add(cancelButton);
                    progressBarPanel.add(progressBarAndButtonPanel);

                    return true;
                }
            }).setUploadProgressHandler(new UploadProgressHandler() {
                public boolean onUploadProgress(UploadProgressEvent uploadProgressEvent) {
                    ProgressBar progressBar = progressBars.get(uploadProgressEvent.getFile().getId());
                    progressBar.setProgress((double) uploadProgressEvent.getBytesComplete()
                            / uploadProgressEvent.getBytesTotal());
                    return true;
                }
            }).setUploadCompleteHandler(new UploadCompleteHandler() {
                public boolean onUploadComplete(UploadCompleteEvent uploadCompleteEvent) {
                    cancelButtons.get(uploadCompleteEvent.getFile().getId()).removeFromParent();
                    uploader.startUpload();
                    return true;
                }
            }).setFileDialogStartHandler(new FileDialogStartHandler() {
                public boolean onFileDialogStartEvent(FileDialogStartEvent fileDialogStartEvent) {
                    if (uploader.getStats().getUploadsInProgress() <= 0) {
                        // Clear the uploads that have completed, if none are in process
                        progressBarPanel.clear();
                        progressBars.clear();
                        cancelButtons.clear();
                    }
                    return true;
                }
            }).setFileDialogCompleteHandler(new FileDialogCompleteHandler() {
                public boolean onFileDialogComplete(FileDialogCompleteEvent fileDialogCompleteEvent) {
                    if (fileDialogCompleteEvent.getTotalFilesInQueue() > 0) {
                        if (uploader.getStats().getUploadsInProgress() <= 0) {
                            uploader.startUpload();
                        }
                    }
                    return true;
                }
            }).setFileQueueErrorHandler(new FileQueueErrorHandler() {
                public boolean onFileQueueError(FileQueueErrorEvent fileQueueErrorEvent) {
                    Window.alert("Upload of file " + fileQueueErrorEvent.getFile().getName()
                            + " failed due to [" + fileQueueErrorEvent.getErrorCode().toString() + "]: "
                            + fileQueueErrorEvent.getMessage());
                    return true;
                }
            }).setUploadErrorHandler(new UploadErrorHandler() {
                public boolean onUploadError(UploadErrorEvent uploadErrorEvent) {
                    cancelButtons.get(uploadErrorEvent.getFile().getId()).removeFromParent();
                    Window.alert("Upload of file " + uploadErrorEvent.getFile().getName() + " failed due to ["
                            + uploadErrorEvent.getErrorCode().toString() + "]: "
                            + uploadErrorEvent.getMessage());
                    return true;
                }
            });

    VerticalPanel verticalPanel = new VerticalPanel();
    verticalPanel.add(uploader);

    if (Uploader.isAjaxUploadWithProgressEventsSupported()) {
        final Label dropFilesLabel = new Label("Drop Files Here");
        dropFilesLabel.setStyleName("dropFilesLabel");
        dropFilesLabel.addDragOverHandler(new DragOverHandler() {
            public void onDragOver(DragOverEvent event) {
                if (!uploader.getButtonDisabled()) {
                    dropFilesLabel.addStyleName("dropFilesLabelHover");
                }
            }
        });
        dropFilesLabel.addDragLeaveHandler(new DragLeaveHandler() {
            public void onDragLeave(DragLeaveEvent event) {
                dropFilesLabel.removeStyleName("dropFilesLabelHover");
            }
        });
        dropFilesLabel.addDropHandler(new DropHandler() {
            public void onDrop(DropEvent event) {
                dropFilesLabel.removeStyleName("dropFilesLabelHover");

                if (uploader.getStats().getUploadsInProgress() <= 0) {
                    progressBarPanel.clear();
                    progressBars.clear();
                    cancelButtons.clear();
                }

                uploader.addFilesToQueue(Uploader.getDroppedFiles(event.getNativeEvent()));
                event.preventDefault();
            }
        });
        verticalPanel.add(dropFilesLabel);
    }

    HorizontalPanel horizontalPanel = new HorizontalPanel();
    horizontalPanel.add(verticalPanel);
    horizontalPanel.add(progressBarPanel);
    horizontalPanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
    horizontalPanel.setCellHorizontalAlignment(uploader, HorizontalPanel.ALIGN_LEFT);
    horizontalPanel.setCellHorizontalAlignment(progressBarPanel, HorizontalPanel.ALIGN_RIGHT);
    return horizontalPanel;
}