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 Classes
    Modifier and Type
    Class
    Description
    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 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

    Fields
    Modifier and Type
    Field
    Description
    protected com.vaadin.flow.server.RequestHandler
    The request handler that handles the download request.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Empty 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.
    Constructs a new download link with given text, static file name and writer.
    Constructs a new download link with given text, static file name and writer.
    Constructs a basic download link with DOWNLOAD icon from VaadinIcon as the "text" and default file name.
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    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.
    Makes the download look like a button instead of a normal link.
    com.vaadin.flow.component.button.Button
     
    protected String
    getFileName(com.vaadin.flow.server.VaadinSession session, com.vaadin.flow.server.VaadinRequest request)
    Deprecated.
    provide FileNameGenerator instead
    com.vaadin.flow.component.shared.Tooltip
     
    protected void
    onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
     
    protected void
    onDetach(com.vaadin.flow.component.DetachEvent detachEvent)
     
    void
    setDisableOnClick(boolean disableOnClick)
    Makes the download link to be disabled after the first click.
    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
    Sets the strategy to creates the name of the downloaded file.
    com.vaadin.flow.component.shared.Tooltip
     
    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, setEnabled

    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 requestHandler
      The request handler that handles the download request.
  • Constructor Details

    • DynamicFileDownloader

      public DynamicFileDownloader(DynamicFileDownloader.ContentWriter writer)
      Constructs a basic download link with DOWNLOAD icon from VaadinIcon as the "text" and default file name.
      Parameters:
      writer - the callback to generate the contents of the file
    • DynamicFileDownloader

      public DynamicFileDownloader(String linkText, DynamicFileDownloader.ContentWriter contentWriter)
      Constructs a new download link with given text, static file name and writer.
      Parameters:
      linkText - the text inside the link
      contentWriter - 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 link
      fileName - the file name of produced files
      contentWriter - 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 download
      fileName - the filename of the generated files
      contentWriter - 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 download
      contentWriter - 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
    • onAttach

      protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
      Overrides:
      onAttach in class com.vaadin.flow.component.Component
    • onDetach

      protected void onDetach(com.vaadin.flow.component.DetachEvent detachEvent)
      Overrides:
      onDetach in class com.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. Note that the UI changes done in the listener don't necessarily happen live if you don't have @Push in use or use UI.setPollInterval(int) method.
      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 have Push in use or use UI.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

      public void setFileName(String fileName)
      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 instead
      Gets the filename of downloaded file. Override if you want to generate the name dynamically.
      Parameters:
      session - the vaadin session
      request - the vaadin request
      Returns:
      the file name
    • asButton

      public DynamicFileDownloader 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

      public void setFileNameGenerator(DynamicFileDownloader.FileNameGenerator fileNameGenerator)
      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
    • setTooltipText

      public com.vaadin.flow.component.shared.Tooltip setTooltipText(String text)
      Specified by:
      setTooltipText in interface com.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:
      getTooltip in interface com.vaadin.flow.component.shared.HasTooltip