|
21 | 21 |
|
22 | 22 | import com.flowingcode.vaadin.addons.fontawesome.FontAwesome;
|
23 | 23 | import com.flowingcode.vaadin.addons.gridhelpers.GridHelper;
|
| 24 | +import com.vaadin.flow.component.Component; |
24 | 25 | import com.vaadin.flow.component.ComponentUtil;
|
| 26 | +import com.vaadin.flow.component.HasEnabled; |
25 | 27 | import com.vaadin.flow.component.grid.ColumnPathRenderer;
|
26 | 28 | import com.vaadin.flow.component.grid.Grid;
|
27 | 29 | import com.vaadin.flow.component.grid.Grid.Column;
|
|
50 | 52 | import java.util.HashMap;
|
51 | 53 | import java.util.List;
|
52 | 54 | import java.util.Map;
|
| 55 | +import java.util.Objects; |
53 | 56 | import java.util.Optional;
|
54 | 57 | import java.util.concurrent.CopyOnWriteArrayList;
|
55 | 58 | import java.util.concurrent.TimeUnit;
|
@@ -148,21 +151,25 @@ public static <T> GridExporter<T> createFor(
|
148 | 151 | if (exporter.autoAttachExportButtons) {
|
149 | 152 | if (exporter.isExcelExportEnabled()) {
|
150 | 153 | Anchor excelLink = new Anchor("", FontAwesome.Regular.FILE_EXCEL.create());
|
151 |
| - excelLink.setHref(exporter.getExcelStreamResource(excelCustomTemplate)); |
| 154 | + excelLink |
| 155 | + .setHref(exporter.getExcelStreamResource(excelCustomTemplate) |
| 156 | + .forComponent(excelLink)); |
152 | 157 | excelLink.getElement().setAttribute("download", true);
|
153 | 158 | footerToolbar.add(
|
154 | 159 | new FooterToolbarItem(excelLink, FooterToolbarItemPosition.EXPORT_BUTTON));
|
155 | 160 | }
|
156 | 161 | if (exporter.isDocxExportEnabled()) {
|
157 | 162 | Anchor docLink = new Anchor("", FontAwesome.Regular.FILE_WORD.create());
|
158 |
| - docLink.setHref(exporter.getDocxStreamResource(docxCustomTemplate)); |
| 163 | + docLink.setHref( |
| 164 | + exporter.getDocxStreamResource(docxCustomTemplate).forComponent(docLink)); |
159 | 165 | docLink.getElement().setAttribute("download", true);
|
160 | 166 | footerToolbar
|
161 | 167 | .add(new FooterToolbarItem(docLink, FooterToolbarItemPosition.EXPORT_BUTTON));
|
162 | 168 | }
|
163 | 169 | if (exporter.isPdfExportEnabled()) {
|
164 | 170 | Anchor docLink = new Anchor("", FontAwesome.Regular.FILE_PDF.create());
|
165 |
| - docLink.setHref(exporter.getPdfStreamResource(docxCustomTemplate)); |
| 171 | + docLink.setHref( |
| 172 | + exporter.getPdfStreamResource(docxCustomTemplate).forComponent(docLink)); |
166 | 173 | docLink.getElement().setAttribute("download", true);
|
167 | 174 | footerToolbar
|
168 | 175 | .add(new FooterToolbarItem(docLink, FooterToolbarItemPosition.EXPORT_BUTTON));
|
@@ -286,63 +293,101 @@ else if (r.getValueProviders().containsKey("name")) {
|
286 | 293 | return value;
|
287 | 294 | }
|
288 | 295 |
|
289 |
| - public StreamResource getDocxStreamResource() { |
| 296 | + public GridExporterStreamResource getDocxStreamResource() { |
290 | 297 | return getDocxStreamResource(null);
|
291 | 298 | }
|
292 | 299 |
|
293 |
| - public StreamResource getDocxStreamResource(String template) { |
294 |
| - return new StreamResource(fileName + ".docx", |
| 300 | + public GridExporterStreamResource getDocxStreamResource(String template) { |
| 301 | + return new GridExporterStreamResource(fileName + ".docx", |
295 | 302 | makeConcurrentWriter(new DocxStreamResourceWriter<>(this, template)));
|
296 | 303 | }
|
297 | 304 |
|
298 |
| - public StreamResource getPdfStreamResource() { |
| 305 | + public GridExporterStreamResource getPdfStreamResource() { |
299 | 306 | return getPdfStreamResource(null);
|
300 | 307 | }
|
301 | 308 |
|
302 |
| - public StreamResource getPdfStreamResource(String template) { |
303 |
| - return new StreamResource(fileName + ".pdf", |
| 309 | + public GridExporterStreamResource getPdfStreamResource(String template) { |
| 310 | + return new GridExporterStreamResource(fileName + ".pdf", |
304 | 311 | makeConcurrentWriter(new PdfStreamResourceWriter<>(this, template)));
|
305 | 312 | }
|
306 | 313 |
|
307 | 314 | public StreamResource getCsvStreamResource() {
|
308 | 315 | return new StreamResource(fileName + ".csv", new CsvStreamResourceWriter<>(this));
|
309 | 316 | }
|
310 | 317 |
|
311 |
| - public StreamResource getExcelStreamResource() { |
| 318 | + public GridExporterStreamResource getExcelStreamResource() { |
312 | 319 | return getExcelStreamResource(null);
|
313 | 320 | }
|
314 | 321 |
|
315 |
| - public StreamResource getExcelStreamResource(String template) { |
316 |
| - return new StreamResource(fileName + ".xlsx", |
| 322 | + public GridExporterStreamResource getExcelStreamResource(String template) { |
| 323 | + return new GridExporterStreamResource(fileName + ".xlsx", |
317 | 324 | makeConcurrentWriter(new ExcelStreamResourceWriter<>(this, template)));
|
318 | 325 | }
|
319 | 326 |
|
320 |
| - private StreamResourceWriter makeConcurrentWriter(StreamResourceWriter writer) { |
321 |
| - return new ConcurrentStreamResourceWriter(writer) { |
322 |
| - @Override |
323 |
| - public float getCost(VaadinSession session) { |
324 |
| - return concurrentDownloadCost; |
325 |
| - } |
| 327 | + private GridExporterConcurrentStreamResourceWriter makeConcurrentWriter( |
| 328 | + StreamResourceWriter writer) { |
| 329 | + return new GridExporterConcurrentStreamResourceWriter(writer); |
| 330 | + } |
326 | 331 |
|
327 |
| - @Override |
328 |
| - public long getTimeout() { |
329 |
| - // It would have been possible to specify a different timeout for each instance but I cannot |
330 |
| - // figure out a good use case for that. The timeout returned herebecomes relevant when the |
331 |
| - // semaphore has been acquired by any other download, so the timeout must reflect how long |
332 |
| - // it is reasonable to wait for "any other download" to complete and release the semaphore. |
333 |
| - // |
334 |
| - // Since the reasonable timeout would depend on the duration of "any other download", it |
335 |
| - // makes sense that it's a global setting instead of a per-instance setting. |
336 |
| - return concurrentDownloadTimeoutNanos; |
337 |
| - } |
| 332 | + public class GridExporterStreamResource extends StreamResource { |
| 333 | + private final GridExporterConcurrentStreamResourceWriter writer; |
338 | 334 |
|
339 |
| - @Override |
340 |
| - protected void onTimeout() { |
341 |
| - fireConcurrentDownloadTimeout(); |
342 |
| - } |
| 335 | + GridExporterStreamResource(String name, GridExporterConcurrentStreamResourceWriter writer) { |
| 336 | + super(name, writer); |
| 337 | + this.writer = Objects.requireNonNull(writer); |
| 338 | + } |
| 339 | + |
| 340 | + public GridExporterStreamResource forComponent(Component component) { |
| 341 | + writer.button = component; |
| 342 | + return this; |
| 343 | + } |
| 344 | + } |
| 345 | + |
| 346 | + private class GridExporterConcurrentStreamResourceWriter extends ConcurrentStreamResourceWriter { |
343 | 347 |
|
| 348 | + GridExporterConcurrentStreamResourceWriter(StreamResourceWriter delegate) { |
| 349 | + super(delegate); |
| 350 | + } |
| 351 | + |
| 352 | + private Component button; |
344 | 353 |
|
345 |
| - }; |
| 354 | + @Override |
| 355 | + public float getCost(VaadinSession session) { |
| 356 | + return concurrentDownloadCost; |
| 357 | + } |
| 358 | + |
| 359 | + @Override |
| 360 | + public long getTimeout() { |
| 361 | + // It would have been possible to specify a different timeout for each instance but I cannot |
| 362 | + // figure out a good use case for that. The timeout returned herebecomes relevant when the |
| 363 | + // semaphore has been acquired by any other download, so the timeout must reflect how long |
| 364 | + // it is reasonable to wait for "any other download" to complete and release the semaphore. |
| 365 | + // |
| 366 | + // Since the reasonable timeout would depend on the duration of "any other download", it |
| 367 | + // makes sense that it's a global setting instead of a per-instance setting. |
| 368 | + return concurrentDownloadTimeoutNanos; |
| 369 | + } |
| 370 | + |
| 371 | + @Override |
| 372 | + protected void onTimeout() { |
| 373 | + fireConcurrentDownloadTimeout(); |
| 374 | + } |
| 375 | + |
| 376 | + @Override |
| 377 | + protected void onAccept() { |
| 378 | + setButtonEnabled(false); |
| 379 | + } |
| 380 | + |
| 381 | + @Override |
| 382 | + protected void onFinish() { |
| 383 | + setButtonEnabled(true); |
| 384 | + } |
| 385 | + |
| 386 | + private void setButtonEnabled(boolean enabled) { |
| 387 | + if (button instanceof HasEnabled) { |
| 388 | + grid.getUI().ifPresent(ui -> ui.access(() -> ((HasEnabled) button).setEnabled(enabled))); |
| 389 | + } |
| 390 | + } |
346 | 391 | }
|
347 | 392 |
|
348 | 393 | /**
|
|
0 commit comments