Search in sources :

Example 1 with EntityTableItems

use of com.haulmont.cuba.gui.components.data.meta.EntityTableItems in project cuba by cuba-platform.

the class WebAbstractTable method setItems.

@Override
public void setItems(TableItems<E> tableItems) {
    if (this.dataBinding != null) {
        this.dataBinding.unbind();
        this.dataBinding = null;
        clearFieldDatasources();
        this.component.setContainerDataSource(null);
    }
    if (tableItems != null) {
        // Table supports only EntityTableItems
        EntityTableItems<E> entityTableSource = (EntityTableItems<E>) tableItems;
        if (this.columns.isEmpty()) {
            setupAutowiredColumns(entityTableSource);
        }
        // bind new datasource
        this.dataBinding = createTableDataContainer(tableItems);
        this.dataBinding.setProperties(getPropertyColumns(entityTableSource, columnsOrder));
        this.component.setContainerDataSource(this.dataBinding);
        setupColumnSettings(entityTableSource);
        createColumns(component.getContainerDataSource());
        for (Table.Column column : this.columnsOrder) {
            if (editable && column.getAggregation() != null && (BooleanUtils.isTrue(column.isEditable()))) {
                addAggregationCell(column);
            }
        }
        createStubsForGeneratedColumns();
        setVisibleColumns(getInitialVisibleColumnIds(entityTableSource));
        if (security.isSpecificPermitted(ShowInfoAction.ACTION_PERMISSION)) {
            if (getAction(ShowInfoAction.ACTION_ID) == null) {
                addAction(new ShowInfoAction());
            }
        }
        if (rowsCount != null) {
            rowsCount.setRowsCountTarget(this);
        }
        if (!canBeSorted(tableItems)) {
            setSortable(false);
        } else {
            // restore sortable
            setSortable(sortable);
        }
        // resort data if table have been sorted before setting items
        if (isSortable()) {
            if (getSortInfo() != null) {
                SortDirection sortDirection = getSortInfo().getAscending() ? SortDirection.ASCENDING : SortDirection.DESCENDING;
                Object columnId = getSortInfo().getPropertyId();
                String id = columnId instanceof MetaPropertyPath ? ((MetaPropertyPath) columnId).toPathString() : String.valueOf(columnId);
                sort(id, sortDirection);
            }
        }
        refreshActionsState();
        setUiTestId(tableItems);
    }
    initEmptyState();
}
Also used : ShowInfoAction(com.haulmont.cuba.gui.components.sys.ShowInfoAction) CubaEnhancedTable(com.haulmont.cuba.web.widgets.CubaEnhancedTable) EntityTableItems(com.haulmont.cuba.gui.components.data.meta.EntityTableItems)

Example 2 with EntityTableItems

use of com.haulmont.cuba.gui.components.data.meta.EntityTableItems in project cuba by cuba-platform.

the class WebAbstractTable method getItemDatasource.

@SuppressWarnings("unchecked")
@Override
public Datasource getItemDatasource(Entity item) {
    if (fieldDatasources == null) {
        fieldDatasources = new WeakHashMap<>();
    }
    Object fieldDatasource = fieldDatasources.get(item);
    if (fieldDatasource instanceof Datasource) {
        return (Datasource) fieldDatasource;
    }
    EntityTableItems containerTableItems = (EntityTableItems) getItems();
    Datasource datasource = DsBuilder.create().setAllowCommit(false).setMetaClass(containerTableItems.getEntityMetaClass()).setRefreshMode(CollectionDatasource.RefreshMode.NEVER).setViewName(View.LOCAL).buildDatasource();
    ((DatasourceImplementation) datasource).valid();
    datasource.setItem(item);
    fieldDatasources.put(item, datasource);
    return datasource;
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) EntityTableItems(com.haulmont.cuba.gui.components.data.meta.EntityTableItems) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation)

Example 3 with EntityTableItems

use of com.haulmont.cuba.gui.components.data.meta.EntityTableItems in project cuba by cuba-platform.

the class ExcelExporter method exportTable.

