org.bklab.export.grid.GridColumnDataBuilderFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluent-vaadin-flow Show documentation
Show all versions of fluent-vaadin-flow Show documentation
Broderick Labs for fluent vaadin flow. Inherits common Vaadin components.
package org.bklab.export.grid;
import com.vaadin.flow.component.HasText;
import com.vaadin.flow.component.grid.ColumnTextAlign;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.data.renderer.ComponentRenderer;
import com.vaadin.flow.data.renderer.Renderer;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.bklab.export.data.ColumnDataBuilder;
import java.util.*;
public class GridColumnDataBuilderFactory {
private final Grid grid;
private final Map, String> headerMap = new LinkedHashMap<>();
private ColumnDataBuilder builder;
public GridColumnDataBuilderFactory(Grid grid) {
this.grid = grid;
Map headerNameMap = grid instanceof HasColumnHeaderMapping
? ((HasColumnHeaderMapping) grid).getHeaderMapping() : Collections.emptyMap();
List> columns = grid.getColumns();
for (int i = 0; i < columns.size(); i++) {
Grid.Column c = columns.get(i);
headerMap.put(c, Optional.ofNullable(headerNameMap.getOrDefault(
c.getKey(), c.getKey())).orElse("列" + (i + 1)));
}
}
private HorizontalAlignment convertColumnTextAlign(ColumnTextAlign align) {
if (align == null) return HorizontalAlignment.CENTER;
return switch (align) {
case START -> HorizontalAlignment.LEFT;
case CENTER -> HorizontalAlignment.CENTER;
case END -> HorizontalAlignment.RIGHT;
};
}
public GridColumnDataBuilderFactory headersFromColumn(Map, String> headerMap) {
this.headerMap.putAll(headerMap);
return this;
}
public GridColumnDataBuilderFactory headers(Map headerMap) {
headerMap.forEach((k, v) -> Optional.ofNullable(
grid.getColumnByKey(k)
).ifPresent(c -> this.headerMap.put(c, v)));
return this;
}
public GridColumnDataBuilderFactory removeColumns(String... keys) {
for (String key : keys) {
headerMap.remove(grid.getColumnByKey(key));
}
return this;
}
public GridColumnDataBuilderFactory removeColumns(int... columns) {
ArrayList> allColumns = new ArrayList<>(headerMap.keySet());
Arrays.stream(columns).filter(column -> column < allColumns.size())
.mapToObj(allColumns::get).filter(Objects::nonNull).forEach(headerMap::remove);
return this;
}
@SafeVarargs
public final GridColumnDataBuilderFactory removeColumns(Grid.Column... columns) {
for (Grid.Column column : columns) {
headerMap.remove(column);
}
return this;
}
public ColumnDataBuilder getBuilder() {
if (builder == null) this.builder = createBuilder();
return builder;
}
public ColumnDataBuilder createBuilder() {
ColumnDataBuilder builder = new ColumnDataBuilder<>();
headerMap.forEach((key, value) -> addColumn(builder, value, key));
builder.header(headerMap.values());
return builder;
}
private void addColumn(ColumnDataBuilder builder, String name, Grid.Column column) {
builder.add(name, entity -> {
Renderer renderer = column.getRenderer();
if (renderer instanceof ComponentRenderer) {
return getString(((ComponentRenderer, T>) renderer).createComponent(entity));
}
List strings = new ArrayList<>();
renderer.getValueProviders().forEach((k, v) -> {
String apply = Optional.ofNullable(getString(v.apply(entity))).map(String::strip).orElse("");
if (apply.isBlank() || "null".equalsIgnoreCase(apply)) return;
strings.add(apply);
});
return String.join("\n", strings);
});
builder.align(name, convertColumnTextAlign(column.getTextAlign()));
}
private String getString(Object o) {
if (o == null) return "";
if (o instanceof String) return (String) o;
if (o instanceof HasText) return ((HasText) o).getText();
return String.valueOf(o);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy