Search in sources :

Example 1 with Validator

use of com.vaadin.v7.data.Validator in project SORMAS-Project by hzi-braunschweig.

the class HospitalizationForm method addFields.

@SuppressWarnings("deprecation")
@Override
protected void addFields() {
    if (caze == null || viewMode == null) {
        return;
    }
    Label hospitalizationHeadingLabel = new Label(I18nProperties.getString(Strings.headingHospitalization));
    hospitalizationHeadingLabel.addStyleName(H3);
    getContent().addComponent(hospitalizationHeadingLabel, HOSPITALIZATION_HEADING_LOC);
    Label previousHospitalizationsHeadingLabel = new Label(I18nProperties.getString(Strings.headingPreviousHospitalizations));
    previousHospitalizationsHeadingLabel.addStyleName(H3);
    getContent().addComponent(previousHospitalizationsHeadingLabel, PREVIOUS_HOSPITALIZATIONS_HEADING_LOC);
    TextField facilityField = addCustomField(HEALTH_FACILITY, FacilityReferenceDto.class, TextField.class);
    FacilityReferenceDto healthFacility = caze.getHealthFacility();
    final boolean noneFacility = healthFacility == null || healthFacility.getUuid().equalsIgnoreCase(FacilityDto.NONE_FACILITY_UUID);
    facilityField.setValue(noneFacility || !FacilityType.HOSPITAL.equals(caze.getFacilityType()) ? null : healthFacility.toString());
    facilityField.setReadOnly(true);
    final NullableOptionGroup admittedToHealthFacilityField = addField(HospitalizationDto.ADMITTED_TO_HEALTH_FACILITY, NullableOptionGroup.class);
    final DateField admissionDateField = addField(HospitalizationDto.ADMISSION_DATE, DateField.class);
    final DateField dischargeDateField = addDateField(HospitalizationDto.DISCHARGE_DATE, DateField.class, 7);
    intensiveCareUnit = addField(HospitalizationDto.INTENSIVE_CARE_UNIT, NullableOptionGroup.class);
    intensiveCareUnitStart = addField(HospitalizationDto.INTENSIVE_CARE_UNIT_START, DateField.class);
    intensiveCareUnitStart.setVisible(false);
    intensiveCareUnitEnd = addField(HospitalizationDto.INTENSIVE_CARE_UNIT_END, DateField.class);
    intensiveCareUnitEnd.setVisible(false);
    FieldHelper.setVisibleWhen(intensiveCareUnit, Arrays.asList(intensiveCareUnitStart, intensiveCareUnitEnd), Arrays.asList(YesNoUnknown.YES), true);
    final Field isolationDateField = addField(HospitalizationDto.ISOLATION_DATE);
    final TextArea descriptionField = addField(HospitalizationDto.DESCRIPTION, TextArea.class);
    descriptionField.setRows(4);
    final NullableOptionGroup isolatedField = addField(HospitalizationDto.ISOLATED, NullableOptionGroup.class);
    final NullableOptionGroup leftAgainstAdviceField = addField(HospitalizationDto.LEFT_AGAINST_ADVICE, NullableOptionGroup.class);
    final ComboBox hospitalizationReason = addField(HospitalizationDto.HOSPITALIZATION_REASON);
    final TextField otherHospitalizationReason = addField(HospitalizationDto.OTHER_HOSPITALIZATION_REASON, TextField.class);
    NullableOptionGroup hospitalizedPreviouslyField = addField(HospitalizationDto.HOSPITALIZED_PREVIOUSLY, NullableOptionGroup.class);
    CssStyles.style(hospitalizedPreviouslyField, CssStyles.ERROR_COLOR_PRIMARY);
    PreviousHospitalizationsField previousHospitalizationsField = addField(HospitalizationDto.PREVIOUS_HOSPITALIZATIONS, PreviousHospitalizationsField.class);
    FieldHelper.setEnabledWhen(admittedToHealthFacilityField, Arrays.asList(YesNoUnknown.YES, YesNoUnknown.NO, YesNoUnknown.UNKNOWN), Arrays.asList(facilityField, admissionDateField, dischargeDateField, intensiveCareUnit, intensiveCareUnitStart, intensiveCareUnitEnd, isolationDateField, descriptionField, isolatedField, leftAgainstAdviceField, hospitalizationReason, otherHospitalizationReason), false);
    initializeVisibilitiesAndAllowedVisibilities();
    initializeAccessAndAllowedAccesses();
    if (isVisibleAllowed(HospitalizationDto.ISOLATION_DATE)) {
        FieldHelper.setVisibleWhen(getFieldGroup(), HospitalizationDto.ISOLATION_DATE, HospitalizationDto.ISOLATED, Arrays.asList(YesNoUnknown.YES), true);
    }
    if (isVisibleAllowed(HospitalizationDto.PREVIOUS_HOSPITALIZATIONS)) {
        FieldHelper.setVisibleWhen(getFieldGroup(), HospitalizationDto.PREVIOUS_HOSPITALIZATIONS, HospitalizationDto.HOSPITALIZED_PREVIOUSLY, Arrays.asList(YesNoUnknown.YES), true);
    }
    FieldHelper.setVisibleWhen(getFieldGroup(), HospitalizationDto.OTHER_HOSPITALIZATION_REASON, HospitalizationDto.HOSPITALIZATION_REASON, Collections.singletonList(HospitalizationReasonType.OTHER), true);
    // Validations
    // Add a visual-only validator to check if symptomonsetdate<admissiondate, as saving should be possible either way
    admissionDateField.addValueChangeListener(event -> {
        if (caze.getSymptoms().getOnsetDate() != null && DateTimeComparator.getDateOnlyInstance().compare(admissionDateField.getValue(), caze.getSymptoms().getOnsetDate()) < 0) {
            admissionDateField.setComponentError(new ErrorMessage() {

                @Override
                public ErrorLevel getErrorLevel() {
                    return ErrorLevel.INFO;
                }

                @Override
                public String getFormattedHtmlMessage() {
                    return I18nProperties.getValidationError(Validations.afterDateSoft, admissionDateField.getCaption(), I18nProperties.getPrefixCaption(SymptomsDto.I18N_PREFIX, SymptomsDto.ONSET_DATE));
                }
            });
        } else {
            // remove all invalidity-indicators and re-evaluate field
            admissionDateField.setComponentError(null);
            admissionDateField.markAsDirty();
        }
        // re-evaluate validity of dischargeDate (necessary because discharge has to be after admission)
        dischargeDateField.markAsDirty();
    });
    admissionDateField.addValidator(new DateComparisonValidator(admissionDateField, dischargeDateField, true, false, I18nProperties.getValidationError(Validations.beforeDate, admissionDateField.getCaption(), dischargeDateField.getCaption())));
    dischargeDateField.addValidator(new DateComparisonValidator(dischargeDateField, admissionDateField, false, false, I18nProperties.getValidationError(Validations.afterDate, dischargeDateField.getCaption(), admissionDateField.getCaption())));
    // re-evaluate admission date for consistent validation of all fields
    dischargeDateField.addValueChangeListener(event -> admissionDateField.markAsDirty());
    intensiveCareUnitStart.addValidator(new DateComparisonValidator(intensiveCareUnitStart, admissionDateField, false, false, I18nProperties.getValidationError(Validations.afterDate, intensiveCareUnitStart.getCaption(), admissionDateField.getCaption())));
    intensiveCareUnitStart.addValidator(new DateComparisonValidator(intensiveCareUnitStart, intensiveCareUnitEnd, true, false, I18nProperties.getValidationError(Validations.beforeDate, intensiveCareUnitStart.getCaption(), intensiveCareUnitEnd.getCaption())));
    intensiveCareUnitEnd.addValidator(new DateComparisonValidator(intensiveCareUnitEnd, intensiveCareUnitStart, false, false, I18nProperties.getValidationError(Validations.afterDate, intensiveCareUnitEnd.getCaption(), intensiveCareUnitStart.getCaption())));
    intensiveCareUnitEnd.addValidator(new DateComparisonValidator(intensiveCareUnitEnd, dischargeDateField, true, false, I18nProperties.getValidationError(Validations.beforeDate, intensiveCareUnitEnd.getCaption(), dischargeDateField.getCaption())));
    intensiveCareUnitStart.addValueChangeListener(event -> intensiveCareUnitEnd.markAsDirty());
    intensiveCareUnitEnd.addValueChangeListener(event -> intensiveCareUnitStart.markAsDirty());
    hospitalizedPreviouslyField.addValueChangeListener(e -> updatePrevHospHint(hospitalizedPreviouslyField, previousHospitalizationsField));
    previousHospitalizationsField.addValueChangeListener(e -> updatePrevHospHint(hospitalizedPreviouslyField, previousHospitalizationsField));
}
Also used : NullableOptionGroup(de.symeda.sormas.ui.utils.NullableOptionGroup) FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) TextArea(com.vaadin.v7.ui.TextArea) ComboBox(com.vaadin.v7.ui.ComboBox) Label(com.vaadin.ui.Label) DateComparisonValidator(de.symeda.sormas.ui.utils.DateComparisonValidator) DateField(com.vaadin.v7.ui.DateField) Field(com.vaadin.v7.ui.Field) TextField(com.vaadin.v7.ui.TextField) TextField(com.vaadin.v7.ui.TextField) ErrorLevel(com.vaadin.shared.ui.ErrorLevel) DateField(com.vaadin.v7.ui.DateField) ErrorMessage(com.vaadin.server.ErrorMessage)

