Class BackgroundTask<T>

  • Type Parameters:
    T - the type of the result of the task

    public abstract class BackgroundTask<T>
    extends java.lang.Object
    An abstract class for performing long running tasks on a background thread.
    • Constructor Summary

      Constructors 
      Constructor Description
      BackgroundTask()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract java.lang.String getStatusMessage()  
      abstract boolean handleException​(java.lang.Throwable th)
      Handle an exception thrown by the long running task (called on the Swing event thread).
      abstract T performTask()
      Execute a long running task (called on a non-Swing thread).
      java.util.concurrent.CompletableFuture<T> run()
      Run this task on a background thread.
      java.util.concurrent.CompletableFuture<T> run​(StatusIndicator statusIndicator, java.awt.Component owner)
      Run this task on a background thread.
      java.util.concurrent.CompletableFuture<T> run​(java.awt.Component owner)
      Run this task on a background thread.
      static <T> BackgroundTask<T> task​(java.lang.String statusMessage, java.util.function.Supplier<T> doInBackground, java.util.function.Consumer<T> updateUI)
      Create a task from callbacks.
      static <T> BackgroundTask<T> task​(java.lang.String statusMessage, java.util.function.Supplier<T> doInBackground, java.util.function.Consumer<T> updateUI, java.util.function.Function<java.lang.Throwable,​java.lang.Boolean> onException)
      Create a task from callbacks.
      static <T> BackgroundTask<T> task​(java.util.function.Supplier<T> doInBackground, java.util.function.Consumer<T> updateUI)
      Create a task from callbacks.
      static <T> BackgroundTask<T> task​(java.util.function.Supplier<T> doInBackground, java.util.function.Consumer<T> updateUI, java.util.function.Function<java.lang.Throwable,​java.lang.Boolean> onException)
      Create a task from callbacks.
      abstract void updateUI​(T result)
      Update the UI with the result of the long running task (called on the Swing event thread).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BackgroundTask

        public BackgroundTask()
    • Method Detail

      • getStatusMessage

        public abstract java.lang.String getStatusMessage()
        Returns:
        description of the background task to be displayed to the user.
      • performTask

        public abstract T performTask()
        Execute a long running task (called on a non-Swing thread).
      • updateUI

        public abstract void updateUI​(T result)
        Update the UI with the result of the long running task (called on the Swing event thread).
      • handleException

        public abstract boolean handleException​(java.lang.Throwable th)
        Handle an exception thrown by the long running task (called on the Swing event thread).
        Returns:
        true if the exception has been handled.
      • task

        public static <T> BackgroundTask<T> task​(java.util.function.Supplier<T> doInBackground,
                                                 java.util.function.Consumer<T> updateUI)
        Create a task from callbacks. An error dialog will be displayed if there is an exception.
        Type Parameters:
        T - the type of the task's result
        Parameters:
        doInBackground - the action to perform on the background thread
        updateUI - the action to perform on the Swing Event Dispatch Thread
        Returns:
        the new task
      • task

        public static <T> BackgroundTask<T> task​(java.util.function.Supplier<T> doInBackground,
                                                 java.util.function.Consumer<T> updateUI,
                                                 java.util.function.Function<java.lang.Throwable,​java.lang.Boolean> onException)
        Create a task from callbacks.
        Type Parameters:
        T - the type of the task's result
        Parameters:
        doInBackground - the action to perform on the background thread
        updateUI - the action to perform on the Swing Event Dispatch Thread
        onException - exception handler (returns true if it handled the exception or false to display an error dialog)
        Returns:
        the new task
      • task

        public static <T> BackgroundTask<T> task​(java.lang.String statusMessage,
                                                 java.util.function.Supplier<T> doInBackground,
                                                 java.util.function.Consumer<T> updateUI)
        Create a task from callbacks. An error dialog will be displayed if there is an exception.
        Type Parameters:
        T - the type of the task's result
        Parameters:
        statusMessage - the message to display while the task is running
        doInBackground - the action to perform on the background thread
        updateUI - the action to perform on the Swing Event Dispatch Thread
        Returns:
        the new task
      • task

        public static <T> BackgroundTask<T> task​(java.lang.String statusMessage,
                                                 java.util.function.Supplier<T> doInBackground,
                                                 java.util.function.Consumer<T> updateUI,
                                                 java.util.function.Function<java.lang.Throwable,​java.lang.Boolean> onException)
        Create a task from callbacks.
        Type Parameters:
        T - the type of the task's result
        Parameters:
        statusMessage - the message to display while the task is running
        doInBackground - the action to perform on the background thread
        updateUI - the action to perform on the Swing Event Dispatch Thread
        onException - exception handler (returns true if it handled the exception or false to display an error dialog)
        Returns:
        the new task
      • run

        public java.util.concurrent.CompletableFuture<T> run()
        Run this task on a background thread. Status messages will be sent to the log. An unowned dialog will be used to display any unhandled exception.
      • run

        public java.util.concurrent.CompletableFuture<T> run​(java.awt.Component owner)
        Run this task on a background thread. This method should only be called from the Swing Event Dispatch thread. The UI will be disabled and the status message will be displayed if owner or one if its ancestors is a StatusIndicator.
        Parameters:
        owner - owner component for displaying an error dialog if the task fails
      • run

        public java.util.concurrent.CompletableFuture<T> run​(StatusIndicator statusIndicator,
                                                             java.awt.Component owner)
        Run this task on a background thread. This method should only be called from the Swing Event Dispatch thread.
        Parameters:
        statusIndicator - UI component to receive status messages (disabled while the task is running)
        owner - owner component for displaying an error dialog if the task fails