Search in sources :

Example 16 with Table

use of io.jmix.ui.component.Table in project jmix by jmix-framework.

the class ExcelExporter method exportTable.

@Override
public void exportTable(Downloader downloader, Table<Object> table, ExportMode exportMode) {
    if (downloader == null) {
        throw new IllegalArgumentException("Downloader is null");
    }
    if (table.getItems() == null) {
        throw new IllegalStateException("Table items should not be null");
    }
    @SuppressWarnings("unchecked") List<Table.Column<Object>> columns = Collections.unmodifiableList(table.getNotCollapsedColumns()).stream().map(c -> (Table.Column<Object>) c).collect(Collectors.toList());
    createWorkbookWithSheet();
    createFonts();
    createFormats();
    int r = 0;
    Row row = sheet.createRow(r);
    createAutoColumnSizers(columns.size());
    float maxHeight = sheet.getDefaultRowHeightInPoints();
    CellStyle headerCellStyle = wb.createCellStyle();
    headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    for (Table.Column<Object> 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<Object> column = columns.get(c);
        String caption = column.getCaption();
        Cell cell = row.createCell(c);
        RichTextString richTextString = createStringCellValue(caption);
        richTextString.applyFont(boldFont);
        cell.setCellValue(richTextString);
        ExcelAutoColumnSizer sizer = new ExcelAutoColumnSizer();
        sizer.notifyCellValue(caption, boldFont);
        sizers[c] = sizer;
        cell.setCellStyle(headerCellStyle);
    }
    TableItems<Object> tableItems = table.getItems();
    if (exportMode == ExportMode.SELECTED && table.getSelected().size() > 0) {
        Set<Object> selected = table.getSelected();
        List<Object> ordered = tableItems.getItemIds().stream().map(tableItems::getItem).filter(selected::contains).collect(Collectors.toList());
        for (Object item : ordered) {
            if (checkIsRowNumberExceed(r)) {
                break;
            }
            createRow(table, columns, 0, ++r, Id.of(item).getValue());
        }
    } else {
        if (table.isAggregatable() && exportAggregation && hasAggregatableColumn(table)) {
            if (table.getAggregationStyle() == Table.AggregationStyle.TOP) {
                r = createAggregatableRow(table, columns, ++r, 1);
            }
        }
        if (table instanceof TreeTable) {
            TreeTable<Object> treeTable = (TreeTable<Object>) table;
            TreeTableItems<Object> treeTableSource = (TreeTableItems<Object>) treeTable.getItems();
            if (treeTableSource != null) {
                for (Object itemId : treeTableSource.getRootItemIds()) {
                    if (checkIsRowNumberExceed(r)) {
                        break;
                    }
                    r = createHierarchicalRow(treeTable, columns, exportExpanded, r, itemId);
                }
            }
        } else if (table instanceof GroupTable && tableItems instanceof GroupTableItems && ((GroupTableItems<Object>) tableItems).hasGroups()) {
            GroupTableItems<Object> groupTableSource = (GroupTableItems<Object>) tableItems;
            for (Object item : groupTableSource.rootGroups()) {
                if (checkIsRowNumberExceed(r)) {
                    break;
                }
                r = createGroupRow((GroupTable<Object>) table, columns, ++r, (GroupInfo<?>) item, 0);
            }
        } else {
            if (tableItems != null) {
                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() * COL_WIDTH_MAGIC);
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        wb.write(out);
    } catch (IOException e) {
        throw new RuntimeException("Unable to write document", e);
    }
    if (isXlsMaxRowNumberExceeded()) {
        Notifications notifications = ComponentsHelper.getScreenContext(table).getNotifications();
        notifications.create(Notifications.NotificationType.WARNING).withCaption(messages.getMessage("actions.warningExport.title")).withDescription(messages.getMessage("actions.warningExport.message")).show();
    }
    ByteArrayDataProvider dataProvider = new ByteArrayDataProvider(out.toByteArray(), uiProperties.getSaveExportedByteArrayDataThresholdBytes(), coreProperties.getTempDir());
    switch(exportFormat) {
        case XLSX:
            downloader.download(dataProvider, getFileName(table) + ".xlsx", XLSX);
            break;
        case XLS:
            downloader.download(dataProvider, getFileName(table) + ".xls", XLS);
            break;
    }
}
Also used : Id(io.jmix.core.Id) java.util(java.util) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) EntityDataGridItems(io.jmix.ui.component.data.meta.EntityDataGridItems) ByteArrayDataProvider(io.jmix.ui.download.ByteArrayDataProvider) BooleanUtils(org.apache.commons.lang3.BooleanUtils) ExportMode(io.jmix.uiexport.exporter.ExportMode) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) XLS(io.jmix.ui.download.DownloadFormat.XLS) EntityValues(io.jmix.core.entity.EntityValues) Scope(org.springframework.context.annotation.Scope) Notifications(io.jmix.ui.Notifications) org.apache.poi.ss.usermodel(org.apache.poi.ss.usermodel) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) io.jmix.ui.component.data(io.jmix.ui.component.data) InstanceContainer(io.jmix.ui.model.InstanceContainer) XLSX(io.jmix.ui.download.DownloadFormat.XLSX) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Range(io.jmix.core.metamodel.model.Range) ParseException(java.text.ParseException) Nullable(javax.annotation.Nullable) AbstractTableExporter(io.jmix.uiexport.exporter.AbstractTableExporter) IOException(java.io.IOException) Datatype(io.jmix.core.metamodel.datatype.Datatype) Collectors(java.util.stream.Collectors) Table(io.jmix.ui.component.Table) ExportAction(io.jmix.uiexport.action.ExportAction) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) Component(org.springframework.stereotype.Component) Entity(io.jmix.core.Entity) Downloader(io.jmix.ui.download.Downloader) Element(org.dom4j.Element) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) io.jmix.ui.component(io.jmix.ui.component) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) ByteArrayDataProvider(io.jmix.ui.download.ByteArrayDataProvider) Table(io.jmix.ui.component.Table) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) Notifications(io.jmix.ui.Notifications)

