Class TaskBuilder


  • public class TaskBuilder
    extends java.lang.Object
    A builder object that allows clients to launch tasks in the background, with a progress dialog representing the task.

    Using this class obviates the need for clients to create full class objects to implement the Task interface, which means less boiler-plate code.

    An example of usage:

                    MonitoredRunnable r = 
                            monitor -> doWork(parameter, monitor);
                    new TaskBuilder("Task Title", r)
                            .setHasProgress(true)
                            .setCanCancel(true)
                            .setStatusTextAlignment(SwingConstants.LEADING)
                            .launchModal();         
     
    • Constructor Detail

      • TaskBuilder

        public TaskBuilder​(java.lang.String title,
                           MonitoredRunnable runnable)
        Constructor.
        Parameters:
        title - the required title for your task. This will appear as the title of the task dialog
        runnable - the runnable that will be called when the task is run
    • Method Detail

      • setHasProgress

        public TaskBuilder setHasProgress​(boolean hasProgress)
        Sets whether this task reports progress. The default is true.
        Parameters:
        hasProgress - true if the task reports progress
        Returns:
        this builder
      • setCanCancel

        public TaskBuilder setCanCancel​(boolean canCancel)
        Sets whether the task can be cancelled. The default is true.
        Parameters:
        canCancel - true if the task can be cancelled.
        Returns:
        this builder
      • setParent

        public TaskBuilder setParent​(java.awt.Component parent)
        Sets the component over which the task dialog will be shown. The default is null, which shows the dialog over the active window.
        Parameters:
        parent - the parent
        Returns:
        this builder
      • setDialogWidth

        public TaskBuilder setDialogWidth​(int width)
        The desired width of the dialog. The default is TaskDialog.DEFAULT_WIDTH.
        Parameters:
        width - the width
        Returns:
        this builder
      • setStatusTextAlignment

        public TaskBuilder setStatusTextAlignment​(int alignment)
        Sets the horizontal text alignment of messages shown in the task dialog. The default is SwingConstants.CENTER. Valid values are SwingConstants LEADING, CENTER and TRAILING.
        Parameters:
        alignment - the alignment
        Returns:
        this builder
      • launchModal

        public void launchModal()
        Launches the task built by this builder, using a blocking modal dialog.
      • launchNonModal

        public TaskLauncher launchNonModal()
        Launches the task built by this builder, using a non-blocking dialog.
        Returns:
        the launcher that launched the task