Search in sources :

Example 16 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.

the class WebAbstractTable method getFormattedValue.

protected String getFormattedValue(Column column, Object value) {
    String cellText;
    if (value == null) {
        cellText = "";
    } else {
        if (value instanceof String) {
            cellText = (String) value;
        } else {
            Formatter formatter = column.getFormatter();
            if (formatter != null) {
                // noinspection unchecked
                cellText = formatter.format(value);
            } else {
                Datatype datatype = Datatypes.get(value.getClass());
                if (datatype != null) {
                    UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
                    cellText = datatype.format(value, sessionSource.getLocale());
                } else {
                    cellText = value.toString();
                }
            }
        }
    }
    return cellText;
}
Also used : CollectionFormatter(com.haulmont.cuba.gui.components.formatters.CollectionFormatter) Formatter(com.haulmont.cuba.gui.components.Formatter) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 17 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.

the class WebAbstractTextField method setValue.

@Override
public void setValue(Object value) {
    if (!(value instanceof String)) {
        String formattedValue;
        Datatype<String> stringDatatype = Datatypes.getNN(String.class);
        Datatype datatype = getActualDatatype();
        if (datatype != null && stringDatatype != datatype) {
            formattedValue = datatype.format(value, locale);
        } else {
            MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
            formattedValue = metadataTools.format(value);
        }
        super.setValue(formattedValue);
    } else {
        super.setValue(value);
    }
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 18 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.

the class AppPropertiesEdit method init.

@Override
public void init(Map<String, Object> params) {
    cannotEditValueLabel.setVisible(item.getOverridden());
    Datatype datatype = Datatypes.get(item.getDataTypeName());
    fieldGroup.addCustomField("currentValue", (datasource, propertyId) -> {
        if (item.getOverridden()) {
            TextField textField = componentsFactory.createComponent(TextField.class);
            textField.setValue(item.getDisplayedCurrentValue());
            textField.setEditable(false);
            return textField;
        }
        if (item.getEnumValues() != null) {
            return createLookupField(Arrays.asList(item.getEnumValues().split(",")), item.getCurrentValue());
        } else {
            if (datatype instanceof BooleanDatatype) {
                return createLookupField(Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()), item.getCurrentValue());
            } else {
                if (Boolean.TRUE.equals(item.getSecret())) {
                    PasswordField passwordField = componentsFactory.createComponent(PasswordField.class);
                    passwordField.setValue(item.getCurrentValue());
                    passwordField.addValueChangeListener(e -> {
                        appPropertyDs.getItem().setCurrentValue(e.getValue() == null ? null : e.getValue().toString());
                    });
                    return passwordField;
                } else {
                    TextField textField = componentsFactory.createComponent(TextField.class);
                    textField.setValue(item.getCurrentValue());
                    try {
                        datatype.parse(item.getCurrentValue(), userSessionSource.getLocale());
                        textField.setDatatype(datatype);
                    } catch (ParseException e) {
                        // do not assign datatype then
                        log.trace("Localized parsing by datatype cannot be used for value {}", item.getCurrentValue());
                    }
                    textField.addValueChangeListener(e -> {
                        appPropertyDs.getItem().setCurrentValue(e.getValue() == null ? null : e.getValue().toString());
                    });
                    return textField;
                }
            }
        }
    });
    final Formatter<String> defaultValueFormatter = (value) -> {
        if (datatype instanceof BooleanDatatype) {
            return value;
        }
        try {
            Object parsedDefaultValue = datatype.parse(value);
            return datatype.format(parsedDefaultValue, userSessionSource.getLocale());
        } catch (ParseException e) {
            log.trace("Localized parsing by datatype cannot be used for value {}", value, e);
        }
        return value;
    };
    displayedDefaultValueField.setFormatter(defaultValueFormatter);
    appPropertyDs.setItem(metadata.getTools().copy(item));
}
Also used : Arrays(java.util.Arrays) Datasource(com.haulmont.cuba.gui.data.Datasource) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Datatypes(com.haulmont.chile.core.datatypes.Datatypes) BooleanDatatype(com.haulmont.chile.core.datatypes.impl.BooleanDatatype) ConfigStorageService(com.haulmont.cuba.core.app.ConfigStorageService) AppBeans(com.haulmont.cuba.core.global.AppBeans) UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) Metadata(com.haulmont.cuba.core.global.Metadata) Inject(javax.inject.Inject) ConfigurationClientImpl(com.haulmont.cuba.client.sys.ConfigurationClientImpl) List(java.util.List) Datatype(com.haulmont.chile.core.datatypes.Datatype) AppPropertyEntity(com.haulmont.cuba.core.config.AppPropertyEntity) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) Configuration(com.haulmont.cuba.core.global.Configuration) WindowParam(com.haulmont.cuba.gui.WindowParam) Map(java.util.Map) Named(javax.inject.Named) ParseException(java.text.ParseException) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) BooleanDatatype(com.haulmont.chile.core.datatypes.impl.BooleanDatatype) ParseException(java.text.ParseException) BooleanDatatype(com.haulmont.chile.core.datatypes.impl.BooleanDatatype) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 19 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.