public void exportTable(Table<Entity> table, List<Table.Column> columns, Boolean exportExpanded, @Nullable ExportDisplay display, @Nullable List<String> filterDescription, @Nullable String fileName, ExportMode exportMode) {
    if (display == null) {
        throw new IllegalArgumentException("ExportDisplay is null");
    }
    createWorkbookWithSheet();
    createFonts();
    createFormats();
    int r = 0;
    if (filterDescription != null) {
        for (r = 0; r < filterDescription.size(); r++) {
            String line = filterDescription.get(r);
            Row row = sheet.createRow(r);
            if (r == 0) {
                RichTextString richTextFilterName = excelExportHelper.createRichTextString(line);
                richTextFilterName.applyFont(boldFont);
                row.createCell(0).setCellValue(richTextFilterName);
            } else {
                row.createCell(0).setCellValue(line);
            }
        }
        r++;
    }
    Row row = sheet.createRow(r);
    createAutoColumnSizers(columns.size());
    float maxHeight = sheet.getDefaultRowHeightInPoints();
    CellStyle headerCellStyle = wb.createCellStyle();
    headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    for (Table.Column column : columns) {
        String caption = column.getCaption();
        int countOfReturnSymbols = StringUtils.countMatches(caption, "\n");
        if (countOfReturnSymbols > 0) {
            maxHeight = Math.max(maxHeight, (countOfReturnSymbols + 1) * sheet.getDefaultRowHeightInPoints());
            headerCellStyle.setWrapText(true);
        }
    }
    row.setHeightInPoints(maxHeight);
    for (int c = 0; c < columns.size(); c++) {
        Table.Column column = columns.get(c);
        String caption = column.getCaption();
        Cell cell = row.createCell(c);
        RichTextString richTextString = excelExportHelper.createRichTextString(caption);
        richTextString.applyFont(boldFont);
        cell.setCellValue(richTextString);
        ExcelAutoColumnSizer sizer = new ExcelAutoColumnSizer();
        sizer.notifyCellValue(caption, boldFont);
        sizers[c] = sizer;
        cell.setCellStyle(headerCellStyle);
    }
    TableItems<Entity> tableItems = table.getItems();
    if (exportMode == ExportMode.SELECTED_ROWS && table.getSelected().size() > 0) {
        Set<Entity> selected = table.getSelected();
        List<Entity> ordered = tableItems.getItemIds().stream().map(tableItems::getItem).filter(selected::contains).collect(Collectors.toList());
        for (Entity item : ordered) {
            if (checkIsRowNumberExceed(r)) {
                break;
            }
            createRow(table, columns, 0, ++r, item.getId());
        }
    } else {
        if (table.isAggregatable() && exportAggregation && hasAggregatableColumn(table)) {
            if (table.getAggregationStyle() == Table.AggregationStyle.TOP) {
                r = createAggregatableRow(table, columns, ++r, 1);
            }
        }
        if (table instanceof TreeTable) {
            TreeTable treeTable = (TreeTable) table;
            TreeTableItems treeTableSource = (TreeTableItems) treeTable.getItems();
            for (Object itemId : treeTableSource.getRootItemIds()) {
                if (checkIsRowNumberExceed(r)) {
                    break;
                }
                r = createHierarhicalRow(treeTable, columns, exportExpanded, r, itemId);
            }
        } else if (table instanceof GroupTable && tableItems instanceof GroupTableItems && ((GroupTableItems) tableItems).hasGroups()) {
            GroupTableItems groupTableSource = (GroupTableItems) tableItems;
            for (Object item : groupTableSource.rootGroups()) {
                if (checkIsRowNumberExceed(r)) {
                    break;
                }
                r = createGroupRow((GroupTable) table, columns, ++r, (GroupInfo) item, 0);
            }
        } else {
            for (Object itemId : tableItems.getItemIds()) {
                if (checkIsRowNumberExceed(r)) {
                    break;
                }
                createRow(table, columns, 0, ++r, itemId);
            }
        }
        if (table.isAggregatable() && exportAggregation && hasAggregatableColumn(table)) {
            if (table.getAggregationStyle() == Table.AggregationStyle.BOTTOM) {
                r = createAggregatableRow(table, columns, ++r, 1);
            }
        }
    }
    for (int c = 0; c < columns.size(); c++) {
        sheet.setColumnWidth(c, sizers[c].getWidth() * excelOptions.getColWidthMagic());
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        wb.write(out);
    } catch (IOException e) {
        throw new RuntimeException("Unable to write document", e);
    }
    if (fileName == null) {
        fileName = messages.getTools().getEntityCaption(((EntityTableItems) tableItems).getEntityMetaClass());
    }
    display.show(new ByteArrayDataProvider(out.toByteArray()), fileName + excelOptions.getExtension(), excelOptions.getExportFormat());
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) GroupTableItems(com.haulmont.cuba.gui.components.data.GroupTableItems) Table(com.haulmont.cuba.gui.components.Table) EntityTableItems(com.haulmont.cuba.gui.components.data.meta.EntityTableItems) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) TreeTableItems(com.haulmont.cuba.gui.components.data.TreeTableItems)

