Search in sources :

Example 1 with CubaFileUpload

use of com.haulmont.cuba.web.toolkit.ui.CubaFileUpload in project cuba by cuba-platform.

the class WebFileMultiUploadField method setPasteZone.

@Override
public void setPasteZone(Container pasteZone) {
    super.setPasteZone(pasteZone);
    if (component instanceof CubaFileUpload) {
        if (pasteZone == null) {
            ((CubaFileUpload) component).setPasteZone(null);
        } else {
            Component vComponent = pasteZone.unwrapComposition(Component.class);
            ((CubaFileUpload) component).setPasteZone(vComponent);
        }
    }
}
Also used : CubaFileUpload(com.haulmont.cuba.web.toolkit.ui.CubaFileUpload) CubaAbstractUploadComponent(com.haulmont.cuba.web.toolkit.ui.CubaAbstractUploadComponent) Component(com.vaadin.ui.Component)

Example 2 with CubaFileUpload

use of com.haulmont.cuba.web.toolkit.ui.CubaFileUpload in project cuba by cuba-platform.

the class WebFileMultiUploadField method initComponent.

protected void initComponent() {
    CubaFileUpload impl = createComponent();
    impl.setMultiSelect(true);
    Messages messages = AppBeans.get(Messages.NAME);
    impl.setProgressWindowCaption(messages.getMainMessage("upload.uploadingProgressTitle"));
    impl.setUnableToUploadFileMessage(messages.getMainMessage("upload.unableToUploadFile"));
    impl.setCancelButtonCaption(messages.getMainMessage("upload.cancel"));
    impl.setCaption(messages.getMainMessage("upload.submit"));
    impl.setDropZonePrompt(messages.getMainMessage("upload.dropZonePrompt"));
    impl.setDescription(null);
    Configuration configuration = AppBeans.get(Configuration.NAME);
    final int maxUploadSizeMb = configuration.getConfig(ClientConfig.class).getMaxUploadSizeMb();
    final int maxSizeBytes = maxUploadSizeMb * BYTES_IN_MEGABYTE;
    impl.setFileSizeLimit(maxSizeBytes);
    impl.setReceiver((fileName, MIMEType) -> {
        FileOutputStream outputStream;
        try {
            FileUploadingAPI.FileInfo fileInfo = fileUploading.createFile();
            tempFileId = fileInfo.getId();
            File tmpFile = fileInfo.getFile();
            outputStream = new FileOutputStream(tmpFile);
        } catch (Exception e) {
            throw new RuntimeException("Unable to receive file", e);
        }
        return outputStream;
    });
    impl.addStartedListener(event -> fireFileUploadStart(event.getFileName(), event.getContentLength()));
    impl.addQueueUploadFinishedListener(event -> fireQueueUploadComplete());
    impl.addSucceededListener(event -> {
        files.put(tempFileId, event.getFileName());
        fireFileUploadFinish(event.getFileName(), event.getContentLength());
    });
    impl.addFailedListener(event -> {
        try {
            // close and remove temp file
            fileUploading.deleteFile(tempFileId);
            tempFileId = null;
        } catch (Exception e) {
            if (e instanceof FileStorageException) {
                FileStorageException fse = (FileStorageException) e;
                if (fse.getType() != FileStorageException.Type.FILE_NOT_FOUND) {
                    LoggerFactory.getLogger(WebFileMultiUploadField.class).warn("Could not remove temp file {} after broken uploading", tempFileId);
                }
            }
            LoggerFactory.getLogger(WebFileMultiUploadField.class).warn("Error while delete temp file {}", tempFileId);
        }
        fireFileUploadError(event.getFileName(), event.getContentLength(), event.getReason());
    });
    impl.addFileSizeLimitExceededListener(e -> {
        String warningMsg = messages.formatMessage(WebFileMultiUploadField.class, "multiupload.filesizeLimitExceed", e.getFileName(), getFileSizeLimitString());
        getFrame().showNotification(warningMsg, Frame.NotificationType.WARNING);
    });
    impl.addFileExtensionNotAllowedListener(e -> {
        String warningMsg = messages.formatMainMessage("upload.fileIncorrectExtension.message", e.getFileName());
        getFrame().showNotification(warningMsg, Frame.NotificationType.WARNING);
    });
    component = impl;
}
Also used : Messages(com.haulmont.cuba.core.global.Messages) Configuration(com.haulmont.cuba.core.global.Configuration) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) IOException(java.io.IOException) CubaFileUpload(com.haulmont.cuba.web.toolkit.ui.CubaFileUpload) FileUploadingAPI(com.haulmont.cuba.gui.upload.FileUploadingAPI) FileOutputStream(java.io.FileOutputStream) ClientConfig(com.haulmont.cuba.client.ClientConfig) File(java.io.File)

Example 3 with CubaFileUpload

use of com.haulmont.cuba.web.toolkit.ui.CubaFileUpload in project cuba by cuba-platform.

the class WebFileMultiUploadField method setDropZone.

@Override
public void setDropZone(DropZone dropZone) {
    super.setDropZone(dropZone);
    if (component instanceof CubaFileUpload) {
        if (dropZone == null) {
            ((CubaFileUpload) component).setDropZone(null);
        } else {
            com.haulmont.cuba.gui.components.Component target = dropZone.getTarget();
            if (target instanceof Window.Wrapper) {
                target = ((Window.Wrapper) target).getWrappedWindow();
            }
            Component vComponent = target.unwrapComposition(Component.class);
            ((CubaFileUpload) this.component).setDropZone(vComponent);
        }
    }
}
Also used : Window(com.haulmont.cuba.gui.components.Window) MultiUploadFieldListenerWrapper(com.haulmont.cuba.gui.components.compatibility.MultiUploadFieldListenerWrapper) CubaFileUpload(com.haulmont.cuba.web.toolkit.ui.CubaFileUpload) CubaAbstractUploadComponent(com.haulmont.cuba.web.toolkit.ui.CubaAbstractUploadComponent) Component(com.vaadin.ui.Component)

