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.haulmont.cuba.web.gui.components.WebEmbedded.java

License:Apache License

protected void provideType() {
    switch (type) {
    case OBJECT:/*from w  w  w.  jav a  2 s  . c o m*/
        component.setType(com.vaadin.ui.Embedded.TYPE_OBJECT);
        break;
    case IMAGE:
        component.setType(com.vaadin.ui.Embedded.TYPE_IMAGE);
        break;
    case BROWSER:
        component.setType(com.vaadin.ui.Embedded.TYPE_BROWSER);
        if (resource == null) {
            component.setSource(new StreamResource((StreamResource.StreamSource) () -> {
                return new ByteArrayInputStream("<html></html>".getBytes());
            }, UUID.randomUUID() + ".html"));
        }
        break;
    }
}

From source file:com.haulmont.cuba.web.gui.components.WebFileDescriptorResource.java

License:Apache License

@Override
protected void createResource() {
    String name = StringUtils.isNotEmpty(fileName) ? fileName : fileDescriptor.getName();

    resource = new StreamResource(() -> {
        try {// w  w w  .  j ava 2 s.  c  o  m
            return new ByteArrayDataProvider(AppBeans.get(FileStorageService.class).loadFile(fileDescriptor))
                    .provide();
        } catch (FileStorageException e) {
            throw new RuntimeException(FILE_STORAGE_EXCEPTION_MESSAGE, e);
        }
    }, name);

    StreamResource streamResource = (StreamResource) this.resource;

    streamResource.setCacheTime(cacheTime);
    streamResource.setBufferSize(bufferSize);
}

From source file:com.hivesys.dashboard.view.repository.DragAndDropBox.java

private void showFile(final String name, final String type, final ByteArrayOutputStream bas) {
    // resource for serving the file contents
    final StreamResource.StreamSource streamSource = () -> {
        if (bas != null) {
            final byte[] byteArray = bas.toByteArray();
            return new ByteArrayInputStream(byteArray);
        }//from   w  w  w  .  ja v  a  2s .c o m
        return null;
    };
    final StreamResource resource = new StreamResource(streamSource, name);

    // show the file contents - images only for now
    final Embedded embedded = new Embedded(name, resource);
    showComponent(embedded, name);
}

From source file:com.hris.payroll.alphalist.AlphaListMainUI.java

