Search in sources :

Example 81 with MetaPropertyPath

use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.

the class WebAbstractTable method addGeneratedColumn.

@Override
public void addGeneratedColumn(String columnId, ColumnGenerator<? super E> generator) {
    checkNotNullArgument(columnId, "columnId is null");
    checkNotNullArgument(generator, "generator is null for column id '%s'", columnId);
    MetaPropertyPath targetCol = getDatasource().getMetaClass().getPropertyPath(columnId);
    Object generatedColumnId = targetCol != null ? targetCol : columnId;
    Column column = getColumn(columnId);
    Column associatedRuntimeColumn = null;
    if (column == null) {
        Column newColumn = new Column(generatedColumnId);
        columns.put(newColumn.getId(), newColumn);
        columnsOrder.add(newColumn);
        associatedRuntimeColumn = newColumn;
        newColumn.setOwner(this);
    }
    // save column order
    Object[] visibleColumns = component.getVisibleColumns();
    boolean removeOldGeneratedColumn = component.getColumnGenerator(generatedColumnId) != null;
    // replace generator for column if exist
    if (removeOldGeneratedColumn) {
        component.removeGeneratedColumn(generatedColumnId);
    }
    component.addGeneratedColumn(generatedColumnId, new CustomColumnGenerator(generator, associatedRuntimeColumn) {

        @SuppressWarnings("unchecked")
        @Override
        public Object generateCell(com.vaadin.ui.Table source, Object itemId, Object columnId) {
            Entity entity = getDatasource().getItem(itemId);
            com.haulmont.cuba.gui.components.Component component = getColumnGenerator().generateCell(entity);
            if (component == null) {
                return null;
            }
            if (component instanceof PlainTextCell) {
                return ((PlainTextCell) component).getText();
            }
            if (component instanceof BelongToFrame) {
                BelongToFrame belongToFrame = (BelongToFrame) component;
                if (belongToFrame.getFrame() == null) {
                    belongToFrame.setFrame(getFrame());
                }
            }
            component.setParent(WebAbstractTable.this);
            com.vaadin.ui.Component vComponent = component.unwrapComposition(Component.class);
            // wrap field for show required asterisk
            if ((vComponent instanceof com.vaadin.ui.Field) && (((com.vaadin.ui.Field) vComponent).isRequired())) {
                VerticalLayout layout = new VerticalLayout();
                layout.addComponent(vComponent);
                if (vComponent.getWidth() < 0) {
                    layout.setWidthUndefined();
                }
                layout.addComponent(vComponent);
                vComponent = layout;
            }
            return vComponent;
        }
    });
    if (removeOldGeneratedColumn) {
        // restore column order
        component.setVisibleColumns(visibleColumns);
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) com.vaadin.ui(com.vaadin.ui) Component(com.vaadin.ui.Component)

Example 82 with MetaPropertyPath

use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.

the class WebAbstractTable method removeGeneratedColumn.

@Override
public void removeGeneratedColumn(String columnId) {
    MetaPropertyPath targetCol = getDatasource().getMetaClass().getPropertyPath(columnId);
    removeGeneratedColumn(targetCol == null ? columnId : targetCol);
}
Also used : MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath)

Example 83 with MetaPropertyPath

use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.

the class WebAbstractTable method setDatasource.

