Example usage for com.vaadin.ui Upload Upload

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

Introduction

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

Prototype

public Upload(String caption, Receiver uploadReceiver) 

Source Link

Usage

From source file:org.escidoc.browser.ui.listeners.EditMetaDataFileItemComponentBehaviour.java

License:Open Source License

public void showWindow() {
    final Window subwindow = new Window(ViewConstants.EDIT_METADATA);
    subwindow.setWidth("600px");
    subwindow.setModal(true);// w  w  w .j  av  a2  s  . com
    status = new Label(ViewConstants.EDIT_METADATA_UPLOAD_MESSAGE);

    // Make uploading start immediately when file is selected
    receiver = new MetadataFileReceiver();
    receiver.clearBuffer();

    upload = new Upload("", receiver);
    upload.setImmediate(true);
    upload.setButtonCaption("Select file");
    upload.setEnabled(true);
    progressLayout.setSpacing(true);
    progressLayout.setVisible(false);
    progressLayout.addComponent(progressIndicator);
    progressLayout.setComponentAlignment(progressIndicator, 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);
            progressIndicator.setValue(Float.valueOf(0.5f));
            progressIndicator.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");
            if (isValidXml(receiver.getFileContent())) {
                status.setValue(ViewConstants.XML_IS_WELL_FORMED);
                horizontalLayout.setVisible(true);
                upload.setEnabled(false);
            } else {
                status.setValue(ViewConstants.XML_IS_NOT_WELL_FORMED);
                receiver.clearBuffer();
            }
        }
    });

    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) {

            progressLayout.setVisible(false);
            upload.setVisible(true);
            upload.setCaption("Select another file");
        }
    });

    final Button btnAdd = new Button("Save", new Button.ClickListener() {
        Item item;

        @Override
        public void buttonClick(final ClickEvent event) {

            metadataRecord.setContent(metadataContent);
            controller.updateMetaDataComponent(metadataRecord, itemProxy, component);
            upload.setEnabled(true);

            subwindow.getParent().removeWindow(subwindow);
        }
    });

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

    subwindow.addComponent(status);
    subwindow.addComponent(upload);
    subwindow.addComponent(progressLayout);
    subwindow.addComponent(horizontalLayout);
    mainWindow.addWindow(subwindow);
}

From source file:org.escidoc.browser.ui.listeners.OnAddContainerMetadata.java

License:Open Source License

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

    receiver = new MetadataFileReceiver();

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

    progressLayout.setSpacing(true);
    progressLayout.setVisible(false);
    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(@SuppressWarnings("unused") 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() {
        Container container;

        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 {
                        container = repositories.container().findContainerById(resourceProxy.getId());
                        metadataRecord.setContent(getMetadataContent());
                        repositories.container().addMetaData(metadataRecord, container);

                        upload.setEnabled(true);
                        subwindow.getParent().removeWindow(subwindow);
                    } catch (final EscidocClientException e) {
                        LOG.error(e.getLocalizedMessage());
                        mdName.setComponentError(new UserError(
                                "Failed to add the new Metadata record" + e.getLocalizedMessage()));
                    } catch (final SAXException e) {
                        LOG.error(e.getLocalizedMessage());
                        mdName.setComponentError(new UserError(
                                "Failed to add the new Metadata record" + e.getLocalizedMessage()));
                    } catch (final IOException e) {
                        LOG.error(e.getLocalizedMessage());
                        mdName.setComponentError(new UserError(
                                "Failed to add the new Metadata record" + e.getLocalizedMessage()));
                    } catch (final ParserConfigurationException e) {
                        LOG.error(e.getLocalizedMessage());
                        mdName.setComponentError(new UserError(
                                "Failed to add the new Metadata record" + e.getLocalizedMessage()));
                    }
                }
            }
        }

        private Element getMetadataContent() throws SAXException, IOException, ParserConfigurationException {
            return XmlUtil.string2Dom(receiver.getFileContent()).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.listeners.OnEditContainerMetadata.java

License:Open Source License

private void buildUpload() {
    upload = new Upload("", receiver);
    upload.setImmediate(true);//from   www. ja v  a  2  s  .  c  o  m
    upload.setButtonCaption(ViewConstants.SELECT_FILE);
    addListeners();
}

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);/*from w ww.  j  a  va  2 s .com*/

    // 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

