Search in sources :

Example 1 with WeakItemPropertyChangeListener

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

the class WebDateField 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(weakItemChangeListener);
        weakItemChangeListener = null;
        // noinspection unchecked
        this.datasource.removeItemPropertyChangeListener(weakItemPropertyChangeListener);
        weakItemPropertyChangeListener = null;
        this.datasource = null;
        if (itemWrapper != null) {
            itemWrapper.unsubscribe();
        }
        timeZone = null;
        disableBeanValidator();
    }
    if (datasource != null) {
        // noinspection unchecked
        this.datasource = datasource;
        MetaClass metaClass = datasource.getMetaClass();
        resolveMetaPropertyPath(metaClass, 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();
                dateField.setTimeZone(timeZone);
            }
        }
        itemChangeListener = e -> {
            if (updatingInstance) {
                return;
            }
            Date value = getEntityValue(e.getItem());
            setValueToFields(value);
            fireValueChanged(value);
        };
        weakItemChangeListener = new WeakItemChangeListener(datasource, itemChangeListener);
        // noinspection unchecked
        datasource.addItemChangeListener(weakItemChangeListener);
        itemPropertyChangeListener = e -> {
            if (updatingInstance) {
                return;
            }
            if (!isBuffered() && e.getProperty().equals(metaPropertyPath.toString())) {
                setValueToFields((Date) e.getValue());
                fireValueChanged(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())) {
                Date value = getEntityValue(datasource.getItem());
                setValueToFields(value);
                fireValueChanged(value);
            }
        }
        initRequired(metaPropertyPath);
        initDateFormat(metaProperty);
        if (metaProperty.isReadOnly()) {
            setEditable(false);
        }
        initBeanValidator();
        setDateRangeByProperty(metaProperty);
    }
}
Also used : WeakItemChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemChangeListener) MetaClass(com.haulmont.chile.core.model.MetaClass) WeakItemPropertyChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener)

Example 2 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 3 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 4 with WeakItemPropertyChangeListener

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

the class DesktopAbstractTable method setDatasource.

