Search in sources :

Example 1 with TreeTableItems

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

the class ExcelExporter method createHierarhicalRow.

protected int createHierarhicalRow(TreeTable table, List<Table.Column> columns, Boolean exportExpanded, int rowNumber, Object itemId) {
    TreeTableItems treeTableSource = (TreeTableItems) table.getItems();
    createRow(table, columns, 0, ++rowNumber, itemId);
    if (BooleanUtils.isTrue(exportExpanded) && !table.isExpanded(itemId) && !treeTableSource.getChildren(itemId).isEmpty()) {
        return rowNumber;
    } else {
        Collection children = treeTableSource.getChildren(itemId);
        if (children != null && !children.isEmpty()) {
            for (Object id : children) {
                if (BooleanUtils.isTrue(exportExpanded) && !table.isExpanded(id) && !treeTableSource.getChildren(id).isEmpty()) {
                    createRow(table, columns, 0, ++rowNumber, id);
                    continue;
                }
                rowNumber = createHierarhicalRow(table, columns, exportExpanded, rowNumber, id);
            }
        }
    }
    return rowNumber;
}
Also used : TreeTableItems(com.haulmont.cuba.gui.components.data.TreeTableItems)

Example 2 with TreeTableItems

use of com.haulmont.cuba.gui.components.data.TreeTableItems 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)

Aggregations

TreeTableItems (com.haulmont.cuba.gui.components.data.TreeTableItems)2 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 EntityTableItems (com.haulmont.cuba.gui.components.data.meta.EntityTableItems)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1