Example 2 with Validator

use of com.vaadin.v7.data.Validator in project SORMAS-Project by hzi-braunschweig.

the class ResizableTextAreaWrapper method wrap.

@Override
public ComponentContainer wrap(T textField, String caption, boolean withMargin) {
    this.textField = textField;
    this.caption = caption;
    VerticalLayout layout = new VerticalLayout();
    layout.setSpacing(false);
    layout.setMargin(false);
    layout.setWidth(100, Sizeable.Unit.PERCENTAGE);
    if (withMargin) {
        layout.addStyleName(CssStyles.FIELD_WRAPPER);
    }
    textField.setWidth(100, Sizeable.Unit.PERCENTAGE);
    textField.addStyleName(CssStyles.RESIZABLE);
    textField.setNullRepresentation("");
    textField.setTextChangeTimeout(200);
    Stream<Validator> maxLengthValidatorStream = textField.getValidators().stream().filter(v -> v instanceof MaxLengthValidator);
    if (withMaxLength) {
        maxLengthValidatorStream.findFirst().map(v -> ((MaxLengthValidator) v).getMaxLength()).ifPresent(textField::setMaxLength);
        labelField = new Label(buildLabelMessage(textField.getValue(), textField, caption));
        labelField.setId(textField.getId() + "_label");
        labelField.setWidth(100, Sizeable.Unit.PERCENTAGE);
        labelField.addStyleNames(CssStyles.ALIGN_RIGHT, CssStyles.FIELD_EXTRA_INFO, CssStyles.LABEL_ITALIC);
        layout.addComponents(labelField);
    } else {
        maxLengthValidatorStream.iterator().forEachRemaining(v -> textField.removeValidator(v));
    }
    textField.addTextChangeListener(e -> {
        updateTextfieldAppearance();
    });
    textField.addValueChangeListener(e -> {
        updateTextfieldAppearance();
    });
    layout.addComponents(textField);
    if (withMaxLength) {
        layout.addComponents(labelField);
    }
    return layout;
}
Also used : Strings(com.google.common.base.Strings) Stream(java.util.stream.Stream) VerticalLayout(com.vaadin.v7.ui.VerticalLayout) Validator(com.vaadin.v7.data.Validator) TextArea(com.vaadin.v7.ui.TextArea) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) CharMatcher(com.google.common.base.CharMatcher) AbstractTextField(com.vaadin.v7.ui.AbstractTextField) Sizeable(com.vaadin.server.Sizeable) ComponentContainer(com.vaadin.ui.ComponentContainer) Label(com.vaadin.v7.ui.Label) Label(com.vaadin.v7.ui.Label) VerticalLayout(com.vaadin.v7.ui.VerticalLayout) Validator(com.vaadin.v7.data.Validator)

