Search in sources :

Example 6 with EntityTableItems

use of io.jmix.ui.component.data.meta.EntityTableItems in project jmix by jmix-framework.

the class AbstractTable 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);
    EntityTableItems<E> entityTableSource = (EntityTableItems<E>) getItems();
    MetaPropertyPath targetCol = entityTableSource != null ? entityTableSource.getEntityMetaClass().getPropertyPath(columnId) : null;
    Object generatedColumnId = targetCol != null ? targetCol : columnId;
    Column column = getColumn(columnId);
    Column associatedRuntimeColumn = null;
    if (column == null) {
        Column<E> newColumn = createColumn(generatedColumnId, this);
        columns.put(newColumn.getId(), newColumn);
        columnsOrder.add(newColumn);
        associatedRuntimeColumn = newColumn;
    }
    // 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) {

        @Nullable
        @Override
        public Object generateCell(com.vaadin.v7.ui.Table source, Object itemId, Object columnId) {
            EntityTableItems<E> entityTableSource = (EntityTableItems<E>) getItems();
            if (entityTableSource == null) {
                return null;
            }
            E entity = entityTableSource.getItem(itemId);
            io.jmix.ui.component.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(AbstractTable.this);
            AbstractComponent vComponent = component.unwrapComposition(AbstractComponent.class);
            if (component instanceof HasValueSource) {
                HasValueSource<?> hasValueSource = (HasValueSource) component;
                vComponent.setData(hasValueSource);
            }
            // wrap field for show required asterisk
            if ((vComponent instanceof com.vaadin.v7.ui.Field) && (((com.vaadin.v7.ui.Field) vComponent).isRequired())) {
                VerticalLayout layout = new VerticalLayout();
                layout.setMargin(false);
                layout.setSpacing(false);
                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 : MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) EntityTableItems(io.jmix.ui.component.data.meta.EntityTableItems) AbstractComponent(com.vaadin.ui.AbstractComponent) com.vaadin.ui(com.vaadin.ui) AbstractComponent(com.vaadin.ui.AbstractComponent) Component(com.vaadin.ui.Component) HasValueSource(io.jmix.ui.component.data.HasValueSource) Nullable(javax.annotation.Nullable)

Example 7 with EntityTableItems

use of io.jmix.ui.component.data.meta.EntityTableItems in project jmix by jmix-framework.

the class AbstractTableSettingsBinder method applyColumnSettings.

protected void applyColumnSettings(TableSettings tableSettings, Table table) {
    com.vaadin.v7.ui.Table vTable = getVTable(table);
    List<TableSettings.ColumnSettings> settingsColumns = CollectionUtils.isEmpty(tableSettings.getColumns()) ? Collections.emptyList() : tableSettings.getColumns();
    Object[] oldColumns = vTable.getVisibleColumns();
    List<Object> newColumns = new ArrayList<>();
    // add columns from saved settings
    for (TableSettings.ColumnSettings columnSetting : settingsColumns) {
        for (Object column : oldColumns) {
            if (column.toString().equals(columnSetting.getId())) {
                newColumns.add(column);
                Integer width = columnSetting.getWidth();
                vTable.setColumnWidth(column, width == null ? -1 : width);
                Boolean visible = columnSetting.getVisible();
                if (visible != null) {
                    if (vTable.isColumnCollapsingAllowed()) {
                        // throws exception if not
                        vTable.setColumnCollapsed(column, !visible);
                    }
                }
                break;
            }
        }
    }
    // add columns not saved in settings (perhaps new)
    for (Object column : oldColumns) {
        if (!newColumns.contains(column)) {
            newColumns.add(column);
        }
    }
    // if the table contains only one column, always show it
    if (newColumns.size() == 1) {
        if (vTable.isColumnCollapsingAllowed()) {
            // throws exception if not
            vTable.setColumnCollapsed(newColumns.get(0), false);
        }
    }
    vTable.setVisibleColumns(newColumns.toArray());
    EntityTableItems entityTableSource = (EntityTableItems) table.getItems();
    if (table.isSortable() && !isApplyDataLoadingSettings(table) && entityTableSource != null) {
        String sortProp = tableSettings.getSortProperty();
        if (!StringUtils.isEmpty(sortProp)) {
            MetaPropertyPath sortProperty = entityTableSource.getEntityMetaClass().getPropertyPath(sortProp);
            if (newColumns.contains(sortProperty)) {
                vTable.setSortContainerPropertyId(null);
                vTable.setSortAscending(Boolean.TRUE.equals(tableSettings.getSortAscending()));
                vTable.setSortContainerPropertyId(sortProperty);
            }
        } else {
            vTable.setSortContainerPropertyId(null);
        }
    }
}
Also used : TableSettings(io.jmix.ui.settings.component.TableSettings) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) EntityTableItems(io.jmix.ui.component.data.meta.EntityTableItems)

Example 8 with EntityTableItems

use of io.jmix.ui.component.data.meta.EntityTableItems in project jmix by jmix-framework.

the class AbstractTableSettingsBinder method applyDataLoadingSettings.

