Skip to content

Commit

Permalink
feat: add support for dynamic filename
Browse files Browse the repository at this point in the history
Close #113
  • Loading branch information
javier-godoy committed Sep 26, 2024
1 parent 7ac1962 commit 9ab519b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
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

0 comments on commit 9ab519b

Please sign in to comment.