Package org.vaadin.firitin.components
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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceGenerates the content of HTTP response header 'Content-Type'.static interfaceWrites the content of the downloaded file to the given output stream.static classEvent fired when the file download fails.static classEvent fired when the file download has been streamed to the client.static classEvent fired when the file download has been streamed to the client.static interfaceGenerates 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
FieldsModifier and TypeFieldDescriptionprotected com.vaadin.flow.server.RequestHandlerThe request handler that handles the download request. -
Constructor Summary
ConstructorsConstructorDescriptionEmpty 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 fromVaadinIconas the "text" and default file name. -
Method Summary
Modifier and TypeMethodDescriptioncom.vaadin.flow.shared.RegistrationaddDownloadFailedListener(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.RegistrationaddDownloadFinishedListener(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.RegistrationaddDownloadStartedListener(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 voidensurePollingOrPush(com.vaadin.flow.component.AttachEvent attachEvent) com.vaadin.flow.component.button.Buttonprotected StringgetFileName(com.vaadin.flow.server.VaadinSession session, com.vaadin.flow.server.VaadinRequest request) Deprecated.provide FileNameGenerator insteadcom.vaadin.flow.component.shared.TooltipConfigures 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 voidonAttach(com.vaadin.flow.component.AttachEvent attachEvent) protected voidonDetach(com.vaadin.flow.component.DetachEvent detachEvent) voidsetContentTypeGenerator(DynamicFileDownloader.ContentTypeGenerator contentTypeGenerator) voidsetDisableOnClick(boolean disableOnClick) Makes the download link to be disabled after the first click.voidsetEnabled(boolean enabled) voidsetFileHandler(com.vaadin.flow.function.SerializableConsumer<OutputStream> contentWriter) Sets the file handler that generates the file content.voidsetFileName(String fileName) Sets the file name of downloaded file.voidsetFileNameGenerator(DynamicFileDownloader.FileNameGenerator fileNameGenerator) Sets the strategy to creates the name of the downloaded file.com.vaadin.flow.component.shared.TooltipsetTooltipText(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, setTargetMethods inherited from class com.vaadin.flow.component.HtmlComponent
getTitle, setTitleMethods 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, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.BlurNotifier
addBlurListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface org.vaadin.firitin.fluency.ui.FluentAttachNotifier
withAttachListenerMethods inherited from interface org.vaadin.firitin.fluency.ui.FluentComponent
navigate, setId, setVisible, withId, withVisibleMethods inherited from interface org.vaadin.firitin.fluency.ui.FluentDetachNotifier
withDetachListenerMethods inherited from interface org.vaadin.firitin.fluency.ui.FluentHasEnabled
withEnabledMethods inherited from interface org.vaadin.firitin.fluency.ui.FluentHasTooltip
withTooltipMethods inherited from interface com.vaadin.flow.component.Focusable
addFocusShortcut, blur, focus, getTabIndex, setTabIndexMethods inherited from interface com.vaadin.flow.component.FocusNotifier
addFocusListenerMethods inherited from interface com.vaadin.flow.component.HasAriaLabel
getAriaLabel, getAriaLabelledBy, setAriaLabel, setAriaLabelledByMethods inherited from interface com.vaadin.flow.component.HasComponents
add, add, add, addComponentAsFirst, addComponentAtIndex, remove, remove, removeAllMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasEnabled
isEnabledMethods 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, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameMethods 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 fromVaadinIconas 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:
setEnabledin interfacecom.vaadin.flow.component.HasEnabled
-
onAttach
protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent) - Overrides:
onAttachin 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:
onDetachin 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
Registrationyou 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
Registrationyou 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 havePushin use or useUI.setPollInterval(int)method.- Parameters:
listener- the listener- Returns:
- the
Registrationyou 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:
setTooltipTextin interfacecom.vaadin.flow.component.shared.HasTooltip- Parameters:
text- the tooltip text- See Also:
-
Note, that tooltips are only supported if the content of the link supports them. For example, tooltips are supported if the #asButton() method is called.
-
getTooltip
public com.vaadin.flow.component.shared.Tooltip getTooltip()- Specified by:
getTooltipin 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
-