Example usage for com.vaadin.ui Upload setEnabled

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

Introduction

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

Prototype

@Override
    public void setEnabled(boolean enabled) 

Source Link

Usage

From source file:org.escidoc.browser.ui.maincontent.OnAddOrgUnitMetadata.java

License:Open Source License

public void showAddWindow() {
    final Window subwindow = new Window(ViewConstants.ADD_ORGANIZATIONAL_UNIT_S_METADATA);
    subwindow.setWidth("600px");
    subwindow.setModal(true);/*ww  w  .  j a  v  a 2 s .  c  o  m*/

    // Make uploading start immediately when file is selected
    final Upload upload = new Upload("", receiver);
    upload.setImmediate(true);
    upload.setButtonCaption("Select file");

    progressLayout.setSpacing(true);
    progressLayout.setVisible(false);

    final ProgressIndicator pi = new ProgressIndicator();
    progressLayout.addComponent(pi);
    progressLayout.setComponentAlignment(pi, Alignment.MIDDLE_LEFT);

    /**
     * =========== Add needed listener for the upload component: start, progress, finish, success, fail ===========
     */

    upload.addListener(new Upload.StartedListener() {
        @Override
        public void uploadStarted(final StartedEvent event) {

            upload.setVisible(false);
            progressLayout.setVisible(true);
            pi.setValue(Float.valueOf(0f));
            pi.setPollingInterval(500);
            status.setValue("Uploading file \"" + event.getFilename() + "\"");
        }
    });

    upload.addListener(new Upload.SucceededListener() {

        @Override
        public void uploadSucceeded(final SucceededEvent event) {
            // This method gets called when the upload finished successfully
            status.setValue("Uploading file \"" + event.getFilename() + "\" succeeded");
            final String fileContent = receiver.getFileContent();
            final boolean isWellFormed = XmlUtil.isWellFormed(fileContent);
            receiver.setWellFormed(isWellFormed);
            if (isWellFormed) {
                status.setValue(ViewConstants.XML_IS_WELL_FORMED);
                hl.setVisible(true);
                upload.setEnabled(false);
            } else {
                status.setValue(ViewConstants.XML_IS_NOT_WELL_FORMED);
                hl.setVisible(false);
            }
        }
    });

    upload.addListener(new Upload.FailedListener() {
        @Override
        public void uploadFailed(final FailedEvent event) {
            // This method gets called when the upload failed
            status.setValue("Uploading interrupted");
        }
    });

    upload.addListener(new Upload.FinishedListener() {
        @Override
        public void uploadFinished(final FinishedEvent event) {
            // This method gets called always when the upload finished,
            // either succeeding or failing
            progressLayout.setVisible(false);
            upload.setVisible(true);
            upload.setCaption("Select another file");
        }
    });

    mdName = new TextField("Metadata name");
    mdName.setValue("");
    mdName.setImmediate(true);
    mdName.setValidationVisible(false);

    hl = new HorizontalLayout();
    hl.setMargin(true);
    final Button btnAdd = new Button("Save", new Button.ClickListener() {

        private boolean containSpace(final String text) {
            final Pattern pattern = Pattern.compile("\\s");
            final Matcher matcher = pattern.matcher(text);
            return matcher.find();
        }

        @Override
        public void buttonClick(final ClickEvent event) {

            if (mdName.getValue().equals("")) {
                mdName.setComponentError(new UserError("You have to add a name for your MetaData"));
            } else if (containSpace(((String) mdName.getValue()))) {
                mdName.setComponentError(new UserError("The name of MetaData can not contain space"));
            } else {
                mdName.setComponentError(null);
                if (receiver.getFileContent().isEmpty()) {
                    upload.setComponentError(
                            new UserError("Please select a well formed XML file as metadata."));
                } else if (!receiver.isWellFormed()) {
                    upload.setComponentError(new UserError(ViewConstants.XML_IS_NOT_WELL_FORMED));
                } else {

                    final MetadataRecord metadataRecord = new MetadataRecord(mdName.getValue().toString());
                    try {
                        metadataRecord.setContent(getMetadataContent());
                        controller.addMetaData(metadataRecord);
                        controller.refreshView();
                        upload.setEnabled(true);
                        subwindow.getParent().removeWindow(subwindow);
                    } catch (final SAXException e) {
                        LOG.error(e.getMessage());
                        mdName.setComponentError(new UserError(
                                "Failed to add the new Metadata record" + e.getLocalizedMessage()));
                    } catch (final IOException e) {
                        LOG.error(e.getMessage());
                        mdName.setComponentError(new UserError(
                                "Failed to add the new Metadata record" + e.getLocalizedMessage()));
                    } catch (final ParserConfigurationException e) {
                        LOG.error(e.getMessage());
                        mdName.setComponentError(new UserError(
                                "Failed to add the new Metadata record" + e.getLocalizedMessage()));
                    }
                }
            }
        }

        private Element getMetadataContent() throws SAXException, IOException, ParserConfigurationException {
            final String fileContent = receiver.getFileContent();
            return XmlUtil.string2Dom(fileContent).getDocumentElement();
        }
    });

    final Button cnclAdd = new Button("Cancel", new Button.ClickListener() {
        @Override
        public void buttonClick(final ClickEvent event) {
            (subwindow.getParent()).removeWindow(subwindow);
        }
    });

    hl.addComponent(btnAdd);
    hl.addComponent(cnclAdd);
    subwindow.addComponent(mdName);
    subwindow.addComponent(status);
    subwindow.addComponent(upload);
    subwindow.addComponent(progressLayout);
    subwindow.addComponent(hl);
    mainWindow.addWindow(subwindow);
}

