Package ghidra.util.task
Class CachingSwingWorker<T>
java.lang.Object
ghidra.util.task.CachingSwingWorker<T>
- Type Parameters:
T
- the value type
- All Implemented Interfaces:
CachingLoader<T>
Class for managing the creation of some slow loading object that may be used by multiple threads,
including the Swing thread. Further, repeated calls to this object will used the
cached value.
The basic uses cases are:
-
Call
get(TaskMonitor)
from the Swing thread - this will block the Swing thread, showing a modal dialog, as needed. -
Call
get(TaskMonitor)
from a non-Swing thread - this will block the calling thread, with no effect on the UI. - Call
startLoading()
- this will trigger this worker to load in the background without blocking the calling thread. -
Call
getCachedValue()
- this is a way to see if the value has been loaded without blocking the current thread. -
Override
done()
- this method will be called when the initial loading is finished.
-
Constructor Summary
ConstructorDescriptionCachingSwingWorker
(String name, boolean hasProgress) Create a new CachingSwingWorker -
Method Summary
Modifier and TypeMethodDescriptionvoid
cancel()
Cancels this swing workervoid
clear()
Clears the cached value for the object causing it to be recreated on the next call to get()void
done()
A method for clients to use as a callback for completion.get
(TaskMonitor monitor) Returns the object that this class is managing/caching.Returns the value only if it is cached, otherwise return null.protected abstract T
runInBackground
(TaskMonitor monitor) Subclasses must implement this method to create the object being managed/cached.void
setTaskDialogDelay
(int delay) Sets the initial delay before showing a progress dialog.void
Allows clients to start this worker loading without blocking.
-
Constructor Details
-
CachingSwingWorker
Create a new CachingSwingWorker- Parameters:
name
- the name of worker. (Displayed in the progress dialog)hasProgress
- true if the dialog should show progress or be indeterminate.
-
-
Method Details
-
setTaskDialogDelay
public void setTaskDialogDelay(int delay) Sets the initial delay before showing a progress dialog. The default is 100ms.- Parameters:
delay
- the delay to wait before displaying a progress dialog.
-
runInBackground
Subclasses must implement this method to create the object being managed/cached.- Parameters:
monitor
- A task monitor that can be used to provide progress information to a progress dialog is it is being shown. Implementers should also check the monitor periodically to check for cancelled. If cancelled, this method should not throw a cancelled exception but instead either return null or a partial result. (For example, if the object is a list being generated by a search, then it might make sense to return a list of the items found so far.)- Returns:
- the newly created object.
-
get
Returns the object that this class is managing/caching. It will return the object if it is already created or it will block until the object can be created. If called from the Swing thread, it will also launch a modal progress dialog while waiting for the object to be created.- Specified by:
get
in interfaceCachingLoader<T>
- Parameters:
monitor
- the monitor (may be null)- Returns:
- the object that this class is managing/caching
- See Also:
-
startLoading
public void startLoading()Allows clients to start this worker loading without blocking. -
getCachedValue
Returns the value only if it is cached, otherwise return null.- Returns:
- the value only if it is cached, otherwise return null.
-
cancel
public void cancel()Cancels this swing worker -
clear
public void clear()Clears the cached value for the object causing it to be recreated on the next call to get()- Specified by:
clear
in interfaceCachingLoader<T>
-
done
public void done()A method for clients to use as a callback for completion. This method will be called in the Swing thread, after the value has been set.
-