Search in sources :

Example 6 with WeakItemPropertyChangeListener

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

the class WebEntityLinkField method setDatasource.

@Override
public void setDatasource(Datasource datasource, String property) {
    Preconditions.checkNotNullArgument(datasource, "datasource is null");
    if (this.datasource != null) {
        throw new UnsupportedOperationException("Changing datasource is not supported by the EntityLinkField component");
    }
    // noinspection unchecked
    this.datasource = datasource;
    MetaClass metaClass = datasource.getMetaClass();
    resolveMetaPropertyPath(metaClass, property);
    if (metaProperty.getRange().isClass()) {
        this.metaClass = metaProperty.getRange().asClass();
    }
    ItemWrapper wrapper = createDatasourceWrapper(datasource, Collections.singleton(metaPropertyPath));
    Property itemProperty = wrapper.getItemProperty(metaPropertyPath);
    component.setPropertyDataSource(itemProperty);
    itemChangeListener = e -> {
        Object newValue = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
        setValue(newValue);
    };
    // noinspection unchecked
    datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
    itemPropertyChangeListener = e -> {
        if (e.getProperty().equals(metaPropertyPath.toString())) {
            setValue(e.getValue());
        }
    };
    // noinspection unchecked
    datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
    if (datasource.getState() == Datasource.State.VALID && datasource.getItem() != null) {
        if (property.equals(metaPropertyPath.toString())) {
            Object newValue = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());
            setValue(newValue);
        }
    }
    setRequired(metaProperty.isMandatory());
    if (StringUtils.isEmpty(getRequiredMessage())) {
        MessageTools messageTools = AppBeans.get(MessageTools.NAME);
        setRequiredMessage(messageTools.getDefaultRequiredMessage(metaClass, property));
    }
}
Also used : ItemWrapper(com.haulmont.cuba.web.gui.data.ItemWrapper) WeakItemChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemChangeListener) MessageTools(com.haulmont.cuba.core.global.MessageTools) MetaClass(com.haulmont.chile.core.model.MetaClass) WeakItemPropertyChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener) Property(com.vaadin.data.Property)

Example 7 with WeakItemPropertyChangeListener

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

the class WebPickerField method setDatasource.

@SuppressWarnings("unchecked")
@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);
        this.datasource.removeItemChangeListener(securityWeakItemChangeListener);
        securityWeakItemChangeListener = null;
        this.datasource.removeItemChangeListener(weakItemChangeListener);
        weakItemChangeListener = null;
        this.datasource.removeItemPropertyChangeListener(weakItemPropertyChangeListener);
        weakItemPropertyChangeListener = null;
        this.datasource = null;
        if (itemWrapper != null) {
            itemWrapper.unsubscribe();
        }
        disableBeanValidator();
    }
    if (datasource != null) {
        checkDatasourceProperty(datasource, property);
        // noinspection unchecked
        this.datasource = datasource;
        metaPropertyPath = getResolvedMetaPropertyPath(datasource.getMetaClass(), property);
        metaProperty = metaPropertyPath.getMetaProperty();
        itemWrapper = createDatasourceWrapper(datasource, Collections.singleton(metaPropertyPath));
        Property itemProperty = itemWrapper.getItemProperty(metaPropertyPath);
        component.setPropertyDataSource(itemProperty);
        itemChangeListener = e -> {
            Object newValue = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
            setValue(newValue);
        };
        weakItemChangeListener = new WeakItemChangeListener(datasource, itemChangeListener);
        // noinspection unchecked
        datasource.addItemChangeListener(weakItemChangeListener);
        itemPropertyChangeListener = e -> {
            if (!isBuffered() && e.getProperty().equals(metaPropertyPath.toString())) {
                setValue(e.getValue());
            }
        };
        weakItemPropertyChangeListener = new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener);
        // noinspection unchecked
        datasource.addItemPropertyChangeListener(weakItemPropertyChangeListener);
        if (datasource.getState() == Datasource.State.VALID && datasource.getItem() != null) {
            if (property.equals(metaPropertyPath.toString())) {
                Object newValue = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());
                setValue(newValue);
            }
        }
        initRequired(metaPropertyPath);
        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) WeakItemPropertyChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener) Property(com.vaadin.data.Property) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 8 with WeakItemPropertyChangeListener

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

the class DesktopAbstractOptionsField method setDatasource.

@Override
public void setDatasource(Datasource datasource, String property) {
    this.datasource = datasource;
    if (datasource == null) {
        setValue(null);
        return;
    }
    MetaClass metaClass = datasource.getMetaClass();
    resolveMetaPropertyPath(metaClass, property);
    itemChangeListener = e -> {
        if (updatingInstance)
            return;
        Object value = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
        updateComponent(value);
        fireChangeListeners(value);
    };
    // noinspection unchecked
    datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
    itemPropertyChangeListener = e -> {
        if (updatingInstance)
            return;
        if (e.getProperty().equals(metaPropertyPath.toString())) {
            updateComponent(e.getValue());
            fireChangeListeners(e.getValue());
        }
    };
    // noinspection unchecked
    datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
    initRequired(metaPropertyPath);
    if (metaProperty.getRange().isEnum()) {
        Enumeration enumeration = metaProperty.getRange().asEnumeration();
        @SuppressWarnings("unchecked") Class<Enum> javaClass = enumeration.getJavaClass();
        setOptionsList(Arrays.asList(javaClass.getEnumConstants()));
        setCaptionMode(CaptionMode.ITEM);
    }
    if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
        CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty);
        if (categoryAttribute != null && categoryAttribute.getDataType() == PropertyType.ENUMERATION) {
            setOptionsMap(categoryAttribute.getLocalizedEnumerationMap());
        }
    }
    if ((datasource.getState() == Datasource.State.VALID) && (datasource.getItem() != null)) {
        Object newValue = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());
        updateComponent(newValue);
        fireChangeListeners(newValue);
    }
    if (metaProperty.isReadOnly()) {
        setEditable(false);
    }
    handleFilteredAttributes(this, this.datasource, metaPropertyPath);
    securityItemChangeListener = e -> handleFilteredAttributes(this, this.datasource, metaPropertyPath);
    // noinspection unchecked
    this.datasource.addItemChangeListener(new WeakItemChangeListener(this.datasource, securityItemChangeListener));
    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) WeakItemPropertyChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener)