From source file:org.escidoc.browser.ui.view.helpers.OnEditContextMetadata.java

License:Open Source License

@SuppressWarnings("serial")
private void addListeners(final Upload upload) {
    upload.addListener(new Upload.StartedListener() {
        @Override/*from   w  ww. j ava2  s . c  o m*/
        public void uploadStarted(final StartedEvent event) {
            upload.setVisible(false);
            progressLayout.setVisible(true);
            pi.setValue(Float.valueOf(0f));
            pi.setPollingInterval(500);
            message.setValue("Uploading file \"" + event.getFilename() + "\"");
        }
    });

    upload.addListener(new Upload.SucceededListener() {

        @Override
        public void uploadSucceeded(final SucceededEvent event) {
            message.setValue("Uploading file \"" + event.getFilename() + "\" succeeded");
            if (isWellFormed(receiver.getFileContent())) {
                message.setValue(ViewConstants.XML_IS_WELL_FORMED);
                buttonLayout.setVisible(true);
                upload.setEnabled(false);
            } else {
                message.setValue(ViewConstants.XML_IS_NOT_WELL_FORMED);
                receiver.clearBuffer();
            }
        }
    });

    upload.addListener(new Upload.FailedListener() {
        @Override
        public void uploadFailed(@SuppressWarnings("unused") final FailedEvent event) {
            message.setValue("Uploading interrupted");
        }
    });

    upload.addListener(new Upload.FinishedListener() {
        @Override
        public void uploadFinished(@SuppressWarnings("unused") final FinishedEvent event) {
            progressLayout.setVisible(false);
            upload.setVisible(true);
            upload.setCaption("Select another file");
        }
    });
}

From source file:views.BatchUpload.java

License:Open Source License

public BatchUpload() {
    setMargin(true);/*w  w w.j  a v  a 2 s. co m*/
    setSpacing(true);

    // file upload component
    Upload upload = new Upload("Upload your file here", uploader);
    addComponent(this.upload);
    upload.setEnabled(false);

    // sample registration button
    register = new Button("Register People");
    register.setVisible(false);
    addComponent(register);

    upload.setButtonCaption("Upload");
    // Listen for events regarding the success of upload.
    upload.addFailedListener(uploader);
    upload.addSucceededListener(uploader);
    FinishedListener uploadFinListener = new FinishedListener() {
        /**
         * 
         */
        private static final long serialVersionUID = -8413963075202260180L;

        public void uploadFinished(FinishedEvent event) {
            String uploadError = uploader.getError();
            File file = uploader.getFile();
            if (file.getPath().endsWith("up_")) {
                String msg = "No file selected.";
                logger.warn(msg);
                Styles.notification("Failed to read file.", msg, NotificationType.ERROR);
                if (!file.delete())
                    logger.error("uploaded tmp file " + file.getAbsolutePath() + " could not be deleted!");
            } else {
                if (uploadError == null || uploadError.isEmpty()) {
                    String msg = "Upload successful!";
                    logger.info(msg);
                    //            try {
                    setRegEnabled(false);
                    SQLBatchParser parser = new SQLBatchParser();
                    if (parser.processTSV()) {
                        // TODO = prep.getObjects();
                        Styles.notification("Upload successful",
                                "New people information successfully uploaded and read.",
                                NotificationType.SUCCESS);
                    } else {
                        String error = parser.getError();
                        Styles.notification("Failed to read file.", error, NotificationType.ERROR);
                        if (!file.delete())
                            logger.error(
                                    "uploaded tmp file " + file.getAbsolutePath() + " could not be deleted!");
                    }
                    //            } catch (IOException e) {
                    //              e.printStackTrace();
                    //            }
                } else {
                    Styles.notification("Failed to upload file.", uploadError, NotificationType.ERROR);
                    if (!file.delete())
                        logger.error("uploaded tmp file " + file.getAbsolutePath() + " could not be deleted!");
                }
            }
        }
    };
    upload.addFinishedListener(uploadFinListener);
    // view.initUpload(upload);

    Button.ClickListener cl = new Button.ClickListener() {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        /**
         * 
         */

        @Override
        public void buttonClick(ClickEvent event) {
            String src = event.getButton().getCaption();
            if (src.equals("Register People")) {
                register.setEnabled(false);
            }
        }
    };
    register.addClickListener(cl);
}

From source file:views.StandaloneTSVImport.java

License:Open Source License

public void initView(Upload upload) {
    HorizontalLayout optionsInfo = new HorizontalLayout();
    optionsInfo.addComponent(importOptions);
    optionsInfo.addComponent(infos);/*  ww  w.j a  va  2  s  . com*/

    // design type selection and info
    addComponent(optionsInfo);

    // file upload component
    this.upload = upload;
    addComponent(this.upload);
    upload.setEnabled(false);

    // missing info input layout
    addComponent(questionaire);

    // summary of imortet samples
    summary = new ExperimentSummaryTable();
    summary.setVisible(false);
    addComponent(summary);

    // sample registration button
    register = new Button("Register All");
    register.setVisible(false);
    addComponent(register);

    // registration progress information
    registerInfo = new Label();
    bar = new ProgressBar();
    registerInfo.setVisible(false);
    bar.setVisible(false);
    addComponent(registerInfo);
    addComponent(bar);
}