Search in sources :

Example 1 with DeclarativeFieldGenerator

use of com.haulmont.cuba.gui.xml.DeclarativeFieldGenerator in project cuba by cuba-platform.

the class FieldGroupLoader method loadComponent.

@Override
public void loadComponent() {
    assignFrame(resultComponent);
    String fieldFactoryBean = element.attributeValue("fieldFactoryBean");
    if (StringUtils.isNotEmpty(fieldFactoryBean)) {
        FieldGroupFieldFactory fieldFactory = AppBeans.get(fieldFactoryBean, FieldGroupFieldFactory.class);
        resultComponent.setFieldFactory(fieldFactory);
    }
    assignXmlDescriptor(resultComponent, element);
    loadVisible(resultComponent, element);
    loadWidth(resultComponent, element);
    loadEditable(resultComponent, element);
    loadEnable(resultComponent, element);
    loadStyleName(resultComponent, element);
    loadIcon(resultComponent, element);
    loadCaption(resultComponent, element);
    loadDescription(resultComponent, element);
    loadHeight(resultComponent, element);
    loadAlign(resultComponent, element);
    loadCaptionAlignment(resultComponent, element);
    loadFieldCaptionWidth(resultComponent, element);
    Datasource ds = loadDatasource(element);
    resultComponent.setDatasource(ds);
    if (element.elements("column").isEmpty()) {
        Iterable<FieldGroup.FieldConfig> rootFields = loadFields(resultComponent, element, ds, null);
        Iterable<FieldGroup.FieldConfig> dynamicAttributeFields = loadDynamicAttributeFields(ds);
        for (FieldGroup.FieldConfig field : dynamicAttributeFields) {
            if (resultComponent.getWidth() > 0 && field.getWidth() == null) {
                field.setWidth("100%");
            }
        }
        for (FieldGroup.FieldConfig field : Iterables.concat(rootFields, dynamicAttributeFields)) {
            resultComponent.addField(field);
        }
    } else {
        @SuppressWarnings("unchecked") List<Element> columnElements = element.elements("column");
        @SuppressWarnings("unchecked") List<Element> fieldElements = element.elements("field");
        if (fieldElements.size() > 0) {
            Map<String, Object> params = new HashMap<>();
            String fieldGroupId = resultComponent.getId();
            if (StringUtils.isNotEmpty(fieldGroupId)) {
                params.put("FieldGroup ID", fieldGroupId);
            }
            throw new GuiDevelopmentException("FieldGroup field elements should be placed within its column.", context.getFullFrameId(), params);
        }
        resultComponent.setColumns(columnElements.size());
        int colIndex = 0;
        for (Element columnElement : columnElements) {
            String flex = columnElement.attributeValue("flex");
            if (StringUtils.isNotEmpty(flex)) {
                resultComponent.setColumnExpandRatio(colIndex, Float.parseFloat(flex));
            }
            String columnWidth = loadThemeString(columnElement.attributeValue("width"));
            Iterable<FieldGroup.FieldConfig> columnFields = loadFields(resultComponent, columnElement, ds, columnWidth);
            if (colIndex == 0) {
                columnFields = Iterables.concat(columnFields, loadDynamicAttributeFields(ds));
            }
            for (FieldGroup.FieldConfig field : columnFields) {
                resultComponent.addField(field, colIndex);
            }
            String columnFieldCaptionWidth = columnElement.attributeValue("fieldCaptionWidth");
            if (StringUtils.isNotEmpty(columnFieldCaptionWidth)) {
                if (columnFieldCaptionWidth.startsWith(MessageTools.MARK)) {
                    columnFieldCaptionWidth = loadResourceString(columnFieldCaptionWidth);
                }
                if (columnFieldCaptionWidth.endsWith("px")) {
                    columnFieldCaptionWidth = columnFieldCaptionWidth.substring(0, columnFieldCaptionWidth.indexOf("px"));
                }
                resultComponent.setFieldCaptionWidth(colIndex, Integer.parseInt(columnFieldCaptionWidth));
            }
            colIndex++;
        }
    }
    for (FieldGroup.FieldConfig field : resultComponent.getFields()) {
        if (!field.isCustom()) {
            if (!DynamicAttributesUtils.isDynamicAttribute(field.getProperty())) {
                // the following does not make sense for dynamic attributes
                loadValidators(resultComponent, field);
                loadRequired(resultComponent, field);
                loadEnable(resultComponent, field);
            }
            loadVisible(resultComponent, field);
            loadEditable(resultComponent, field);
        }
    }
    resultComponent.bind();
    for (FieldGroup.FieldConfig field : resultComponent.getFields()) {
        if (field.getXmlDescriptor() != null) {
            String generator = field.getXmlDescriptor().attributeValue("generator");
            if (generator != null) {
                context.addPostWrapTask((boundContext, window) -> {
                    DeclarativeFieldGenerator fieldGenerator = new DeclarativeFieldGenerator(resultComponent, generator);
                    Component fieldComponent = fieldGenerator.generateField(field.getTargetDatasource(), field.getProperty());
                    field.setComponent(fieldComponent);
                });
            }
        }
    }
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) DeclarativeFieldGenerator(com.haulmont.cuba.gui.xml.DeclarativeFieldGenerator) Element(org.dom4j.Element) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Aggregations

GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)1 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)1 Datasource (com.haulmont.cuba.gui.data.Datasource)1 DeclarativeFieldGenerator (com.haulmont.cuba.gui.xml.DeclarativeFieldGenerator)1 Element (org.dom4j.Element)1