Class ActionButton<T>

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.Composite<com.vaadin.flow.component.html.Div>
org.vaadin.firitin.components.button.ActionButton<T>
Type Parameters:
T - the type of the result of the slow task
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasStyle, Serializable

public class ActionButton<T> extends com.vaadin.flow.component.Composite<com.vaadin.flow.component.html.Div>
A button that can be used to run a slow action in the background without blocking the UI. Even if you would disable/block other parts of your UI during the action, this can be better approach as the global progress indicator can make people think the app has crashed. A good UI pattern for long-running actions (that can't run on background and keep the UI funtional), is to show for example a dialog with a progress indicator and possibly a cancel button.

NOTE! This class is still in early development and likely to get some changes still in the future. Suggestions/contributions are more than welcome!

The button will be disabled while the task is running and re-enabled when the task is done.

The actual task, set with setAction(Supplier) or setCompletableFutureAction(Supplier), is run in a separate thread. If your task wants to update the UI during its execution, you need to synchronize with the UI thread using UI.access(Command).

UI updates are suggested to be done in #setPostTaskAction(Consumer) which is called after the task has completed or #setPreTaskAction(Runnable) which is called before the task is started. These are run in the UI thread.

The button will automatically enable polling if push is not enabled and call push() if "manual server push" is active.

See Also:
  • Constructor Details

  • Method Details

    • setAction

      public ActionButton<T> setAction(Supplier<T> action)
    • setAction

      public ActionButton<Void> setAction(Runnable action)
    • setCompletableFutureAction

      public ActionButton<T> setCompletableFutureAction(Supplier<CompletableFuture<T>> task)
    • setPostUiAction

      public ActionButton<T> setPostUiAction(Consumer<? super T> postUiAction)
      Sets an action to update the UI after the actual slow actions is completed. This is "run in the UI thread", meaning you don't need to synchronize with the UI using UI.access().
      Parameters:
      postUiAction - the action to run after the slow task
      Returns:
      this for chaining
    • setPostUiUpdate

      @Deprecated public ActionButton<T> setPostUiUpdate(Consumer<? super T> postUiUpdate)
      Deprecated.
      Parameters:
      postUiUpdate - the action to run after the slow task
      Returns:
      this for chaining
    • setPreUiAction

      public ActionButton<T> setPreUiAction(Runnable preUiAction)
      Sets an action to update the UI before the actual slow actions is started. This is "run in the UI thread", meaning you don't need to synchronize with the UI using UI.access().
      Parameters:
      preUiAction - the action to run before the slow task
      Returns:
      this for chaining
    • setPreUiUpdate

      @Deprecated(forRemoval=true) public ActionButton<T> setPreUiUpdate(Runnable preUiUpdate)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • onAttach

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

      public CompletableFuture<T> getCompletableFuture()
    • setText

      public void setText(String s)
    • setBusyText

      public ActionButton setBusyText(String text)
      Set the text of the button to show while the task is running (and button disabled).
      Parameters:
      text - the text to show
      Returns:
    • reEnableAfterAction

      protected void reEnableAfterAction()
    • isShowProgressBar

      public boolean isShowProgressBar()
    • setShowProgressBar

      public void setShowProgressBar(boolean showProgressBar)
      Parameters:
      showProgressBar - true if the built-in progress bar should be shown while the task is running
    • prepareProgressBar

      protected VProgressBar prepareProgressBar()
    • updateProgressAsync

      public void updateProgressAsync(double progress, double min, double max)
      API for updating the progress bar from the task. This method is safe to call from the task thread.
      Parameters:
      progress - the progress value to set
      min - the minimum value of the progress bar
      max - the maximum value of the progress bar
    • updateProgressAsync

      public void updateProgressAsync(double progress)
      API for updating the progress bar from the task. This method is safe to call from the task thread.
      Parameters:
      progress - the progress value to set
    • getExecutor

      public Executor getExecutor()
    • setExecutor

      public void setExecutor(Executor executor)
      Set an executor to be used for running the task. If not set, the default executor is used.
      Parameters:
      executor - the executor to use for the task
    • getEstimatedDuration

      public Integer getEstimatedDuration()
    • setEstimatedDuration

      public void setEstimatedDuration(Integer estimatedDuration)
      Set the estimated duration of the task in milliseconds. This can be used to show a progress bar that is not indeterminate.
      Parameters:
      estimatedDuration -
    • getButton

      public VButton getButton()
      Returns the underlying button component, for further configaration.
      Returns:
      the underlying button component
    • isEnableAfterAction

      public boolean isEnableAfterAction()
      Returns:
      true if the button should be automatically enabled after the action is completed
    • setEnableAfterAction

      public ActionButton setEnableAfterAction(boolean enableAfterAction)
      Parameters:
      enableAfterAction - true (default) if the button should be automatically enabled after the action is completed
      Returns:
      this for chaining