Example usage for com.vaadin.server StreamResource StreamResource

List of usage examples for com.vaadin.server StreamResource StreamResource

Introduction

In this page you can find the example usage for com.vaadin.server StreamResource StreamResource.

Prototype

public StreamResource(StreamSource streamSource, String filename) 

Source Link

Document

Creates a new stream resource for downloading from stream.

Usage

From source file:org.jumpmind.metl.ui.views.deploy.EditAgentPanel.java

License:Open Source License

protected void exportConfiguration() {
    final String export = context.getConfigurationService().export(agent);
    StreamSource ss = new StreamSource() {
        private static final long serialVersionUID = 1L;

        public InputStream getStream() {
            try {
                return new ByteArrayInputStream(export.getBytes());
            } catch (Exception e) {
                log.error("Failed to export configuration", e);
                CommonUiUtils.notify("Failed to export configuration.", Type.ERROR_MESSAGE);
                return null;
            }//from w w w. j a  v a2s. co  m

        }
    };
    String datetime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    StreamResource resource = new StreamResource(ss,
            String.format("%s-config-%s.sql", agent.getName().toLowerCase().replaceAll(" ", "-"), datetime));
    final String KEY = "export";
    setResource(KEY, resource);
    Page.getCurrent().open(ResourceReference.create(resource, this, KEY).getURL(), null);
}

From source file:org.jumpmind.metl.ui.views.ExploreDirectoryView.java

License:Open Source License

protected Component fileLinkComponent(Table source, Object itemId, Object propertyId) {
    if (itemId instanceof FileInfo) {
        final FileInfo file = (FileInfo) itemId;
        if (!file.isDirectory()) {
            final Button button = new Button(file.getName());
            button.addStyleName(ValoTheme.BUTTON_LINK);
            button.addStyleName(ValoTheme.BUTTON_SMALL);
            button.setIcon(FontAwesome.FILE);
            StreamResource resource = new StreamResource(() -> stream(file), file.getName());
            FileDownloader fileDownloader = new FileDownloader(resource);
            fileDownloader.extend(button);
            return button;
        } else {//from   www .j a  v  a2s .c  om
            return new Label(file.getName());
        }
    } else {
        return new Label(((DirectoryResource) itemId).getName());

    }
}

From source file:org.jumpmind.vaadin.ui.common.ReadOnlyTextAreaDialog.java

License:Open Source License

private void buildDownloadButton(String title) {
    downloadButton = new Button("Download");
    final byte[] lobData = getLobData(title);
    if (lobData != null) {
        Resource resource = new StreamResource(new StreamSource() {

            private static final long serialVersionUID = 1L;

            public InputStream getStream() {
                return new ByteArrayInputStream(lobData);
            }/* ww w.  j a v a 2 s . c om*/

        }, title);
        FileDownloader fileDownloader = new FileDownloader(resource);
        fileDownloader.extend(downloadButton);
        buttonLayout.addComponent(downloadButton);
        buttonLayout.setComponentAlignment(downloadButton, Alignment.BOTTOM_CENTER);

        long fileSize = lobData.length;
        String sizeText = fileSize + " Bytes";
        if (fileSize / 1024 > 0) {
            sizeText = Math.round(fileSize / 1024.0) + " kB";
            fileSize /= 1024;
        }
        if (fileSize / 1024 > 0) {
            sizeText = Math.round(fileSize / 1024.0) + " MB";
            fileSize /= 1024;
        }
        if (fileSize / 1024 > 0) {
            sizeText = Math.round(fileSize / 1024.0) + " GB";
        }
        Label sizeLabel = new Label(sizeText);
        buttonLayout.addComponent(sizeLabel);
        buttonLayout.setExpandRatio(sizeLabel, 1.0f);
        buttonLayout.setComponentAlignment(sizeLabel, Alignment.BOTTOM_CENTER);
    }
}

From source file:org.jumpmind.vaadin.ui.sqlexplorer.DbExportDialog.java

License:Open Source License

