Search in sources :

Example 1 with ComponentPosition

use of com.haulmont.cuba.gui.components.form.ComponentPosition in project cuba by cuba-platform.

the class WebForm method calculateComponentAreas.

protected List<ComponentArea> calculateComponentAreas() {
    List<ComponentArea> componentAreas = new ArrayList<>();
    // Inspired by GridLayoutLoader logic
    boolean[][] spanMatrix = new boolean[this.component.getColumns()][this.component.getRows()];
    for (int col = 0; col < columnComponentMapping.size(); col++) {
        int row = 0;
        List<ComponentPosition> columnFields = columnComponentMapping.get(col);
        for (ComponentPosition componentPosition : columnFields) {
            while (spanMatrix[col][row]) {
                row++;
            }
            Component component = componentPosition.getComponent();
            int colSpan = componentPosition.getColSpan();
            int rowSpan = componentPosition.getRowSpan();
            if (colSpan == 1 && rowSpan == 1) {
                componentAreas.add(new ComponentArea(component, col, row, col, row));
            } else {
                fillSpanMatrix(spanMatrix, col, row, colSpan, rowSpan);
                int endColumn = col + colSpan - 1;
                int endRow = row + rowSpan - 1;
                componentAreas.add(new ComponentArea(component, col, row, endColumn, endRow));
            }
            row++;
        }
    }
    return componentAreas;
}
Also used : ComponentPosition(com.haulmont.cuba.gui.components.form.ComponentPosition) ComponentArea(com.haulmont.cuba.gui.components.form.ComponentArea)

Example 2 with ComponentPosition

use of com.haulmont.cuba.gui.components.form.ComponentPosition in project cuba by cuba-platform.

the class FormLoader method loadComponent.

protected ComponentPosition loadComponent(Element element, @Nullable String columnWidth, @Nullable Float flex) {
    Component component;
    if ("field".equals(element.getName())) {
        component = loadField(element);
    } else {
        LayoutLoader loader = getLayoutLoader();
        ComponentLoader childComponentLoader = loader.createComponent(element);
        childComponentLoader.loadComponent();
        component = childComponentLoader.getResultComponent();
    }
    // Set default width
    String componentWidth = element.attributeValue("width");
    if (Strings.isNullOrEmpty(componentWidth)) {
        if (columnWidth != null) {
            component.setWidth(columnWidth);
        } else if (flex != null) {
            component.setWidthFull();
        }
    }
    // Set default caption
    if (component instanceof HasValueSource && ((HasValueSource) component).getValueSource() instanceof EntityValueSource && component instanceof Component.HasCaption && ((Component.HasCaption) component).getCaption() == null) {
        EntityValueSource valueSource = ((EntityValueSource) ((HasValueSource) component).getValueSource());
        MetaPropertyPath metaPropertyPath = valueSource.getMetaPropertyPath();
        String propertyName = metaPropertyPath != null ? metaPropertyPath.getMetaProperty().getName() : null;
        if (metaPropertyPath != null) {
            if (isDynamicAttribute(metaPropertyPath.getMetaProperty())) {
                CategoryAttribute categoryAttribute = getCategoryAttribute(metaPropertyPath.getMetaProperty());
                ((Component.HasCaption) component).setCaption(categoryAttribute != null ? categoryAttribute.getLocaleName() : propertyName);
                ((Component.HasCaption) component).setDescription(categoryAttribute != null ? categoryAttribute.getLocaleDescription() : null);
            } else {
                MetaClass propertyMetaClass = getMetadataTools().getPropertyEnclosingMetaClass(metaPropertyPath);
                String propertyCaption = getMessageTools().getPropertyCaption(propertyMetaClass, propertyName);
                ((Component.HasCaption) component).setCaption(propertyCaption);
            }
        }
    }
    int colspan = loadSpan(element, "colspan");
    int rowspan = loadSpan(element, "rowspan");
    return new ComponentPosition(component, colspan, rowspan);
}
Also used : LayoutLoader(com.haulmont.cuba.gui.xml.layout.LayoutLoader) ComponentPosition(com.haulmont.cuba.gui.components.form.ComponentPosition) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) DynamicAttributesUtils.getCategoryAttribute(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils.getCategoryAttribute) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader)

Example 3 with ComponentPosition

use of com.haulmont.cuba.gui.components.form.ComponentPosition in project cuba by cuba-platform.

the class FormLoader method loadColumns.

