Search in sources :

Example 1 with ContainerValueSource

use of com.haulmont.cuba.gui.components.data.value.ContainerValueSource in project cuba by cuba-platform.

the class AbstractComponentGenerationStrategy method createEntityField.

@SuppressWarnings("unchecked")
protected Component createEntityField(ComponentGenerationContext context, MetaPropertyPath mpp) {
    String linkAttribute = null;
    Element xmlDescriptor = context.getXmlDescriptor();
    if (xmlDescriptor != null) {
        linkAttribute = xmlDescriptor.attributeValue("link");
    }
    if (!Boolean.parseBoolean(linkAttribute)) {
        Options options = context.getOptions();
        boolean useOptionsLoader = false;
        if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
            DynamicAttributesMetaProperty metaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
            CategoryAttribute attribute = metaProperty.getAttribute();
            CategoryAttributeConfiguration configuration = attribute.getConfiguration();
            if (Boolean.TRUE.equals(attribute.getLookup()) && configuration.hasOptionsLoader()) {
                useOptionsLoader = true;
            }
        }
        PickerField pickerField;
        if (options == null && !useOptionsLoader) {
            pickerField = uiComponents.create(PickerField.class);
            setValueSource(pickerField, context);
            if (mpp.getMetaProperty().getType() == MetaProperty.Type.ASSOCIATION) {
                guiActionSupport.createActionById(pickerField, PickerField.ActionType.LOOKUP.getId());
                if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
                    DynamicAttributesMetaProperty dynamicAttributesMetaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
                    getDynamicAttributesGuiTools().initEntityPickerField(pickerField, dynamicAttributesMetaProperty.getAttribute());
                }
                boolean actionsByMetaAnnotations = guiActionSupport.createActionsByMetaAnnotations(pickerField);
                if (!actionsByMetaAnnotations) {
                    guiActionSupport.createActionById(pickerField, PickerField.ActionType.CLEAR.getId());
                }
            } else {
                guiActionSupport.createActionById(pickerField, PickerField.ActionType.OPEN.getId());
                guiActionSupport.createActionById(pickerField, PickerField.ActionType.CLEAR.getId());
            }
        } else {
            LookupPickerField lookupPickerField = uiComponents.create(LookupPickerField.class);
            setValueSource(lookupPickerField, context);
            if (useOptionsLoader) {
                DynamicAttributesMetaProperty metaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
                CategoryAttribute attribute = metaProperty.getAttribute();
                ValueSource valueSource = context.getValueSource();
                if (valueSource instanceof ContainerValueSource) {
                    setOptionsLoader(attribute, lookupPickerField, (ContainerValueSource) valueSource);
                }
            } else {
                lookupPickerField.setOptions(options);
            }
            pickerField = lookupPickerField;
            guiActionSupport.createActionsByMetaAnnotations(pickerField);
        }
        if (xmlDescriptor != null) {
            String captionProperty = xmlDescriptor.attributeValue("captionProperty");
            if (StringUtils.isNotEmpty(captionProperty)) {
                pickerField.setCaptionMode(CaptionMode.PROPERTY);
                pickerField.setCaptionProperty(captionProperty);
            }
        }
        setValidators(pickerField, context);
        return pickerField;
    } else {
        EntityLinkField linkField = uiComponents.create(EntityLinkField.class);
        setValueSource(linkField, context);
        setLinkFieldAttributes(linkField, context);
        return linkField;
    }
}
Also used : ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) ListOptions(com.haulmont.cuba.gui.components.data.options.ListOptions) Options(com.haulmont.cuba.gui.components.data.Options) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) Element(org.dom4j.Element)

Example 2 with ContainerValueSource

use of com.haulmont.cuba.gui.components.data.value.ContainerValueSource in project cuba by cuba-platform.

the class AbstractComponentGenerationStrategy method setOptionsLoader.

