Search in sources :

Example 16 with WeakItemChangeListener

use of com.haulmont.cuba.gui.data.impl.WeakItemChangeListener in project cuba by cuba-platform.

the class WebAbstractOptionsField 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)) || (metaPropertyPath == null && property == null)))
        return;
    if (this.datasource != null) {
        metaProperty = null;
        metaPropertyPath = null;
        component.setPropertyDataSource(null);
        // noinspection unchecked
        this.datasource.removeItemChangeListener(securityWeakItemChangeListener);
        securityWeakItemChangeListener = null;
        this.datasource = null;
        if (itemWrapper != null) {
            itemWrapper.unsubscribe();
        }
        disableBeanValidator();
    }
    if (datasource != null) {
        // noinspection unchecked
        this.datasource = datasource;
        MetaClass metaClass = datasource.getMetaClass();
        resolveMetaPropertyPath(metaClass, property);
        if (metaProperty.getRange().getCardinality() != null) {
            setMultiSelect(metaProperty.getRange().getCardinality().isMany());
        }
        itemWrapper = createDatasourceWrapper(datasource, Collections.singleton(metaPropertyPath));
        Property itemProperty = itemWrapper.getItemProperty(metaPropertyPath);
        initRequired(metaPropertyPath);
        if (metaProperty.getRange().isEnum()) {
            Enumeration enumeration = metaProperty.getRange().asEnumeration();
            List options = Arrays.asList(enumeration.getJavaClass().getEnumConstants());
            setComponentContainerDs(createEnumContainer(options));
            setCaptionMode(CaptionMode.ITEM);
        }
        if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
            CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty);
            if (categoryAttribute != null && categoryAttribute.getDataType() == PropertyType.ENUMERATION) {
                setOptionsMap(categoryAttribute.getLocalizedEnumerationMap());
            }
        }
        component.setPropertyDataSource(itemProperty);
        if (metaProperty.isReadOnly()) {
            setEditable(false);
        }
        handleFilteredAttributes(this, this.datasource, metaPropertyPath);
        securityItemChangeListener = e -> handleFilteredAttributes(this, this.datasource, metaPropertyPath);
        securityWeakItemChangeListener = new WeakItemChangeListener(datasource, securityItemChangeListener);
        // noinspection unchecked
        this.datasource.addItemChangeListener(securityWeakItemChangeListener);
        initBeanValidator();
    }
}
Also used : WeakItemChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemChangeListener) Enumeration(com.haulmont.chile.core.datatypes.Enumeration) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) Property(com.vaadin.data.Property)

Aggregations

WeakItemChangeListener (com.haulmont.cuba.gui.data.impl.WeakItemChangeListener)16 WeakItemPropertyChangeListener (com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener)14 MetaClass (com.haulmont.chile.core.model.MetaClass)7 Property (com.vaadin.data.Property)3 Enumeration (com.haulmont.chile.core.datatypes.Enumeration)2 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)2 Date (java.util.Date)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)1 MessageTools (com.haulmont.cuba.core.global.MessageTools)1 TextInputField (com.haulmont.cuba.gui.components.TextInputField)1 ItemWrapper (com.haulmont.cuba.web.gui.data.ItemWrapper)1 java.awt (java.awt)1