Example usage for com.vaadin.ui Upload.Receiver Upload.Receiver

List of usage examples for com.vaadin.ui Upload.Receiver Upload.Receiver

Introduction

In this page you can find the example usage for com.vaadin.ui Upload.Receiver Upload.Receiver.

Prototype

Upload.Receiver

Source Link

Usage

From source file:org.bubblecloud.ilves.module.content.AssetFlowlet.java

License:Apache License

@Override
public void initialize() {
    entityManager = getSite().getSiteContext().getObject(EntityManager.class);

    final GridLayout gridLayout = new GridLayout(1, 3);
    gridLayout.setSizeFull();/*w  ww.  ja va  2s.  c  o m*/
    gridLayout.setMargin(false);
    gridLayout.setSpacing(true);
    gridLayout.setRowExpandRatio(1, 1f);
    setViewContent(gridLayout);

    assetEditor = new ValidatingEditor(
            FieldSetDescriptorRegister.getFieldSetDescriptor(Asset.class).getFieldDescriptors());
    assetEditor.setCaption("Asset");
    assetEditor.addListener(this);
    gridLayout.addComponent(assetEditor, 0, 1);

    final Upload upload = new Upload(getSite().localize("field-file-upload"), new Upload.Receiver() {
        @Override
        public OutputStream receiveUpload(String filename, String mimeType) {
            try {
                temporaryFile = File.createTempFile(entity.getAssetId(), ".upload");
                return new FileOutputStream(temporaryFile, false);
            } catch (IOException e) {
                throw new SiteException("Unable to create temporary file for upload.", e);
            }
        }
    });
    upload.setButtonCaption(getSite().localize("button-start-upload"));
    upload.addSucceededListener(new Upload.SucceededListener() {
        @Override
        public void uploadSucceeded(Upload.SucceededEvent event) {
            if (event.getLength() == 0) {
                return;
            }
            if (temporaryFile.length() > Long
                    .parseLong(PropertiesUtil.getProperty("site", "asset-maximum-size"))) {
                Notification.show(getSite().localize("message-file-too-large"),
                        Notification.Type.ERROR_MESSAGE);
                return;
            }

            entity.setName(event.getFilename().substring(0, event.getFilename().lastIndexOf('.')));
            entity.setExtension(event.getFilename().substring(event.getFilename().lastIndexOf('.') + 1));
            entity.setType(event.getMIMEType());
            entity.setSize((int) event.getLength());

            assetEditor.setItem(new BeanItem<Asset>(entity), assetEditor.isNewItem());
            save();
        }
    });
    gridLayout.addComponent(upload, 0, 0);

    final HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    gridLayout.addComponent(buttonLayout, 0, 2);

    saveButton = getSite().getButton("save");
    buttonLayout.addComponent(saveButton);
    saveButton.addClickListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            if (isValid()) {
                save();
            } else {
                Notification.show(getSite().localize("message-invalid-form-asset"),
                        Notification.Type.HUMANIZED_MESSAGE);
            }
        }
    });

    discardButton = getSite().getButton("discard");
    buttonLayout.addComponent(discardButton);
    discardButton.addClickListener(new ClickListener() {
        /** Serial version UID. */
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(final ClickEvent event) {
            assetEditor.discard();
            if (temporaryFile != null) {
                temporaryFile.deleteOnExit();
                temporaryFile = null;
            }
        }
    });

    editPrivilegesButton = getSite().getButton("edit-privileges");
    buttonLayout.addComponent(editPrivilegesButton);
    editPrivilegesButton.addClickListener(new ClickListener() {
        @Override
        public void buttonClick(ClickEvent event) {
            final PrivilegesFlowlet privilegesFlowlet = getFlow().getFlowlet(PrivilegesFlowlet.class);
            privilegesFlowlet.edit(entity.getName(), entity.getAssetId(), "view", "edit");
            getFlow().forward(PrivilegesFlowlet.class);
        }
    });
}