Example 4 with CubaFileUpload

use of com.haulmont.cuba.web.toolkit.ui.CubaFileUpload in project cuba by cuba-platform.

the class WebFileUploadField method setPasteZone.

@Override
public void setPasteZone(Container pasteZone) {
    super.setPasteZone(pasteZone);
    if (uploadButton instanceof CubaFileUpload) {
        if (pasteZone == null) {
            ((CubaFileUpload) uploadButton).setPasteZone(null);
        } else {
            Component vComponent = pasteZone.unwrapComposition(Component.class);
            ((CubaFileUpload) uploadButton).setPasteZone(vComponent);
        }
    }
}
Also used : CubaFileUpload(com.haulmont.cuba.web.toolkit.ui.CubaFileUpload) UploadComponent(com.haulmont.cuba.web.toolkit.ui.UploadComponent) Component(com.vaadin.ui.Component)

Example 5 with CubaFileUpload

use of com.haulmont.cuba.web.toolkit.ui.CubaFileUpload in project cuba by cuba-platform.

the class WebFileUploadField method initUploadButton.

protected void initUploadButton() {
    uploadButton = createComponent();
    CubaFileUpload impl = (CubaFileUpload) uploadButton;
    impl.setProgressWindowCaption(messages.getMainMessage("upload.uploadingProgressTitle"));
    impl.setUnableToUploadFileMessage(messages.getMainMessage("upload.unableToUploadFile"));
    impl.setCancelButtonCaption(messages.getMainMessage("upload.cancel"));
    impl.setCaption(messages.getMainMessage("upload.submit"));
    impl.setDropZonePrompt(messages.getMainMessage("upload.singleDropZonePrompt"));
    impl.setDescription(null);
    impl.setFileSizeLimit(getActualFileSizeLimit());
    impl.setReceiver((fileName1, MIMEType) -> {
        FileOutputStream outputStream;
        try {
            FileUploadingAPI.FileInfo fileInfo = fileUploading.createFile();
            tempFileId = fileInfo.getId();
            File tmpFile = fileInfo.getFile();
            outputStream = new FileOutputStream(tmpFile);
        } catch (Exception e) {
            throw new RuntimeException("Unable to receive file", e);
        }
        return outputStream;
    });
    impl.addStartedListener(event -> fireFileUploadStart(event.getFileName(), event.getContentLength()));
    impl.addFinishedListener(event -> fireFileUploadFinish(event.getFileName(), event.getContentLength()));
    impl.addSucceededListener(event -> {
        fileName = event.getFileName();
        fileId = tempFileId;
        saveFile(getFileDescriptor());
        component.setFileNameButtonCaption(fileName);
        fireFileUploadSucceed(event.getFileName(), event.getContentLength());
    });
    impl.addFailedListener(event -> {
        try {
            fileUploading.deleteFile(tempFileId);
            tempFileId = null;
        } catch (Exception e) {
            if (e instanceof FileStorageException) {
                FileStorageException fse = (FileStorageException) e;
                if (fse.getType() != FileStorageException.Type.FILE_NOT_FOUND) {
                    log.warn(String.format("Could not remove temp file %s after broken uploading", tempFileId));
                }
            }
            log.warn(String.format("Error while delete temp file %s", tempFileId));
        }
        fireFileUploadError(event.getFileName(), event.getContentLength(), event.getReason());
    });
    impl.addFileSizeLimitExceededListener(e -> {
        String warningMsg = messages.formatMainMessage("upload.fileTooBig.message", e.getFileName(), getFileSizeLimitString());
        getFrame().showNotification(warningMsg, NotificationType.WARNING);
    });
    impl.addFileExtensionNotAllowedListener(e -> {
        String warningMsg = messages.formatMainMessage("upload.fileIncorrectExtension.message", e.getFileName());
        getFrame().showNotification(warningMsg, NotificationType.WARNING);
    });
}
Also used : CubaFileUpload(com.haulmont.cuba.web.toolkit.ui.CubaFileUpload) FileUploadingAPI(com.haulmont.cuba.gui.upload.FileUploadingAPI) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileStorageException(com.haulmont.cuba.core.global.FileStorageException)

Aggregations

CubaFileUpload (com.haulmont.cuba.web.toolkit.ui.CubaFileUpload)6 Component (com.vaadin.ui.Component)4 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)2 Window (com.haulmont.cuba.gui.components.Window)2 FileUploadingAPI (com.haulmont.cuba.gui.upload.FileUploadingAPI)2 CubaAbstractUploadComponent (com.haulmont.cuba.web.toolkit.ui.CubaAbstractUploadComponent)2 UploadComponent (com.haulmont.cuba.web.toolkit.ui.UploadComponent)2 ClientConfig (com.haulmont.cuba.client.ClientConfig)1 Configuration (com.haulmont.cuba.core.global.Configuration)1 Messages (com.haulmont.cuba.core.global.Messages)1 FileUploadFieldListenerWrapper (com.haulmont.cuba.gui.components.compatibility.FileUploadFieldListenerWrapper)1 MultiUploadFieldListenerWrapper (com.haulmont.cuba.gui.components.compatibility.MultiUploadFieldListenerWrapper)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1