Class DynamicFileDownloader
java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.HtmlComponent
com.vaadin.flow.component.HtmlContainer
com.vaadin.flow.component.html.Anchor
org.vaadin.firitin.components.DynamicFileDownloader
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier
,com.vaadin.flow.component.BlurNotifier<com.vaadin.flow.component.html.Anchor>
,com.vaadin.flow.component.DetachNotifier
,com.vaadin.flow.component.Focusable<com.vaadin.flow.component.html.Anchor>
,com.vaadin.flow.component.FocusNotifier<com.vaadin.flow.component.html.Anchor>
,com.vaadin.flow.component.HasAriaLabel
,com.vaadin.flow.component.HasComponents
,com.vaadin.flow.component.HasElement
,com.vaadin.flow.component.HasEnabled
,com.vaadin.flow.component.HasSize
,com.vaadin.flow.component.HasStyle
,com.vaadin.flow.component.HasText
,com.vaadin.flow.component.shared.HasTooltip
,Serializable
,FluentAttachNotifier<DynamicFileDownloader>
,FluentComponent<DynamicFileDownloader>
,FluentDetachNotifier<DynamicFileDownloader>
,FluentHasEnabled<DynamicFileDownloader>
,FluentHasTooltip<DynamicFileDownloader>
public class DynamicFileDownloader
extends com.vaadin.flow.component.html.Anchor
implements FluentComponent<DynamicFileDownloader>, FluentHasEnabled<DynamicFileDownloader>, FluentHasTooltip<DynamicFileDownloader>
An anchor which links to a file whose content is produced dynamically.
- Author:
- mstahv
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Generates the content of HTTP response header 'Content-Type'.static interface
Writes the content of the downloaded file to the given output stream.static class
Event fired when the file download fails.static class
Event fired when the file download has been streamed to the client.static class
Event fired when the file download has been streamed to the client.static interface
Generates name dynamically per request.Nested classes/interfaces inherited from interface com.vaadin.flow.component.BlurNotifier
com.vaadin.flow.component.BlurNotifier.BlurEvent<C extends com.vaadin.flow.component.Component>
Nested classes/interfaces inherited from interface com.vaadin.flow.component.FocusNotifier
com.vaadin.flow.component.FocusNotifier.FocusEvent<C extends com.vaadin.flow.component.Component>
Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasText
com.vaadin.flow.component.HasText.WhiteSpace
-
Field Summary
Modifier and TypeFieldDescriptionprotected com.vaadin.flow.server.RequestHandler
The request handler that handles the download request. -
Constructor Summary
ConstructorDescriptionEmpty constructor file downloader.DynamicFileDownloader
(com.vaadin.flow.component.Component downloadComponent, String fileName, DynamicFileDownloader.ContentWriter contentWriter) Constructs a download link with given component as the content that ignites the download.DynamicFileDownloader
(com.vaadin.flow.component.Component downloadComponent, DynamicFileDownloader.ContentWriter contentWriter) Constructs a download link with given component as the content that ignites the download.DynamicFileDownloader
(String linkText, String fileName, DynamicFileDownloader.ContentWriter contentWriter) Constructs a new download link with given text, static file name and writer.DynamicFileDownloader
(String linkText, DynamicFileDownloader.ContentWriter contentWriter) Constructs a new download link with given text, static file name and writer.Constructs a basic download link with DOWNLOAD icon fromVaadinIcon
as the "text" and default file name. -
Method Summary
Modifier and TypeMethodDescriptioncom.vaadin.flow.shared.Registration
addDownloadFailedListener
(com.vaadin.flow.component.ComponentEventListener<DynamicFileDownloader.DownloadFailedEvent> listener) Adds a listener that is executed when the file content streaming has failed due to an exception.com.vaadin.flow.shared.Registration
addDownloadFinishedListener
(com.vaadin.flow.component.ComponentEventListener<DynamicFileDownloader.DownloadFinishedEvent> listener) Adds a listener that is executed when the file content has been streamed.com.vaadin.flow.shared.Registration
addDownloadStartedListener
(com.vaadin.flow.component.ComponentEventListener<DynamicFileDownloader.DownloadStartedEvent> listener) Adds a listener that is executed when the file content streaming has started.asButton()
Makes the download look like a button instead of a normal link.protected void
ensurePollingOrPush
(com.vaadin.flow.component.AttachEvent attachEvent) com.vaadin.flow.component.button.Button
protected String
getFileName
(com.vaadin.flow.server.VaadinSession session, com.vaadin.flow.server.VaadinRequest request) Deprecated.provide FileNameGenerator insteadcom.vaadin.flow.component.shared.Tooltip
Configures the download to open in a new window and removes the download attribute and content disposition headers instructing to download the target as a file.protected void
onAttach
(com.vaadin.flow.component.AttachEvent attachEvent) protected void
onDetach
(com.vaadin.flow.component.DetachEvent detachEvent) void
setContentTypeGenerator
(DynamicFileDownloader.ContentTypeGenerator contentTypeGenerator) void
setDisableOnClick
(boolean disableOnClick) Makes the download link to be disabled after the first click.void
setEnabled
(boolean enabled) void
setFileHandler
(com.vaadin.flow.function.SerializableConsumer<OutputStream> contentWriter) Sets the file handler that generates the file content.void
setFileName
(String fileName) Sets the file name of downloaded file.void
setFileNameGenerator
(DynamicFileDownloader.FileNameGenerator fileNameGenerator) Sets the strategy to creates the name of the downloaded file.com.vaadin.flow.component.shared.Tooltip
setTooltipText
(String text) withContentTypeGenerator
(DynamicFileDownloader.ContentTypeGenerator contentTypeGenerator) withFileNameGenerator
(DynamicFileDownloader.FileNameGenerator fileNameGenerator) Fluent method to set the strategy to creates the name of the downloaded.Methods inherited from class com.vaadin.flow.component.html.Anchor
getHref, getTarget, getTargetValue, isRouterIgnore, onEnabledStateChanged, removeHref, setHref, setHref, setRouterIgnore, setTarget, setTarget
Methods inherited from class com.vaadin.flow.component.HtmlComponent
getTitle, setTitle
Methods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisible
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListener
Methods inherited from interface com.vaadin.flow.component.BlurNotifier
addBlurListener
Methods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListener
Methods inherited from interface org.vaadin.firitin.fluency.ui.FluentAttachNotifier
withAttachListener
Methods inherited from interface org.vaadin.firitin.fluency.ui.FluentComponent
setId, setVisible, withId, withVisible
Methods inherited from interface org.vaadin.firitin.fluency.ui.FluentDetachNotifier
withDetachListener
Methods inherited from interface org.vaadin.firitin.fluency.ui.FluentHasEnabled
withEnabled
Methods inherited from interface org.vaadin.firitin.fluency.ui.FluentHasTooltip
withTooltip
Methods inherited from interface com.vaadin.flow.component.Focusable
addFocusShortcut, blur, focus, getTabIndex, setTabIndex
Methods inherited from interface com.vaadin.flow.component.FocusNotifier
addFocusListener
Methods inherited from interface com.vaadin.flow.component.HasAriaLabel
getAriaLabel, getAriaLabelledBy, setAriaLabel, setAriaLabelledBy
Methods inherited from interface com.vaadin.flow.component.HasComponents
add, add, add, addComponentAsFirst, addComponentAtIndex, remove, remove, removeAll
Methods inherited from interface com.vaadin.flow.component.HasElement
getElement
Methods inherited from interface com.vaadin.flow.component.HasEnabled
isEnabled
Methods inherited from interface com.vaadin.flow.component.HasSize
getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull
Methods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
Methods inherited from interface com.vaadin.flow.component.HasText
getText, getWhiteSpace, setText, setWhiteSpace
-
Field Details
-
requestHandler
protected com.vaadin.flow.server.RequestHandler requestHandlerThe request handler that handles the download request.
-
-
Constructor Details
-
DynamicFileDownloader
Constructs a basic download link with DOWNLOAD icon fromVaadinIcon
as the "text" and default file name.- Parameters:
writer
- the callback to generate the contents of the file
-
DynamicFileDownloader
Constructs a new download link with given text, static file name and writer.- Parameters:
linkText
- the text inside the linkcontentWriter
- the content writer that generates the actual content.
-
DynamicFileDownloader
public DynamicFileDownloader(String linkText, String fileName, DynamicFileDownloader.ContentWriter contentWriter) Constructs a new download link with given text, static file name and writer.- Parameters:
linkText
- the text inside the linkfileName
- the file name of produced filescontentWriter
- the content writer that generates the actual content.
-
DynamicFileDownloader
public DynamicFileDownloader(com.vaadin.flow.component.Component downloadComponent, String fileName, DynamicFileDownloader.ContentWriter contentWriter) Constructs a download link with given component as the content that ignites the download.- Parameters:
downloadComponent
- the component to be clicked by the user to start the downloadfileName
- the filename of the generated filescontentWriter
- the content writer of the generated file
-
DynamicFileDownloader
public DynamicFileDownloader(com.vaadin.flow.component.Component downloadComponent, DynamicFileDownloader.ContentWriter contentWriter) Constructs a download link with given component as the content that ignites the download.- Parameters:
downloadComponent
- the component to be clicked by the user to start the downloadcontentWriter
- the content writer of the generated file
-
DynamicFileDownloader
public DynamicFileDownloader()Empty constructor file downloader. Be sure to call setFileHandler before the component is attached.
-
-
Method Details
-
setDisableOnClick
public void setDisableOnClick(boolean disableOnClick) Makes the download link to be disabled after the first click.- Parameters:
disableOnClick
- true to disable the link after the first click
-
setEnabled
public void setEnabled(boolean enabled) - Specified by:
setEnabled
in interfacecom.vaadin.flow.component.HasEnabled
-
onAttach
protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent) - Overrides:
onAttach
in classcom.vaadin.flow.component.Component
-
ensurePollingOrPush
protected void ensurePollingOrPush(com.vaadin.flow.component.AttachEvent attachEvent) -
onDetach
protected void onDetach(com.vaadin.flow.component.DetachEvent detachEvent) - Overrides:
onDetach
in classcom.vaadin.flow.component.Component
-
addDownloadFinishedListener
public com.vaadin.flow.shared.Registration addDownloadFinishedListener(com.vaadin.flow.component.ComponentEventListener<DynamicFileDownloader.DownloadFinishedEvent> listener) Adds a listener that is executed when the file content has been streamed.- Parameters:
listener
- the listener- Returns:
- the
Registration
you can use to remove this listener.
-
addDownloadStartedListener
public com.vaadin.flow.shared.Registration addDownloadStartedListener(com.vaadin.flow.component.ComponentEventListener<DynamicFileDownloader.DownloadStartedEvent> listener) Adds a listener that is executed when the file content streaming has started.- Parameters:
listener
- the listener- Returns:
- the
Registration
you can use to remove this listener.
-
addDownloadFailedListener
public com.vaadin.flow.shared.Registration addDownloadFailedListener(com.vaadin.flow.component.ComponentEventListener<DynamicFileDownloader.DownloadFailedEvent> listener) Adds a listener that is executed when the file content streaming has failed due to an exception. Note that the UI changes done in the listener don't necessarily happen live if you don't havePush
in use or useUI.setPollInterval(int)
method.- Parameters:
listener
- the listener- Returns:
- the
Registration
you can use to remove this listener.
-
setFileHandler
public void setFileHandler(com.vaadin.flow.function.SerializableConsumer<OutputStream> contentWriter) Sets the file handler that generates the file content.- Parameters:
contentWriter
- the file handler
-
setFileName
Sets the file name of downloaded file.- Parameters:
fileName
- the file name
-
getFileName
@Deprecated protected String getFileName(com.vaadin.flow.server.VaadinSession session, com.vaadin.flow.server.VaadinRequest request) Deprecated.provide FileNameGenerator insteadGets the filename of downloaded file. Override if you want to generate the name dynamically.- Parameters:
session
- the vaadin sessionrequest
- the vaadin request- Returns:
- the file name
-
asButton
Makes the download look like a button instead of a normal link.- Returns:
- the same instance, fluent method
-
getButton
public com.vaadin.flow.component.button.Button getButton()- Returns:
- a Button component wrapped inside the file downloader, if configured as a Button
-
setFileNameGenerator
Sets the strategy to creates the name of the downloaded file.- Parameters:
fileNameGenerator
- the generator
-
withFileNameGenerator
public DynamicFileDownloader withFileNameGenerator(DynamicFileDownloader.FileNameGenerator fileNameGenerator) Fluent method to set the strategy to creates the name of the downloaded.- Parameters:
fileNameGenerator
- the generator- Returns:
- the same instance, fluent method
-
setContentTypeGenerator
public void setContentTypeGenerator(DynamicFileDownloader.ContentTypeGenerator contentTypeGenerator) -
withContentTypeGenerator
public DynamicFileDownloader withContentTypeGenerator(DynamicFileDownloader.ContentTypeGenerator contentTypeGenerator) -
setTooltipText
- Specified by:
setTooltipText
in interfacecom.vaadin.flow.component.shared.HasTooltip
- Parameters:
text
- the tooltip text- See Also:
-
getTooltip
public com.vaadin.flow.component.shared.Tooltip getTooltip()- Specified by:
getTooltip
in interfacecom.vaadin.flow.component.shared.HasTooltip
-
inNewWindow
Configures the download to open in a new window and removes the download attribute and content disposition headers instructing to download the target as a file. So essentially we are giving the browser a chance to make a choise what should be done. This allows the component to be used to generated e.g. PDF files on the fly and show them in browser by default.- Returns:
- the same instance, fluent method
-