Search in sources :

Example 11 with GuiDevelopmentException

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

the class DataGridLoader method loadColumn.

protected Column loadColumn(DataGrid component, Element element, Datasource ds) {
    String id = element.attributeValue("id");
    String property = element.attributeValue("property");
    if (id == null) {
        if (property != null) {
            id = property;
        } else {
            throw new GuiDevelopmentException("A column must have whether id or property specified", context.getCurrentFrameId(), "DataGrid ID", component.getId());
        }
    }
    Column column;
    if (property != null) {
        MetaPropertyPath metaPropertyPath = AppBeans.get(MetadataTools.NAME, MetadataTools.class).resolveMetaPropertyPath(ds.getMetaClass(), property);
        column = component.addColumn(id, metaPropertyPath);
    } else {
        column = component.addColumn(id, null);
    }
    String expandRatio = element.attributeValue("expandRatio");
    if (StringUtils.isNotEmpty(expandRatio)) {
        column.setExpandRatio(Integer.parseInt(expandRatio));
    }
    String collapsed = element.attributeValue("collapsed");
    if (StringUtils.isNotEmpty(collapsed)) {
        column.setCollapsed(Boolean.parseBoolean(collapsed));
    }
    String collapsible = element.attributeValue("collapsible");
    if (StringUtils.isNotEmpty(collapsible)) {
        column.setCollapsible(Boolean.parseBoolean(collapsible));
    }
    String collapsingToggleCaption = element.attributeValue("collapsingToggleCaption");
    if (StringUtils.isNotEmpty(collapsingToggleCaption)) {
        collapsingToggleCaption = loadResourceString(collapsingToggleCaption);
        column.setCollapsingToggleCaption(collapsingToggleCaption);
    }
    String sortable = element.attributeValue("sortable");
    if (StringUtils.isNotEmpty(sortable)) {
        column.setSortable(Boolean.parseBoolean(sortable));
    }
    String resizable = element.attributeValue("resizable");
    if (StringUtils.isNotEmpty(resizable)) {
        column.setResizable(Boolean.parseBoolean(resizable));
    }
    String editable = element.attributeValue("editable");
    if (StringUtils.isNotEmpty(editable)) {
        column.setEditable(Boolean.parseBoolean(editable));
    }
    // Default caption set to columns when it is added to a DataGrid,
    // so we need to set caption as null to get caption from
    // metaProperty if 'caption' attribute is empty
    column.setCaption(null);
    String caption = loadCaption(element);
    if (caption == null) {
        String columnCaption;
        if (column.getPropertyPath() != null) {
            MetaProperty metaProperty = column.getPropertyPath().getMetaProperty();
            String propertyName = metaProperty.getName();
            if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
                CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty);
                columnCaption = LocaleHelper.isLocalizedValueDefined(categoryAttribute.getLocaleNames()) ? categoryAttribute.getLocaleName() : StringUtils.capitalize(categoryAttribute.getName());
            } else {
                MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(column.getPropertyPath());
                columnCaption = messageTools.getPropertyCaption(propertyMetaClass, propertyName);
            }
        } else {
            Class<?> declaringClass = ds.getMetaClass().getJavaClass();
            String className = declaringClass.getName();
            int i = className.lastIndexOf('.');
            if (i > -1) {
                className = className.substring(i + 1);
            }
            columnCaption = messages.getMessage(declaringClass, className + "." + id);
        }
        column.setCaption(columnCaption);
    } else {
        column.setCaption(caption);
    }
    column.setXmlDescriptor(element);
    Integer width = loadWidth(element, "width");
    if (width != null) {
        column.setWidth(width);
    }
    Integer minimumWidth = loadWidth(element, "minimumWidth");
    if (minimumWidth != null) {
        column.setMinimumWidth(minimumWidth);
    }
    Integer maximumWidth = loadWidth(element, "maximumWidth");
    if (maximumWidth != null) {
        column.setMaximumWidth(maximumWidth);
    }
    column.setFormatter(loadFormatter(element));
    return column;
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) Column(com.haulmont.cuba.gui.components.DataGrid.Column) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 12 with GuiDevelopmentException

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

the class DatePickerLoader method loadRangeEnd.

