Skip to content

feat: add support for dynamic filename #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.vaadin.addons.flowingcode</groupId>
<artifactId>grid-exporter-addon</artifactId>
<version>2.4.1-SNAPSHOT</version>
<version>2.5.0-SNAPSHOT</version>
<name>Grid Exporter Add-on</name>
<description>Grid Exporter Add-on for Vaadin Flow</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class GridExporter<T> implements Serializable {

String title = "Grid Export";

String fileName = "export";
private SerializableSupplier<String> fileNameSupplier = () -> "export";

int sheetNumber = 0;

Expand Down Expand Up @@ -298,7 +298,7 @@ public GridExporterStreamResource getDocxStreamResource() {
}

public GridExporterStreamResource getDocxStreamResource(String template) {
return new GridExporterStreamResource(fileName + ".docx",
return new GridExporterStreamResource(getFileName("docx"),
makeConcurrentWriter(new DocxStreamResourceWriter<>(this, template)));
}

Expand All @@ -307,20 +307,20 @@ public GridExporterStreamResource getPdfStreamResource() {
}

public GridExporterStreamResource getPdfStreamResource(String template) {
return new GridExporterStreamResource(fileName + ".pdf",
return new GridExporterStreamResource(getFileName("pdf"),
makeConcurrentWriter(new PdfStreamResourceWriter<>(this, template)));
}

public StreamResource getCsvStreamResource() {
return new StreamResource(fileName + ".csv", new CsvStreamResourceWriter<>(this));
return new StreamResource(getFileName("csv"), new CsvStreamResourceWriter<>(this));
}

public GridExporterStreamResource getExcelStreamResource() {
return getExcelStreamResource(null);
}

public GridExporterStreamResource getExcelStreamResource(String template) {
return new GridExporterStreamResource(fileName + ".xlsx",
return new GridExporterStreamResource(getFileName("xlsx"),
makeConcurrentWriter(new ExcelStreamResourceWriter<>(this, template)));
}

Expand Down Expand Up @@ -490,7 +490,11 @@ public void setTitle(String title) {
}

public String getFileName() {
return fileName;
return fileNameSupplier.get();
}

private String getFileName(String extension) {
return Objects.requireNonNull(getFileName()) + "." + extension;
}

/**
Expand All @@ -499,7 +503,19 @@ public String getFileName() {
* @param fileName
*/
public void setFileName(String fileName) {
this.fileName = fileName;
Objects.requireNonNull(fileName, "File name cannot be null");
fileNameSupplier = () -> fileName;
}

/**
* Sets a dynamic filename for the exported file.
*
* @param fileNameSupplier a supplier that returns the name of the exported file, without
* extension.
*/
public void setFileName(SerializableSupplier<String> fileNameSupplier) {
this.fileNameSupplier =
Objects.requireNonNull(fileNameSupplier, "File name supplier cannot be null");
}

public boolean isAutoAttachExportButtons() {
Expand Down
Loading