@Override
public void setDatasource(final CollectionDatasource datasource) {
    Preconditions.checkNotNullArgument(datasource, "datasource is null");
    if (this.datasource != null) {
        if (!this.datasource.getMetaClass().equals(datasource.getMetaClass())) {
            throw new IllegalArgumentException("The new datasource must correspond to the same MetaClass");
        }
        if (fieldDatasources != null) {
            fieldDatasources.clear();
        }
        if (collectionDsListenersWrapper != null) {
            collectionDsListenersWrapper.unbind(this.datasource);
            if (containerDatasource != null) {
                containerDatasource.unsubscribe();
                containerDatasource = null;
            }
        }
    }
    MessageTools messageTools = AppBeans.get(MessageTools.NAME);
    final Collection<Object> columns;
    if (this.columns.isEmpty()) {
        Collection<MetaPropertyPath> paths = datasource.getView() != null ? // if a view is specified - use view properties
        metadataTools.getViewPropertyPaths(datasource.getView(), datasource.getMetaClass()) : // otherwise use all properties from meta-class
        metadataTools.getPropertyPaths(datasource.getMetaClass());
        for (MetaPropertyPath metaPropertyPath : paths) {
            MetaProperty property = metaPropertyPath.getMetaProperty();
            if (!property.getRange().getCardinality().isMany() && !metadataTools.isSystem(property)) {
                Table.Column column = new Table.Column(metaPropertyPath);
                String propertyName = property.getName();
                MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
                column.setCaption(messageTools.getPropertyCaption(propertyMetaClass, propertyName));
                column.setType(metaPropertyPath.getRangeJavaClass());
                Element element = DocumentHelper.createElement("column");
                column.setXmlDescriptor(element);
                addColumn(column);
            }
        }
    }
    columns = this.columns.keySet();
    this.datasource = datasource;
    if (collectionDsListenersWrapper == null) {
        collectionDsListenersWrapper = createCollectionDsListenersWrapper();
    }
    containerDatasource = createContainerDatasource(datasource, getPropertyColumns(), collectionDsListenersWrapper);
    component.setContainerDataSource(containerDatasource);
    List<MetaPropertyPath> editableColumns = null;
    if (isEditable()) {
        editableColumns = new LinkedList<>();
    }
    MetaClass metaClass = datasource.getMetaClass();
    for (final Object columnId : columns) {
        final Table.Column column = this.columns.get(columnId);
        final String caption;
        if (column != null) {
            caption = getColumnCaption(columnId, column);
        } else {
            caption = StringUtils.capitalize(getColumnCaption(columnId));
        }
        setColumnHeader(columnId, caption);
        if (column != null) {
            if (editableColumns != null && column.isEditable() && (columnId instanceof MetaPropertyPath)) {
                MetaPropertyPath propertyPath = ((MetaPropertyPath) columnId);
                if (security.isEntityAttrUpdatePermitted(metaClass, propertyPath.toString())) {
                    editableColumns.add(propertyPath);
                }
            }
            if (column.isCollapsed() && component.isColumnCollapsingAllowed()) {
                if (!(columnId instanceof MetaPropertyPath) || security.isEntityAttrReadPermitted(metaClass, columnId.toString())) {
                    component.setColumnCollapsed(column.getId(), true);
                }
            }
            if (column.getAggregation() != null && isAggregatable()) {
                checkAggregation(column.getAggregation());
                component.addContainerPropertyAggregation(column.getId(), WebComponentsHelper.convertAggregationType(column.getAggregation().getType()));
            }
        }
    }
    if (editableColumns != null && !editableColumns.isEmpty()) {
        setEditableColumns(editableColumns);
    }
    createColumns(containerDatasource);
    for (Table.Column column : this.columnsOrder) {
        if (editable && column.getAggregation() != null && (BooleanUtils.isTrue(column.isEditable()) || BooleanUtils.isTrue(column.isCalculatable()))) {
            addAggregationCell(column);
        }
    }
    createStubsForGeneratedColumns();
    setVisibleColumns(getInitialVisibleColumns());
    if (security.isSpecificPermitted(ShowInfoAction.ACTION_PERMISSION)) {
        ShowInfoAction action = (ShowInfoAction) getAction(ShowInfoAction.ACTION_ID);
        if (action == null) {
            action = new ShowInfoAction();
            addAction(action);
        }
        action.setDatasource(datasource);
    }
    if (rowsCount != null) {
        rowsCount.setDatasource(datasource);
    }
    collectionDsListenersWrapper.bind(datasource);
    for (Action action : getActions()) {
        action.refreshState();
    }
    if (!canBeSorted(datasource))
        setSortable(false);
    assignAutoDebugId();
}
Also used : Table(com.haulmont.cuba.gui.components.Table) CubaEnhancedTable(com.haulmont.cuba.web.toolkit.ui.CubaEnhancedTable) Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 84 with MetaPropertyPath

