Example usage for com.vaadin.ui Upload focus

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

Introduction

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

Prototype

boolean focus

To view the source code for com.vaadin.ui Upload focus.

Click Source Link

Document

Should the field be focused on next repaint?

Usage

From source file:io.mateu.ui.vaadin.framework.MyUI.java

License:Apache License

/**
 * abre el dilogo para cambiar la foto/*from ww w  . java  2 s .  c o m*/
 *
 *
 */
private void uploadFoto() {
    // Create a sub-window and set the content
    Window subWindow = new Window("My photo");

    subWindow.setWidth("475px");

    FormLayout f = new FormLayout();
    f.setMargin(true);

    Label e = new Label();

    Image image = new Image();

    class MyUploader implements Upload.Receiver, Upload.SucceededListener {

        File file;

        public File getFile() {
            return file;
        }

        public OutputStream receiveUpload(String fileName, String mimeType) {
            // Create and return a file output stream

            System.out.println("receiveUpload(" + fileName + "," + mimeType + ")");

            FileOutputStream os = null;
            if (fileName != null && !"".equals(fileName)) {

                long id = fileId++;
                String extension = ".tmp";
                if (fileName == null || "".equals(fileName.trim()))
                    fileName = "" + id + extension;
                if (fileName.lastIndexOf(".") < fileName.length() - 1) {
                    extension = fileName.substring(fileName.lastIndexOf("."));
                    fileName = fileName.substring(0, fileName.lastIndexOf("."));
                }
                File temp = null;
                try {
                    temp = File.createTempFile(fileName, extension);
                    os = new FileOutputStream(file = temp);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return os;
        }

        public void uploadSucceeded(Upload.SucceededEvent event) {
            // Show the uploaded file in the image viewer
            image.setSource(new FileResource(file));
            System.out.println("uploadSucceeded(" + file.getAbsolutePath() + ")");

        }

        public FileLocator getFileLocator() throws IOException {

            String extension = ".tmp";
            String fileName = file.getName();

            if (file.getName() == null || "".equals(file.getName().trim()))
                fileName = "" + getId();
            if (fileName.lastIndexOf(".") < fileName.length() - 1) {
                extension = fileName.substring(fileName.lastIndexOf("."));
                fileName = fileName.substring(0, fileName.lastIndexOf(".")).replaceAll(" ", "_");
            }

            java.io.File temp = (System.getProperty("tmpdir") == null)
                    ? java.io.File.createTempFile(fileName, extension)
                    : new java.io.File(new java.io.File(System.getProperty("tmpdir")), fileName + extension);

            System.out.println("java.io.tmpdir=" + System.getProperty("java.io.tmpdir"));
            System.out.println("Temp file : " + temp.getAbsolutePath());

            if (System.getProperty("tmpdir") == null || !temp.exists()) {
                System.out.println("writing temp file to " + temp.getAbsolutePath());
                Files.copy(file, temp);
            } else {
                System.out.println("temp file already exists");
            }

            String baseUrl = System.getProperty("tmpurl");
            URL url = null;
            try {
                if (baseUrl == null) {
                    url = file.toURI().toURL();
                } else
                    url = new URL(baseUrl + "/" + file.getName());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            return new FileLocator(0, file.getName(), url.toString(), file.getAbsolutePath());
        }
    }
    ;
    MyUploader receiver = new MyUploader();

    Upload upload = new Upload(null, receiver);
    //upload.setImmediateMode(false);
    upload.addSucceededListener(receiver);

    f.addComponent(image);

    f.addComponent(upload);

    f.addComponent(e);

    VerticalLayout v = new VerticalLayout();
    v.addComponent(f);

    HorizontalLayout footer = new HorizontalLayout();
    footer.setWidth("100%");
    footer.setSpacing(true);
    footer.addStyleName(ValoTheme.WINDOW_BOTTOM_TOOLBAR);

    Label footerText = new Label("");
    footerText.setSizeUndefined();

    Button ok = new Button("Change it!", new ClickListener() {
        @Override
        public void buttonClick(ClickEvent clickEvent) {
            e.setValue("Changing photo...");
            try {
                FileLocator loc = receiver.getFileLocator();

                io.mateu.ui.core.client.app.MateuUI.getBaseService()
                        .updateFoto(getApp().getUserData().getLogin(), loc, new AsyncCallback<Void>() {

                            @Override
                            public void onFailure(Throwable caught) {
                                e.setValue("" + caught.getClass().getName() + ": " + caught.getMessage());
                            }

                            @Override
                            public void onSuccess(Void result) {
                                e.setValue("OK!");
                                getApp().getUserData().setPhoto(loc.getUrl());
                                foto = (getApp().getUserData().getPhoto() != null)
                                        ? new ExternalResource(getApp().getUserData().getPhoto())
                                        : new ClassResource("profile-pic-300px.jpg");
                                subWindow.close();

                                refreshSettings();

                            }
                        });

            } catch (IOException e1) {
                e1.printStackTrace();
                io.mateu.ui.core.client.app.MateuUI.alert("" + e1.getClass().getName() + ":" + e1.getMessage());
            }

        }
    });
    ok.addStyleName(ValoTheme.BUTTON_PRIMARY);
    ok.setClickShortcut(ShortcutAction.KeyCode.ENTER);

    Button cancel = new Button("Cancel");

    footer.addComponents(footerText, ok); //, cancel);
    footer.setExpandRatio(footerText, 1);

    v.addComponent(footer);

    subWindow.setContent(v);

    // Center it in the browser window
    subWindow.center();

    subWindow.setModal(true);

    // Open it in the UI
    UI.getCurrent().addWindow(subWindow);

    upload.focus();
}