Example usage for com.vaadin.server ResourceReference getURL

List of usage examples for com.vaadin.server ResourceReference getURL

Introduction

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

Prototype

@Override
    public String getURL() 

Source Link

Usage

From source file:org.azrul.langkuik.framework.customtype.attachment.AttachmentCustomTypeUICreator.java

@Override
public Component createUIForForm(final C currentBean, final Class<? extends CustomType> attachmentClass,
        final String pojoFieldName, final BeanView beanView, final DataAccessObject<C> conatainerClassDao,
        final DataAccessObject<? extends CustomType> customTypeDao,
        final RelationManagerFactory relationManagerFactory, final Configuration config,
        final ComponentState componentState, final Window window) {
    final FormLayout form = new FormLayout();

    final DataAccessObject<AttachmentCustomType> attachmentDao = ((DataAccessObject<AttachmentCustomType>) customTypeDao);
    final Collection<AttachmentCustomType> attachments = attachmentDao.find(currentBean, pojoFieldName, null,
            true, 0, Integer.parseInt(config.get("uploadCountLimit")));

    final WebEntityItemContainer attachmentIC = new WebEntityItemContainer(attachmentClass);
    if (!attachments.isEmpty()) {
        attachmentIC.addAll(attachments);
    }// w  w  w .  j av  a 2s  .co  m
    final ListSelect attachmentList = new ListSelect("", attachmentIC);
    createAtachmentList(attachmentList, attachments, config, beanView, form);

    final String relativePath = currentBean.getClass().getCanonicalName() + File.separator
            + conatainerClassDao.getIdentifierValue(currentBean);
    final String fullPath = config.get("attachmentRepository") + File.separator + relativePath;
    MyUploadHandler<C> uploadFinishHandler = new MyUploadHandler<>(currentBean,
            Integer.parseInt(config.get("uploadCountLimit").trim()), pojoFieldName, fullPath, relativePath,
            attachmentList, attachmentIC, customTypeDao, attachmentClass, relationManagerFactory);

    //File upload/download/delete buttons
    HorizontalLayout attachmentButtons = new HorizontalLayout();
    attachmentButtons.setSpacing(true);

    UploadStateWindow uploadStateWindow = new UploadStateWindow();
    MultiFileUpload fileUpload = new MultiFileUpload(uploadFinishHandler, uploadStateWindow);
    uploadFinishHandler.setFileUpload(fileUpload);

    fileUpload.getSmartUpload().setEnabled(true);
    fileUpload.getSmartUpload().setMaxFileSize(Integer.parseInt(config.get("uploadSizeLimit").trim()));
    fileUpload.getSmartUpload().setUploadButtonCaptions("Upload files", "Upload files");
    fileUpload.getSmartUpload().setId("Upload files");
    fileUpload.setId("Upload files2");

    Button deleteAttachmentBtn = new Button("Delete file", new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            Collection<AttachmentCustomType> attachments = (Collection<AttachmentCustomType>) attachmentList
                    .getValue();
            if (!attachments.isEmpty()) {
                customTypeDao.unlinkAndDelete(attachments, currentBean, pojoFieldName,
                        relationManagerFactory.create(currentBean.getClass(), attachmentClass));
                Collection<AttachmentCustomType> a = attachmentDao.find(currentBean, pojoFieldName, null, true,
                        0, Integer.parseInt(config.get("uploadCountLimit")));
                attachmentIC.removeAllItems();
                attachmentIC.addAll(a);
                attachmentIC.refreshItems();
            }
        }
    });
    deleteAttachmentBtn.setId(deleteAttachmentBtn.getCaption());

    Button downloadAttachmentBtn = new Button("Download file", new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {

            Collection<AttachmentCustomType> attachments = (Collection<AttachmentCustomType>) attachmentList
                    .getValue();
            if (!attachments.isEmpty()) {
                AttachmentCustomType attachment = attachments.iterator().next();
                final String fullPath = config.get("attachmentRepository") + File.separator
                        + attachment.getRelativeLocation() + File.separator + attachment.getFileName();
                FileResource res = new FileResource(new File(fullPath));
                beanView.setViewResource("DOWNLOAD", res);
                ResourceReference rr = ResourceReference.create(res, beanView, "DOWNLOAD");
                Page.getCurrent().open(rr.getURL(), null);
            }
        }
    });
    downloadAttachmentBtn.setId(downloadAttachmentBtn.getCaption());

    Button closeWindowBtn = new Button("Close", new Button.ClickListener() {
        @Override
        public void buttonClick(ClickEvent event) {
            window.close();
        }
    });
    closeWindowBtn.setId(closeWindowBtn.getCaption());

    if (componentState.equals(ComponentState.EDITABLE)) {
        attachmentButtons.addComponent(fileUpload);
        attachmentButtons.addComponent(deleteAttachmentBtn);
    }

    if (componentState.equals(ComponentState.EDITABLE) || componentState.equals(ComponentState.READ_ONLY)) {
        attachmentButtons.addComponent(downloadAttachmentBtn);
    }

    attachmentButtons.addComponent(closeWindowBtn);

    form.addComponent(attachmentButtons);
    form.setMargin(true);
    //beanView.addComponent(form);
    return form;
}

From source file:org.azrul.langkuik.framework.customtype.attachment.AttachmentCustomTypeUICreator.java

private void createAtachmentList(final ListSelect attachmentList,
        final Collection<? extends CustomType> attachments, final Configuration config, final BeanView view,
        final FormLayout form) {

    attachmentList.setHeight(attachments.size() + 2, Sizeable.Unit.EM);
    attachmentList.setMultiSelect(true);
    VerticalLayout attachmentListCont = new VerticalLayout();
    attachmentListCont.setCaption("Attachments");
    attachmentListCont.addComponent(attachmentList);
    attachmentListCont.addLayoutClickListener(new LayoutEvents.LayoutClickListener() {

        @Override// w  w w. ja v  a  2 s .co  m
        public void layoutClick(LayoutEvents.LayoutClickEvent event) {
            if (event.getClickedComponent() == null) {
                return;
            }
            if (event.getClickedComponent().equals(attachmentList)) {
                Collection<AttachmentCustomType> attachments = (Collection<AttachmentCustomType>) attachmentList
                        .getValue();
                if (!attachments.isEmpty()) {
                    AttachmentCustomType attachment = attachments.iterator().next();
                    final String fullPath = config.get("attachmentRepository") + File.separator
                            + attachment.getRelativeLocation() + File.separator + attachment.getFileName();
                    FileResource res = new FileResource(new File(fullPath));
                    view.setViewResource("DOWNLOAD", res);
                    ResourceReference rr = ResourceReference.create(res, view, "DOWNLOAD");

                    Page.getCurrent().open(rr.getURL(), null);
                }
            }
        }
    });
    form.addComponent(attachmentListCont);
}