Search in sources :

Example 1 with ContainerValueSourceProvider

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

the class FormLoader method loadDataContainer.

protected void loadDataContainer(Form resultComponent, Element element) {
    String containerId = element.attributeValue("dataContainer");
    if (!Strings.isNullOrEmpty(containerId)) {
        FrameOwner frameOwner = getComponentContext().getFrame().getFrameOwner();
        ScreenData screenData = UiControllerUtils.getScreenData(frameOwner);
        InstanceContainer container = screenData.getContainer(containerId);
        // noinspection unchecked
        resultComponent.setValueSourceProvider(new ContainerValueSourceProvider(container));
    }
}
Also used : ContainerValueSourceProvider(com.haulmont.cuba.gui.components.data.value.ContainerValueSourceProvider) FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner)

Example 2 with ContainerValueSourceProvider

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

the class FormLoader method loadDynamicAttributeComponents.

protected List<ComponentPosition> loadDynamicAttributeComponents(ValueSourceProvider provider, @Nullable String columnWidth) {
    if (provider instanceof ContainerValueSourceProvider && getMetadataTools().isPersistent(((ContainerValueSourceProvider) provider).getContainer().getEntityMetaClass())) {
        String windowId = getWindowId(context);
        InstanceContainer instanceContainer = ((ContainerValueSourceProvider) provider).getContainer();
        MetaClass metaClass = instanceContainer.getEntityMetaClass();
        List<CategoryAttribute> attributesToShow = getDynamicAttributesGuiTools().getSortedAttributesToShowOnTheScreen(metaClass, windowId, resultComponent.getId());
        if (!attributesToShow.isEmpty()) {
            List<ComponentPosition> components = new ArrayList<>();
            if (instanceContainer instanceof HasLoader) {
                DataLoader dataLoader = ((HasLoader) instanceContainer).getLoader();
                if (dataLoader instanceof InstanceLoader) {
                    ((InstanceLoader) dataLoader).setLoadDynamicAttributes(true);
                }
            }
            for (CategoryAttribute attribute : attributesToShow) {
                String code = DynamicAttributesUtils.encodeAttributeCode(attribute.getCode());
                ValueSource<?> valueSource = provider.getValueSource(code);
                Component dynamicAttrComponent;
                if (Boolean.TRUE.equals(attribute.getIsCollection())) {
                    dynamicAttrComponent = getDynamicAttributesComponentsGenerator().generateComponent(provider.getValueSource(code), attribute);
                } else {
                    ComponentGenerationContext context = new ComponentGenerationContext(metaClass, code);
                    context.setValueSource(valueSource);
                    dynamicAttrComponent = getUiComponentsGenerator().generate(context);
                }
                if (dynamicAttrComponent instanceof Component.HasCaption) {
                    ((Component.HasCaption) dynamicAttrComponent).setCaption(attribute.getLocaleName());
                    ((Component.HasCaption) dynamicAttrComponent).setDescription(attribute.getLocaleDescription());
                }
                if (dynamicAttrComponent instanceof HasValueSource) {
                    // noinspection unchecked
                    ((HasValueSource) dynamicAttrComponent).setValueSource(valueSource);
                }
                if (dynamicAttrComponent instanceof Component.Editable && Boolean.TRUE.equals(attribute.getConfiguration().isReadOnly())) {
                    ((Component.Editable) dynamicAttrComponent).setEditable(false);
                }
                if (dynamicAttrComponent instanceof Field) {
                    ((Field) dynamicAttrComponent).setRequired(attribute.getRequired());
                    ((Field) dynamicAttrComponent).setRequiredMessage(getMessages().formatMainMessage("validation.required.defaultMsg", attribute.getLocaleName()));
                }
                String defaultWidth = Strings.isNullOrEmpty(attribute.getWidth()) ? columnWidth : attribute.getWidth();
                loadWidth(dynamicAttrComponent, defaultWidth);
                components.add(new ComponentPosition(dynamicAttrComponent, 1, 1));
            }
            return components;
        }
    }
    return Collections.emptyList();
}
Also used : ComponentPosition(com.haulmont.cuba.gui.components.form.ComponentPosition) ContainerValueSourceProvider(com.haulmont.cuba.gui.components.data.value.ContainerValueSourceProvider) ArrayList(java.util.ArrayList) DynamicAttributesUtils.getCategoryAttribute(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils.getCategoryAttribute) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource)

Aggregations

ContainerValueSourceProvider (com.haulmont.cuba.gui.components.data.value.ContainerValueSourceProvider)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 DynamicAttributesUtils.getCategoryAttribute (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils.getCategoryAttribute)1 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)1 HasValueSource (com.haulmont.cuba.gui.components.data.HasValueSource)1 ComponentPosition (com.haulmont.cuba.gui.components.form.ComponentPosition)1 FrameOwner (com.haulmont.cuba.gui.screen.FrameOwner)1 ArrayList (java.util.ArrayList)1