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:com.hivesys.dashboard.view.repository.DragAndDropBox.java

@Override
public void drop(final DragAndDropEvent dropEvent) {

    // expecting this to be an html5 drag
    final DragAndDropWrapper.WrapperTransferable tr = (DragAndDropWrapper.WrapperTransferable) dropEvent
            .getTransferable();/*from w w  w  .j  ava 2 s .c  om*/
    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 100Mb files are accepted!",
                        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) {
                        progress.setVisible(false);
                        showFile(fileName, html5File.getType(), bas);
                        processFile(fileName, html5File.getType(), bas);
                    }

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

                    @Override
                    public boolean isInterrupted() {
                        return false;
                    }
                };
                html5File.setStreamVariable(streamVariable);
                progress.setVisible(true);
            }
        }

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

From source file:com.klwork.explorer.ui.custom.UploadComponent.java

License:Apache License

public void drop(DragAndDropEvent event) {
    WrapperTransferable transferable = (WrapperTransferable) event.getTransferable();
    Html5File[] files = transferable.getFiles();
    if (files.length > 0) {
        final Html5File file = files[0]; // only support for one file upload at this moment
        file.setStreamVariable(new StreamVariable() {

            private static final long serialVersionUID = 1L;

            public void streamingStarted(StreamingStartEvent event) {
                uploadStarted(null); // event doesnt matter here
            }// w w  w.  j a v a  2s  .c o m

            public void streamingFinished(StreamingEndEvent event) {
                uploadFinished(null); // event doesnt matter here
            }

            public void streamingFailed(StreamingErrorEvent event) {
                uploadFailed(null);
            }

            public void onProgress(StreamingProgressEvent event) {
                updateProgress(event.getBytesReceived(), event.getContentLength());
            }

            public boolean listenProgress() {
                return true;
            }

            public boolean isInterrupted() {
                return false;
            }

            public OutputStream getOutputStream() {
                return receiver.receiveUpload(file.getFileName(), file.getType());
            }
        });
    }
}

From source file:com.peergreen.webconsole.scope.deployment.internal.dd.DeploymentDropHandler.java

License:Open Source License

@Override
public void drop(DragAndDropEvent event) {
    final Component component = event.getTransferable().getSourceComponent();
    Collection<DeployableEntry> draggedDeployableEntries;
    if (component instanceof Table) {
        Table table = (Table) component;
        draggedDeployableEntries = (Collection<DeployableEntry>) table.getValue();

        DataBoundTransferable t = (DataBoundTransferable) event.getTransferable();
        DeployableEntry deployableEntry = (DeployableEntry) t.getItemId();

        if (draggedDeployableEntries.size() <= 1 || (draggedDeployableEntries.size() > 1
                && !draggedDeployableEntries.contains(deployableEntry))) {
            draggedDeployableEntries = Collections.singleton(deployableEntry);
        }//  www.  j  a  v a2s.  c o m

        for (final DeployableEntry entry : draggedDeployableEntries) {
            if (!deployableContainer.equals(entry.getContainer())) {
                if (entry.isDeployable()) {
                    // single artifact
                    deployableContainer.receive(entry);
                } else {
                    // dragging a directory
                    String message = String.format(
                            "Would you like to create a deployment plan <br />with the content of <b>%s</b> ?",
                            entry.getName());
                    Label label = new Label(message, ContentMode.HTML);
                    ConfirmDialog.show(deployableContainer.getView().getUI(), label,
                            new ConfirmDialog.Listener() {
                                @Override
                                public void onClose(boolean isConfirmed) {
                                    if (isConfirmed) {
                                        for (DeployableEntry child : entry.getChildren()) {
                                            deploymentViewManager.addToDeploymentPlan(child.getUri());
                                        }
                                        deploymentViewManager.showDeploymentPlanView();
                                    }
                                }
                            });
                }
            }
        }
    } else {
        try {
            Html5File[] files = ((DragAndDropWrapper.WrapperTransferable) event.getTransferable()).getFiles();
            for (Html5File file : files) {
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(
                            Constants.STORAGE_DIRECTORY + File.separator + file.getFileName(), true);
                } catch (FileNotFoundException e) {
                    // do nothing
                }
                file.setStreamVariable(new DesktopDragAndDropStream(fos, file.getFileName(), notifierService,
                        deployableContainer));
            }
        } catch (Exception e) {
            // do nothing
        }
    }
}

From source file:gov.va.ds4p.ds4pmobileportal.ui.eHealthDirect.java

License:Open Source License