Example 17 with Table

use of io.jmix.ui.component.Table in project jmix by jmix-framework.

the class AbbreviatedCellClickListener method accept.

@Override
public void accept(Table.Column.ClickEvent clickEvent) {
    if (!clickEvent.isText() || clickEvent.getSource().getMaxTextLength() == null) {
        return;
    }
    Table.Column<?> column = clickEvent.getSource();
    Table owner = column.getOwner();
    if (owner == null || owner.getFrame() == null) {
        return;
    }
    Object rowItem = clickEvent.getItem();
    MetaPropertyPath mpp = column.getMetaPropertyPathNN();
    Object itemValue = EntityValues.getValueEx(rowItem, mpp);
    String stringItemValue = metadataTools.format(itemValue, mpp.getMetaProperty());
    boolean isMultiLineCell = StringUtils.contains(stringItemValue, "\n");
    if (StringUtils.isEmpty(stringItemValue) || (stringItemValue.length() <= column.getMaxTextLength() + MAX_TEXT_LENGTH_GAP && !isMultiLineCell)) {
        return;
    }
    VerticalLayout layout = createRootLayout();
    JmixTextArea textArea = createTextArea(stringItemValue);
    layout.addComponent(createContent(textArea));
    owner.withUnwrapped(JmixEnhancedTable.class, enhancedTable -> {
        enhancedTable.showCustomPopup(layout);
        enhancedTable.setCustomPopupAutoClose(false);
    });
}
Also used : JmixTextArea(io.jmix.ui.widget.JmixTextArea) JmixEnhancedTable(io.jmix.ui.widget.JmixEnhancedTable) Table(io.jmix.ui.component.Table) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) VerticalLayout(com.vaadin.ui.VerticalLayout)

Aggregations

Table (io.jmix.ui.component.Table)16 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)13 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)5 Element (org.dom4j.Element)5 MetaClass (io.jmix.core.metamodel.model.MetaClass)4 JmixEnhancedTable (io.jmix.ui.widget.JmixEnhancedTable)4 Entity (io.jmix.core.Entity)3 GuiDevelopmentException (io.jmix.ui.GuiDevelopmentException)3 io.jmix.ui.component (io.jmix.ui.component)3 TableSettings (io.jmix.ui.settings.component.TableSettings)3 Messages (io.jmix.core.Messages)2 Metadata (io.jmix.core.Metadata)2 Action (io.jmix.ui.action.Action)2 BaseAction (io.jmix.ui.action.BaseAction)2 ScreenContext (io.jmix.ui.screen.ScreenContext)2 Strings (com.google.common.base.Strings)1 ItemTrackingAction (com.haulmont.cuba.gui.components.actions.ItemTrackingAction)1 LegacyFrame (com.haulmont.cuba.gui.screen.compatibility.LegacyFrame)1 KeyCode (com.vaadin.event.ShortcutAction.KeyCode)1 Resource (com.vaadin.server.Resource)1