Giter VIP home page Giter VIP logo

vaadin-lazy-download-button's People

Contributors

stefanuebe avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

vaadin-lazy-download-button's Issues

Add support for error handling

If createInputStream fails, it's not possible to intercept the exception and handle it.
Moreover, if setDisableOnClick is set, the button will remain disabled (because no DownloadStartsEvent will occur).

Executors.newSingleThreadExecutor().execute(() -> {
try {
InputStream inputStream = inputStreamCallback.createInputStream();
optionalUI.ifPresent(ui -> ui.access(() -> {
StreamResource href = new StreamResource(fileNameCallback.get(), () -> inputStream);
href.setCacheTime(0);
anchor.setHref(href);
anchor.getElement().callJsFunction("click");
}));
} catch (Exception e) {
throw new RuntimeException(e);
}
});

As a workaround, one need to write error handling logic inside of the InputStreamFactory:

btnDownload = new LazyDownloadButton("Download",
	() -> "foo.pdf",
	() -> {
		try {
			return new ByteArrayInputStream(generateFile());
		} catch (RuntimeException e) {
			btnDownload.getUI().ifPresent(ui->ui.access(()->{
				// restore normal state, so that the user can download the content again
				btnDownload.setText("Download");
				btnDownload.setEnabled(true);
				Notification.show("Download failed").addThemeVariants(NotificationVariant.LUMO_ERROR);
			}));
			throw e;
		}
	});

Setting input stream after download button creation

Hello.

Download button working super great when you set filename callback and inputstream callback and constructor.

But when i set filename and inputstream callback after the button was created, the download does not occur.

Example:

 private final LazyDownloadButton downloadButton = new LazyDownloadButton(VaadinIcon.FILE_ZIP.create());
 downloadButton.setFileNameCallback(() -> filename);
 downloadButton.setInputStreamCallback(() -> new ByteArrayInputStream(byte array content));

Reason: looks like the constructor with only ICON does not add click listener for downloading

Update2 ():
1)

   private LazyDownloadButton downloadButton = new LazyDownloadButton(VaadinIcon.FILE_ZIP.create(), null, null);
 downloadButton.setFileNameCallback(() -> filename);
 downloadButton.setInputStreamCallback(() -> new ByteArrayInputStream(byte array content));

Error:

java.lang.NullPointerException: File name callback must not be null
	at java.util.Objects.requireNonNull(Objects.java:233) ~[?:?]
	at org.vaadin.stefan.LazyDownloadButton.lambda$null$3(LazyDownloadButton.java:100) ~[lazy-download-button-1.0.0.jar:1.0.0]

Vaadin 23.2.3, 23.1.10
Java 17

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.