private Panel getDropPanel() {
    Panel p = new Panel();
    VerticalLayout v = (VerticalLayout) p.getContent();
    v.setSpacing(true);/*from   w  w w  . ja  v a  2s  . c o  m*/
    //v.setHeight("100%");
    p.setStyleName(Runo.PANEL_LIGHT);

    Panel wrapperPanel = new Panel();
    VerticalLayout vDPanel = (VerticalLayout) wrapperPanel.getContent();
    deleteBTN.setIcon(new ThemeResource("../runo/icons/64/folder-add.png"));
    deleteBTN.setWidth("64px");
    deleteBTN.setHeight("64px");

    vDPanel.addComponent(deleteBTN);
    final DragAndDropWrapper wrapper = new DragAndDropWrapper(wrapperPanel);
    wrapper.setWidth("100px");
    wrapper.setHeight("100px");

    wrapperPanel.setStyleName(Runo.PANEL_LIGHT);

    wrapper.setDropHandler(new DropHandler() {

        @Override
        public void drop(DragAndDropEvent event) {
            // expecting this to be an html5 drag
            DragAndDropWrapper.WrapperTransferable tr = (DragAndDropWrapper.WrapperTransferable) event
                    .getTransferable();
            Html5File[] files = tr.getFiles();
            if (files != null) {
                for (final Html5File html5File : files) {
                    final String fileName = html5File.getFileName();

                    if (html5File.getFileSize() > FILE_SIZE_LIMIT) {
                        getWindow().showNotification("File rejected. Max 2MB files are accepted by Sampler",
                                Window.Notification.TYPE_WARNING_MESSAGE);
                    } else {

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

                            public OutputStream getOutputStream() {
                                return bas;
                            }

                            public boolean listenProgress() {
                                return false;
                            }

                            public void onProgress(StreamVariable.StreamingProgressEvent event) {
                            }

                            public void streamingStarted(StreamVariable.StreamingStartEvent event) {
                            }

                            public void streamingFinished(StreamVariable.StreamingEndEvent event) {
                                progress.setVisible(false);
                                Boolean res = updateProviderProcessingInbox(bas.toByteArray());
                                if (res.booleanValue()) {
                                    getWindow().showNotification(
                                            "Processing Complete: " + fileName + " " + html5File.getType() + " "
                                                    + html5File.getFileSize(),
                                            Window.Notification.TYPE_HUMANIZED_MESSAGE);
                                } else {
                                    getWindow().showNotification(
                                            "Processing Error: You Do Not Have Necessary Permissions to Receive and Process Files.",
                                            Window.Notification.TYPE_TRAY_NOTIFICATION);
                                }
                            }

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

                            public boolean isInterrupted() {
                                return false;
                            }
                        };
                        html5File.setStreamVariable(streamVariable);
                        progress.setVisible(true);
                    }
                }

            } else {
                String text = tr.getText();
                if (text != null) {
                    getWindow().showNotification(text, Window.Notification.TYPE_HUMANIZED_MESSAGE);
                }
            }
        }

        @Override
        public AcceptCriterion getAcceptCriterion() {
            return AcceptAll.get();
        }
    });

    v.addComponent(wrapper);

    return p;
}

From source file:info.magnolia.ui.form.field.upload.AbstractUploadField.java

License:Open Source License

/**
 * Drop zone Handler.//  w ww . j ava 2  s  .  com
 */
@Override
public void drop(DragAndDropEvent event) {
    final DragAndDropWrapper.WrapperTransferable transferable = (WrapperTransferable) event.getTransferable();
    final Html5File[] files = transferable.getFiles();
    if (files == null) {
        return;
    }

    // start polling immediately on drop
    startPolling();

    for (final Html5File html5File : files) {
        html5File.setStreamVariable(new StreamVariable() {

            private String name;
            private String mime;

            @Override
            public OutputStream getOutputStream() {
                return getValue().receiveUpload(name, mime);
            }

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

            @Override
            public void onProgress(StreamingProgressEvent event) {
                updateProgress(event.getBytesReceived(), event.getContentLength());
            }

            @Override
            public void streamingStarted(StreamingStartEvent event) {
                setDragAndDropUploadInterrupted(false);
                name = event.getFileName();
                mime = event.getMimeType();
                if (StringUtils.isEmpty(mime)) {
                    String extension = StringUtils.substringAfterLast(name, ".");
                    mime = MIMEMapping.getMIMEType(extension);
                    if (StringUtils.isEmpty(mime)) {
                        log.warn("Couldn't find mimeType in MIMEMappings for file extension: {}", extension);
                    }
                }
                StartedEvent startEvent = new StartedEvent(upload, name, mime, event.getContentLength());
                uploadStarted(startEvent);
            }

            @Override
            public void streamingFinished(StreamingEndEvent event) {
                FinishedEvent uploadEvent = new FinishedEvent(upload, event.getFileName(), event.getMimeType(),
                        event.getContentLength());
                uploadFinished(uploadEvent);
            }

            @Override
            public void streamingFailed(StreamingErrorEvent event) {
                FailedEvent failedEvent = new FailedEvent(upload, event.getFileName(), event.getMimeType(),
                        event.getContentLength());
                uploadFailed(failedEvent);
                setDragAndDropUploadInterrupted(false);
            }

            @Override
            public synchronized boolean isInterrupted() {
                return isDragAndDropUploadInterrupted();
            }

        });
    }
}

From source file:net.pkhsolutions.pecsapp.ui.components.PictureLayout.java

License:Open Source License

