Class FileScannerThread
- All Implemented Interfaces:
Runnable
This class also improved progress reporting, compared to the FileSearchListener approach. This class goes through the SimpleProgressListener interface, which provides discrete events for begins, progress, completed, and canceled. Callers still have the option of cancelling the search by returning false from the progress callback, but now they have better visibility into the current step and total step counts.
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 text and Markdown files in a directory (recursion is true by default):
FileScannerThread scanner = new FileScannerThread(new File("/path/to/search"))
.addExtensionsToMatch(List.of(".txt", ".md"))
.addCompletionListener(results -> handleResults(results))
.addCancelListener(() -> handleCancellation());
MultiProgressDialog progressDialog = new MultiProgressDialog(parentFrame, "Scanning for files...");
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.addExtensionsToMatch(List<String> extensions) Adds to the list of extensions that should be matched during the search, while also considering whether our search is inverted (see setInvertSearch()).booleanbooleanvoidrun()setInvertSearch(boolean invertSearch) Changes how the extensionsToMatch list is interpreted.setRecursive(boolean recursive) Decides whether the search should include subdirectories, or just rootDir itself.Methods inherited from class SimpleProgressWorker
addProgressListener, clearProgressListeners, fireProgressBegins, fireProgressCanceled, fireProgressComplete, fireProgressError, fireProgressUpdate, removeProgressListener
-
Constructor Details
-
FileScannerThread
-
-
Method Details
-
isRecursive
public boolean isRecursive() -
setRecursive
Decides whether the search should include subdirectories, or just rootDir itself. By default, all searches are recursive. -
isSearchInverted
public boolean isSearchInverted() -
setInvertSearch
Changes how the extensionsToMatch list is interpreted. By default, searches are non-inverted, and files must match at least one of the given extensions in order to be returned. An inverted search will only return files that do NOT match any of the given extensions. -
getExtensionsToMatch
-
addExtensionsToMatch
Adds to the list of extensions that should be matched during the search, while also considering whether our search is inverted (see setInvertSearch()).Extensions can include the dot or not: ".txt" and "txt" are treated the same. Extensions are normalized to lowercase and trimmed for matching purposes.
An empty or null list is acceptable. For regular (non-inverted) searches, this means "match everything". For inverted searches, this means "exclude nothing". This means you'll get a list of ALL files in either case.
-
clearExtensionsToMatch
-
getResults
-
addCompletionListener
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
-
removeCancelListener
-
run
public void run()
-