Search in sources :

Example 31 with CategoryAttribute

use of com.haulmont.cuba.core.entity.CategoryAttribute in project cuba by cuba-platform.

the class AbstractTableLoader method loadColumn.

protected Table.Column loadColumn(Element element, Datasource ds) {
    String id = element.attributeValue("id");
    MetaPropertyPath metaPropertyPath = AppBeans.get(MetadataTools.NAME, MetadataTools.class).resolveMetaPropertyPath(ds.getMetaClass(), id);
    Table.Column column = new Table.Column(metaPropertyPath != null ? metaPropertyPath : id);
    String editable = element.attributeValue("editable");
    if (StringUtils.isNotEmpty(editable)) {
        column.setEditable(Boolean.parseBoolean(editable));
    }
    String collapsed = element.attributeValue("collapsed");
    if (StringUtils.isNotEmpty(collapsed)) {
        column.setCollapsed(Boolean.parseBoolean(collapsed));
    }
    String groupAllowed = element.attributeValue("groupAllowed");
    if (StringUtils.isNotEmpty(groupAllowed)) {
        column.setGroupAllowed(Boolean.parseBoolean(groupAllowed));
    }
    String sortable = element.attributeValue("sortable");
    if (StringUtils.isNotEmpty(sortable)) {
        column.setSortable(Boolean.parseBoolean(sortable));
    }
    loadCaption(column, element);
    loadDescription(column, element);
    if (column.getCaption() == null) {
        String columnCaption;
        if (column.getId() instanceof MetaPropertyPath) {
            MetaPropertyPath mpp = (MetaPropertyPath) column.getId();
            MetaProperty metaProperty = mpp.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(mpp);
                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);
    }
    column.setXmlDescriptor(element);
    if (metaPropertyPath != null)
        column.setType(metaPropertyPath.getRangeJavaClass());
    String width = loadThemeString(element.attributeValue("width"));
    if (!StringUtils.isBlank(width)) {
        if (StringUtils.endsWith(width, "px")) {
            width = StringUtils.substring(width, 0, width.length() - 2);
        }
        try {
            column.setWidth(Integer.parseInt(width));
        } catch (NumberFormatException e) {
            throw new GuiDevelopmentException("Property 'width' must contain only numeric value", context.getCurrentFrameId(), "width", element.attributeValue("width"));
        }
    }
    String align = element.attributeValue("align");
    if (StringUtils.isNotBlank(align)) {
        column.setAlignment(Table.ColumnAlignment.valueOf(align));
    }
    column.setFormatter(loadFormatter(element));
    loadAggregation(column, element);
    loadCalculatable(column, element);
    loadMaxTextLength(column, element);
    return column;
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 32 with CategoryAttribute

use of com.haulmont.cuba.core.entity.CategoryAttribute in project cuba by cuba-platform.

the class DataGridLoader method addDynamicAttributes.

protected void addDynamicAttributes(DataGrid component, Datasource ds, List<Column> availableColumns) {
    if (metadataTools.isPersistent(ds.getMetaClass())) {
        Set<CategoryAttribute> attributesToShow = dynamicAttributesGuiTools.getAttributesToShowOnTheScreen(ds.getMetaClass(), context.getFullFrameId(), component.getId());
        if (CollectionUtils.isNotEmpty(attributesToShow)) {
            ds.setLoadDynamicAttributes(true);
            for (CategoryAttribute attribute : attributesToShow) {
                final MetaPropertyPath metaPropertyPath = DynamicAttributesUtils.getMetaPropertyPath(ds.getMetaClass(), attribute);
                Object columnWithSameId = IterableUtils.find(availableColumns, column -> {
                    MetaPropertyPath propertyPath = column.getPropertyPath();
                    return propertyPath != null && propertyPath.equals(metaPropertyPath);
                });
                if (columnWithSameId != null) {
                    continue;
                }
                final Column column = component.addColumn(metaPropertyPath.getMetaProperty().getName(), metaPropertyPath);
                column.setCaption(LocaleHelper.isLocalizedValueDefined(attribute.getLocaleNames()) ? attribute.getLocaleName() : StringUtils.capitalize(attribute.getName()));
            }
        }
        dynamicAttributesGuiTools.listenDynamicAttributesChanges(ds);
    }
}
Also used : CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) Column(com.haulmont.cuba.gui.components.DataGrid.Column) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath)

Example 33 with CategoryAttribute

use of com.haulmont.cuba.core.entity.CategoryAttribute in project cuba by cuba-platform.