Example 9 with WeakItemPropertyChangeListener

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

the class DesktopAbstractTextField method setDatasource.

@Override
public void setDatasource(Datasource datasource, String property) {
    this.datasource = datasource;
    if (datasource == null) {
        setValue(null);
        return;
    }
    resolveMetaPropertyPath(datasource.getMetaClass(), property);
    valueFormatter.setMetaProperty(metaProperty);
    itemChangeListener = e -> {
        if (updatingInstance) {
            return;
        }
        Object value = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
        updateComponent(value);
        fireChangeListeners(value);
    };
    // noinspection unchecked
    datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
    itemPropertyChangeListener = e -> {
        if (updatingInstance) {
            return;
        }
        if (e.getProperty().equals(metaPropertyPath.toString())) {
            updateComponent(e.getValue());
            fireChangeListeners(e.getValue());
        }
    };
    // noinspection unchecked
    datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
    initRequired(metaPropertyPath);
    if ((datasource.getState() == Datasource.State.VALID) && (datasource.getItem() != null)) {
        Object value = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());
        updateComponent(value);
        fireChangeListeners();
    }
    Integer maxLength = (Integer) metaProperty.getAnnotations().get("length");
    if (maxLength != null && this instanceof TextInputField.MaxLengthLimited) {
        ((TextInputField.MaxLengthLimited) this).setMaxLength(maxLength);
    }
    if (metaProperty.isReadOnly()) {
        setEditable(false);
    }
    initBeanValidator();
}
Also used : WeakItemChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemChangeListener) WeakItemPropertyChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener) TextInputField(com.haulmont.cuba.gui.components.TextInputField)

Example 10 with WeakItemPropertyChangeListener

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

the class DesktopDateField method setDatasource.

@Override
public void setDatasource(Datasource datasource, String property) {
    this.datasource = datasource;
    if (datasource == null) {
        setValue(null);
        return;
    }
    resolveMetaPropertyPath(datasource.getMetaClass(), property);
    if (metaProperty.getRange().isDatatype() && metaProperty.getRange().asDatatype().getJavaClass().equals(Date.class) && timeZone == null) {
        MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
        Boolean ignoreUserTimeZone = metadataTools.getMetaAnnotationValue(metaProperty, IgnoreUserTimeZone.class);
        if (!Boolean.TRUE.equals(ignoreUserTimeZone)) {
            timeZone = userSession.getTimeZone();
        }
    }
    itemChangeListener = e -> {
        if (updatingInstance) {
            return;
        }
        Date value = getEntityValue(e.getItem());
        updateComponent(value);
        fireChangeListeners(value);
    };
    // noinspection unchecked
    datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
    itemPropertyChangeListener = e -> {
        if (updatingInstance) {
            return;
        }
        if (e.getProperty().equals(metaPropertyPath.toString())) {
            updateComponent((Date) e.getValue());
            fireChangeListeners(e.getValue());
        }
    };
    // noinspection unchecked
    datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
    if (datasource.getState() == Datasource.State.VALID && datasource.getItem() != null) {
        if (property.equals(metaPropertyPath.toString())) {
            Date value = getEntityValue(datasource.getItem());
            updateComponent(value);
            fireChangeListeners(value);
        }
    }
    initRequired(metaPropertyPath);
    initDateFormat(metaProperty);
    if (metaProperty.isReadOnly()) {
        setEditable(false);
    }
    initBeanValidator();
    setDateRangeByProperty(metaProperty);
}
Also used : WeakItemChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemChangeListener) WeakItemPropertyChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener)

Aggregations

WeakItemPropertyChangeListener (com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener)15 WeakItemChangeListener (com.haulmont.cuba.gui.data.impl.WeakItemChangeListener)14 MetaClass (com.haulmont.chile.core.model.MetaClass)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 Property (com.vaadin.data.Property)2 Date (java.util.Date)2 Enumeration (com.haulmont.chile.core.datatypes.Enumeration)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)1 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)1 MessageTools (com.haulmont.cuba.core.global.MessageTools)1 RowSorterImpl (com.haulmont.cuba.desktop.gui.data.RowSorterImpl)1 FocusableTable (com.haulmont.cuba.desktop.sys.vcl.FocusableTable)1 Action (com.haulmont.cuba.gui.components.Action)1 TextInputField (com.haulmont.cuba.gui.components.TextInputField)1 CollectionDsActionsNotifier (com.haulmont.cuba.gui.data.impl.CollectionDsActionsNotifier)1 WeakCollectionChangeListener (com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener)1 ItemWrapper (com.haulmont.cuba.web.gui.data.ItemWrapper)1 java.awt (java.awt)1 AbstractAction (javax.swing.AbstractAction)1