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:com.save.reports.maintenance.MaintenanceReportUI.java

void processExportDataToExcel() {
    ExportDataGridToExcel export = new ExportDataGridToExcel(mrDataGrid);
    export.workSheet();/*from  w  ww .  java  2 s  .  com*/

    StreamResource.StreamSource source = () -> {
        try {
            File f = new File(export.getFilePath());
            FileInputStream fis = new FileInputStream(f);
            return fis;
        } catch (Exception e) {
            ErrorLoggedNotification.showErrorLoggedOnWindow(e.toString(), this.getClass().getName());
            return null;
        }
    };

    Date date = new Date();

    StreamResource resource = new StreamResource(source, "MaintenanceReport" + "-" + date.getTime() + ".xls");
    resource.setMIMEType("application/vnd.ms-office");

    Page.getCurrent().open(resource, null, false);
}

From source file:com.save.reports.PromoDealAcknowledgementReport.java

public PromoDealAcknowledgementReport(int promoId) {
    this.promoId = promoId;

    setCaption("Acknowledgement Report");
    setWidth("800px");
    setHeight("600px");
    center();/* w ww .  j a  v  a 2s . c o m*/

    Connection conn = DBConnection.connect();

    HashMap hm = new HashMap();
    hm.put("PROMO_ID", getPromoId());

    InputStream template = this.getClass()
            .getResourceAsStream("/reports/PromoDealAcknowledgementFormReport.jasper");

    try {
        JasperPrint print = JasperFillManager.fillReport(template, hm, conn);
        file = File.createTempFile("output", ".pdf");
        JasperExportManager.exportReportToPdfFile(print, file.getPath());
    } catch (JRException | IOException ex) {
        Logger.getLogger(PromoDealAcknowledgementReport.class.getName()).log(Level.SEVERE, null, ex);
    }

    StreamResource.StreamSource source = () -> {
        try {
            FileInputStream fis = new FileInputStream(file);
            return fis;
        } catch (Exception e) {
            e.getMessage();
            return null;
        }
    };

    StreamResource resource = new StreamResource(source, "PromoDealAcknowledgementForm.pdf");
    resource.setMIMEType("application/pdf");

    VerticalLayout v = new VerticalLayout();
    v.setSizeFull();
    Embedded e = new Embedded();
    e.setSource(resource);
    e.setSizeFull();
    e.setType(Embedded.TYPE_BROWSER);
    v.addComponent(e);

    setContent(v);
}

From source file:com.save.reports.promodeals.PromoDealReportUI.java

void processExportDataToExcel() {
    ExportDataGridToExcel export = new ExportDataGridToExcel(promoDealGrid);
    export.workSheet();/*from w  w  w .j a v a  2  s  .c  om*/

    StreamResource.StreamSource source = () -> {
        try {
            File f = new File(export.getFilePath());
            FileInputStream fis = new FileInputStream(f);
            return fis;
        } catch (Exception e) {
            ErrorLoggedNotification.showErrorLoggedOnWindow(e.toString(), this.getClass().getName());
            return null;
        }
    };

    Date date = new Date();

    StreamResource resource = new StreamResource(source, "PromoDealsReport" + "-" + date.getTime() + ".xls");
    resource.setMIMEType("application/vnd.ms-office");

    Page.getCurrent().open(resource, null, false);
}

From source file:com.save.reports.reimbursement.ReimbursementReportUI.java

void processExportDataToExcel() {
    ExportDataGridToExcel export = new ExportDataGridToExcel(mrDataGrid);
    export.workSheet();/* w w w .j  ava2s . co m*/

    StreamResource.StreamSource source = () -> {
        try {
            File f = new File(export.getFilePath());
            FileInputStream fis = new FileInputStream(f);
            return fis;
        } catch (Exception e) {
            ErrorLoggedNotification.showErrorLoggedOnWindow(e.toString(), this.getClass().getName());
            return null;
        }
    };

    Date date = new Date();

    StreamResource resource = new StreamResource(source, "ReimbursementReport" + "-" + date.getTime() + ".xls");
    resource.setMIMEType("application/vnd.ms-office");

    Page.getCurrent().open(resource, null, false);
}

From source file:control.Functions.java

License:Open Source License

