Class DirectoryScannerThread
- All Implemented Interfaces:
Runnable
This class uses the SimpleProgressListener interface, which provides discrete events for begins, progress, completed, and canceled. Callers can cancel the search by returning false from the progress callback.
Instead of (or in addition to) using the progress listener callbacks, callers can also register CompletionListener and CancelListener callbacks, which will be invoked when the search completes or is canceled, respectively. This allows callers to be notified of these events without having to implement the full SimpleProgressListener interface. Note that these callbacks are invoked from the worker thread! If you need to do UI updates as a result of these callbacks, you must marshal those calls to the Swing Event Dispatching Thread using SwingUtilities.invokeLater() or similar.
EXAMPLE USAGE
// Find all subdirectories under a given path (recursion is true by default):
DirectoryScannerThread scanner = new DirectoryScannerThread(new File("/path/to/search"))
.addCompletionListener(results -> handleResults(results))
.addCancelListener(() -> handleCancellation());
MultiProgressDialog progressDialog = new MultiProgressDialog(parentFrame, "Scanning for directories...");
progressDialog.runWorker(scanner, true); // run the scan and dispose when finished.
- Since:
- swing-extras 3.0
- Author:
- scorbo2
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic interface -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRegister to receive notification if the search is canceled by any listener.Register to receive notification when the search completes.booleanvoidrun()setRecursive(boolean recursive) Decides whether the search should include subdirectories recursively, or just the immediate children of rootDir.Methods inherited from class SimpleProgressWorker
addProgressListener, clearProgressListeners, fireProgressBegins, fireProgressCanceled, fireProgressComplete, fireProgressError, fireProgressUpdate, removeProgressListener
-
Constructor Details
-
DirectoryScannerThread
-
-
Method Details
-
isRecursive
public boolean isRecursive() -
setRecursive
Decides whether the search should include subdirectories recursively, or just the immediate children of rootDir. By default, all searches are recursive. -
getResults
-
addCompletionListener
public DirectoryScannerThread addCompletionListener(DirectoryScannerThread.CompletionListener listener) Register to receive notification when the search completes. This will not be invoked if the search is canceled. This hook might be easier to use than implementing an entire SimpleProgressListener in your code.IMPORTANT NOTE: This callback is invoked from the worker thread! If you need to do UI updates in response to this, you must marshal them to the Swing Event Dispatching Thread using SwingUtilities.invokeLater() or similar.
-
addCancelListener
Register to receive notification if the search is canceled by any listener. This will not be invoked if the search completes normally. This hook might be easier to use than implementing an entire SimpleProgressListener in your code.IMPORTANT NOTE: This callback is invoked from the worker thread! If you need to do UI updates in response to this, you must marshal them to the Swing Event Dispatching Thread using SwingUtilities.invokeLater() or similar.
-
removeCompletionListener
public DirectoryScannerThread removeCompletionListener(DirectoryScannerThread.CompletionListener listener) -
removeCancelListener
-
run
public void run()
-