@Override
public void applyDataLoadingSettings(Table table, SettingsWrapper wrapper) {
    EntityTableItems entityTableSource = (EntityTableItems) table.getItems();
    if (table.isSortable() && isApplyDataLoadingSettings(table) && entityTableSource != null) {
        TableSettings tableSettings = wrapper.getSettings();
        List<TableSettings.ColumnSettings> columns = tableSettings.getColumns();
        if (columns != null) {
            String sortProp = tableSettings.getSortProperty();
            if (sortProp != null) {
                List visibleColumns = Arrays.asList(getVTable(table).getVisibleColumns());
                MetaPropertyPath sortProperty = entityTableSource.getEntityMetaClass().getPropertyPath(sortProp);
                if (visibleColumns.contains(sortProperty)) {
                    if (table.getItems() instanceof TableItems.Sortable) {
                        ((TableItems.Sortable) table.getItems()).suppressSorting();
                    }
                    try {
                        com.vaadin.v7.ui.Table vTable = getVTable(table);
                        vTable.setSortContainerPropertyId(null);
                        vTable.setSortAscending(Boolean.TRUE.equals(tableSettings.getSortAscending()));
                        vTable.setSortContainerPropertyId(sortProperty);
                    } finally {
                        if (table.getItems() instanceof TableItems.Sortable) {
                            ((TableItems.Sortable) table.getItems()).enableSorting();
                        }
                    }
                }
            } else if (table.getSortInfo() != null) {
                TableItems tableItems = table.getItems();
                if (tableItems instanceof TableItems.Sortable) {
                    ((TableItems.Sortable) tableItems).resetSortOrder();
                }
            }
        }
    }
}
Also used : TableSettings(io.jmix.ui.settings.component.TableSettings) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) EntityTableItems(io.jmix.ui.component.data.meta.EntityTableItems) TableItems(io.jmix.ui.component.data.TableItems) EntityTableItems(io.jmix.ui.component.data.meta.EntityTableItems)

Example 9 with EntityTableItems

use of io.jmix.ui.component.data.meta.EntityTableItems in project jmix by jmix-framework.

the class AbstractTable method removeGeneratedColumn.

@Override
public void removeGeneratedColumn(String columnId) {
    EntityTableItems<E> entityTableSource = (EntityTableItems<E>) getItems();
    MetaPropertyPath targetCol = entityTableSource != null ? entityTableSource.getEntityMetaClass().getPropertyPath(columnId) : null;
    removeGeneratedColumnInternal(targetCol == null ? columnId : targetCol);
}
Also used : MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) EntityTableItems(io.jmix.ui.component.data.meta.EntityTableItems)

Example 10 with EntityTableItems

use of io.jmix.ui.component.data.meta.EntityTableItems in project jmix by jmix-framework.

the class AbstractTable method getInstanceContainer.

@SuppressWarnings("unchecked")
@Override
public InstanceContainer<E> getInstanceContainer(E item) {
    if (fieldDatasources == null) {
        fieldDatasources = new WeakHashMap<>();
    }
    Object fieldDatasource = fieldDatasources.get(item);
    if (fieldDatasource instanceof InstanceContainer) {
        return (InstanceContainer<E>) fieldDatasource;
    }
    EntityTableItems containerTableItems = (EntityTableItems) getItems();
    if (containerTableItems == null) {
        throw new IllegalStateException("Table is not bound to items");
    }
    InstanceContainer<E> instanceContainer;
    MetaClass metaClass = containerTableItems.getEntityMetaClass();
    if (metaClass instanceof KeyValueMetaClass) {
        instanceContainer = (InstanceContainer<E>) dataComponents.createKeyValueContainer(metaClass);
    } else {
        instanceContainer = dataComponents.createInstanceContainer(metaClass.getJavaClass());
    }
    FetchPlan view = viewRepository.getFetchPlan(metaClass, FetchPlan.LOCAL);
    instanceContainer.setFetchPlan(view);
    instanceContainer.setItem(item);
    fieldDatasources.put(item, instanceContainer);
    return instanceContainer;
}
Also used : KeyValueMetaClass(io.jmix.core.impl.keyvalue.KeyValueMetaClass) MetaClass(io.jmix.core.metamodel.model.MetaClass) KeyValueMetaClass(io.jmix.core.impl.keyvalue.KeyValueMetaClass) EntityTableItems(io.jmix.ui.component.data.meta.EntityTableItems) InstanceContainer(io.jmix.ui.model.InstanceContainer) FetchPlan(io.jmix.core.FetchPlan)

Aggregations

EntityTableItems (io.jmix.ui.component.data.meta.EntityTableItems)11 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)6 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)3 Datasource (com.haulmont.cuba.gui.data.Datasource)3 DatasourceImplementation (com.haulmont.cuba.gui.data.impl.DatasourceImplementation)3 MetaClass (io.jmix.core.metamodel.model.MetaClass)2 TableSettings (io.jmix.ui.settings.component.TableSettings)2 Nullable (javax.annotation.Nullable)2 com.vaadin.ui (com.vaadin.ui)1 AbstractComponent (com.vaadin.ui.AbstractComponent)1 Component (com.vaadin.ui.Component)1 FetchPlan (io.jmix.core.FetchPlan)1 KeyValueMetaClass (io.jmix.core.impl.keyvalue.KeyValueMetaClass)1 GroupTable (io.jmix.ui.component.GroupTable)1 HasValueSource (io.jmix.ui.component.data.HasValueSource)1 TableItems (io.jmix.ui.component.data.TableItems)1 InstanceContainer (io.jmix.ui.model.InstanceContainer)1 GroupTableSettings (io.jmix.ui.settings.component.GroupTableSettings)1 JmixGroupTable (io.jmix.ui.widget.JmixGroupTable)1 ArrayList (java.util.ArrayList)1