Example 4 with EntityTableItems

use of com.haulmont.cuba.gui.components.data.meta.EntityTableItems in project cuba by cuba-platform.

the class WebAbstractTable 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>) new KeyValueContainerImpl((KeyValueMetaClass) metaClass);
    } else {
        instanceContainer = dataComponents.createInstanceContainer(metaClass.getJavaClass());
    }
    View view = viewRepository.getView(metaClass, View.LOCAL);
    instanceContainer.setView(view);
    instanceContainer.setItem(item);
    fieldDatasources.put(item, instanceContainer);
    return instanceContainer;
}
Also used : KeyValueMetaClass(com.haulmont.cuba.core.app.keyvalue.KeyValueMetaClass) KeyValueMetaClass(com.haulmont.cuba.core.app.keyvalue.KeyValueMetaClass) KeyValueContainerImpl(com.haulmont.cuba.gui.model.impl.KeyValueContainerImpl) EntityTableItems(com.haulmont.cuba.gui.components.data.meta.EntityTableItems)

Example 5 with EntityTableItems

use of com.haulmont.cuba.gui.components.data.meta.EntityTableItems in project cuba by cuba-platform.

the class WebAbstractTable method applyDataLoadingSettings.

@Override
public void applyDataLoadingSettings(Element element) {
    if (!isSettingsEnabled()) {
        return;
    }
    if (isSortable() && isApplyDataLoadingSettings()) {
        @SuppressWarnings("unchecked") EntityTableItems<E> entityTableSource = (EntityTableItems) getItems();
        Element columnsElem = element.element("columns");
        if (columnsElem != null) {
            String sortProp = columnsElem.attributeValue("sortProperty");
            if (!StringUtils.isEmpty(sortProp)) {
                List columns = Arrays.asList(component.getVisibleColumns());
                MetaPropertyPath sortProperty = entityTableSource.getEntityMetaClass().getPropertyPath(sortProp);
                if (columns.contains(sortProperty)) {
                    boolean sortAscending = Boolean.parseBoolean(columnsElem.attributeValue("sortAscending"));
                    if (getItems() instanceof TableItems.Sortable) {
                        ((TableItems.Sortable<E>) getItems()).suppressSorting();
                    }
                    try {
                        component.setSortContainerPropertyId(null);
                        component.setSortAscending(sortAscending);
                        component.setSortContainerPropertyId(sortProperty);
                    } finally {
                        if (getItems() instanceof TableItems.Sortable) {
                            ((TableItems.Sortable<E>) getItems()).enableSorting();
                        }
                    }
                }
            }
        }
    }
}
Also used : Element(org.dom4j.Element) EntityTableItems(com.haulmont.cuba.gui.components.data.meta.EntityTableItems)

Aggregations

EntityTableItems (com.haulmont.cuba.gui.components.data.meta.EntityTableItems)8 Element (org.dom4j.Element)3 MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 KeyValueMetaClass (com.haulmont.cuba.core.app.keyvalue.KeyValueMetaClass)1 Entity (com.haulmont.cuba.core.entity.Entity)1 Table (com.haulmont.cuba.gui.components.Table)1 GroupTableItems (com.haulmont.cuba.gui.components.data.GroupTableItems)1 TreeTableItems (com.haulmont.cuba.gui.components.data.TreeTableItems)1 ShowInfoAction (com.haulmont.cuba.gui.components.sys.ShowInfoAction)1 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)1 Datasource (com.haulmont.cuba.gui.data.Datasource)1 DatasourceImplementation (com.haulmont.cuba.gui.data.impl.DatasourceImplementation)1 KeyValueContainerImpl (com.haulmont.cuba.gui.model.impl.KeyValueContainerImpl)1 CubaEnhancedTable (com.haulmont.cuba.web.widgets.CubaEnhancedTable)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1