the class WebAbstractOptionsField method setDatasource.

@Override
public void setDatasource(Datasource datasource, String property) {
    if ((datasource == null && property != null) || (datasource != null && property == null))
        throw new IllegalArgumentException("Datasource and property should be either null or not null at the same time");
    if (datasource == this.datasource && ((metaPropertyPath != null && metaPropertyPath.toString().equals(property)) || (metaPropertyPath == null && property == null)))
        return;
    if (this.datasource != null) {
        metaProperty = null;
        metaPropertyPath = null;
        component.setPropertyDataSource(null);
        // noinspection unchecked
        this.datasource.removeItemChangeListener(securityWeakItemChangeListener);
        securityWeakItemChangeListener = null;
        this.datasource = null;
        if (itemWrapper != null) {
            itemWrapper.unsubscribe();
        }
        disableBeanValidator();
    }
    if (datasource != null) {
        // noinspection unchecked
        this.datasource = datasource;
        MetaClass metaClass = datasource.getMetaClass();
        resolveMetaPropertyPath(metaClass, property);
        if (metaProperty.getRange().getCardinality() != null) {
            setMultiSelect(metaProperty.getRange().getCardinality().isMany());
        }
        itemWrapper = createDatasourceWrapper(datasource, Collections.singleton(metaPropertyPath));
        Property itemProperty = itemWrapper.getItemProperty(metaPropertyPath);
        initRequired(metaPropertyPath);
        if (metaProperty.getRange().isEnum()) {
            Enumeration enumeration = metaProperty.getRange().asEnumeration();
            List options = Arrays.asList(enumeration.getJavaClass().getEnumConstants());
            setComponentContainerDs(createEnumContainer(options));
            setCaptionMode(CaptionMode.ITEM);
        }
        if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
            CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty);
            if (categoryAttribute != null && categoryAttribute.getDataType() == PropertyType.ENUMERATION) {
                setOptionsMap(categoryAttribute.getLocalizedEnumerationMap());
            }
        }
        component.setPropertyDataSource(itemProperty);
        if (metaProperty.isReadOnly()) {
            setEditable(false);
        }
        handleFilteredAttributes(this, this.datasource, metaPropertyPath);
        securityItemChangeListener = e -> handleFilteredAttributes(this, this.datasource, metaPropertyPath);
        securityWeakItemChangeListener = new WeakItemChangeListener(datasource, securityItemChangeListener);
        // noinspection unchecked
        this.datasource.addItemChangeListener(securityWeakItemChangeListener);
        initBeanValidator();
    }
}
Also used : WeakItemChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemChangeListener) Enumeration(com.haulmont.chile.core.datatypes.Enumeration) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) Property(com.vaadin.data.Property)

Example 34 with CategoryAttribute

use of com.haulmont.cuba.core.entity.CategoryAttribute in project cuba by cuba-platform.

the class WebAbstractTable method getColumnCaption.

protected String getColumnCaption(Object columnId, Column column) {
    String caption = column.getCaption();
    if (caption != null) {
        return caption;
    }
    if (!(columnId instanceof MetaPropertyPath)) {
        return StringUtils.capitalize(getColumnCaption(columnId));
    }
    MetaPropertyPath mpp = (MetaPropertyPath) columnId;
    MetaProperty metaProperty = mpp.getMetaProperty();
    if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
        CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty);
        if (LocaleHelper.isLocalizedValueDefined(categoryAttribute.getLocaleNames())) {
            return categoryAttribute.getLocaleName();
        }
        caption = StringUtils.capitalize(categoryAttribute.getName());
    } else {
        caption = StringUtils.capitalize(getColumnCaption(columnId));
    }
    return caption;
}
Also used : CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)34 MetaClass (com.haulmont.chile.core.model.MetaClass)15 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)12 MetaProperty (com.haulmont.chile.core.model.MetaProperty)10 Nullable (javax.annotation.Nullable)6 Element (org.dom4j.Element)5 DynamicAttributesMetaProperty (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty)4 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)4 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)4 DynamicAttributesGuiTools (com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)4 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)3 Joiner (com.google.common.base.Joiner)2 Strings (com.google.common.base.Strings)2 Dom4j (com.haulmont.bali.util.Dom4j)2 Enumeration (com.haulmont.chile.core.datatypes.Enumeration)2 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)2 Category (com.haulmont.cuba.core.entity.Category)2 Entity (com.haulmont.cuba.core.entity.Entity)2 AppBeans (com.haulmont.cuba.core.global.AppBeans)2 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)2