protected void setOptionsLoader(CategoryAttribute categoryAttribute, LookupField lookupField, ContainerValueSource valueSource) {
    InstanceContainer<?> container = valueSource.getContainer();
    Entity entity = container.getItemOrNull();
    if (entity != null) {
        List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) entity, categoryAttribute);
        // noinspection unchecked
        lookupField.setOptions(new ListOptions(options));
    }
    container.addItemChangeListener(e -> {
        List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) e.getItem(), categoryAttribute);
        // noinspection unchecked
        lookupField.setOptions(new ListOptions(options));
    });
    List<CategoryAttribute> dependsOnAttributes = categoryAttribute.getConfiguration().getDependsOnAttributes();
    if (dependsOnAttributes != null && !dependsOnAttributes.isEmpty()) {
        List<String> dependsOnAttributesCodes = dependsOnAttributes.stream().map(a -> DynamicAttributesUtils.encodeAttributeCode(a.getCode())).collect(Collectors.toList());
        container.addItemPropertyChangeListener(e -> {
            if (dependsOnAttributesCodes.contains(e.getProperty())) {
                List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) e.getItem(), categoryAttribute);
                // noinspection unchecked
                lookupField.setOptions(new ListOptions(options));
                if (!options.contains(lookupField.getValue())) {
                    // noinspection unchecked
                    lookupField.setValue(null);
                }
            }
        });
    }
}
Also used : Resolution(com.haulmont.cuba.gui.components.DateField.Resolution) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools) Time(java.sql.Time) Date(java.util.Date) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) BooleanUtils(org.apache.commons.lang3.BooleanUtils) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) AppBeans(com.haulmont.cuba.core.global.AppBeans) StringUtils(org.apache.commons.lang3.StringUtils) MetaClass(com.haulmont.chile.core.model.MetaClass) java.time(java.time) Datatype(com.haulmont.chile.core.datatypes.Datatype) ListOptions(com.haulmont.cuba.gui.components.data.options.ListOptions) com.haulmont.cuba.core.entity(com.haulmont.cuba.core.entity) com.haulmont.cuba.core.app.dynamicattributes(com.haulmont.cuba.core.app.dynamicattributes) Method(java.lang.reflect.Method) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) Nullable(javax.annotation.Nullable) OpenType(com.haulmont.cuba.gui.WindowManager.OpenType) Range(com.haulmont.chile.core.model.Range) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Collection(java.util.Collection) Options(com.haulmont.cuba.gui.components.data.Options) FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) Messages(com.haulmont.cuba.core.global.Messages) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) CurrencyValue(com.haulmont.cuba.core.entity.annotation.CurrencyValue) GuiActionSupport(com.haulmont.cuba.gui.components.actions.GuiActionSupport) Consumer(java.util.function.Consumer) List(java.util.List) ComponentsHelper(com.haulmont.cuba.gui.ComponentsHelper) Element(org.dom4j.Element) UiComponents(com.haulmont.cuba.gui.UiComponents) ListOptions(com.haulmont.cuba.gui.components.data.options.ListOptions) List(java.util.List)

Example 3 with ContainerValueSource

use of com.haulmont.cuba.gui.components.data.value.ContainerValueSource in project cuba by cuba-platform.

the class WebTokenList method getViewForField.

/**
 * If the value for a component is selected from the lookup screen there may be cases when lookup screen contains a list of entities loaded with a
 * view that doesn't contain all attributes, required by the token list.
 * <p>
 * The method evaluates the view that was is for loading entities in the token list
 *
 * @return a view or null if the view cannot be evaluated
 */
@Nullable
protected View getViewForField() {
    ValueSource valueSource = getValueSource();
    if (valueSource instanceof ContainerValueSource) {
        ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
        InstanceContainer container = containerValueSource.getContainer();
        View view = container.getView();
        if (view != null) {
            MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
            View curView = view;
            if (metaPropertyPath != null) {
                for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
                    ViewProperty viewProperty = curView.getProperty(metaProperty.getName());
                    if (viewProperty != null) {
                        curView = viewProperty.getView();
                        if (curView == null)
                            return null;
                    }
                }
                MetaProperty inverseMetaProperty = metaPropertyPath.getMetaProperty().getInverse();
                if (inverseMetaProperty != null && !inverseMetaProperty.getRange().getCardinality().isMany()) {
                    curView.addProperty(inverseMetaProperty.getName());
                }
            }
            if (curView != view) {
                return curView;
            }
        }
    }
    return null;
}
Also used : ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) LegacyCollectionDsValueSource(com.haulmont.cuba.gui.components.data.value.LegacyCollectionDsValueSource) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 4 with ContainerValueSource

use of com.haulmont.cuba.gui.components.data.value.ContainerValueSource in project cuba by cuba-platform.

