Search in sources :

Example 1 with StringToEntityConverter

use of com.haulmont.cuba.web.toolkit.ui.converters.StringToEntityConverter 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)

Aggregations

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