private Upload buildUpload() {
    receiver = new MetadataFileReceiver();
    receiver.clearBuffer();/* w  w w .  j av  a  2  s .  c  om*/

    upload = new Upload("", receiver);
    upload.setImmediate(true);
    upload.setButtonCaption(ViewConstants.SELECT_FILE);
    addListeners(upload);
    return upload;
}

From source file:org.ikasan.dashboard.ui.mappingconfiguration.window.MappingConfigurationImportWindow.java

License:BSD License

/**
 * Helper method to initialise this object.
 *///  w  w  w.j a  va2s  .  c o  m
protected void init() {
    this.setModal(true);
    super.setHeight(40.0f, Unit.PERCENTAGE);
    super.setWidth(40.0f, Unit.PERCENTAGE);
    super.center();
    super.setStyleName("ikasan");

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

    final Upload upload = new Upload("", receiver);
    upload.addSucceededListener(receiver);

    upload.addFinishedListener(new Upload.FinishedListener() {
        public void uploadFinished(FinishedEvent event) {
            upload.setVisible(false);
            try {
                parseUploadFile();
            } catch (Exception e) {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);

                Notification.show("Caught exception trying to import a Mapping Configuration!\n", sw.toString(),
                        Notification.Type.ERROR_MESSAGE);
            }
        }
    });

    final Button importButton = new Button("Import");
    importButton.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            upload.interruptUpload();
        }
    });

    upload.addStartedListener(new Upload.StartedListener() {
        public void uploadStarted(StartedEvent event) {
            // This method gets called immediately after upload is started
            upload.setVisible(false);
        }
    });

    upload.addProgressListener(new Upload.ProgressListener() {
        public void updateProgress(long readBytes, long contentLength) {
            // This method gets called several times during the update

        }

    });

    importButton.setStyleName(ValoTheme.BUTTON_SMALL);
    importButton.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            try {
                saveImportedMappingConfiguration();
                mappingConfiguration = null;
                progressLayout.setVisible(false);
                upload.setVisible(true);

                IkasanAuthentication authentication = (IkasanAuthentication) VaadinService.getCurrentRequest()
                        .getWrappedSession().getAttribute(DashboardSessionValueConstants.USER);

                systemEventService.logSystemEvent(MappingConfigurationConstants.MAPPING_CONFIGURATION_SERVICE,
                        "Imported mapping configuration: [Client="
                                + mappingConfiguration.getConfigurationServiceClient().getName()
                                + "] [Source Context=" + mappingConfiguration.getSourceContext().getName()
                                + "] [Target Context=" + mappingConfiguration.getTargetContext().getName()
                                + "] [Type=" + mappingConfiguration.getConfigurationType().getName() + "]",
                        authentication.getName());
            } catch (MappingConfigurationServiceException e) {
                if (e.getCause() instanceof DataIntegrityViolationException) {
                    Notification.show("Caught exception trying to save an imported Mapping Configuration!",
                            "This is due to the fact "
                                    + "that, combined, the client, type, source and target context values are unique for a Mapping Configuration. The values"
                                    + " that are being imported and not unique.",
                            Notification.Type.ERROR_MESSAGE);
                } else {
                    StringWriter sw = new StringWriter();
                    PrintWriter pw = new PrintWriter(sw);
                    e.printStackTrace(pw);

                    Notification.show("Caught exception trying to save an imported Mapping Configuration!\n",
                            sw.toString(), Notification.Type.ERROR_MESSAGE);
                }
            }

            close();
        }
    });

    progressLayout.addComponent(importButton);

    VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    layout.addComponent(new Label("Select file to upload mapping configurations."));
    layout.addComponent(upload);

    layout.addComponent(progressLayout);

    Button cancelButton = new Button("Cancel");
    cancelButton.setStyleName(ValoTheme.BUTTON_SMALL);

    cancelButton.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            mappingConfiguration = null;
            progressLayout.setVisible(false);
            upload.setVisible(true);
            close();
        }
    });

    HorizontalLayout hlayout = new HorizontalLayout();
    hlayout.addComponent(cancelButton);

    layout.addComponent(hlayout);

    super.setContent(layout);
}

From source file:org.ikasan.dashboard.ui.mappingconfiguration.window.MappingConfigurationValuesImportWindow.java

License:BSD License

/**
 * Helper method to initialise this object.
 *///from w ww.  j ava 2  s .c  o  m