the class DynamicAttributesGuiTools method getValueChangeEventListener.

/**
 * Returns {@code ValueChangeEventListener} for dynamic attribute that has one or more dependent attributes.
 * This listener recalculates values for all dependent dynamic attributes hierarchically. The listener uses
 * {@code recalculationInProgress} ThreadLocal variable to avoid unnecessary calculation.
 */
@SuppressWarnings("unchecked")
public Consumer<HasValue.ValueChangeEvent> getValueChangeEventListener(final CategoryAttribute attribute) {
    if (attribute.getConfiguration().getDependentAttributes() != null && !attribute.getConfiguration().getDependentAttributes().isEmpty()) {
        return valueChangeEvent -> {
            if (Boolean.TRUE.equals(recalculationInProgress.get())) {
                return;
            }
            try {
                recalculationInProgress.set(true);
                com.haulmont.cuba.gui.components.Component component = valueChangeEvent.getComponent();
                if (component instanceof HasValueSource) {
                    {
                        BaseGenericIdEntity entity = null;
                        String attributeCode = DynamicAttributesUtils.encodeAttributeCode(attribute.getCode());
                        if (((HasValueSource) component).getValueSource() instanceof ContainerValueSource) {
                            ContainerValueSource valueSource = (ContainerValueSource) ((HasValueSource) component).getValueSource();
                            InstanceContainer container = valueSource.getContainer();
                            if (container.getItem() instanceof BaseGenericIdEntity) {
                                entity = (BaseGenericIdEntity) container.getItem();
                            }
                        } else if (((HasValueSource) component).getValueSource() instanceof DatasourceValueSource) {
                            DatasourceValueSource valueSource = (DatasourceValueSource) ((HasValueSource) component).getValueSource();
                            if (valueSource.getItem() instanceof BaseGenericIdEntity) {
                                entity = (BaseGenericIdEntity) valueSource.getItem();
                            } else if (valueSource.getItem() instanceof DynamicAttributesEntity) {
                                entity = ((DynamicAttributesEntity) valueSource.getItem()).getMainItem();
                            }
                        }
                        entity.setValue(attributeCode, valueChangeEvent.getValue());
                        recalculationTools.recalculateDynamicAttributes(entity, attribute);
                    }
                }
            } finally {
                recalculationInProgress.remove();
            }
        };
    }
    return null;
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) com.haulmont.cuba.gui.components.validation(com.haulmont.cuba.gui.components.validation) LookupAction(com.haulmont.cuba.gui.actions.picker.LookupAction) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) DynamicAttributesEntity(com.haulmont.cuba.gui.data.DynamicAttributesEntity) DecimalFormatSymbols(java.text.DecimalFormatSymbols) ZonedDateTime(java.time.ZonedDateTime) DatatypeRegistry(com.haulmont.chile.core.datatypes.DatatypeRegistry) AdaptiveNumberDatatype(com.haulmont.chile.core.datatypes.impl.AdaptiveNumberDatatype) StringUtils(org.apache.commons.lang3.StringUtils) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation) BigDecimal(java.math.BigDecimal) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) PickerField(com.haulmont.cuba.gui.components.PickerField) OpenMode(com.haulmont.cuba.gui.screen.OpenMode) DynamicAttributes(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributes) OpenType(com.haulmont.cuba.gui.WindowManager.OpenType) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) GuiActionSupport(com.haulmont.cuba.gui.components.actions.GuiActionSupport) WindowParams(com.haulmont.cuba.gui.WindowParams) EntityOp(com.haulmont.cuba.security.entity.EntityOp) java.util(java.util) DatasourceValueSource(com.haulmont.cuba.gui.components.data.value.DatasourceValueSource) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) ParamsMap(com.haulmont.bali.util.ParamsMap) DynamicAttributesUtils(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils) BooleanUtils(org.apache.commons.lang3.BooleanUtils) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) Datatype(com.haulmont.chile.core.datatypes.Datatype) HasValue(com.haulmont.cuba.gui.components.HasValue) com.haulmont.cuba.core.entity(com.haulmont.cuba.core.entity) MapScreenOptions(com.haulmont.cuba.gui.screen.MapScreenOptions) Nullable(javax.annotation.Nullable) DynamicAttributesRecalculationTools(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesRecalculationTools) CommonLookupController(com.haulmont.cuba.gui.commonlookup.CommonLookupController) DecimalFormat(java.text.DecimalFormat) ScreensHelper(com.haulmont.cuba.gui.sys.ScreensHelper) Preconditions(com.haulmont.bali.util.Preconditions) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) WindowConfig(com.haulmont.cuba.gui.config.WindowConfig) PropertyType(com.haulmont.cuba.core.app.dynamicattributes.PropertyType) Action(com.haulmont.cuba.gui.components.Action) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) DatasourceValueSource(com.haulmont.cuba.gui.components.data.value.DatasourceValueSource) DynamicAttributesEntity(com.haulmont.cuba.gui.data.DynamicAttributesEntity) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) Component(org.springframework.stereotype.Component) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource)

