Search in sources :

Example 16 with EntityValueSource

use of com.haulmont.cuba.gui.components.data.meta.EntityValueSource in project cuba by cuba-platform.

the class WebCurrencyField method valueBindingConnected.

@Override
protected void valueBindingConnected(ValueSource<V> valueSource) {
    super.valueBindingConnected(valueSource);
    if (valueSource instanceof EntityValueSource) {
        MetaPropertyPath metaPropertyPath = ((EntityValueSource) valueSource).getMetaPropertyPath();
        if (metaPropertyPath.getRange().isDatatype()) {
            Datatype datatype = metaPropertyPath.getRange().asDatatype();
            if (!Number.class.isAssignableFrom(datatype.getJavaClass())) {
                throw new IllegalArgumentException("CurrencyField doesn't support Datatype with class: " + datatype.getJavaClass());
            }
        } else {
            throw new IllegalArgumentException("CurrencyField doesn't support properties with association");
        }
        MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
        Object annotation = metaProperty.getAnnotations().get(CurrencyValue.class.getName());
        if (annotation == null) {
            return;
        }
        // noinspection unchecked
        Map<String, Object> annotationProperties = (Map<String, Object>) annotation;
        String currencyName = (String) annotationProperties.get("currency");
        component.setCurrency(currencyName);
        String labelPosition = ((com.haulmont.cuba.core.entity.annotation.CurrencyLabelPosition) annotationProperties.get("labelPosition")).name();
        setCurrencyLabelPosition(CurrencyLabelPosition.valueOf(labelPosition));
    }
}
Also used : EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) Datatype(com.haulmont.chile.core.datatypes.Datatype) CurrencyValue(com.haulmont.cuba.core.entity.annotation.CurrencyValue) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Map(java.util.Map)

Example 17 with EntityValueSource

use of com.haulmont.cuba.gui.components.data.meta.EntityValueSource in project cuba by cuba-platform.

the class WebCurrencyField method convertToModel.

@Override
protected V convertToModel(String componentRawValue) throws ConversionException {
    String value = StringUtils.trimToNull(emptyToNull(componentRawValue));
    Datatype<V> datatype = getDatatypeInternal();
    if (datatype != null) {
        try {
            return datatype.parse(value, locale);
        } catch (ValueConversionException e) {
            throw new ConversionException(e.getLocalizedMessage(), e);
        } catch (ParseException e) {
            throw new ConversionException(getConversionErrorMessageInternal(), e);
        }
    }
    if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
        Datatype<V> propertyDataType = entityValueSource.getMetaPropertyPath().getRange().asDatatype();
        try {
            return propertyDataType.parse(componentRawValue, locale);
        } catch (ValueConversionException e) {
            throw new ConversionException(e.getLocalizedMessage(), e);
        } catch (ParseException e) {
            throw new ConversionException(getConversionErrorMessageInternal(), e);
        }
    }
    return super.convertToModel(componentRawValue);
}
Also used : ConversionException(com.haulmont.cuba.gui.components.data.ConversionException) ValueConversionException(com.haulmont.chile.core.datatypes.ValueConversionException) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) ParseException(java.text.ParseException) ValueConversionException(com.haulmont.chile.core.datatypes.ValueConversionException)

Example 18 with EntityValueSource

use of com.haulmont.cuba.gui.components.data.meta.EntityValueSource in project cuba by cuba-platform.

the class WebDatePicker method convertToModel.

@SuppressWarnings("unchecked")
@Override
protected V convertToModel(LocalDate componentRawValue) throws ConversionException {
    if (componentRawValue == null) {
        return null;
    }
    LocalDateTime localDateTime = LocalDateTime.of(componentRawValue, LocalTime.MIDNIGHT);
    ValueSource<V> valueSource = getValueSource();
    if (valueSource instanceof EntityValueSource) {
        MetaProperty metaProperty = ((EntityValueSource) valueSource).getMetaPropertyPath().getMetaProperty();
        return (V) convertFromLocalDateTime(localDateTime, metaProperty.getRange().asDatatype().getJavaClass());
    }
    return (V) convertFromLocalDateTime(localDateTime, datatype == null ? Date.class : datatype.getJavaClass());
}
Also used : EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 19 with EntityValueSource

use of com.haulmont.cuba.gui.components.data.meta.EntityValueSource in project cuba by cuba-platform.

the class WebDatePicker method valueBindingConnected.

@Override
protected void valueBindingConnected(ValueSource<V> valueSource) {
    super.valueBindingConnected(valueSource);
    if (valueSource instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueSource;
        DataAwareComponentsTools dataAwareComponentsTools = beanLocator.get(DataAwareComponentsTools.class);
        dataAwareComponentsTools.setupDateRange(this, entityValueSource);
    }
}
Also used : EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) DataAwareComponentsTools(com.haulmont.cuba.gui.components.data.DataAwareComponentsTools)

Example 20 with EntityValueSource

use of com.haulmont.cuba.gui.components.data.meta.EntityValueSource in project cuba by cuba-platform.

the class WebAbstractTextArea method getConversionErrorMessageInternal.

protected String getConversionErrorMessageInternal() {
    String customErrorMessage = getConversionErrorMessage();
    if (StringUtils.isNotEmpty(customErrorMessage)) {
        return customErrorMessage;
    }
    Datatype<V> datatype = this.datatype;
    if (datatype == null && valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
        datatype = entityValueSource.getMetaPropertyPath().getRange().asDatatype();
    }
    if (datatype != null) {
        String msg = getDatatypeConversionErrorMsg(datatype);
        if (StringUtils.isNotEmpty(msg)) {
            return msg;
        }
    }
    return beanLocator.get(Messages.class).getMainMessage("databinding.conversion.error");
}
Also used : EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) Messages(com.haulmont.cuba.core.global.Messages)

Aggregations

EntityValueSource (com.haulmont.cuba.gui.components.data.meta.EntityValueSource)39 MetaProperty (com.haulmont.chile.core.model.MetaProperty)10 DataAwareComponentsTools (com.haulmont.cuba.gui.components.data.DataAwareComponentsTools)10 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)9 ValueSource (com.haulmont.cuba.gui.components.data.ValueSource)9 HasValueSource (com.haulmont.cuba.gui.components.data.HasValueSource)6 Messages (com.haulmont.cuba.core.global.Messages)5 ConversionException (com.haulmont.cuba.gui.components.data.ConversionException)5 Datatype (com.haulmont.chile.core.datatypes.Datatype)4 ValueConversionException (com.haulmont.chile.core.datatypes.ValueConversionException)4 Entity (com.haulmont.cuba.core.entity.Entity)4 ParseException (java.text.ParseException)4 MetaClass (com.haulmont.chile.core.model.MetaClass)3 Range (com.haulmont.chile.core.model.Range)2 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)2 Field (com.haulmont.cuba.gui.components.Field)2 DatasourceValueSource (com.haulmont.cuba.gui.components.data.value.DatasourceValueSource)2 Pair (com.haulmont.bali.datastruct.Pair)1 Subscription (com.haulmont.bali.events.Subscription)1 Preconditions (com.haulmont.bali.util.Preconditions)1