protected void loadColumns(Form resultComponent, Element element) {
    ValueSourceProvider valueSourceProvider = resultComponent.getValueSourceProvider();
    if (element.elements("column").isEmpty()) {
        Iterable<ComponentPosition> rootComponents = loadComponents(element, null, null);
        Iterable<ComponentPosition> dynamicAttributeComponents = loadDynamicAttributeComponents(valueSourceProvider, null);
        for (ComponentPosition component : Iterables.concat(rootComponents, dynamicAttributeComponents)) {
            resultComponent.add(component.getComponent(), 0, component.getColSpan(), component.getRowSpan());
        }
    } else {
        List<Element> columnElements = element.elements("column");
        if (element.elements().size() > columnElements.size()) {
            String fieldGroupId = resultComponent.getId();
            Map<String, Object> params = Strings.isNullOrEmpty(fieldGroupId) ? Collections.emptyMap() : ParamsMap.of("Form ID", fieldGroupId);
            throw new GuiDevelopmentException("Form component elements have to be placed within its column.", context, params);
        }
        resultComponent.setColumns(columnElements.size());
        int colIndex = 0;
        for (Element columnElement : columnElements) {
            String columnWidth = loadThemeString(columnElement.attributeValue("width"));
            String flexAttr = columnElement.attributeValue("flex");
            Float flex = null;
            if (!Strings.isNullOrEmpty(flexAttr)) {
                flex = Float.parseFloat(flexAttr);
                resultComponent.setColumnFlex(colIndex, flex);
            }
            Iterable<ComponentPosition> columnComponents = loadComponents(columnElement, columnWidth, flex);
            if (colIndex == 0) {
                columnComponents = Iterables.concat(columnComponents, loadDynamicAttributeComponents(valueSourceProvider, columnWidth));
            }
            for (ComponentPosition component : columnComponents) {
                resultComponent.add(component.getComponent(), colIndex, component.getColSpan(), component.getRowSpan());
            }
            loadChildrenCaptionAlignment(resultComponent, columnElement, colIndex);
            loadChildrenCaptionWidth(resultComponent, columnElement, colIndex);
            colIndex++;
        }
    }
}
Also used : ValueSourceProvider(com.haulmont.cuba.gui.components.data.ValueSourceProvider) ContainerValueSourceProvider(com.haulmont.cuba.gui.components.data.value.ContainerValueSourceProvider) ComponentPosition(com.haulmont.cuba.gui.components.form.ComponentPosition) Element(org.dom4j.Element) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Example 4 with ComponentPosition

use of com.haulmont.cuba.gui.components.form.ComponentPosition 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)

Example 5 with ComponentPosition

use of com.haulmont.cuba.gui.components.form.ComponentPosition in project cuba by cuba-platform.

the class FormLoader method loadComponents.

protected List<ComponentPosition> loadComponents(Element element, @Nullable String columnWidth, @Nullable Float flex) {
    List<Element> elements = element.elements();
    if (elements.isEmpty()) {
        return Collections.emptyList();
    } else {
        List<ComponentPosition> components = new ArrayList<>(elements.size());
        for (Element componentElement : elements) {
            ComponentPosition component = loadComponent(componentElement, columnWidth, flex);
            components.add(component);
        }
        return components;
    }
}
Also used : ComponentPosition(com.haulmont.cuba.gui.components.form.ComponentPosition) Element(org.dom4j.Element) ArrayList(java.util.ArrayList)

Aggregations

ComponentPosition (com.haulmont.cuba.gui.components.form.ComponentPosition)8 MetaClass (com.haulmont.chile.core.model.MetaClass)2 DynamicAttributesUtils.getCategoryAttribute (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils.getCategoryAttribute)2 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)2 HasValueSource (com.haulmont.cuba.gui.components.data.HasValueSource)2 ContainerValueSourceProvider (com.haulmont.cuba.gui.components.data.value.ContainerValueSourceProvider)2 ComponentArea (com.haulmont.cuba.gui.components.form.ComponentArea)2 ArrayList (java.util.ArrayList)2 Element (org.dom4j.Element)2 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)1 ValueSourceProvider (com.haulmont.cuba.gui.components.data.ValueSourceProvider)1 EntityValueSource (com.haulmont.cuba.gui.components.data.meta.EntityValueSource)1 ComponentLoader (com.haulmont.cuba.gui.xml.layout.ComponentLoader)1 LayoutLoader (com.haulmont.cuba.gui.xml.layout.LayoutLoader)1