com.squadd.UI.UploadGroupImageWindow.java Source code

Java tutorial

Introduction

Here is the source code for com.squadd.UI.UploadGroupImageWindow.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.squadd.UI;

import com.squadd.javaBeans.File;
import com.squadd.javaBeans.Group;
import com.squadd.managers.DBManager;
import com.squadd.technical.ImageGetter;
import com.vaadin.server.FileResource;
import com.vaadin.server.Page;
import com.vaadin.ui.Image;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Upload;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 *
 * @author SharkNado
 */
public class UploadGroupImageWindow extends UploadImageWindow {
    private Group grp;

    public UploadGroupImageWindow(Group contact) {
        this.grp = contact;
    }

    protected void configureDownload() {
        // Show uploaded file in this placeholder
        image = new Image("");
        image.setVisible(false);
        image.setHeight("256px");

        // Implement both receiver that saves upload in a file and
        // listener for successful upload
        class ImageReceiver implements Upload.Receiver, Upload.SucceededListener {
            private static final long serialVersionUID = -1276759102490466761L;

            public java.io.File file;

            public OutputStream receiveUpload(String filename, String mimeType) {
                // Create upload stream
                FileOutputStream fos = null; // Stream to write to
                try {
                    // Open the file for writing.
                    grp.setLastUploadDate(System.currentTimeMillis());
                    String path = new ImageGetter().getPath(grp);
                    file = new java.io.File(path);
                    fos = new FileOutputStream(file);
                } catch (final java.io.FileNotFoundException e) {
                    new Notification("Could not open file<br/>", e.getMessage(), Notification.Type.ERROR_MESSAGE)
                            .show(Page.getCurrent());
                    return null;
                }
                return fos; // Return the output stream to write to
            }

            public void uploadSucceeded(Upload.SucceededEvent event) {
                // Show the uploaded file in the image viewer
                userImageFile = new File();
                userImageFile.setPath(file.getPath());

                image.setVisible(true);
                image.setSource(new FileResource(file));

            }
        }
        ;
        ImageReceiver receiver = new ImageReceiver();

        // Create the upload with a caption and set receiver later
        upload = new Upload("", receiver);
        //upload.setButtonCaption("Ok");
        upload.addSucceededListener(receiver);
    }

}