Package ghidra.util.task
Class TaskBuilder
java.lang.Object
ghidra.util.task.TaskBuilder
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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
launchInBackground
(TaskMonitor monitor) Runs the task in a background thread with the given monitor that cannot be null.void
Launches the task built by this builder, using a blocking modal dialog.void
Launches the task built by this builder, using a non-blocking dialog.setCanCancel
(boolean canCancel) Sets whether the task can be cancelled.setDialogWidth
(int width) The desired width of the dialog.setHasProgress
(boolean hasProgress) Sets whether this task reports progress.setLaunchDelay
(int delay) Sets the amount of time that will pass before showing the dialog.Sets the component over which the task dialog will be shown.setStatusTextAlignment
(int alignment) Sets the horizontal text alignment of messages shown in the task dialog.Sets the title of this task.static TaskBuilder
A convenience method to start a builder using the given runnable.static TaskBuilder
A convenience method to start a builder using the given task.
-
Constructor Details
-
TaskBuilder
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 Details
-
withRunnable
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
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
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
Sets whether this task reports progress. The default istrue
.- Parameters:
hasProgress
- true if the task reports progress- Returns:
- this builder
-
setCanCancel
Sets whether the task can be cancelled. The default istrue
.- Parameters:
canCancel
- true if the task can be cancelled.- Returns:
- this builder
-
setParent
Sets the component over which the task dialog will be shown. The default isnull
, which shows the dialog over the active window.- Parameters:
parent
- the parent- Returns:
- this builder
-
setLaunchDelay
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
The desired width of the dialog. The default isTaskDialog.DEFAULT_WIDTH
.- Parameters:
width
- the width- Returns:
- this builder
-
setStatusTextAlignment
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
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
-