public static StreamResource getFileStream(final String content, String name, String extension) {
    StreamResource resource = new StreamResource(new StreamResource.StreamSource() {
        @Override/*from   w w  w  .  jav a2  s .com*/
        public InputStream getStream() {
            try {
                InputStream is = new ByteArrayInputStream(content.getBytes());
                return is;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }, name + "." + extension);
    return resource;
}

From source file:control.WizardController.java

License:Open Source License

public StreamResource getGraphStream(final String content, String name) {
    StreamResource resource = new StreamResource(new StreamResource.StreamSource() {
        @Override//from  w ww.  ja v  a2s .c  o m
        public InputStream getStream() {
            try {
                InputStream is = new ByteArrayInputStream(content.getBytes());
                return is;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }, String.format("%s.graphml", name));
    return resource;
}

From source file:control.WizardController.java

License:Open Source License

public StreamResource getTSVStream(final String content, String name) {
    StreamResource resource = new StreamResource(new StreamResource.StreamSource() {
        @Override//from   w  w  w  .  java 2 s . c o m
        public InputStream getStream() {
            try {
                InputStream is = new ByteArrayInputStream(content.getBytes());
                return is;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }, String.format("%s.tsv", name));
    return resource;
}

From source file:de.fatalix.bookery.view.common.BookDetailLayout.java

License:Open Source License

private HorizontalLayout createImageLayout() {
    image = new Image();
    image.setImmediate(true);//w w  w  . j a va  2 s.  com
    image.setHeight("200px");
    image.setWidth("130px");

    downloadButton = new Button("download", FontAwesome.DOWNLOAD);
    downloadButton.addStyleName(ValoTheme.BUTTON_FRIENDLY);
    downloadButton.addClickListener(new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            try {
                loadData(presenter.updateShared(bookEntry,
                        SecurityUtils.getSubject().getPrincipal().toString()));
            } catch (SolrServerException | IOException ex) {
                Notification.show("Unexpected Error!\n" + ex.getMessage(), Notification.Type.ERROR_MESSAGE);
                logger.error(ex, ex);
            }
        }
    });

    fileDownloader = new FileDownloader(new StreamResource(new BookStreamSource(null), "blbla.epub"));
    fileDownloader.extend(downloadButton);

    sendToKindleButton = new Button("Kindle", FontAwesome.BOOK);
    sendToKindleButton.addStyleName(ValoTheme.BUTTON_FRIENDLY);
    sendToKindleButton.addClickListener(new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            try {
                presenter.shareBookWithKindle(bookEntry, SecurityUtils.getSubject().getPrincipal().toString());
                Notification.show("Book is sent to kindle", Notification.Type.HUMANIZED_MESSAGE);
                loadData(presenter.updateShared(bookEntry,
                        SecurityUtils.getSubject().getPrincipal().toString()));
            } catch (SolrServerException | IOException | MessagingException ex) {
                Notification.show("Unexpected Error!\n" + ex.getMessage(), Notification.Type.ERROR_MESSAGE);
                logger.error(ex, ex);
            }
        }
    });

    likeButton = new Button("0 likes", FontAwesome.THUMBS_O_UP);
    likeButton.addStyleName(ValoTheme.BUTTON_LINK);
    likeButton.addClickListener(new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            try {
                BookEntry updatedEntry = presenter.updateLike(bookEntry,
                        SecurityUtils.getSubject().getPrincipal().toString());
                loadData(updatedEntry);
            } catch (SolrServerException | IOException ex) {
                Notification.show("Unexpected Error!\n" + ex.getMessage(), Notification.Type.ERROR_MESSAGE);
                logger.error(ex, ex);
            }
        }
    });

    watchListButton = new Button("merken", FontAwesome.STAR_O);
    watchListButton.addStyleName(ValoTheme.BUTTON_LINK);
    watchListButton.addClickListener(new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            presenter.addRemoveFromWatchList(bookEntry, SecurityUtils.getSubject().getPrincipal().toString());
            loadData(bookEntry);
        }
    });
    downloadCount = new Label("0 downloads");

    VerticalLayout rightLayout = new VerticalLayout(sendToKindleButton, downloadButton, watchListButton,
            likeButton, downloadCount);
    rightLayout.setComponentAlignment(downloadCount, Alignment.MIDDLE_CENTER);
    rightLayout.setSpacing(true);

    HorizontalLayout layout = new HorizontalLayout(image, rightLayout);
    layout.setSpacing(true);
    return layout;
}

From source file:de.fatalix.bookery.view.common.BookDetailLayout.java

License:Open Source License

