Example usage for com.vaadin.ui Html5File setStreamVariable

List of usage examples for com.vaadin.ui Html5File setStreamVariable

Introduction

In this page you can find the example usage for com.vaadin.ui Html5File setStreamVariable.

Prototype

public void setStreamVariable(StreamVariable streamVariable) 

Source Link

Document

Sets the StreamVariable that into which the file contents will be written.

Usage

From source file:ui.helper.ImageDropBox.java

License:Apache License

@Override
public void drop(final DragAndDropEvent dropEvent) {

    // expecting this to be an html5 drag
    Notification.show("Drop Event...", Notification.Type.WARNING_MESSAGE);
    final DragAndDropWrapper.WrapperTransferable tr = (DragAndDropWrapper.WrapperTransferable) dropEvent
            .getTransferable();//from   www  .  ja v  a2  s.  c  o m
    final Html5File[] files = tr.getFiles();
    if (files != null) {
        for (final Html5File html5File : files) {
            final String fileName = html5File.getFileName();

            if (html5File.getFileSize() > FILE_SIZE_LIMIT) {
                Notification.show("File rejected. Max 2Mb files are accepted by Sampler",
                        Notification.Type.WARNING_MESSAGE);
            } else {

                final ByteArrayOutputStream bas = new ByteArrayOutputStream();
                final StreamVariable streamVariable = new StreamVariable() {

                    @Override
                    public OutputStream getOutputStream() {
                        return bas;
                    }

                    @Override
                    public boolean listenProgress() {
                        return false;
                    }

                    @Override
                    public void onProgress(final StreamVariable.StreamingProgressEvent event) {
                    }

                    @Override
                    public void streamingStarted(final StreamVariable.StreamingStartEvent event) {
                    }

                    @Override
                    public void streamingFinished(final StreamVariable.StreamingEndEvent event) {
                        storeImageInDB(bas.toByteArray());
                        showFile(fileName, html5File.getType(), bas);
                        progress.setVisible(false);
                    }

                    @Override
                    public void streamingFailed(final StreamVariable.StreamingErrorEvent event) {
                        progress.setVisible(false);
                    }

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

                    private void storeImageInDB(byte[] bytes) {
                        Notification.show("Storing in DB", Notification.Type.WARNING_MESSAGE);
                        ServiceLocator.findLifetimeService().addUserPhoto(userId, bytes);
                    }

                };
                html5File.setStreamVariable(streamVariable);
                progress.setVisible(true);
            }
        }

    } else {
        final String text = tr.getText();
        if (text != null) {
            showText(text);
        }
    }
}

From source file:v7cr.vaadin.V7MultiFileUpload.java

License:Open Source License

public void drop(DragAndDropEvent event) {
    DragAndDropWrapper.WrapperTransferable transferable = (WrapperTransferable) event.getTransferable();
    Html5File[] files = transferable.getFiles();
    for (final Html5File html5File : files) {
        final ProgressIndicator pi = new ProgressIndicator();
        pi.setCaption(html5File.getFileName());
        progressBars.addComponent(pi);/*  w  w  w . j  av  a  2s  .  c  o m*/
        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(StreamingProgressEvent event) {
                float p = (float) event.getBytesReceived() / (float) event.getContentLength();
                pi.setValue(p);
            }

            public void streamingStarted(StreamingStartEvent event) {
                name = event.getFileName();
                mime = event.getMimeType();

            }

            public void streamingFinished(StreamingEndEvent event) {
                progressBars.removeComponent(pi);
                handleFile(receiver.getFile(), html5File.getFileName(), html5File.getType(),
                        html5File.getFileSize());
                receiver.setValue(null);
            }

            public void streamingFailed(StreamingErrorEvent event) {
                progressBars.removeComponent(pi);
            }

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

}