the class NumberFormatter method format.

@Override
public String format(Number value) {
    if (value == null) {
        return null;
    }
    String pattern = element != null ? element.attributeValue("format") : null;
    if (pattern == null) {
        Datatype datatype = Datatypes.getNN(value.getClass());
        return datatype.format(value, userSessionSource.getLocale());
    } else {
        if (pattern.startsWith("msg://")) {
            pattern = messages.getMainMessage(pattern.substring(6, pattern.length()));
        }
        FormatStrings formatStrings = Datatypes.getFormatStrings(userSessionSource.getLocale());
        if (formatStrings == null)
            throw new IllegalStateException("FormatStrings are not defined for " + userSessionSource.getLocale());
        DecimalFormat format = new DecimalFormat(pattern, formatStrings.getFormatSymbols());
        return format.format(value);
    }
}
Also used : FormatStrings(com.haulmont.chile.core.datatypes.FormatStrings) DecimalFormat(java.text.DecimalFormat) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 20 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.

the class DateValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    if (value == null)
        return;
    boolean result;
    if (value instanceof String) {
        try {
            Datatype datatype = Datatypes.getNN(java.sql.Date.class);
            UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
            datatype.parse((String) value, sessionSource.getLocale());
            result = true;
        } catch (ParseException e) {
            result = false;
        }
    } else {
        result = value instanceof Date;
    }
    if (!result) {
        String msg = message != null ? messages.getTools().loadString(messagesPack, message) : "Invalid value '%s'";
        throw new ValidationException(String.format(msg, value));
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) ValidationException(com.haulmont.cuba.gui.components.ValidationException) ParseException(java.text.ParseException) Date(java.util.Date) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Aggregations

Datatype (com.haulmont.chile.core.datatypes.Datatype)27 ParseException (java.text.ParseException)9 Entity (com.haulmont.cuba.core.entity.Entity)6 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)6 Datatypes (com.haulmont.chile.core.datatypes.Datatypes)3 AdaptiveNumberDatatype (com.haulmont.chile.core.datatypes.impl.AdaptiveNumberDatatype)3 MetaClass (com.haulmont.chile.core.model.MetaClass)3 Element (org.dom4j.Element)3 Logger (org.slf4j.Logger)3 JsonObject (com.google.gson.JsonObject)2 EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)2 AppBeans (com.haulmont.cuba.core.global.AppBeans)2 Metadata (com.haulmont.cuba.core.global.Metadata)2 ComponentsFactory (com.haulmont.cuba.gui.xml.layout.ComponentsFactory)2 RestAPIException (com.haulmont.restapi.exception.RestAPIException)2 Map (java.util.Map)2 Inject (javax.inject.Inject)2 Document (org.dom4j.Document)2