Example 3 with Validator

use of com.vaadin.v7.data.Validator in project opencms-core by alkacon.

the class CmsEditSiteForm method setupValidatorAliase.

/**
 * Setup for the aliase validator.<p>
 */
void setupValidatorAliase() {
    for (I_CmsEditableGroupRow row : m_aliasGroup.getRows()) {
        FormLayout layout = (FormLayout) (row.getComponent());
        TextField field = (TextField) layout.getComponent(0);
        field.removeAllValidators();
        field.addValidator(new AliasValidator());
    }
}
Also used : FormLayout(com.vaadin.ui.FormLayout) TextField(com.vaadin.v7.ui.TextField) I_CmsEditableGroupRow(org.opencms.ui.components.editablegroup.I_CmsEditableGroupRow)

Example 4 with Validator

use of com.vaadin.v7.data.Validator in project opencms-core by alkacon.

the class CmsDbSynchronizationView method removeEmptyResourceFields.

/**
 * Removes empty resource fields.<p>
 */
protected void removeEmptyResourceFields() {
    Set<Component> componentsToRemove = new HashSet<Component>();
    int count = 0;
    Component lastElement = null;
    for (Component c : m_resources) {
        if (c instanceof CmsRemovableFormRow<?>) {
            count++;
            AbstractField<?> field = (AbstractField<?>) ((CmsRemovableFormRow) c).getInput();
            String value = (String) (field.getValue());
            if (value.isEmpty()) {
                componentsToRemove.add(c);
                lastElement = c;
            }
        }
    }
    // if all fields are empty -> keep one of them to attach validator to
    if (componentsToRemove.size() == count) {
        componentsToRemove.remove(lastElement);
    }
    if (count == 0) {
        addResource("");
    }
    for (Component c : componentsToRemove) {
        m_resources.removeComponent(c);
        m_componentsToValidate.remove(c);
    }
}
Also used : AbstractField(com.vaadin.v7.ui.AbstractField) CmsRemovableFormRow(org.opencms.ui.components.CmsRemovableFormRow) Component(com.vaadin.ui.Component) HashSet(java.util.HashSet)

