com.wintindustries.pfserver.uploads.PFUploadUnit.java Source code

Java tutorial

Introduction

Here is the source code for com.wintindustries.pfserver.uploads.PFUploadUnit.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wintindustries.pfserver.uploads;

import com.vaadin.server.Page;
import com.vaadin.server.StreamVariable;
import com.vaadin.ui.Html5File;
import com.vaadin.ui.Notification;
import com.vaadin.ui.ProgressBar;
import com.vaadin.ui.ProgressIndicator;
import java.io.File;
import java.io.OutputStream;
import java.util.Collection;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.vaadin.easyuploads.FileBuffer;
import org.vaadin.easyuploads.FileFactory;
import org.vaadin.easyuploads.MultiFileUpload;
import org.vaadin.easyuploads.MultiUpload;
import org.vaadin.easyuploads.MultiUploadHandler;
import org.vaadin.easyuploads.UploadField;

/**
 *
 * @author Admin
 */
public class PFUploadUnit extends MultiFileUpload {

    private final PFUploadProgress progress = new PFUploadProgress();
    private Html5File html5File;

    public PFUploadUnit(Html5File file) {
        this.html5File = file;
        progress.setProgressState(progress.kUPLOADPROGRESS_IN_QUEUE);
    }

    @Override
    protected void initialize() {
        //    prepareUpload();

    }

    protected FileBuffer createReceiver() {
        FileBuffer receiver;
        receiver = new FileBuffer(UploadField.FieldType.FILE) {
            @Override
            public FileFactory getFileFactory() {
                return PFUploadUnit.this.getFileFactory();
            }
        };
        return receiver;
    }

    public void upload() {
        final ProgressIndicator pi = new ProgressIndicator();
        pi.setCaption(html5File.getFileName());
        progress.setCaption(html5File.getFileName());
        getprogressBarsLayout().addComponent(pi);
        final FileBuffer receiver = createReceiver();
        html5File.setStreamVariable(new StreamVariable() {

            private String name;
            private String mime;

            public OutputStream getOutputStream() {
                return receiver.receiveUpload(name, mime);
            }

            public boolean listenProgress() {
                return true;
            }

            public void onProgress(StreamVariable.StreamingProgressEvent event) {
                float p = (float) event.getBytesReceived() / (float) event.getContentLength();
                pi.setValue(p);
                progress.setProgress(p);
            }

            public void streamingStarted(StreamVariable.StreamingStartEvent event) {
                name = event.getFileName();
                mime = event.getMimeType();
                progress.setProgressState(progress.kUPLOADPROGRESS_UPLOADING);

            }

            public void streamingFinished(StreamVariable.StreamingEndEvent event) {
                getprogressBarsLayout().removeComponent(pi);
                handleFile(receiver.getFile(), html5File.getFileName(), html5File.getType(),
                        html5File.getFileSize());
                receiver.setValue(null);
                progress.setProgressState(progress.kUPLOADPROGRESS_DONE);
            }

            public void streamingFailed(StreamVariable.StreamingErrorEvent event) {
                getprogressBarsLayout().removeComponent(pi);
                progress.setProgressState(progress.kUPLOADPROGRESS_ERROR);
                progress.setErrmsg(event.toString());
                Notification note = new Notification("Upload Failed", event.toString(),
                        Notification.Type.ERROR_MESSAGE);
                note.show(Page.getCurrent());

            }

            public boolean isInterrupted() {
                return false;
            }
        });
    }

    @Override
    protected void handleFile(File file, String fileName, String mimeType, long length) {
        Notification note = new Notification(fileName + " Uploaded", Notification.Type.TRAY_NOTIFICATION);
        note.show(Page.getCurrent());

        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}