Class TaskBuilder

java.lang.Object
ghidra.util.task.TaskBuilder

public class TaskBuilder extends 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();
 
Or,
 
 TaskBuilder.withRunnable(monitor -> doWork(parameter, monitor))
     .setTitle("Task Title")
     .setHasProgress(true)
     .setCanCancel(true)
     .setStatusTextAlignment(SwingConstants.LEADING)
     .launchModal();
 
Or,
 TaskBuilder.withTask(new AwesomeTask(awesomeStuff)).launchModal();
 
Or,
 TaskLauncher.launch(new AwesomeTask(awesomeStuff));
 

Note: this class will check to see if it is in a headless environment before launching its task. This makes it safe to use this class in headed or headless environments.

  • Constructor Details

    • TaskBuilder

      public TaskBuilder(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 Details

    • withRunnable

      public static TaskBuilder withRunnable(MonitoredRunnable r)
      A convenience method to start a builder using the given runnable. After calling this method you are still required to call setTitle(String).

      This method allows for a more attractive fluent API usage than does the constructor (see the javadoc header).

      Parameters:
      r - the runnable
      Returns:
      this builder
    • withTask

      public static TaskBuilder withTask(Task t)
      A convenience method to start a builder using the given task. The title of the task will be the value of Task.getTaskTitle().

      This method allows for a more attractive fluent API usage than does the constructor (see the javadoc header).

      Parameters:
      t - the task
      Returns:
      this builder
    • setTitle

      public TaskBuilder setTitle(String title)
      Sets the title of this task. The title must be set before calling any of the launch methods.
      Parameters:
      title - the title
      Returns:
      this builder
    • 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(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
    • setLaunchDelay

      public TaskBuilder setLaunchDelay(int delay)
      Sets the amount of time that will pass before showing the dialog. The default is TaskLauncher.INITIAL_DELAY_MS for non-modal tasks and TaskLauncher.INITIAL_MODAL_DELAY_MS for modal tasks.
      Parameters:
      delay - the delay time
      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. The task will be run in the current thread if in a headless environment.
    • launchNonModal

      public void launchNonModal()
      Launches the task built by this builder, using a non-blocking dialog. The task will be run in the current thread if in a headless environment.
    • launchInBackground

      public void launchInBackground(TaskMonitor monitor)
      Runs the task in a background thread with the given monitor that cannot be null. This is a special case for clients that already have a task monitor widget in their UI and they wish to let it show the progress of the given task while not blocking the Swing thread.
      Parameters:
      monitor - the task monitor; may not be null