public void loadData(BookEntry bookEntry) {
    this.bookEntry = bookEntry;
    titleLabel.setValue(bookEntry.getTitle() + "-" + bookEntry.getAuthor());
    descriptionLabel.setValue(bookEntry.getDescription());
    StreamResource.StreamSource source = new ByteStreamResource(bookEntry.getCover());
    image.setSource(new StreamResource(source, bookEntry.getId() + ".png"));
    likeButton.setCaption(bookEntry.getLikes() + " likes");
    downloadCount.setValue(bookEntry.getDownloads() + " downloads");
    try {/*from ww  w.ja  va2s .c om*/
        byte[] data = presenter.getEbookFile(bookEntry.getId());
        fileDownloader.setFileDownloadResource(new StreamResource(new BookStreamSource(data),
                bookEntry.getTitle() + "-" + bookEntry.getAuthor() + ".epub"));
    } catch (SolrServerException ex) {
        logger.error(ex, ex);
    }

    if (presenter.isOnWatchList(bookEntry, SecurityUtils.getSubject().getPrincipal().toString())) {
        watchListButton.setCaption("vergessen");
        watchListButton.setIcon(FontAwesome.STAR);
    } else {
        watchListButton.setCaption("merken");
        watchListButton.setIcon(FontAwesome.STAR_O);
    }

}

From source file:de.fatalix.bookery.view.common.BookSearchLayout.java

License:Open Source License

private VerticalLayout createBookCoverLayout(final BookEntry bookEntry) {
    Image image = new Image();
    image.setDescription(bookEntry.getTitle() + " von " + bookEntry.getAuthor());
    image.setHeight("200px");
    image.setWidth("130px");
    image.setImmediate(true);/*from   w  w w. jav  a2  s  .  c  o m*/
    if (bookEntry.getThumbnail() != null) {
        StreamResource.StreamSource source = new ByteStreamResource(bookEntry.getThumbnail());
        image.setSource(new StreamResource(source, bookEntry.getId() + "_thumb.png"));
    } else if (bookEntry.getCover() != null) {
        StreamResource.StreamSource source = new ByteStreamResource(bookEntry.getCover());
        image.setSource(new StreamResource(source, bookEntry.getId() + ".png"));
    }

    final MButton watchListButton = new MButton()
            .withIcon(presenter.isOnWatchList(bookEntry, SecurityUtils.getSubject().getPrincipal().toString())
                    ? FontAwesome.STAR
                    : FontAwesome.STAR_O)
            .withStyleName(ValoTheme.BUTTON_LINK);
    watchListButton.addStyleName("quick-action");

    watchListButton.addClickListener(new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            watchListButton.setIcon(presenter.addRemoveFromWatchList(bookEntry,
                    SecurityUtils.getSubject().getPrincipal().toString()) ? FontAwesome.STAR
                            : FontAwesome.STAR_O);
        }
    });

    final MButton likeButton = new MButton().withCaption("" + bookEntry.getLikes())
            .withIcon(FontAwesome.THUMBS_O_UP).withStyleName(ValoTheme.BUTTON_LINK);
    likeButton.addStyleName("quick-action");

    likeButton.addClickListener(new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            try {
                BookEntry updatedBook = presenter.updateLike(bookEntry,
                        SecurityUtils.getSubject().getPrincipal().toString());
                bookEntry.setLikes(updatedBook.getLikes());
                bookEntry.setLikedby(updatedBook.getLikedby());
                likeButton.setCaption("" + bookEntry.getLikes());
            } catch (SolrServerException | IOException ex) {
                java.util.logging.Logger.getLogger(BookSearchLayout.class.getName()).log(Level.SEVERE, null,
                        ex);
            }

        }
    });
    final MButton downloadsButton = new MButton().withCaption("" + bookEntry.getDownloads())
            .withIcon(FontAwesome.DOWNLOAD).withStyleName(ValoTheme.BUTTON_LINK);
    downloadsButton.addStyleName("quick-action");
    HorizontalLayout quickActionLayout = new HorizontalLayout(watchListButton, likeButton, downloadsButton);
    quickActionLayout.addStyleName("quick-action-layout");

    VerticalLayout result = new VerticalLayout(image, quickActionLayout);
    //result.setHeight("210px");
    //result.setWidth("140px");
    result.addStyleName("pointer-cursor");
    result.addStyleName("book-card");
    result.setComponentAlignment(image, Alignment.MIDDLE_CENTER);

    result.addLayoutClickListener(new LayoutEvents.LayoutClickListener() {

        @Override
        public void layoutClick(LayoutEvents.LayoutClickEvent event) {
            bookDetailLayout.loadData(bookEntry);
            bookDetailLayout.setLayoutVisible(true);
            //BookDetailDialog dialogInstance = bookDetail.get();
            //dialogInstance.loadData(bookEntry);
            //UI.getCurrent().addWindow(dialogInstance);
        }
    });
    return result;
}