protected void loadRangeEnd(DatePicker resultComponent, Element element) {
    String rangeEnd = element.attributeValue("rangeEnd");
    if (StringUtils.isNotEmpty(rangeEnd)) {
        try {
            SimpleDateFormat rangeDF = new SimpleDateFormat(DATE_PATTERN);
            resultComponent.setRangeEnd(rangeDF.parse(rangeEnd));
        } catch (ParseException e) {
            throw new GuiDevelopmentException("'rangeEnd' parsing error for date picker: " + rangeEnd, context.getFullFrameId(), "DatePicker ID", resultComponent.getId());
        }
    }
}
Also used : GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 13 with GuiDevelopmentException

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

the class FieldGroupLoader method loadFields.

protected List<FieldGroup.FieldConfig> loadFields(FieldGroup resultComponent, List<Element> elements, Datasource ds, @Nullable String columnWidth) {
    List<FieldGroup.FieldConfig> fields = new ArrayList<>(elements.size());
    List<String> ids = new ArrayList<>();
    for (Element fieldElement : elements) {
        FieldGroup.FieldConfig field = loadField(fieldElement, ds, columnWidth);
        if (ids.contains(field.getId())) {
            Map<String, Object> params = new HashMap<>();
            String fieldGroupId = resultComponent.getId();
            if (StringUtils.isNotEmpty(fieldGroupId)) {
                params.put("FieldGroup ID", fieldGroupId);
            }
            throw new GuiDevelopmentException(String.format("FieldGroup column contains duplicate fields '%s'.", field.getId()), context.getFullFrameId(), params);
        }
        fields.add(field);
        ids.add(field.getId());
    }
    return fields;
}
Also used : Element(org.dom4j.Element) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Example 14 with GuiDevelopmentException

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

the class FieldGroupLoader method loadField.

