Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapster committed Jul 8, 2023
1 parent e651cf1 commit 7b063dd
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1319,12 +1319,4 @@ public void encodeBegin(FacesContext context) throws IOException {
}
}
}

public boolean isColumnGroupLegacyEnabled() {
return (boolean) getStateHelper().eval(PropertyKeys.columnGroupLegacyEnabled, true);
}

public void setColumnGroupLegacyEnabled(boolean columnGroupLegacyEnabled) {
getStateHelper().put(PropertyKeys.columnGroupLegacyEnabled, columnGroupLegacyEnabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,10 @@ default boolean hasFooterColumn() {

List<UIComponent> getChildren();

default boolean isColumnGroupLegacyEnabled() {
return true;
}

static void treeColumnsTo2DArray(ColumnNode root, List<List<ColumnNode>> nodes, int columnStart, int columnEnd) {
int idx = -1;
for (int i = 0; i < root.getChildren().size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package org.primefaces.component.datatable;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.el.ELContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.WorkbookUtil;
import org.primefaces.component.api.ColumnNode;
import org.primefaces.component.api.UIColumn;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.component.export.ExcelOptions;
Expand Down Expand Up @@ -112,8 +113,8 @@ protected void exportCellValue(FacesContext context, DataTable table, UIColumn c
}

@Override
protected void exportColumnGroupFacetValue(FacesContext context, DataTable table, UIColumn column,
AtomicInteger colIndex, String text) {
protected void exportColumnGroupFacetValueLegacy(FacesContext context, DataTable table, UIColumn column,
AtomicInteger colIndex, String text) {
Sheet sheet = sheet();
int rowIndex = sheet.getLastRowNum();

Expand Down Expand Up @@ -158,6 +159,27 @@ else if (colSpan > 0) {
}
}

@Override
protected void exportColumnGroupFacetValue(FacesContext context, DataTable table, ColumnNode column, int rowspan, int colspan, String text) {
Sheet sheet = sheet();
int rowIndex = sheet.getLastRowNum();
Row row = row();

int relColIndex = row.getLastCellNum() > 0 ? row.getLastCellNum() : row.getLastCellNum() + 1;
int colIndex = calculateColumnOffset(sheet, rowIndex, relColIndex); // (1-based)

if (colspan > 1 || rowspan > 1) {
sheet().addMergedRegion(new CellRangeAddress(
rowIndex, // first row (0-based)
(rowIndex + rowspan) - 1, // last row (0-based)
colIndex, // first column (0-based)
(colIndex + colspan) - 1) // last column (0-based)
);
}

exportColumnFacetValue(context, table, text, colIndex);
}

@Override
public String getContentType() {
return "application/vnd.ms-excel";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import org.primefaces.component.api.ColumnNode;
import org.primefaces.component.api.UIColumn;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.component.export.ExporterOptions;
Expand Down Expand Up @@ -121,13 +122,18 @@ protected void exportColumnFacetValue(FacesContext context, DataTable table, Str
}

@Override
protected void exportColumnGroupFacetValue(FacesContext context, DataTable table, UIColumn column,
AtomicInteger colIndex, String text) {
protected void exportColumnGroupFacetValueLegacy(FacesContext context, DataTable table, UIColumn column,
AtomicInteger colIndex, String text) {
int rowSpan = column.getExportRowspan() != 0 ? column.getExportRowspan() : column.getRowspan();
int colSpan = column.getExportColspan() != 0 ? column.getExportColspan() : column.getColspan();
addFacetValue(rowSpan, colSpan, text);
}

@Override
protected void exportColumnGroupFacetValue(FacesContext context, DataTable table, ColumnNode column, int rowspan, int colspan, String text) {
addFacetValue(rowspan, colspan, text);
}

@Override
protected void exportCellValue(FacesContext context, DataTable table, UIColumn col, String text, int index) {
PdfPCell cell = createCell(col, new Paragraph(text, cellFont));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.primefaces.component.api.UIColumn;
import org.primefaces.component.api.UITable;
import org.primefaces.component.celleditor.CellEditor;
import org.primefaces.component.columngroup.ColumnGroup;
import org.primefaces.component.overlaypanel.OverlayPanel;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.Constants;
Expand Down Expand Up @@ -202,6 +203,20 @@ else if (columnType == TableExporter.ColumnType.FOOTER) {
return Objects.toString(textValue, Constants.EMPTY_STRING);
}

public static String getColumnFacetValue(FacesContext context, Object component, TableExporter.ColumnType columnType) {
if (component instanceof UIColumn) {
return getColumnFacetValue(context, (UIColumn) component, columnType);
}
else if (component instanceof ColumnGroup) {
ColumnGroup group = (ColumnGroup) component;
if (TableExporter.ColumnType.HEADER == columnType) {
return group.getHeaderText();
}
}

return Constants.EMPTY_STRING;
}

public static String getColumnExportTag(FacesContext context, UIColumn column) {
// lowerCase really? camelCase at best
String columnTag = column.getExportTag();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@
import javax.faces.component.visit.VisitResult;
import javax.faces.context.FacesContext;

import org.primefaces.component.api.DynamicColumn;
import org.primefaces.component.api.UIColumn;
import org.primefaces.component.api.UITable;
import org.primefaces.component.api.*;
import org.primefaces.component.columngroup.ColumnGroup;
import org.primefaces.model.ColumnMeta;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.IOUtils;
import org.primefaces.util.LangUtils;

Expand Down Expand Up @@ -128,8 +127,16 @@ protected void exportTable(FacesContext context, T table, int index) throws IOEx

if (exportConfiguration.isExportHeader()) {
addTableFacets(context, table, ColumnType.HEADER);
boolean headerGroup = addColumnGroupFacets(context, table, ColumnType.HEADER);
if (!headerGroup) {
if (table.isColumnGroupLegacyEnabled()) {
boolean headerGroup = addColumnGroupFacetsLegacy(context, table, ColumnType.HEADER);
if (!headerGroup) {
addColumnFacets(context, table, ColumnType.HEADER);
}
}
else if (ComponentUtils.hasChildOfType(table, ColumnGroup.class)) {
addColumnGroupFacets(context, table, ColumnType.HEADER);
}
else {
addColumnFacets(context, table, ColumnType.HEADER);
}
}
Expand Down Expand Up @@ -179,7 +186,7 @@ protected void addColumnFacets(FacesContext context, T table, ColumnType columnT
});
}

protected boolean addColumnGroupFacets(FacesContext context, T table, ColumnType columnType) {
protected boolean addColumnGroupFacetsLegacy(FacesContext context, T table, ColumnType columnType) {
if (!supportedFacetTypes.contains(FacetType.COLUMN_GROUP)) {
return false;
}
Expand All @@ -201,7 +208,7 @@ protected boolean addColumnGroupFacets(FacesContext context, T table, ColumnType
table,
colIndex.get(),
total,
() -> exportColumnGroupFacetValue(context, table, column, colIndex, text));
() -> exportColumnGroupFacetValueLegacy(context, table, column, colIndex, text));

colIndex.incrementAndGet();
}
Expand All @@ -212,6 +219,31 @@ protected boolean addColumnGroupFacets(FacesContext context, T table, ColumnType
return true;
}

protected void addColumnGroupFacets(FacesContext context, T table, ColumnType columnType) {
List<List<ColumnNode>> matrix = new ArrayList<>();
ColumnNode root = ColumnNode.root(table);
UITable.treeColumnsTo2DArray(root, matrix, 0, getExportableColumns(table).size());

int depth = matrix.size();
for (List<ColumnNode> rows : matrix) {
for (int colIndex = 0; colIndex < rows.size(); colIndex++) {
ColumnNode node = rows.get(colIndex);
String text = ExporterUtils.getColumnFacetValue(context, node.getUiComp(), columnType);

int colSpan = node.getColspan();
int rowSpan = node.getUiComp() instanceof UIColumn
? (depth - node.getLevel()) + 1
: 1;

proxifyWithRowExport(context,
table,
colIndex,
rows.size(),
() -> exportColumnGroupFacetValue(context, table, node, rowSpan, colSpan, text));
}
}
}

protected void addCells(FacesContext context, T table) {
addRow(context, table, (col, i) ->
exportCellValue(context, table, col, ExporterUtils.getColumnValue(context, table, col, cellJoinComponents), i)
Expand All @@ -230,7 +262,15 @@ protected void exportColumnFacetValue(FacesContext context, T table, String text
}
}

protected void exportColumnGroupFacetValue(FacesContext context, T table, UIColumn column, AtomicInteger colIndex, String text) {
@Deprecated
protected void exportColumnGroupFacetValueLegacy(FacesContext context, T table, UIColumn column, AtomicInteger colIndex, String text) {
if (supportedFacetTypes.contains(FacetType.COLUMN_GROUP)) {
throw new UnsupportedOperationException(getClass().getName() + "#exportColumnGroupFacetValueLegacy() must be implemented");
}
}

protected void exportColumnGroupFacetValue(FacesContext context, T table, ColumnNode column,
int rowspan, int colspan, String text) {
if (supportedFacetTypes.contains(FacetType.COLUMN_GROUP)) {
throw new UnsupportedOperationException(getClass().getName() + "#exportColumnGroupFacetValue() must be implemented");
}
Expand Down Expand Up @@ -339,10 +379,37 @@ public void export(FacesContext context) {
* @return the List<UIColumn> that are exportable
*/
protected List<UIColumn> getExportableColumns(T table) {
if (exportableColumns.containsKey(table)) {
return exportableColumns.get(table);
}
return exportableColumns.computeIfAbsent(table, dt -> {

boolean colGroupLegacy = dt.isColumnGroupLegacyEnabled();
if (!colGroupLegacy) {
Map<UIColumn, Integer> columnMeta = new LinkedHashMap<>();
ForEachRowColumn
.from(dt)
.hints(ForEachRowColumn.ColumnHint.RENDERED, ForEachRowColumn.ColumnHint.EXPORTABLE, ForEachRowColumn.ColumnHint.VISIBLE)
.invoke(new RowColumnVisitor.Adapter() {

@Override
public void visitColumn(int index, UIColumn column) {
int displayPriority = column.getDisplayPriority();
columnMeta.put(column, displayPriority);
}
});

return columnMeta.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
else {
return getExportableColumnsLegacy(dt);
}
});
}

@Deprecated
protected List<UIColumn> getExportableColumnsLegacy(UITable table) {
int allColumnsSize = table.getColumns().size();
List<UIColumn> exportcolumns = new ArrayList<>(allColumnsSize);
boolean visibleColumnsOnly = exportConfiguration.isVisibleOnly();
Expand Down Expand Up @@ -378,8 +445,6 @@ protected List<UIColumn> getExportableColumns(T table) {
table.invokeOnColumn(metaColumnKey, -1, exportcolumns::add);
}

exportableColumns.put(table, exportcolumns);

return exportcolumns;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ protected void exportCellValue(FacesContext context, TreeTable table, UIColumn c
}

@Override
protected void exportColumnGroupFacetValue(FacesContext context, TreeTable table, UIColumn column,
AtomicInteger colIndex, String text) {
protected void exportColumnGroupFacetValueLegacy(FacesContext context, TreeTable table, UIColumn column,
AtomicInteger colIndex, String text) {
Sheet sheet = sheet();
int rowIndex = sheet.getLastRowNum();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ protected void exportColumnFacetValue(FacesContext context, TreeTable table, Str
}

@Override
protected void exportColumnGroupFacetValue(FacesContext context, TreeTable table, UIColumn column,
AtomicInteger colIndex, String text) {
protected void exportColumnGroupFacetValueLegacy(FacesContext context, TreeTable table, UIColumn column,
AtomicInteger colIndex, String text) {
int rowSpan = column.getExportRowspan() != 0 ? column.getExportRowspan() : column.getRowspan();
int colSpan = column.getExportColspan() != 0 ? column.getExportColspan() : column.getColspan();
addFacetValue(rowSpan, colSpan, text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
*/
package org.primefaces.util;

import static org.primefaces.renderkit.RendererUtils.getRenderKit;

import java.io.IOException;
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.FacesWrapper;
Expand All @@ -53,6 +53,7 @@
import org.primefaces.config.PrimeConfiguration;
import org.primefaces.context.PrimeApplicationContext;
import org.primefaces.context.PrimeRequestContext;
import static org.primefaces.renderkit.RendererUtils.getRenderKit;
public class ComponentUtils {

public static final Set<VisitHint> VISIT_HINTS_SKIP_UNRENDERED = Collections.unmodifiableSet(
Expand Down

0 comments on commit 7b063dd

Please sign in to comment.