protected void init() {
    this.setModal(true);
    super.setHeight(40.0f, Unit.PERCENTAGE);
    super.setWidth(40.0f, Unit.PERCENTAGE);
    super.center();
    super.setStyleName("ikasan");

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

    final Upload upload = new Upload("", receiver);
    upload.addSucceededListener(receiver);

    upload.addFinishedListener(new Upload.FinishedListener() {
        public void uploadFinished(FinishedEvent event) {
            upload.setVisible(false);
            parseUploadFile();
        }
    });

    final Button importButton = new Button("Import");
    importButton.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            upload.interruptUpload();
        }
    });

    upload.addStartedListener(new Upload.StartedListener() {
        public void uploadStarted(StartedEvent event) {
            // This method gets called immediately after upload is started
            upload.setVisible(false);
        }
    });

    upload.addProgressListener(new Upload.ProgressListener() {
        public void updateProgress(long readBytes, long contentLength) {
            // This method gets called several times during the update

        }

    });

    importButton.setStyleName(ValoTheme.BUTTON_SMALL);
    importButton.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            try {
                saveImportedMappingConfigurationValues();

                IkasanAuthentication authentication = (IkasanAuthentication) VaadinService.getCurrentRequest()
                        .getWrappedSession().getAttribute(DashboardSessionValueConstants.USER);

                systemEventService.logSystemEvent(MappingConfigurationConstants.MAPPING_CONFIGURATION_SERVICE,
                        "Imported mapping configuration values for mapping configuration: [Client="
                                + mappingConfiguration.getConfigurationServiceClient().getName()
                                + "] [Source Context=" + mappingConfiguration.getSourceContext().getName()
                                + "] [Target Context=" + mappingConfiguration.getTargetContext().getName()
                                + "] [Type=" + mappingConfiguration.getConfigurationType().getName() + "]",
                        authentication.getName());

                logger.info("User: " + authentication.getName()
                        + " successfully imported the following Mapping Configuration: "
                        + mappingConfiguration);
            } catch (Exception e) {
                Notification.show(
                        "An error occurred trying to import a mapping configuration: " + e.getMessage(),
                        Notification.Type.ERROR_MESSAGE);
            }
            mappingConfigurationConfigurationValuesTable.populateTable(mappingConfiguration);
            close();
        }
    });

    progressLayout.addComponent(importButton);

    VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    layout.addComponent(new Label("Select file to upload mapping configurations."));
    layout.addComponent(upload);

    layout.addComponent(progressLayout);

    Button cancelButton = new Button("Cancel");
    cancelButton.setStyleName(ValoTheme.BUTTON_SMALL);

    cancelButton.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            close();
        }
    });

    HorizontalLayout hlayout = new HorizontalLayout();
    hlayout.addComponent(cancelButton);

    layout.addComponent(hlayout);

    super.setContent(layout);
}

From source file:org.jumpmind.metl.ui.views.design.ImportXmlTemplateWindow.java

License:Open Source License

public ImportXmlTemplateWindow(ImportXmlListener listener) {
    this.listener = listener;
    setCaption("Import XML Template");
    setModal(true);/*w  w w  . j  a  v  a 2  s  .c  om*/
    setWidth(600.0f, Unit.PIXELS);
    setHeight(500.0f, Unit.PIXELS);

    VerticalLayout layout = new VerticalLayout();
    layout.setSpacing(true);
    layout.setMargin(true);
    layout.addStyleName(ValoTheme.WINDOW_BOTTOM_TOOLBAR);
    setContent(layout);

    layout.addComponent(new Label("Import XML from either an XSD or WSDL source."));

    optionGroup = new OptionGroup("Select the location of the XSD or WSDL.");
    optionGroup.addItem(OPTION_TEXT);
    optionGroup.addItem(OPTION_FILE);
    optionGroup.addItem(OPTION_URL);
    optionGroup.setNullSelectionAllowed(false);
    optionGroup.setImmediate(true);
    optionGroup.select(OPTION_TEXT);
    optionGroup.addValueChangeListener(this);
    layout.addComponent(optionGroup);

    optionLayout = new VerticalLayout();
    editor = new AceEditor();
    editor.setCaption("Enter the XML text:");
    editor.setMode(AceMode.xml);
    editor.setWidth(100f, Unit.PERCENTAGE);
    editor.setHighlightActiveLine(true);
    editor.setShowPrintMargin(false);

    upload = new Upload(null, this);
    upload.addSucceededListener(this);
    upload.setButtonCaption(null);
    urlTextField = new TextField("Enter the URL:");
    urlTextField.setWidth(100.0f, Unit.PERCENTAGE);
    layout.addComponent(optionLayout);
    layout.setExpandRatio(optionLayout, 1.0f);
    rebuildOptionLayout();

    Button importButton = new Button("Import");
    importButton.addClickListener(this);
    layout.addComponent(importButton);
}