private StreamResource createResource() {

    final String format = (String) formatSelect.getValue().toString();
    StreamSource ss = new StreamSource() {
        private static final long serialVersionUID = 1L;

        public InputStream getStream() {

            List<String> list = tableSelectionLayout.getSelectedTables();
            String[] array = new String[list.size()];
            list.toArray(array);//from w  w  w  . j  ava  2 s .  c o m

            createDbExport();

            String script;
            try {
                script = dbExport.exportTables(array);
                return new ByteArrayInputStream(script.getBytes());
            } catch (IOException e) {
                String msg = "Failed to export to a file";
                log.error(msg, e);
                CommonUiUtils.notify(msg, e);
            }
            return null;
        }
    };
    String datetime = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
    StreamResource sr = new StreamResource(ss,
            String.format("table-export-%s." + format.toLowerCase(), datetime));
    return sr;
}

From source file:org.lunifera.vaaclipse.ui.preferences.addon.internal.impexp.ExportPreferences.java

License:Open Source License

@SuppressWarnings("serial")
private StreamResource createResource() {
    return new StreamResource(new StreamSource() {
        @Override//  w w w  .  j a  v a  2s  .com
        public InputStream getStream() {
            if (preferencesBytes != null) {
                ByteArrayInputStream is = new ByteArrayInputStream(preferencesBytes);
                return is;
            } else
                return null;
        }
    }, "preferences.epf");
}

From source file:org.peergreen.vaadin.diagram.Compartment.java

License:Apache License

@Override
public void attach(final Diagram diagram) {
    // If there is an icon provided, wrap it in a StreamResource and make it available to client-side through the connector
    if (iconUrl != null) {
        diagram.addSharedResource(iconType, new StreamResource(new IconStreamSource(), iconType));
    }//from  www. j a  va  2s . com
    super.attach(diagram);
}

From source file:org.vaadin.addon.ewopener.demo.DemoUI.java

License:Apache License

private StreamResource generateResource() {
    StreamResource streamResource = new StreamResource(makeStreamSource(), "simpleTextFile.txt");
    streamResource.setCacheTime(0); // do not cache
    streamResource.setMIMEType("text/plain");
    return streamResource;
}

From source file:org.vaadin.addon.ewopener.demo.DemoUI.java

License:Apache License

private StreamResource streamContent() {
    StreamResource streamResource = new StreamResource(
            () -> new ByteArrayInputStream(LocalDateTime.now().toString().getBytes()), "simpleTextFile.txt");
    streamResource.setCacheTime(0); // do not cache
    streamResource.setMIMEType("text/plain");
    return streamResource;
}

From source file:org.vaadin.addon.ewopener.EnhancedBrowserWindowOpener.java

License:Apache License

/**
 * Sets a {@code resource} for this instance whose content will be generated
 * when the window will be opened./*from   w  w  w .j  a v  a2  s  .  com*/
 *
 * The content will be generated through {@code generator} instance.
 * The {@link StreamResource} could be customized through given {@code resourceCustomizer}.
 *
 * @param filename           The filename for the generated resource.
 * @param generator          The content generator.
 * @param resourceCustomizer The {@link StreamResource} customizer
 * @return current object for further customization
 */
public EnhancedBrowserWindowOpener withGeneratedContent(String filename, StreamResource.StreamSource generator,
        StreamResourceCustomizer resourceCustomizer) {
    Objects.requireNonNull(generator);
    Objects.requireNonNull(resourceCustomizer);
    StreamResource resource = new StreamResource(null, filename) {
        @Override
        public StreamResource.StreamSource getStreamSource() {
            return generator::getStream;
        }
    };
    resourceCustomizer.customize(resource);
    setResource(resource);
    return this;
}

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

License:Apache License

@Override
protected void updateDisplayComponent() {
    try {//  w w  w. j a  v a 2 s  .  co m
        Image image = (Image) display;
        // check if upload is an image
        if (getValue() != null && ImageIO.read(new ByteArrayInputStream(getValue())) != null) {
            // Update the image according to
            // https://vaadin.com/book/vaadin7/-/page/components.embedded.html
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String filename = df.format(new Date()) + getLastFileName();
            StreamResource resource = new StreamResource(new ImageSource(getValue()), filename);
            resource.setCacheTime(0);
            image.setSource(resource);
            image.setVisible(true);
        } else {
            image.setVisible(false);
            image.setSource(null);
            setValue(null);
        }
        image.markAsDirty();
        if (display.getParent() == null) {
            getRootLayout().addComponent(display);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}