Search in sources :

Example 26 with EntityValueSource

use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.

the class CurrencyFieldImpl 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 = ((io.jmix.core.entity.annotation.CurrencyLabelPosition) annotationProperties.get("labelPosition")).name();
        setCurrencyLabelPosition(CurrencyLabelPosition.valueOf(labelPosition));
    }
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) Datatype(io.jmix.core.metamodel.datatype.Datatype) CurrencyValue(io.jmix.core.entity.annotation.CurrencyValue) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Map(java.util.Map)

Example 27 with EntityValueSource

use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.

the class CurrencyFieldImpl 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 applicationContext.getBean(Messages.class).getMessage("databinding.conversion.error");
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) Messages(io.jmix.core.Messages)

Example 28 with EntityValueSource

use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.

the class CurrencyFieldImpl method convertToPresentation.

@Override
protected String convertToPresentation(@Nullable V modelValue) throws ConversionException {
    Datatype<V> datatype = getDatatypeInternal();
    // Vaadin TextField does not permit `null` value
    if (datatype != null) {
        return nullToEmpty(datatype.format(modelValue, locale));
    }
    if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
        Datatype<V> propertyDataType = entityValueSource.getMetaPropertyPath().getRange().asDatatype();
        return nullToEmpty(propertyDataType.format(modelValue, locale));
    }
    return nullToEmpty(super.convertToPresentation(modelValue));
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource)

Example 29 with EntityValueSource

use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.

the class DateFieldImpl method constructModelValue.

@SuppressWarnings("unchecked")
@Nullable
protected V constructModelValue() {
    LocalDate dateValue = dateField.getValue();
    if (dateValue == null) {
        return null;
    }
    LocalTime timeValue = timeField.getValue() != null ? timeField.getValue() : LocalTime.MIDNIGHT;
    LocalDateTime localDateTime = LocalDateTime.of(dateValue, timeValue);
    ValueSource<V> valueSource = getValueSource();
    if (valueSource instanceof EntityValueSource) {
        MetaProperty metaProperty = ((EntityValueSource) valueSource).getMetaPropertyPath().getMetaProperty();
        return (V) convertFromLocalDateTime(localDateTime, zoneId, metaProperty.getRange().asDatatype().getJavaClass());
    }
    return (V) convertFromLocalDateTime(localDateTime, zoneId, datatype == null ? Date.class : datatype.getJavaClass());
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 30 with EntityValueSource

use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.

the class DatePickerImpl method valueBindingConnected.

@Override
protected void valueBindingConnected(ValueSource<V> valueSource) {
    super.valueBindingConnected(valueSource);
    if (valueSource instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueSource;
        DataAwareComponentsTools dataAwareComponentsTools = applicationContext.getBean(DataAwareComponentsTools.class);
        dataAwareComponentsTools.setupDateRange(this, entityValueSource);
    }
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) DataAwareComponentsTools(io.jmix.ui.component.data.DataAwareComponentsTools)

Aggregations

EntityValueSource (io.jmix.ui.component.data.meta.EntityValueSource)40 DataAwareComponentsTools (io.jmix.ui.component.data.DataAwareComponentsTools)11 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)10 ValueSource (io.jmix.ui.component.data.ValueSource)9 Nullable (javax.annotation.Nullable)9 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)8 Messages (io.jmix.core.Messages)5 ConversionException (io.jmix.ui.component.data.ConversionException)5 HasValueSource (io.jmix.ui.component.data.HasValueSource)5 Datatype (io.jmix.core.metamodel.datatype.Datatype)4 MetaClass (io.jmix.core.metamodel.model.MetaClass)4 ValueConversionException (io.jmix.ui.component.data.ValueConversionException)4 ParseException (java.text.ParseException)4 UiEntityAttributeContext (io.jmix.ui.accesscontext.UiEntityAttributeContext)3 io.jmix.ui.component (io.jmix.ui.component)3 Nested (io.jmix.ui.model.Nested)3 Range (io.jmix.core.metamodel.model.Range)2 DataContext (io.jmix.ui.model.DataContext)2 DatasourceValueSource (com.haulmont.cuba.gui.components.data.value.DatasourceValueSource)1 LegacyCollectionDsValueSource (com.haulmont.cuba.gui.components.data.value.LegacyCollectionDsValueSource)1