protected FieldGroup.FieldConfig loadField(Element element, Datasource ds, String columnWidth) {
    String id = element.attributeValue("id");
    String property = element.attributeValue("property");
    if (Strings.isNullOrEmpty(id) && Strings.isNullOrEmpty(property)) {
        throw new GuiDevelopmentException(String.format("id/property is not defined for field of FieldGroup '%s'. " + "Set id or property attribute.", resultComponent.getId()), context.getFullFrameId());
    }
    if (Strings.isNullOrEmpty(property)) {
        property = id;
    } else if (Strings.isNullOrEmpty(id)) {
        id = property;
    }
    Datasource targetDs = ds;
    Datasource datasource = loadDatasource(element);
    if (datasource != null) {
        targetDs = datasource;
    }
    CollectionDatasource optionsDs = null;
    String optDsName = element.attributeValue("optionsDatasource");
    if (StringUtils.isNotBlank(optDsName)) {
        DsContext dsContext = getContext().getFrame().getDsContext();
        optionsDs = findDatasourceRecursively(dsContext, optDsName);
        if (optionsDs == null) {
            throw new GuiDevelopmentException(String.format("Options datasource %s not found for field %s", optDsName, id), context.getFullFrameId());
        }
    }
    boolean customField = false;
    String custom = element.attributeValue("custom");
    if (StringUtils.isNotEmpty(custom)) {
        customField = Boolean.parseBoolean(custom);
    }
    if (StringUtils.isNotEmpty(element.attributeValue("generator"))) {
        customField = true;
    }
    List<Element> elements = Dom4j.elements(element);
    List<Element> customElements = elements.stream().filter(e -> !("formatter".equals(e.getName()) || "validator".equals(e.getName()))).collect(Collectors.toList());
    if (!customElements.isEmpty()) {
        if (customElements.size() > 1) {
            throw new GuiDevelopmentException(String.format("FieldGroup field %s element cannot contains two or more custom field definitions", id), context.getCurrentFrameId());
        }
        if (customField) {
            throw new GuiDevelopmentException(String.format("FieldGroup field %s cannot use both custom/generator attribute and inline component definition", id), context.getCurrentFrameId());
        }
        customField = true;
    }
    if (!customField && targetDs == null) {
        throw new GuiDevelopmentException(String.format("Datasource is not defined for FieldGroup field '%s'. " + "Only custom fields can have no datasource.", property), context.getFullFrameId());
    }
    FieldGroup.FieldConfig field = resultComponent.createField(id);
    if (property != null) {
        field.setProperty(property);
    }
    if (datasource != null) {
        field.setDatasource(datasource);
    }
    if (optionsDs != null) {
        field.setOptionsDatasource(optionsDs);
    }
    String stylename = element.attributeValue("stylename");
    if (StringUtils.isNotEmpty(stylename)) {
        field.setStyleName(stylename);
    }
    MetaPropertyPath metaPropertyPath = null;
    if (targetDs != null && property != null) {
        MetaClass metaClass = targetDs.getMetaClass();
        metaPropertyPath = metadataTools.resolveMetaPropertyPath(targetDs.getMetaClass(), property);
        if (metaPropertyPath == null) {
            if (!customField) {
                throw new GuiDevelopmentException(String.format("Property '%s' is not found in entity '%s'", property, metaClass.getName()), context.getFullFrameId());
            }
        }
    }
    String propertyName = metaPropertyPath != null ? metaPropertyPath.getMetaProperty().getName() : null;
    if (metaPropertyPath != null && DynamicAttributesUtils.isDynamicAttribute(metaPropertyPath.getMetaProperty())) {
        CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaPropertyPath.getMetaProperty());
        field.setCaption(categoryAttribute != null ? categoryAttribute.getLocaleName() : propertyName);
    } else {
        loadCaption(field, element);
        if (field.getCaption() == null) {
            field.setCaption(getDefaultCaption(field, targetDs));
        }
    }
    loadDescription(field, element);
    loadContextHelp(field, element);
    field.setXmlDescriptor(element);
    com.haulmont.cuba.gui.components.Formatter formatter = loadFormatter(element);
    if (formatter != null) {
        field.setFormatter(formatter);
    }
    String defaultWidth = element.attributeValue("width");
    if (StringUtils.isEmpty(defaultWidth)) {
        defaultWidth = columnWidth;
    }
    loadWidth(field, defaultWidth);
    if (customField) {
        field.setCustom(true);
    }
    String required = element.attributeValue("required");
    if (StringUtils.isNotEmpty(required)) {
        field.setRequired(Boolean.parseBoolean(required));
    }
    String requiredMsg = element.attributeValue("requiredMessage");
    if (requiredMsg != null) {
        requiredMsg = loadResourceString(requiredMsg);
        field.setRequiredMessage(requiredMsg);
    }
    String tabIndex = element.attributeValue("tabIndex");
    if (StringUtils.isNotEmpty(tabIndex)) {
        field.setTabIndex(Integer.parseInt(tabIndex));
    }
    loadInputPrompt(field, element);
    if (customElements.size() == 1) {
        // load nested component defined as inline
        Element customFieldElement = customElements.get(0);
        LayoutLoader loader = new LayoutLoader(context, factory, layoutLoaderConfig);
        loader.setLocale(getLocale());
        loader.setMessagesPack(getMessagesPack());
        ComponentLoader childComponentLoader = loader.createComponent(customFieldElement);
        childComponentLoader.loadComponent();
        Component customComponent = childComponentLoader.getResultComponent();
        String inlineAttachMode = element.attributeValue("inlineAttachMode");
        if (StringUtils.isNotEmpty(inlineAttachMode)) {
            field.setComponent(customComponent, FieldGroup.FieldAttachMode.valueOf(inlineAttachMode));
        } else {
            field.setComponent(customComponent);
        }
    }
    return field;
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Iterables(com.google.common.collect.Iterables) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Datasource(com.haulmont.cuba.gui.data.Datasource) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools) Dom4j(com.haulmont.bali.util.Dom4j) DynamicAttributeCustomFieldGenerator(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributeCustomFieldGenerator) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) DynamicAttributesUtils(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils) AppBeans(com.haulmont.cuba.core.global.AppBeans) MetaClass(com.haulmont.chile.core.model.MetaClass) BooleanUtils(org.apache.commons.lang.BooleanUtils) Strings(com.google.common.base.Strings) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) Preconditions.checkNotNullArgument(com.haulmont.bali.util.Preconditions.checkNotNullArgument) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) DsContext(com.haulmont.cuba.gui.data.DsContext) Nullable(javax.annotation.Nullable) LayoutLoader(com.haulmont.cuba.gui.xml.layout.LayoutLoader) MetaProperty(com.haulmont.chile.core.model.MetaProperty) DeclarativeFieldGenerator(com.haulmont.cuba.gui.xml.DeclarativeFieldGenerator) CustomFieldGenerator(com.haulmont.cuba.gui.components.FieldGroup.CustomFieldGenerator) Collectors(java.util.stream.Collectors) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader) EntityOp(com.haulmont.cuba.security.entity.EntityOp) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) ComponentsHelper(com.haulmont.cuba.gui.ComponentsHelper) FieldCaptionAlignment(com.haulmont.cuba.gui.components.FieldGroup.FieldCaptionAlignment) Element(org.dom4j.Element) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) MessageTools(com.haulmont.cuba.core.global.MessageTools) LayoutLoader(com.haulmont.cuba.gui.xml.layout.LayoutLoader) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) DsContext(com.haulmont.cuba.gui.data.DsContext) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Example 15 with GuiDevelopmentException

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