private Button exportToExcelButton() {
    ExportDataGridToExcel exportGrid = new ExportDataGridToExcel(grid);

    exportToExcelButton.setEnabled(false);
    exportToExcelButton.setWidth("200px");
    exportToExcelButton.setIcon(FontAwesome.EXCLAMATION_CIRCLE);
    exportToExcelButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
    exportToExcelButton.addStyleName(ValoTheme.BUTTON_SMALL);
    exportToExcelButton.addClickListener((Button.ClickEvent e) -> {
        exportGrid.workSheet();//  w  ww.  j a  va2s  . c  o m

        StreamResource.StreamSource source = () -> {
            try {
                File f = new File(exportGrid.getFilePath());
                FileInputStream fis = new FileInputStream(f);
                return fis;
            } catch (Exception e1) {
                e1.getMessage();
                return null;
            }
        };

        Date date = new Date();
        String branchName = cs.getBranchById(getBranchId());
        int tradeId = cs.getTradeIdByBranchId(getBranchId());
        String tradeName = cs.getTradeById(tradeId);
        int corporateId = cs.getCorporateIdByTradeId(tradeId);
        String corporateName = cs.getCorporateById(corporateId);

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

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

    return exportToExcelButton;
}

From source file:com.hris.payroll.reports.ReportViewer.java

public ReportViewer(String reportType, int branchId, Date payrollDate) {
    this.reportType = reportType;
    this.branchId = branchId;
    this.payrollDate = payrollDate;

    setWidth("900px");
    setHeight("600px");
    center();/*from www . ja va  2  s  .co m*/

    StreamResource resource = null;

    switch (reportType) {
    case "Payslip": {
        String filename = "Payslip-" + new Date().getTime() + ".pdf";
        resource = new StreamResource(new PayslipReportPDF(getBranchId(), getPayrollDate()), filename);
        break;
    }

    case "Advances Summary": {
        String filename = "Advances-" + new Date().getTime() + ".pdf";
        resource = new StreamResource(new AdvancesSummaryReportPdf(getBranchId(), getPayrollDate()), filename);
        break;
    }

    default: {
        String filename = "Advances-" + new Date().getTime() + ".pdf";
        resource = new StreamResource(new AdvancesReportPdf(getBranchId(), getPayrollDate(), reportType),
                filename);
        break;
    }
    }

    resource.setMIMEType("application/pdf");

    VerticalLayout v = new VerticalLayout();
    v.setSizeFull();
    v.setSpacing(true);
    v.setMargin(new MarginInfo(false, false, true, false));

    Embedded em = new Embedded();
    em.setSource(resource);
    em.setSizeFull();
    em.setType(Embedded.TYPE_BROWSER);
    v.addComponent(em);
    v.setExpandRatio(em, 1);

    setContent(v);
}

From source file:com.hris.payroll.thirteenthmonth.ExportDataGridToExcel.java

public void workSheet() {
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("Sample sheet");

    int rownum = 1;
    for (Object itemId : getDataGrid().getContainerDataSource().getItemIds()) {
        System.out.println("itemId: " + itemId);
    }//from w ww . j a  v a 2 s  .  com
    for (Object itemId : getDataGrid().getContainerDataSource().getItemIds()) {
        Row row = sheet.createRow(rownum);
        int cellcount = 0;
        if ((rownum - 1) == 0) {
            Row rowHeader = sheet.createRow(rownum - 1);
            for (Object propertyId : getDataGrid().getContainerDataSource().getContainerPropertyIds()) {
                Cell cell = rowHeader.createCell(cellcount);
                if (propertyId.toString().contains("salary")) {
                    if (!propertyId.toString().equals("salary grand total")) {
                        cell.setCellValue(propertyId.toString().replace("salary", "").toUpperCase());
                    } else {
                        cell.setCellValue(propertyId.toString().toUpperCase());
                    }
                } else if (propertyId.toString().contains("tax")) {
                    if (!propertyId.toString().equals("tax grand total")) {
                        cell.setCellValue(propertyId.toString().replace("tax", "").toUpperCase());
                    } else {
                        cell.setCellValue(propertyId.toString().toUpperCase());
                    }
                } else if (propertyId.toString().contains("sss")) {
                    if (!propertyId.toString().equals("sss grand total")) {
                        cell.setCellValue(propertyId.toString().replace("sss", "").toUpperCase());
                    } else {
                        cell.setCellValue(propertyId.toString().toUpperCase());
                    }
                } else if (propertyId.toString().contains("phic")) {
                    if (!propertyId.toString().equals("phic grand total")) {
                        cell.setCellValue(propertyId.toString().replace("phic", "").toUpperCase());
                    } else {
                        cell.setCellValue(propertyId.toString().toUpperCase());
                    }
                } else if (propertyId.toString().contains("hdmf")) {
                    if (!propertyId.toString().equals("hdmf grand total")) {
                        cell.setCellValue(propertyId.toString().replace("hdmf", "").toUpperCase());
                    } else {
                        cell.setCellValue(propertyId.toString().toUpperCase());
                    }
                }

                else {
                    cell.setCellValue(propertyId.toString().toUpperCase());
                }

                sheet.autoSizeColumn(cellcount);
                cellcount++;
            }
        }

        Item item = getDataGrid().getContainerDataSource().getItem(itemId);
        int cellnum = 0;
        for (Object propertyId : item.getItemPropertyIds()) {
            Cell cell = row.createCell(cellnum);
            if (propertyId.equals("employee")) {
                cell.setCellValue(item.getItemProperty(propertyId).getValue().toString().toUpperCase());
            } else {
                cell.setCellValue((item.getItemProperty(propertyId).getValue() == null) ? " "
                        : item.getItemProperty(propertyId).getValue().toString());
            }

            sheet.autoSizeColumn(cellnum);
            cellnum++;
        }
        rownum++;
    }

    FileOutputStream fos;
    try {
        Date date = new Date();
        path = "C:/payroll-files/format-" + date.getTime() + ".xls";
        file = new File(path);
        fos = new FileOutputStream(path);
        workbook.write(fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException ex) {
        ErrorLoggedNotification.showErrorLoggedOnWindow(ex.toString(), this.getClass().getName());
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        ErrorLoggedNotification.showErrorLoggedOnWindow(ex.toString(), this.getClass().getName());
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
    }

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

    StreamResource resource = new StreamResource(source, "ThirteenthMonth.xls");
    resource.setMIMEType("application/vnd.ms-office");

}

From source file:com.hris.payroll.thirteenthmonth.ExportDataGridToExcel.java

public StreamResource getExcelStream(ByteArrayOutputStream bos) {
    StreamResource resource = new StreamResource(() -> {
        return new ByteArrayInputStream(bos.toByteArray());
    }, "ThirteenthMonth.xls");
    resource.setMIMEType("application/vnd.ms-office");
    return resource;
}

From source file:com.hris.payroll.thirteenthmonth.ThirteenthMonth.java

private Button exportToExcelButton() {
    exportToExcelButton.setWidth("200px");
    exportToExcelButton.setIcon(FontAwesome.EXCLAMATION_CIRCLE);
    exportToExcelButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
    exportToExcelButton.addStyleName(ValoTheme.BUTTON_SMALL);
    exportToExcelButton.addClickListener((Button.ClickEvent e) -> {
        ExportDataGridToExcel exportGrid = new ExportDataGridToExcel(grid);
        exportGrid.workSheet();/*from   ww  w. j a va2s .c om*/

        StreamResource.StreamSource source = () -> {
            try {
                File f = new File(exportGrid.getFilePath());
                FileInputStream fis = new FileInputStream(f);
                return fis;
            } catch (Exception e1) {
                e1.getMessage();
                return null;
            }
        };

        Date date = new Date();
        String branchName = cs.getBranchById(getBranchId());
        int tradeId = cs.getTradeIdByBranchId(getBranchId());
        String tradeName = cs.getTradeById(tradeId);
        int corporateId = cs.getCorporateIdByTradeId(tradeId);
        String corporateName = cs.getCorporateById(corporateId);

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

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

    return exportToExcelButton;
}

From source file:com.hris.payroll.thirteenthmonth.ThirteenthMonth.java

StreamResource streamResource(String path) {
    StreamResource.StreamSource source = () -> {
        try {/*www.ja v a  2  s . com*/
            File f = new File(path);
            FileInputStream fis = new FileInputStream(f);
            return fis;
        } catch (Exception e1) {
            e1.getMessage();
            return null;
        }
    };

    StreamResource resource = new StreamResource(source, path);
    resource.setMIMEType("application/vnd.ms-excel");

    return resource;
}

From source file:com.hybridbpm.ui.component.access.UserLayout.java

License:Apache License

public UserLayout(User user) {
    Design.read(this);
    if (user == null) {
        user = new User();
        user.setLocale(HybridbpmUI.getCurrent().getLocale().toString());
    }//from   ww w . j  a  v  a 2 s  . c o  m
    binder.setItemDataSource(user);
    binder.bind(username, "username");
    binder.bind(firstName, "firstName");
    binder.bind(lastName, "lastName");
    binder.bind(email, "email");
    binder.bind(manager, "manager");
    binder.bind(userLocale, "locale");
    binder.bind(firstVisibleHourOfDay, "firstVisibleHourOfDay");
    binder.bind(lastVisibleHourOfDay, "lastVisibleHourOfDay");
    binder.setBuffered(true);

    if (user.getId() != null) {
        username.setReadOnly(true);
    }
    if (user.getImage() != null) {
        image = user.getImage().toStream();
        StreamResource.StreamSource imagesource = new UserImageSource(image);
        StreamResource resource = new StreamResource(imagesource, UUID.randomUUID().toString());
        userImage.setSource(resource);
    } else {
        userImage.setSource(new ThemeResource("img/profile-pic-300px.jpg"));
    }
    imageUpload.setImmediate(true);
    imageUpload.addSucceededListener(imageUploader);
    imageUpload.setReceiver(imageUploader);
    //        password1.addValueChangeListener(new PasswordChangeListener());
    password1.setImmediate(true);
    password2.setImmediate(true);
    //        password2.addValueChangeListener(new PasswordChangeListener());

    manager.addContainerProperty(NAME, String.class, null);
    manager.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY);
    manager.setItemCaptionPropertyId(NAME);
    for (User u : HybridbpmUI.getAccessAPI().getAllUsers()) {
        Item item = manager.addItem(u);
        item.getItemProperty(NAME).setValue(u.getFullName() + " (" + u.getEmail() + ")");
    }
    for (Locale l : Locale.getAvailableLocales()) {
        Item item = userLocale.addItem(l.toString());
        userLocale.setItemCaptionMode(AbstractSelect.ItemCaptionMode.EXPLICIT);
        userLocale.setItemCaption(l.toString(), l.getDisplayName());
    }

    if (HybridbpmUI.getAccessAPI().isAdministrator()) {
        readOnly = false;
        errorLabel.setVisible(false);
        manager.setEnabled(true);
    } else if (Objects.equals(user.getUsername(), HybridbpmUI.getUser().getUsername())) {
        readOnly = false;
        errorLabel.setVisible(false);
        manager.setEnabled(false);
        manager.setReadOnly(true);
    } else {
        readOnly = true;
        password1.setVisible(false);
        password2.setVisible(false);
        imageUpload.setVisible(false);
        errorLabel.setVisible(false);
        manager.setEnabled(false);
        manager.setReadOnly(true);
    }
}