@Override
public void setDatasource(final CollectionDatasource datasource) {
    Preconditions.checkNotNullArgument(datasource, "datasource is null");
    final Collection<Object> properties;
    if (this.columns.isEmpty()) {
        MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
        MessageTools messageTools = AppBeans.get(MessageTools.NAME);
        Collection<MetaPropertyPath> paths = datasource.getView() != null ? // if a view is specified - use view properties
        metadataTools.getViewPropertyPaths(datasource.getView(), datasource.getMetaClass()) : // otherwise use only string properties from meta-class - the temporary solution for KeyValue datasources
        metadataTools.getPropertyPaths(datasource.getMetaClass()).stream().filter(mpp -> mpp.getRangeJavaClass().equals(String.class)).collect(Collectors.toList());
        for (MetaPropertyPath metaPropertyPath : paths) {
            MetaProperty property = metaPropertyPath.getMetaProperty();
            if (!property.getRange().getCardinality().isMany() && !metadataTools.isSystem(property)) {
                Table.Column column = new Table.Column(metaPropertyPath);
                String propertyName = property.getName();
                MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
                column.setCaption(messageTools.getPropertyCaption(propertyMetaClass, propertyName));
                column.setType(metaPropertyPath.getRangeJavaClass());
                Element element = DocumentHelper.createElement("column");
                column.setXmlDescriptor(element);
                addColumn(column);
            }
        }
    }
    properties = this.columns.keySet();
    this.datasource = datasource;
    collectionChangeListener = e -> {
        switch(e.getOperation()) {
            case CLEAR:
            case REFRESH:
                fieldDatasources.clear();
                break;
            case UPDATE:
            case REMOVE:
                for (Object entity : e.getItems()) {
                    fieldDatasources.remove(entity);
                }
                break;
            case ADD:
                // no action
                break;
        }
    };
    // noinspection unchecked
    datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, collectionChangeListener));
    initTableModel(datasource);
    initChangeListener();
    setColumnIdentifiers();
    if (isSortable()) {
        impl.setRowSorter(new RowSorterImpl(tableModel));
    }
    initSelectionListener(datasource);
    List<MetaPropertyPath> editableColumns = null;
    if (isEditable()) {
        editableColumns = new LinkedList<>();
    }
    MetaClass metaClass = datasource.getMetaClass();
    for (final Object property : properties) {
        final Table.Column column = this.columns.get(property);
        if (column != null) {
            if (column.isCollapsed() && getColumnControlVisible()) {
                TableColumn tableColumn = getColumn(column);
                if (tableColumn instanceof TableColumnExt) {
                    ((TableColumnExt) tableColumn).setVisible(false);
                }
            }
            if (editableColumns != null && column.isEditable() && (property instanceof MetaPropertyPath)) {
                MetaPropertyPath propertyPath = (MetaPropertyPath) property;
                if (security.isEntityAttrUpdatePermitted(metaClass, property.toString())) {
                    editableColumns.add(propertyPath);
                }
            }
        }
    }
    if (editableColumns != null && !editableColumns.isEmpty()) {
        setEditableColumns(editableColumns);
    }
    List<Object> columnsOrder = new ArrayList<>();
    for (Table.Column column : this.columnsOrder) {
        if (column.getId() instanceof MetaPropertyPath) {
            MetaPropertyPath metaPropertyPath = (MetaPropertyPath) column.getId();
            if (security.isEntityAttrReadPermitted(metaClass, metaPropertyPath.toString())) {
                columnsOrder.add(column.getId());
            }
        } else {
            columnsOrder.add(column.getId());
        }
    }
    setVisibleColumns(columnsOrder);
    if (security.isSpecificPermitted(ShowInfoAction.ACTION_PERMISSION)) {
        ShowInfoAction action = (ShowInfoAction) getAction(ShowInfoAction.ACTION_ID);
        if (action == null) {
            action = new ShowInfoAction();
            addAction(action);
        }
        action.setDatasource(datasource);
    }
    securityCollectionChangeListener = e -> {
        onDataChange();
        packRows();
        // #PL-2035, reload selection from ds
        Set<E> selectedItems1 = getSelected();
        if (selectedItems1 == null) {
            selectedItems1 = Collections.emptySet();
        }
        Set<E> newSelection = new HashSet<>();
        for (E entity : selectedItems1) {
            if (e.getDs().containsItem(entity.getId())) {
                newSelection.add(entity);
            }
        }
        if (e.getDs().getState() == Datasource.State.VALID && e.getDs().getItem() != null) {
            if (e.getDs().containsItem(e.getDs().getItem().getId())) {
                newSelection.add((E) e.getDs().getItem());
            }
        }
        if (newSelection.isEmpty()) {
            setSelected((E) null);
        } else {
            setSelected(newSelection);
        }
    };
    // noinspection unchecked
    datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, securityCollectionChangeListener));
    itemPropertyChangeListener = e -> {
        List<Column> columns1 = getColumns();
        boolean find = false;
        int i = 0;
        while ((i < columns1.size()) & !find) {
            Object columnId = columns1.get(i).getId();
            if (columnId instanceof MetaPropertyPath) {
                String propertyName = ((MetaPropertyPath) columnId).getMetaProperty().getName();
                if (propertyName.equals(e.getProperty())) {
                    find = true;
                }
            }
            i++;
        }
        if (find) {
            onDataChange();
        }
        packRows();
    };
    // noinspection unchecked
    datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
    if (rowsCount != null) {
        rowsCount.setDatasource(datasource);
    }
    collectionDsActionsNotifier = new CollectionDsActionsNotifier(this);
    collectionDsActionsNotifier.bind(datasource);
    for (Action action : getActions()) {
        action.refreshState();
    }
    if (!canBeSorted(datasource))
        setSortable(false);
}
Also used : RowSorterImpl(com.haulmont.cuba.desktop.gui.data.RowSorterImpl) AbstractAction(javax.swing.AbstractAction) ShowInfoAction(com.haulmont.cuba.gui.components.sys.ShowInfoAction) BoundAction(org.jdesktop.swingx.action.BoundAction) Action(com.haulmont.cuba.gui.components.Action) Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) WeakCollectionChangeListener(com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener) TableColumn(javax.swing.table.TableColumn) WeakItemPropertyChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener) MetaProperty(com.haulmont.chile.core.model.MetaProperty) JXTable(org.jdesktop.swingx.JXTable) FocusableTable(com.haulmont.cuba.desktop.sys.vcl.FocusableTable) TableColumn(javax.swing.table.TableColumn) ShowInfoAction(com.haulmont.cuba.gui.components.sys.ShowInfoAction) MetaClass(com.haulmont.chile.core.model.MetaClass) TableColumnExt(org.jdesktop.swingx.table.TableColumnExt) CollectionDsActionsNotifier(com.haulmont.cuba.gui.data.impl.CollectionDsActionsNotifier)

Example 5 with WeakItemPropertyChangeListener

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

the class DesktopCheckBox method setDatasource.

@SuppressWarnings("unchecked")
@Override
public void setDatasource(Datasource datasource, String property) {
    this.datasource = datasource;
    if (datasource == null) {
        setValue(null);
        return;
    }
    resolveMetaPropertyPath(datasource.getMetaClass(), property);
    itemChangeListener = e -> {
        if (updatingInstance)
            return;
        Boolean value = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
        if (value == null) {
            value = false;
        }
        updateComponent(value);
        fireChangeListeners(value);
    };
    datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
    itemPropertyChangeListener = e -> {
        if (updatingInstance) {
            return;
        }
        if (e.getProperty().equals(metaPropertyPath.toString())) {
            Object value = e.getValue();
            if (e.getValue() == null) {
                value = false;
            }
            updateComponent(value);
            fireChangeListeners(value);
        }
    };
    datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
    if (datasource.getItemIfValid() != null) {
        Object newValue = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());
        if (!Objects.equals(prevValue, newValue)) {
            updateComponent(newValue);
            fireChangeListeners(newValue);
        }
    }
    if (metaProperty.isReadOnly()) {
        setEditable(false);
    }
    initBeanValidator();
}
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 ShowInfoAction (com.haulmont.cuba.gui.components.sys.ShowInfoAction)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