Class DirectoryScannerThread

java.lang.Object
ca.corbett.extras.progress.SimpleProgressWorker
ca.corbett.extras.io.DirectoryScannerThread
All Implemented Interfaces:
Runnable

public class DirectoryScannerThread extends SimpleProgressWorker
A handy utility class that can wrap the findSubdirectories 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 subdirectories in a thread-safe way (that is, without blocking the UI thread).

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

    • DirectoryScannerThread

      public DirectoryScannerThread(File rootDir)
  • Method Details

    • isRecursive

      public boolean isRecursive()
    • setRecursive

      public DirectoryScannerThread setRecursive(boolean recursive)
      Decides whether the search should include subdirectories recursively, or just the immediate children of rootDir. By default, all searches are recursive.
    • getResults

      public List<File> 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

      public DirectoryScannerThread removeCancelListener(DirectoryScannerThread.CancelListener listener)
    • run

      public void run()