Class FileScannerThread

All Implemented Interfaces:
Runnable

public class FileScannerThread extends SimpleProgressWorker
A handy utility class that can wrap the various find methods in FileSystemUtil in the form of a worker thread that can be easily wired up to a MultiProgressDialog. This saves a modest amount of work for client applications that just want to find files in a thread-safe way (that is, without blocking the UI thread).

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
  • Constructor Details

    • FileScannerThread

      public FileScannerThread(File rootDir)
  • Method Details

    • isRecursive

      public boolean isRecursive()
    • setRecursive

      public FileScannerThread setRecursive(boolean recursive)
      Decides whether the search should include subdirectories, or just rootDir itself. By default, all searches are recursive.
    • isSearchInverted

      public boolean isSearchInverted()
    • setInvertSearch

      public FileScannerThread setInvertSearch(boolean invertSearch)
      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

      public List<String> getExtensionsToMatch()
    • addExtensionsToMatch

      public FileScannerThread 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()).

      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

      public FileScannerThread clearExtensionsToMatch()
    • getResults

      public List<File> getResults()
    • addCompletionListener

      public FileScannerThread addCompletionListener(FileScannerThread.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

      public FileScannerThread addCancelListener(FileScannerThread.CancelListener listener)
      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 FileScannerThread removeCompletionListener(FileScannerThread.CompletionListener listener)
    • removeCancelListener

      public FileScannerThread removeCancelListener(FileScannerThread.CancelListener listener)
    • run

      public void run()