Package ghidra.util.task
Class TaskBuilder
- java.lang.Object
-
- ghidra.util.task.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();
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 Summary
Constructors Constructor Description TaskBuilder(java.lang.String title, MonitoredRunnable runnable)
Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
launchInBackground(TaskMonitor monitor)
Runs the task in a background thread with the given monitor that cannot be null.void
launchModal()
Launches the task built by this builder, using a blocking modal dialog.void
launchNonModal()
Launches the task built by this builder, using a non-blocking dialog.TaskBuilder
setCanCancel(boolean canCancel)
Sets whether the task can be cancelled.TaskBuilder
setDialogWidth(int width)
The desired width of the dialog.TaskBuilder
setHasProgress(boolean hasProgress)
Sets whether this task reports progress.TaskBuilder
setLaunchDelay(int delay)
Sets the amount of time that will pass before showing the dialog.TaskBuilder
setParent(java.awt.Component parent)
Sets the component over which the task dialog will be shown.TaskBuilder
setStatusTextAlignment(int alignment)
Sets the horizontal text alignment of messages shown in the task dialog.TaskBuilder
setTitle(java.lang.String title)
Sets the title of this task.static TaskBuilder
withRunnable(MonitoredRunnable r)
A convenience method to start a builder using the given runnable.static TaskBuilder
withTask(Task t)
A convenience method to start a builder using the given task.
-
-
-
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 dialogrunnable
- the runnable that will be called when the task is run
-
-
Method Detail
-
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 callsetTitle(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. Thetitle
of the task will be the value ofTask.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(java.lang.String title)
Sets the title of this task. The title must be set before calling any of thelaunch
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(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
-
setLaunchDelay
public TaskBuilder setLaunchDelay(int delay)
Sets the amount of time that will pass before showing the dialog. The default isTaskLauncher.INITIAL_DELAY_MS
for non-modal tasks andTaskLauncher.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 isTaskDialog.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 isSwingConstants.CENTER
. Valid values areSwingConstants
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
-
-