From source file:org.jumpmind.vaadin.ui.common.ReadOnlyTextAreaDialog.java

License:Open Source License

private void buildUploadButton(String title, final String value) {
    final Button uploadButton = new Button("Upload");
    final Button viewTextButton = new Button("View Text");

    LobUploader lobUploader = new LobUploader();
    final Upload upload = new Upload("Upload new " + table.getColumnWithName(title).getMappedType(),
            lobUploader);//from www .j  a v a  2  s  .  com
    upload.addSucceededListener(lobUploader);

    uploadButton.addClickListener(new ClickListener() {
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(ClickEvent event) {
            wrapper.replaceComponent(textField, upload);
            wrapper.setComponentAlignment(upload, Alignment.MIDDLE_CENTER);
            buttonLayout.replaceComponent(uploadButton, viewTextButton);
        }
    });

    viewTextButton.addClickListener(new ClickListener() {
        private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(ClickEvent event) {
            wrapper.replaceComponent(upload, textField);
            wrapper.setComponentAlignment(textField, Alignment.TOP_LEFT);
            buttonLayout.replaceComponent(viewTextButton, uploadButton);
        }
    });

    if (value != null) {
        buttonLayout.addComponent(uploadButton);
        buttonLayout.setComponentAlignment(uploadButton, Alignment.BOTTOM_CENTER);
    } else {
        wrapper.replaceComponent(textField, upload);
        wrapper.setComponentAlignment(upload, Alignment.MIDDLE_CENTER);
    }
}

From source file:org.jumpmind.vaadin.ui.sqlexplorer.DbImportDialog.java

License:Open Source License

