Skip to content

Commit 0b920ec

Browse files
javier-godoypaodb
authored andcommitted
refactor: duplicate method
1 parent e47b1f3 commit 0b920ec

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/main/java/com/flowingcode/vaadin/addons/gridexporter/BaseStreamResourceWriter.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private GridHeader<T> getGridHeader(Grid<T> grid, Column<T> column) {
103103
List<String> headerTexts = new ArrayList<>();
104104
List<HeaderRow> headerRows = grid.getHeaderRows();
105105
for (HeaderRow headerRow : headerRows) {
106-
String headerText = renderCellTextContent(grid, column, GridExporter.COLUMN_HEADER, (col) -> {
106+
String headerText = renderHeaderCellTextContent(grid, column, GridExporter.COLUMN_HEADER, (col) -> {
107107
String value = headerRow.getCell(col).getText();
108108
if (Strings.isBlank(value)) {
109109
Component component = headerRow.getCell(col).getComponent();
@@ -123,13 +123,49 @@ protected List<GridFooter<T>> getGridFooters(Grid<T> grid) {
123123
.map(
124124
column ->
125125
new GridFooter<>(
126-
renderCellTextContent(grid, column, GridExporter.COLUMN_FOOTER, null),column
126+
renderFooterCellTextContent(grid, column, GridExporter.COLUMN_FOOTER, null),column
127127
)
128128
)
129129
.collect(Collectors.toList());
130130
}
131131

132-
private String renderCellTextContent(Grid<T> grid, Column<T> column, String columnType, SerializableFunction<Column<T>,String> obtainCellFunction) {
132+
private String renderHeaderCellTextContent(Grid<T> grid, Column<T> column, String columnType,
133+
SerializableFunction<Column<T>, String> obtainCellFunction) {
134+
String headerOrFooter = (String) ComponentUtil.getData(column, columnType);
135+
if (Strings.isBlank(headerOrFooter)) {
136+
SerializableFunction<Column<?>, Component> getHeaderOrFooterComponent;
137+
if (GridExporter.COLUMN_HEADER.equals(columnType)) {
138+
getHeaderOrFooterComponent = Column::getHeaderComponent;
139+
headerOrFooter = column.getHeaderText();
140+
} else if (GridExporter.COLUMN_FOOTER.equals(columnType)) {
141+
getHeaderOrFooterComponent = Column::getFooterComponent;
142+
headerOrFooter = column.getFooterText();
143+
} else {
144+
throw new IllegalArgumentException();
145+
}
146+
if (Strings.isBlank(headerOrFooter)) {
147+
try {
148+
Component component;
149+
if (obtainCellFunction != null) {
150+
headerOrFooter = obtainCellFunction.apply(column);
151+
} else {
152+
component = getHeaderOrFooterComponent.apply(column);
153+
if (component != null) {
154+
headerOrFooter = component.getElement().getTextRecursively();
155+
}
156+
}
157+
} catch (RuntimeException e) {
158+
throw new IllegalStateException(
159+
"Problem when trying to render header or footer cell text content", e);
160+
}
161+
}
162+
}
163+
164+
return headerOrFooter == null ? "" : headerOrFooter;
165+
}
166+
167+
private String renderFooterCellTextContent(Grid<T> grid, Column<T> column, String columnType,
168+
SerializableFunction<Column<T>, String> obtainCellFunction) {
133169
String headerOrFooter = (String) ComponentUtil.getData(column, columnType);
134170
if (Strings.isBlank(headerOrFooter)) {
135171
SerializableFunction<Column<?>, Component> getHeaderOrFooterComponent;

0 commit comments

Comments
 (0)