Search in sources :

Example 1 with StringToDatatypeConverter

use of com.haulmont.cuba.web.toolkit.ui.converters.StringToDatatypeConverter in project cuba by cuba-platform.

the class CubaCurrencyField method setDatatype.

public void setDatatype(Datatype datatype) {
    this.datatype = datatype;
    try {
        Object parsedValue = datatype.parse(textField.getValue(), getLocale());
        textField.setConverter(new StringToDatatypeConverter(datatype));
        textField.setConvertedValue(parsedValue);
    } catch (ParseException e) {
        String message = String.format("Value %s cannot be parsed as %s datatype", textField.getValue(), datatype);
        throw new RuntimeException(message, e);
    }
}
Also used : ParseException(java.text.ParseException) StringToDatatypeConverter(com.haulmont.cuba.web.toolkit.ui.converters.StringToDatatypeConverter)

Example 2 with StringToDatatypeConverter

use of com.haulmont.cuba.web.toolkit.ui.converters.StringToDatatypeConverter in project cuba by cuba-platform.

the class WebLabel method setDatasource.

@Override
public void setDatasource(Datasource datasource, String property) {
    if ((datasource == null && property != null) || (datasource != null && property == null))
        throw new IllegalArgumentException("Datasource and property should be either null or not null at the same time");
    if (datasource == this.datasource && metaPropertyPath != null && metaPropertyPath.toString().equals(property))
        return;
    if (this.datasource != null)
        unsubscribeDatasource();
    if (datasource != null) {
        // noinspection unchecked
        this.datasource = datasource;
        this.metaPropertyPath = resolveMetaPropertyPath(datasource.getMetaClass(), property);
        this.metaProperty = metaPropertyPath.getMetaProperty();
        switch(metaProperty.getType()) {
            case ASSOCIATION:
                component.setConverter(new StringToEntityConverter() {

                    @Override
                    public Formatter getFormatter() {
                        return WebLabel.this.formatter;
                    }
                });
                break;
            case DATATYPE:
                component.setConverter(new StringToDatatypeConverter(metaProperty.getRange().asDatatype()) {

                    @Override
                    public Formatter getFormatter() {
                        return WebLabel.this.formatter;
                    }
                });
                break;
            case ENUM:
                // noinspection unchecked
                component.setConverter(new StringToEnumConverter((Class<Enum>) metaProperty.getJavaType()) {

                    @Override
                    public Formatter getFormatter() {
                        return WebLabel.this.formatter;
                    }
                });
                break;
            default:
                component.setConverter(new StringToDatatypeConverter(Datatypes.getNN(String.class)) {

                    @Override
                    public Formatter getFormatter() {
                        return WebLabel.this.formatter;
                    }
                });
                break;
        }
        itemWrapper = createDatasourceWrapper(datasource, Collections.singleton(this.metaPropertyPath));
        component.setPropertyDataSource(itemWrapper.getItemProperty(this.metaPropertyPath));
    }
}
Also used : StringToEnumConverter(com.haulmont.cuba.web.toolkit.ui.converters.StringToEnumConverter) Formatter(com.haulmont.cuba.gui.components.Formatter) MetaClass(com.haulmont.chile.core.model.MetaClass) StringToEntityConverter(com.haulmont.cuba.web.toolkit.ui.converters.StringToEntityConverter) StringToDatatypeConverter(com.haulmont.cuba.web.toolkit.ui.converters.StringToDatatypeConverter)

Example 3 with StringToDatatypeConverter

use of com.haulmont.cuba.web.toolkit.ui.converters.StringToDatatypeConverter in project cuba by cuba-platform.

the class CubaCurrencyField method initTextField.

protected void initTextField() {
    textField.setWidth("100%");
    textField.addStyleName(CURRENCYFIELD_TEXT_STYLENAME);
    textField.setValidationVisible(false);
    textField.setShowBufferedSourceException(false);
    textField.setShowErrorForDisabledState(false);
    textField.setImmediate(true);
    textField.setNullRepresentation(StringUtils.EMPTY);
    textField.setConverter(new StringToDatatypeConverter(datatype));
    textField.addValueChangeListener(event -> markAsDirty());
}
Also used : StringToDatatypeConverter(com.haulmont.cuba.web.toolkit.ui.converters.StringToDatatypeConverter)

Aggregations

StringToDatatypeConverter (com.haulmont.cuba.web.toolkit.ui.converters.StringToDatatypeConverter)3 MetaClass (com.haulmont.chile.core.model.MetaClass)1 Formatter (com.haulmont.cuba.gui.components.Formatter)1 StringToEntityConverter (com.haulmont.cuba.web.toolkit.ui.converters.StringToEntityConverter)1 StringToEnumConverter (com.haulmont.cuba.web.toolkit.ui.converters.StringToEnumConverter)1 ParseException (java.text.ParseException)1