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.adho.dhconvalidator.ui.ConverterPanel.java

/** Provides a new FileDownloader with the conversion result. */
private void prepareForResultDownload() {
    downloadInfo.setVisible(true);//w  w w .  ja  va 2 s  .c om

    // detach the old file downloader
    if (currentFileDownloader != null) {
        currentFileDownloader.remove();
    }

    StreamResource resultStreamResource = new StreamResource(new StreamSource() {

        @Override
        public InputStream getStream() {
            return createResultStream();
        }
    }, filename.substring(0, filename.lastIndexOf('.')) + ".dhc");

    resultStreamResource.setCacheTime(0);

    currentFileDownloader = new FileDownloader(resultStreamResource);
    currentFileDownloader.extend(btDownloadResult);

    btDownloadResult.setVisible(true);
}

From source file:org.adho.dhconvalidator.ui.DHConvalidatorExample.java

@Override
protected void init(VaadinRequest request) {
    try {/*from w w w  .  j av  a2 s.com*/
        Messages.setLocaleProvider(VaadinSessionLocaleProvider.INSTANCE);

        VerticalLayout content = new VerticalLayout();
        content.setMargin(true);
        content.setSpacing(true);

        setContent(content);

        HeaderPanel headerPanel = new HeaderPanel(null);
        headerPanel.getBackLink().setVisible(false);

        content.addComponent(headerPanel);

        // prepare downloader for input file
        Button btGetInputfile = new Button(Messages.getString("DHConvalidatorExample.btInputCaption"));
        content.addComponent(btGetInputfile);

        FileDownloader inputFileDownloader = new FileDownloader(new StreamResource(new StreamSource() {

            @Override
            public InputStream getStream() {
                return Thread.currentThread().getContextClassLoader().getResourceAsStream(
                        "/org/adho/dhconvalidator/conversion/example/1_Digital_Humanities.odt");
            }
        }, "1_Digital_Humanities.odt"));
        inputFileDownloader.extend(btGetInputfile);

        // prepare downloader for output file
        Button btGetOutputfile = new Button(
                Messages.getString("DHConvalidatorExample.btConversionResultCaption"));
        content.addComponent(btGetOutputfile);

        FileDownloader outputFileDownloader = new FileDownloader(new StreamResource(new StreamSource() {

            @Override
            public InputStream getStream() {
                return Thread.currentThread().getContextClassLoader().getResourceAsStream(
                        "/org/adho/dhconvalidator/conversion/example/1_Digital_Humanities.dhc");
            }
        }, "1_Digital_Humanities.dhc"));
        outputFileDownloader.extend(btGetOutputfile);

        // setup visual feedback
        Label preview = new Label("", ContentMode.HTML);
        preview.addStyleName("tei-preview");
        preview.setWidth("800px");

        content.addComponent(preview);
        content.setComponentAlignment(preview, Alignment.MIDDLE_CENTER);

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        IOUtils.copy(Thread.currentThread().getContextClassLoader().getResourceAsStream(
                "/org/adho/dhconvalidator/conversion/example/1_Digital_Humanities.html"), buffer);

        preview.setValue(buffer.toString("UTF-8"));

        Page.getCurrent().setTitle(Messages.getString("DHConvalidatorExample.title"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.adho.dhconvalidator.ui.PaperSelectionPanel.java

/** Setup UI. */
private void initComponents() {
    Label info = new Label(Messages.getString("PaperSelectionPanel.hintMsg"), ContentMode.HTML);

    languages = new ComboBox(Messages.getString("PaperSelectionPanel.language"),
            Arrays.asList(SubmissionLanguage.values()));
    languages.setNullSelectionAllowed(false);
    languages.setValue(SubmissionLanguage
            .valueOf(PropertyKey.defaultSubmissionLanguage.getValue(SubmissionLanguage.ENGLISH.name())));

    paperTable = new Table(Messages.getString("PaperSelectionPanel.tableTitle"));
    paperTable.setSelectable(true);/*from  w  w w.  j  av a2  s . c  om*/
    paperTable.setMultiSelect(true);
    paperTable.setPageLength(4);
    paperTable.addContainerProperty("title", String.class, null);
    paperTable.setColumnHeader("title", Messages.getString("PaperSelectionPanel.titleColumnTitle"));
    paperTable.setWidth("100%");
    paperTable.setImmediate(true);

    btGenerate = new Button(Messages.getString("PaperSelectionPanel.generateButtonCaption"));
    StreamResource templateStreamResource = new StreamResource(new StreamSource() {
        @Override
        public InputStream getStream() {
            return createTemplates();
        }
    }, "your_personal_dh_templates.zip");

    templateStreamResource.setCacheTime(0);
    new FileDownloader(templateStreamResource).extend(btGenerate);

    addCenteredComponent(info);
    addCenteredComponent(languages);
    addCenteredComponent(paperTable);
    addCenteredComponent(btGenerate);

    postDownloadLabel = new Label(
            Messages.getString("PaperSelectionPanel.postDownloadInfo",
                    inputConverter.getTextEditorDescription(),
                    PropertyKey.base_url.getValue() + "popup/DHConvalidatorServices#!converter"),
            ContentMode.HTML);
    postDownloadLabel.addStyleName("postDownloadInfoRedAndBold");
    postDownloadLabel.setVisible(false);

    addCenteredComponent(postDownloadLabel);
}

From source file:org.apache.openaz.xacml.admin.components.PolicyEditor.java

License:Apache License

protected void initializeButtons() {
    ////from   w  w  w.j a v a  2s  . c om
    // The Save button
    //
    this.buttonSave.addClickListener(new ClickListener() {
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(ClickEvent event) {
            self.savePolicy();
        }
    });
    //
    // Attach a window opener to the View XML button
    //
    BrowserWindowOpener opener = new BrowserWindowOpener(new StreamResource(new StreamResource.StreamSource() {
        private static final long serialVersionUID = 1L;

        @Override
        public InputStream getStream() {
            try {
                if (logger.isDebugEnabled()) {
                    logger.debug("Setting view xml button to: " + self.file.getAbsolutePath());
                }
                return new FileInputStream(self.file);
            } catch (Exception e) {
                logger.error("Failed to open input stream " + self.file);
            }
            return null;
        }
    }, self.file.getName()));
    opener.setWindowName("_new");
    opener.extend(this.buttonViewXML);
}

From source file:org.apache.openaz.xacml.admin.components.PolicyEditor.java

License:Apache License

protected void initializeDownload() {
    ///*from w ww  . j  ava 2s  . com*/
    // Create a stream resource pointing to the file
    //
    StreamResource r = new StreamResource(new StreamResource.StreamSource() {
        private static final long serialVersionUID = 1L;

        @Override
        public InputStream getStream() {
            try {
                return new FileInputStream(self.file);
            } catch (Exception e) {
                logger.error("Failed to open input stream " + self.file);
            }
            return null;
        }
    }, self.file.getName());
    r.setCacheTime(-1);
    r.setMIMEType("application/xml");
    //
    // Extend a downloader to attach to the Export Button
    //
    FileDownloader downloader = new FileDownloader(r);
    downloader.extend(this.buttonExport);
}

From source file:org.apache.openaz.xacml.admin.util.OnDemandFileDownloader.java

License:Apache License

public OnDemandFileDownloader(OnDemandStreamResource resource) {
    super(new StreamResource(resource, ""));
    this.resource = resource;
    if (this.resource == null) {
        throw new NullPointerException("Can't send null resource");
    }/*from   w ww  .  j  av a  2  s .  c o  m*/
}

From source file:org.dussan.vaadin.dcharts.DCharts.java

License:Apache License

private StreamResource getChartResource() {
    return new StreamResource(new StreamSource() {
        private static final long serialVersionUID = -6463786579404065303L;

        @Override/*  www . j  a v  a2  s.c  o  m*/
        public InputStream getStream() {
            try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                ImageIO.write(getChartImage(), getChartImageFormat().getFormat(), bos);
                return new ByteArrayInputStream(bos.toByteArray());
            } catch (Exception e) {
                return new ByteArrayInputStream("".getBytes());
            }
        }
    }, getDownloadFilename() + getChartImageFormat().getFormatExtension());
}

From source file:org.ikasan.dashboard.ui.mappingconfiguration.panel.MappingConfigurationPanel.java

License:BSD License

/**
 * Helper method to get the stream associated with the export of the file.
 * /* w  w w .  j  a  v  a 2s .  c  o  m*/
 * @return the StreamResource associated with the export.
 */
private StreamResource getMappingConfigurationValuesExportStream() {
    StreamResource.StreamSource source = new StreamResource.StreamSource() {

        public InputStream getStream() {
            ByteArrayOutputStream stream = null;
            try {
                stream = getMappingConfigurationValuesExport();
            } catch (IOException e) {
                e.printStackTrace();
            }
            InputStream input = new ByteArrayInputStream(stream.toByteArray());
            return input;

        }
    };
    StreamResource resource = new StreamResource(source, "mappingConfigurationExport.xml");
    return resource;
}

From source file:org.ikasan.dashboard.ui.mappingconfiguration.panel.MappingConfigurationPanel.java

License:BSD License

/**
 * Helper method to get the stream associated with the export of the file.
 * // www .ja  v a  2s  . com
 * @return the StreamResource associated with the export.
 */
private StreamResource getMappingConfigurationExportStream() {
    StreamResource.StreamSource source = new StreamResource.StreamSource() {

        public InputStream getStream() {
            ByteArrayOutputStream stream = null;
            try {
                stream = getMappingConfigurationExport();
            } catch (IOException e) {
                e.printStackTrace();
            }
            InputStream input = new ByteArrayInputStream(stream.toByteArray());
            return input;

        }
    };
    StreamResource resource = new StreamResource(source, "mappingConfigurationExport.xml");
    return resource;
}

From source file:org.jumpmind.metl.ui.views.admin.LoggingPanel.java

License:Open Source License

private StreamResource getLogFileResource() {
    StreamSource ss = new StreamSource() {
        public InputStream getStream() {
            try {
                return new BufferedInputStream(new FileInputStream(logFile));
            } catch (FileNotFoundException e) {
                Notification note = new Notification("File Not Found",
                        "Could not find " + logFile.getName() + " to download");
                note.show(Page.getCurrent());
                return null;
            }//w w w .  ja v  a  2 s .  co m
        }
    };
    return new StreamResource(ss, logFile.getName());
}