protected void createImportLayout() {
    importLayout = new VerticalLayout();
    importLayout.setSizeFull();//from  w w  w .j a  va  2 s.co  m
    importLayout.addStyleName("v-scrollable");
    importLayout.setMargin(true);
    importLayout.setSpacing(true);

    importLayout.addComponent(new Label("Please select from the following options"));

    FormLayout formLayout = new FormLayout();
    formLayout.setSizeFull();
    importLayout.addComponent(formLayout);
    importLayout.setExpandRatio(formLayout, 1);

    formatSelect = new ComboBox("Format");
    for (DbImportFormat format : DbImportFormat.values()) {
        formatSelect.addItem(format);
    }
    formatSelect.setNullSelectionAllowed(false);
    formatSelect.setValue(DbImportFormat.SQL);
    formatSelect.addValueChangeListener(new Property.ValueChangeListener() {

        private static final long serialVersionUID = 1L;

        @Override
        public void valueChange(ValueChangeEvent event) {
            DbImportFormat format = (DbImportFormat) formatSelect.getValue();

            switch (format) {
            case SQL:
                listOfTablesSelect.setEnabled(false);
                alter.setEnabled(false);
                alterCase.setEnabled(false);
                break;
            case XML:
                listOfTablesSelect.setEnabled(false);
                alter.setEnabled(true);
                alterCase.setEnabled(true);
                break;
            case CSV:
                listOfTablesSelect.setEnabled(true);
                alter.setEnabled(false);
                alterCase.setEnabled(false);
                importButton.setEnabled(importButtonEnable());
                break;
            case SYM_XML:
                listOfTablesSelect.setEnabled(false);
                alter.setEnabled(false);
                alterCase.setEnabled(false);
                break;
            }
        }
    });
    formLayout.addComponent(formatSelect);

    catalogSelect = new ComboBox("Catalog");
    catalogSelect.setImmediate(true);
    CommonUiUtils.addItems(getCatalogs(), catalogSelect);
    catalogSelect.select(databasePlatform.getDefaultCatalog());
    catalogSelect.setNullSelectionAllowed(false);
    formLayout.addComponent(catalogSelect);

    schemaSelect = new ComboBox("Schema");
    schemaSelect.setImmediate(true);
    CommonUiUtils.addItems(getSchemas(), schemaSelect);
    if (selectedTablesSet.iterator().hasNext()) {
        schemaSelect.select(selectedTablesSet.iterator().next().getSchema());
    } else {
        schemaSelect.select(databasePlatform.getDefaultSchema());
    }
    schemaSelect.setNullSelectionAllowed(false);
    schemaSelect.addValueChangeListener(new ValueChangeListener() {

        private static final long serialVersionUID = 1L;

        @Override
        public void valueChange(ValueChangeEvent event) {
            populateListOfTablesSelect();
        }
    });
    formLayout.addComponent(schemaSelect);

    listOfTablesSelect = new ComboBox("Tables");
    populateListOfTablesSelect();
    listOfTablesSelect.setEnabled(false);
    listOfTablesSelect.setNullSelectionAllowed(false);

    if (!this.selectedTablesSet.isEmpty()) {
        if (this.selectedTablesSet.size() == 1) {
            this.selectedTable = this.selectedTablesSet.iterator().next();
            listOfTablesSelect.select(this.selectedTable.getName());
            this.selectedTablesSet.clear();
        } else {
            List<Table> list = new ArrayList<Table>(this.selectedTablesSet);
            listOfTablesSelect.select(list.get(0).getName());
            this.selectedTable = list.get(0);
            this.selectedTablesSet.clear();
        }
    }
    formLayout.addComponent(listOfTablesSelect);

    commitField = new TextField("Rows to Commit");
    commitField.addTextChangeListener(new TextChangeListener() {

        private static final long serialVersionUID = 1L;

        @Override
        public void textChange(TextChangeEvent event) {
            commitField.setValue(event.getText());
            if (fileSelected) {
                importButton.setEnabled(importButtonEnable());
            }
        }
    });
    commitField.setImmediate(true);
    commitField.setTextChangeEventMode(TextChangeEventMode.EAGER);
    commitField.setValue("10000");
    formLayout.addComponent(commitField);

    force = new CheckBox("Force");
    formLayout.addComponent(force);

    ignore = new CheckBox("Ignore");
    formLayout.addComponent(ignore);

    replace = new CheckBox("Replace");
    formLayout.addComponent(replace);

    alter = new CheckBox("Alter");
    alter.setEnabled(false);
    formLayout.addComponent(alter);

    alterCase = new CheckBox("Alter Case");
    alterCase.setEnabled(false);
    formLayout.addComponent(alterCase);

    upload = new Upload("File", new Receiver() {

        private static final long serialVersionUID = 1L;

        @Override
        public OutputStream receiveUpload(String filename, String mimeType) {
            try {
                file = File.createTempFile("dbimport", formatSelect.getValue().toString());
                out = new FileOutputStream(file);
                return new BufferedOutputStream(new FileOutputStream(file));
            } catch (Exception e) {
                log.warn(e.getMessage(), e);
                CommonUiUtils.notify("Failed to import " + filename, e);
            }
            return null;
        }
    });
    upload.addSucceededListener(new SucceededListener() {

        private static final long serialVersionUID = 1L;

        @Override
        public void uploadSucceeded(SucceededEvent event) {
            createDbImport();
            try {
                doDbImport();
            } catch (FileNotFoundException e) {
                log.warn(e.getMessage(), e);
                Notification.show(e.getMessage());
            }
            deleteFileAndResource();
            close();
        }
    });
    upload.addChangeListener(new ChangeListener() {

        private static final long serialVersionUID = 1L;

        public void filenameChanged(ChangeEvent event) {
            fileSelected = true;
            importButton.setEnabled(importButtonEnable());
        }
    });
    upload.setButtonCaption(null);
    formLayout.addComponent(upload);

    cancelButton = new Button("Cancel", new Button.ClickListener() {
        private static final long serialVersionUID = 1L;

        public void buttonClick(ClickEvent event) {
            UI.getCurrent().removeWindow(DbImportDialog.this);
        }
    });

    importButton = CommonUiUtils.createPrimaryButton("Import", new Button.ClickListener() {
        private static final long serialVersionUID = 1L;

        public void buttonClick(ClickEvent event) {
            upload.submitUpload();
        }
    });
    importButton.setEnabled(false);

    addComponent(importLayout, 1);
    addComponent(buildButtonFooter(cancelButton, importButton));
}