the class GridLayoutLoader method createComponent.

@Override
public void createComponent() {
    resultComponent = (GridLayout) factory.createComponent(GridLayout.NAME);
    loadId(resultComponent, element);
    Element columnsElement = element.element("columns");
    if (columnsElement == null) {
        throw new GuiDevelopmentException("'grid' element must contain 'columns' element", context.getFullFrameId(), "Grid ID", resultComponent.getId());
    }
    Element rowsElement = element.element("rows");
    if (rowsElement == null) {
        throw new GuiDevelopmentException("'grid' element must contain 'rows' element", context.getFullFrameId(), "Grid ID", resultComponent.getId());
    }
    int columnCount;
    @SuppressWarnings("unchecked") final List<Element> columnElements = columnsElement.elements("column");
    if (columnElements.size() == 0) {
        try {
            columnCount = Integer.parseInt(columnsElement.attributeValue("count"));
        } catch (NumberFormatException e) {
            throw new GuiDevelopmentException("'grid' element must contain either a set of 'column' elements or a 'count' attribute", context.getFullFrameId(), "Grid ID", resultComponent.getId());
        }
        resultComponent.setColumns(columnCount);
        for (int i = 0; i < columnCount; i++) {
            resultComponent.setColumnExpandRatio(i, 1);
        }
    } else {
        String countAttr = columnsElement.attributeValue("count");
        if (StringUtils.isNotEmpty(countAttr)) {
            throw new GuiDevelopmentException("'grid' element can't contain a set of 'column' elements and a 'count' attribute", context.getFullFrameId(), "Grid ID", resultComponent.getId());
        }
        columnCount = columnElements.size();
        resultComponent.setColumns(columnCount);
        int i = 0;
        for (Element columnElement : columnElements) {
            String flex = columnElement.attributeValue("flex");
            if (!StringUtils.isEmpty(flex)) {
                resultComponent.setColumnExpandRatio(i, Float.parseFloat(flex));
            }
            i++;
        }
    }
    @SuppressWarnings("unchecked") List<Element> rowElements = rowsElement.elements("row");
    Set<Element> invisibleRows = new HashSet<>();
    int rowCount = 0;
    for (Element rowElement : rowElements) {
        String visible = rowElement.attributeValue("visible");
        if (!StringUtils.isEmpty(visible)) {
            Boolean value = Boolean.valueOf(visible);
            if (BooleanUtils.toBoolean(value)) {
                rowCount++;
            } else {
                invisibleRows.add(rowElement);
            }
        } else {
            rowCount++;
        }
    }
    resultComponent.setRows(rowCount);
    int j = 0;
    for (Element rowElement : rowElements) {
        final String flex = rowElement.attributeValue("flex");
        if (!StringUtils.isEmpty(flex)) {
            resultComponent.setRowExpandRatio(j, Float.parseFloat(flex));
        }
        j++;
    }
    spanMatrix = new boolean[columnCount][rowElements.size()];
    int row = 0;
    for (Element rowElement : rowElements) {
        if (!invisibleRows.contains(rowElement)) {
            createSubComponents(resultComponent, rowElement, row);
            row++;
        }
    }
}
Also used : Element(org.dom4j.Element) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Aggregations

GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)55 Element (org.dom4j.Element)23 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)8 Component (com.haulmont.cuba.gui.components.Component)7 Datasource (com.haulmont.cuba.gui.data.Datasource)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 MetaClass (com.haulmont.chile.core.model.MetaClass)5 ComponentLoader (com.haulmont.cuba.gui.xml.layout.ComponentLoader)4 MetaProperty (com.haulmont.chile.core.model.MetaProperty)3 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)3 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)3 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)3 WindowConfig (com.haulmont.cuba.gui.config.WindowConfig)3 LayoutLoader (com.haulmont.cuba.gui.xml.layout.LayoutLoader)3 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)2 WindowManager (com.haulmont.cuba.gui.WindowManager)2 Column (com.haulmont.cuba.gui.components.DataGrid.Column)2 ListComponent (com.haulmont.cuba.gui.components.ListComponent)2 DsContext (com.haulmont.cuba.gui.data.DsContext)2 ByteArrayDataProvider (com.haulmont.cuba.gui.export.ByteArrayDataProvider)2