Example 5 with Validator

use of com.vaadin.v7.data.Validator in project SORMAS-Project by hzi-braunschweig.

the class PersonContactDetailEditForm method addFields.

@SuppressWarnings("deprecation")
@Override
protected void addFields() {
    addField(PersonContactDetailDto.THIRD_PARTY, CheckBox.class).setCaption(I18nProperties.getCaption(Captions.personContactDetailThirdParty));
    addField(PersonContactDetailDto.THIRD_PARTY_ROLE, TextField.class);
    addField(PersonContactDetailDto.THIRD_PARTY_NAME, TextField.class);
    addField(PersonContactDetailDto.PERSON_CONTACT_DETAILS_TYPE);
    addField(PersonContactDetailDto.PHONE_NUMBER_TYPE);
    addField(PersonContactDetailDto.DETAILS, TextField.class);
    addField(PersonContactDetailDto.CONTACT_INFORMATION, TextField.class);
    addField(PersonContactDetailDto.ADDITIONAL_INFORMATION, TextField.class);
    addField(PersonContactDetailDto.PRIMARY_CONTACT, CheckBox.class);
    FieldHelper.setVisibleWhen(getFieldGroup(), Arrays.asList(PersonContactDetailDto.THIRD_PARTY_ROLE, PersonContactDetailDto.THIRD_PARTY_NAME), PersonContactDetailDto.THIRD_PARTY, true, true);
    FieldHelper.setVisibleWhen(getFieldGroup(), Arrays.asList(PersonContactDetailDto.DETAILS), PersonContactDetailDto.PERSON_CONTACT_DETAILS_TYPE, PersonContactDetailType.OTHER, false);
    FieldHelper.setVisibleWhen(getFieldGroup(), Arrays.asList(PersonContactDetailDto.DETAILS), PersonContactDetailDto.PHONE_NUMBER_TYPE, PhoneNumberType.OTHER, false);
    FieldHelper.setVisibleWhen(getFieldGroup(), Arrays.asList(PersonContactDetailDto.PHONE_NUMBER_TYPE), PersonContactDetailDto.PERSON_CONTACT_DETAILS_TYPE, PersonContactDetailType.PHONE, false);
    addFieldListeners(PersonContactDetailDto.PERSON_CONTACT_DETAILS_TYPE, e -> {
        final Field<?> contactInformationField = getFieldGroup().getField(PersonContactDetailDto.CONTACT_INFORMATION);
        final PersonContactDetailType value = (PersonContactDetailType) e.getProperty().getValue();
        for (Validator validator : contactInformationField.getValidators()) {
            contactInformationField.removeValidator(validator);
        }
        if (value == PersonContactDetailType.PHONE) {
            contactInformationField.addValidator(new PhoneNumberValidator(I18nProperties.getValidationError(Validations.validPhoneNumber, contactInformationField.getCaption())));
        } else if (value == PersonContactDetailType.EMAIL) {
            contactInformationField.addValidator(new EmailValidator(I18nProperties.getValidationError(Validations.validEmailAddress, contactInformationField.getCaption())));
        }
    });
}
Also used : PersonContactDetailType(de.symeda.sormas.api.person.PersonContactDetailType) EmailValidator(com.vaadin.v7.data.validator.EmailValidator) CheckBox(com.vaadin.v7.ui.CheckBox) PhoneNumberValidator(de.symeda.sormas.ui.utils.PhoneNumberValidator) Validator(com.vaadin.v7.data.Validator) EmailValidator(com.vaadin.v7.data.validator.EmailValidator) PhoneNumberValidator(de.symeda.sormas.ui.utils.PhoneNumberValidator)

Aggregations

TextArea (com.vaadin.v7.ui.TextArea)4 TextField (com.vaadin.v7.ui.TextField)4 ErrorMessage (com.vaadin.server.ErrorMessage)3 ErrorLevel (com.vaadin.shared.ui.ErrorLevel)3 Label (com.vaadin.ui.Label)3 Validator (com.vaadin.v7.data.Validator)3 CheckBox (com.vaadin.v7.ui.CheckBox)3 ComboBox (com.vaadin.v7.ui.ComboBox)3 DateField (com.vaadin.v7.ui.DateField)3 Field (com.vaadin.v7.ui.Field)3 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)3 Button (com.vaadin.ui.Button)2 ValoTheme (com.vaadin.ui.themes.ValoTheme)2 Property (com.vaadin.v7.data.Property)2 DateRangeValidator (com.vaadin.v7.data.validator.DateRangeValidator)2 Resolution (com.vaadin.v7.shared.ui.datefield.Resolution)2 CountryHelper (de.symeda.sormas.api.CountryHelper)2 Disease (de.symeda.sormas.api.Disease)2 FacadeProvider (de.symeda.sormas.api.FacadeProvider)2 CaseDataDto (de.symeda.sormas.api.caze.CaseDataDto)2