private void uploadFile(Html5File file) {
    final MimeType mimeType = MimeType.valueOf(file.getType());
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final StreamVariable streamVariable = new StreamVariable() {
        @Override/*from ww  w  .j a v  a 2s  .  c  o m*/
        public OutputStream getOutputStream() {
            return baos;
        }

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

        @Override
        public void onProgress(StreamingProgressEvent event) {
        }

        @Override
        public void streamingStarted(StreamingStartEvent event) {
        }

        @Override
        public void streamingFinished(StreamingEndEvent event) {
            hideProgressBar();
            model.upload(new ByteArrayInputStream(baos.toByteArray()), mimeType);
        }

        @Override
        public void streamingFailed(StreamingErrorEvent event) {
            hideProgressBar();
        }

        @Override
        public boolean isInterrupted() {
            return false;
        }
    };
    showProgressBar();
    file.setStreamVariable(streamVariable);
}

From source file:org.activiti.explorer.ui.custom.UploadComponent.java

License:Apache License

public void drop(DragAndDropEvent event) {
    WrapperTransferable transferable = (WrapperTransferable) event.getTransferable();
    Html5File[] files = transferable.getFiles();
    if (files != null && files.length > 0) {
        final Html5File file = files[0]; // only support for one file upload at this moment
        file.setStreamVariable(new StreamVariable() {

            private static final long serialVersionUID = 1L;

            public void streamingStarted(StreamingStartEvent event) {
                uploadStarted(null); // event doesnt matter here
            }/*www . j a v a 2 s . co m*/

            public void streamingFinished(StreamingEndEvent event) {
                uploadFinished(null); // event doesnt matter here
            }

            public void streamingFailed(StreamingErrorEvent event) {
                uploadFailed(null);
            }

            public void onProgress(StreamingProgressEvent event) {
                updateProgress(event.getBytesReceived(), event.getContentLength());
            }

            public boolean listenProgress() {
                return true;
            }

            public boolean isInterrupted() {
                return false;
            }

            public OutputStream getOutputStream() {
                return receiver.receiveUpload(file.getFileName(), file.getType());
            }
        });
    }
}

From source file:org.escidoc.browser.ui.dnd.FilesDropBox.java

License:Open Source License

private void handleFiles(final DragAndDropEvent dropEvent) {
    numberOfFiles = getFilesFrom(dropEvent).length;

    for (final Html5File html5File : getFilesFrom(dropEvent)) {
        if (html5File.getFileSize() > FILE_SIZE_LIMIT) {
            showFileSizeWarning();/*from   w w w.j a v  a  2  s. c  o m*/
            return;
        }
        html5File.setStreamVariable(createStreamVariable(html5File));
        progressView.setVisible(true);
    }
}

From source file:org.hip.vif.web.util.UploadComponent.java

License:Open Source License

private Component createDropBox(final IBibliographyTask inTask) {
    final CssLayout lDropPane = new CssLayout();
    lDropPane.setWidth("200px"); //$NON-NLS-1$
    lDropPane.setHeight("40px"); //$NON-NLS-1$
    lDropPane.addStyleName("v-textfield"); //$NON-NLS-1$
    final Label lHint = new Label(Activator.getMessages().getMessage("ui.upload.drop.box")); //$NON-NLS-1$
    lHint.setStyleName("vif-drop-hint"); //$NON-NLS-1$
    lDropPane.addComponent(lHint);/*from   ww w  .j a  va 2 s . co  m*/

    final DragAndDropWrapper outDrop = new DragAndDropWrapper(lDropPane);
    outDrop.setDropHandler(new DropHandler() {
        @Override
        public AcceptCriterion getAcceptCriterion() { // NOPMD
            return AcceptAll.get();
        }

        @Override
        public void drop(final DragAndDropEvent inEvent) { // NOPMD
            final Transferable lTransferable = inEvent.getTransferable();
            if (lTransferable instanceof WrapperTransferable) {
                final Html5File[] lFiles = ((WrapperTransferable) lTransferable).getFiles();
                for (final Html5File lFile : lFiles) {
                    lFile.setStreamVariable(createStreamVariable(lFile.getFileName(), inTask));
                }
            }
        }
    });

    outDrop.setSizeUndefined();
    outDrop.setImmediate(true);
    return outDrop;
}

From source file:org.vaadin.easyuploads.MultiFileUploadExt.java

License:Open Source License

@Override
public void drop(DragAndDropEvent event) {
    DragAndDropWrapper.WrapperTransferable transferable = (DragAndDropWrapper.WrapperTransferable) event
            .getTransferable();//w  w  w.  j  a v  a  2 s.  c o m
    Html5File[] files = transferable.getFiles();
    for (final Html5File html5File : files) {
        final ProgressBar pi = new ProgressBar();
        pi.setCaption(html5File.getFileName());
        progressBars.addComponent(pi);
        final FileBuffer receiver = createReceiver();
        html5File.setStreamVariable(new StreamVariable() {
            private static final long serialVersionUID = 1L;
            private String name;
            private String mime;

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

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

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

            @Override
            public void streamingStarted(StreamVariable.StreamingStartEvent event) {
                name = event.getFileName();
                mime = event.getMimeType();
                UI.getCurrent().setPollInterval(300);
            }

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

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

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

}