use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.

the class WebAbstractTable method setEditable.

@Override
public void setEditable(boolean editable) {
    if (this.editable != editable) {
        this.editable = editable;
        component.disableContentBufferRefreshing();
        if (datasource != null) {
            com.vaadin.data.Container ds = component.getContainerDataSource();
            @SuppressWarnings("unchecked") final Collection<MetaPropertyPath> propertyIds = (Collection<MetaPropertyPath>) ds.getContainerPropertyIds();
            if (editable) {
                MetaClass metaClass = datasource.getMetaClass();
                final List<MetaPropertyPath> editableColumns = new ArrayList<>(propertyIds.size());
                for (final MetaPropertyPath propertyId : propertyIds) {
                    if (!security.isEntityAttrUpdatePermitted(metaClass, propertyId.toString())) {
                        continue;
                    }
                    final Table.Column column = getColumn(propertyId.toString());
                    if (BooleanUtils.isTrue(column.isEditable())) {
                        com.vaadin.ui.Table.ColumnGenerator generator = component.getColumnGenerator(column.getId());
                        if (generator != null) {
                            if (generator instanceof SystemTableColumnGenerator) {
                                // remove default generator
                                component.removeGeneratedColumn(propertyId);
                            } else {
                                // do not edit generated columns
                                continue;
                            }
                        }
                        editableColumns.add(propertyId);
                    }
                }
                setEditableColumns(editableColumns);
            } else {
                setEditableColumns(Collections.emptyList());
                Window window = ComponentsHelper.getWindowImplementation(this);
                boolean isLookup = window instanceof Window.Lookup;
                // restore generators for some type of attributes
                for (MetaPropertyPath propertyId : propertyIds) {
                    final Table.Column column = columns.get(propertyId);
                    if (column != null) {
                        final String isLink = column.getXmlDescriptor() == null ? null : column.getXmlDescriptor().attributeValue("link");
                        if (component.getColumnGenerator(column.getId()) == null) {
                            if (propertyId.getRange().isClass()) {
                                if (!isLookup && StringUtils.isNotEmpty(isLink)) {
                                    setClickListener(propertyId.toString(), new LinkCellClickListener());
                                }
                            } else if (propertyId.getRange().isDatatype()) {
                                if (!isLookup && !StringUtils.isEmpty(isLink)) {
                                    setClickListener(propertyId.toString(), new LinkCellClickListener());
                                } else {
                                    if (column.getMaxTextLength() != null) {
                                        addGeneratedColumn(propertyId, new AbbreviatedColumnGenerator(column));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        component.setEditable(editable);
        component.enableContentBufferRefreshing(true);
    }
}
Also used : Window(com.haulmont.cuba.gui.components.Window) Table(com.haulmont.cuba.gui.components.Table) CubaEnhancedTable(com.haulmont.cuba.web.toolkit.ui.CubaEnhancedTable) com.haulmont.cuba.gui.data(com.haulmont.cuba.gui.data) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) MetaClass(com.haulmont.chile.core.model.MetaClass)

Aggregations

MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)84 MetaClass (com.haulmont.chile.core.model.MetaClass)34 MetaProperty (com.haulmont.chile.core.model.MetaProperty)27 Element (org.dom4j.Element)16 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)11 Entity (com.haulmont.cuba.core.entity.Entity)9 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)9 Datasource (com.haulmont.cuba.gui.data.Datasource)6 Table (com.haulmont.cuba.gui.components.Table)5 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)5 MessageTools (com.haulmont.cuba.core.global.MessageTools)4 Instance (com.haulmont.chile.core.model.Instance)3 Op (com.haulmont.cuba.core.global.filter.Op)3 FocusableTable (com.haulmont.cuba.desktop.sys.vcl.FocusableTable)3 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)3 Formatter (com.haulmont.cuba.gui.components.Formatter)3 Window (com.haulmont.cuba.gui.components.Window)3 CollectionFormatter (com.haulmont.cuba.gui.components.formatters.CollectionFormatter)3 GroupInfo (com.haulmont.cuba.gui.data.GroupInfo)3 java.util (java.util)3