Example 5 with ContainerValueSource

use of com.haulmont.cuba.gui.components.data.value.ContainerValueSource in project cuba by cuba-platform.

the class WebTableFieldFactory method createField.

@SuppressWarnings("unchecked")
@Override
public com.vaadin.v7.ui.Field<?> createField(com.vaadin.v7.data.Container container, Object itemId, Object propertyId, Component uiContext) {
    String fieldPropertyId = String.valueOf(propertyId);
    Table.Column columnConf = webTable.getColumnsInternal().get(propertyId);
    TableDataContainer tableDataContainer = (TableDataContainer) container;
    Entity entity = (Entity) tableDataContainer.getInternalItem(itemId);
    InstanceContainer instanceContainer = webTable.getInstanceContainer((E) entity);
    com.haulmont.cuba.gui.components.Component columnComponent = createField(new ContainerValueSource(instanceContainer, fieldPropertyId), fieldPropertyId, columnConf.getXmlDescriptor());
    if (columnComponent instanceof Field) {
        Field cubaField = (Field) columnComponent;
        Map<Table.Column, String> requiredColumns = webTable.getRequiredColumnsInternal();
        if (requiredColumns != null && requiredColumns.containsKey(columnConf)) {
            cubaField.setRequired(true);
            cubaField.setRequiredMessage(requiredColumns.get(columnConf));
        }
    }
    if (!(columnComponent instanceof CheckBox)) {
        // todo get rid of concrete CheckBox class !
        columnComponent.setWidthFull();
    }
    if (columnComponent instanceof BelongToFrame) {
        BelongToFrame belongToFrame = (BelongToFrame) columnComponent;
        if (belongToFrame.getFrame() == null) {
            belongToFrame.setFrame(webTable.getFrame());
        }
    }
    applyPermissions(columnComponent);
    columnComponent.setParent(webTable);
    Component componentImpl = getComponentImplementation(columnComponent);
    if (componentImpl instanceof com.vaadin.v7.ui.Field) {
        return (com.vaadin.v7.ui.Field<?>) componentImpl;
    }
    return new EditableColumnFieldWrapper(componentImpl, columnComponent);
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Table(com.haulmont.cuba.gui.components.Table) WebAbstractTable(com.haulmont.cuba.web.gui.components.WebAbstractTable) BelongToFrame(com.haulmont.cuba.gui.components.Component.BelongToFrame) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) Field(com.haulmont.cuba.gui.components.Field) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Component(com.vaadin.ui.Component)

Aggregations

ContainerValueSource (com.haulmont.cuba.gui.components.data.value.ContainerValueSource)9 ValueSource (com.haulmont.cuba.gui.components.data.ValueSource)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)4 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)4 InstanceContainer (com.haulmont.cuba.gui.model.InstanceContainer)4 Nullable (javax.annotation.Nullable)4 Datatype (com.haulmont.chile.core.datatypes.Datatype)2 MetaClass (com.haulmont.chile.core.model.MetaClass)2 com.haulmont.cuba.core.entity (com.haulmont.cuba.core.entity)2 Entity (com.haulmont.cuba.core.entity.Entity)2 OpenType (com.haulmont.cuba.gui.WindowManager.OpenType)2 GuiActionSupport (com.haulmont.cuba.gui.components.actions.GuiActionSupport)2 Options (com.haulmont.cuba.gui.components.data.Options)2 ListOptions (com.haulmont.cuba.gui.components.data.options.ListOptions)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2 BooleanUtils (org.apache.commons.lang3.BooleanUtils)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Element (org.dom4j.Element)2 Strings (com.google.common.base.Strings)1