Module xaos.ui

Class TreeDirectoryMonitor<I,​T>

  • Type Parameters:
    I - Type of the external initiator of the I/O operation.
    T - Type of the object returned by TreeItem.getValue().
    All Implemented Interfaces:
    io.reactivex.disposables.Disposable

    public class TreeDirectoryMonitor<I,​T>
    extends Object
    implements io.reactivex.disposables.Disposable
    TreeDirectoryMonitor combines a DirectoryWatcher, a TreeDirectoryModel, and a TreeDirectoryAsynchronousIO. The added value of this combination is:
    • The tree model is updated automatically to reflect the current state of the file-system;
    • The application can distinguish file-system changes made via the I/O facility (TreeDirectoryAsynchronousIO) from external ones.

    The directory model can be used directly as a model for TreeViews.

    Usage:

       public class UsageExample extends Application {
    
         enum ChangeSource {
           INTERNAL,
           EXTERNAL
         };
    
         public static void main( String[] args ) {
           launch(args);
         }
    
         @Override
         public void start(Stage primaryStage) {
    
           TreeView<Path> view = new TreeView<>();
    
           view.setShowRoot(false);
    
           try {
    
             // Create a TreeDirectoryMonitor instance for use
             // on the JavaFX Application Thread.
             TreeDirectoryMonitor<ChangeSource, Path> dirmon = TreeDirectoryMonitor.build(ChangeSource.EXTERNAL);
     
             // Set directory to watch.
             dirmon.addTopLevelDirectory(Paths.get(System.getProperty("user.home"), "Documents").toAbsolutePath());
             view.setRoot(dirmon.model().getRoot());
             view.setShowRoot(false);
             view.setCellFactory(TreeItems.defaultTreePathCellFactory());
     
             // Stop DirectoryWatcher's thread.
             primaryStage.setOnCloseRequest(val -> dirmon.dispose());
    
           } catch (IOException e) {
             e.printStackTrace();
           }
    
           primaryStage.setOnCloseRequest(event -> dirmon.dispose());
           primaryStage.setScene(new Scene(view, 500, 500));
           primaryStage.show();
    
         }
    
       }
     

    Note: dispose() should be called when the model is no more used (typically when the viewer using it is disposed).

    Author:
    claudio.rosati@esss.se
    See Also:
    LiveDirsFX:org.fxmisc.livedirs.LiveDirsIO
    • Method Detail

      • build

        public static <I> TreeDirectoryMonitor<I,​Path> build​(I externalInitiator)
                                                            throws IOException
        Creates a TreeDirectoryMonitor instance to be used from the JavaFX application thread.
        Type Parameters:
        I - Type of the external initiator of the I/O operation.
        Parameters:
        externalInitiator - Object to represent an initiator of an external file-system change.
        Returns:
        A newly created TreeDirectoryMonitor's instance.
        Throws:
        IOException - If an I/O error occurs.
      • build

        public static <I> TreeDirectoryMonitor<I,​Path> build​(I externalInitiator,
                                                                   Executor clientThreadExecutor)
                                                            throws IOException
        Creates a TreeDirectoryMonitor instance to be used from a designated thread.
        Type Parameters:
        I - Type of the external initiator of the I/O operation.
        Parameters:
        externalInitiator - Object to represent an initiator of an external file-system change.
        clientThreadExecutor - Executor used to execute actions on the caller thread. Used to publish updates and errors on the caller thread.
        Returns:
        A newly created TreeDirectoryMonitor's instance.
        Throws:
        IOException - If an I/O error occurs.
      • build

        public static <I,​T> TreeDirectoryMonitor<I,​T> build​(I externalInitiator,
                                                                        Function<T,​Path> projector,
                                                                        Function<Path,​T> injector,
                                                                        Executor clientThreadExecutor)
                                                                 throws IOException
        Creates a TreeDirectoryMonitor instance to be used from a designated thread.
        Type Parameters:
        I - Type of the external initiator of the I/O operation.
        T - Type of the object returned by TreeItem.getValue().
        Parameters:
        externalInitiator - Object to represent an initiator of an external file-system change.
        projector - Converts the (T) TreeItem.getValue() into a Path object.
        injector - Converts a given Path object into T. The reverse of projector.
        clientThreadExecutor - Executor used to execute actions on the caller thread. Used to publish updates and errors on the caller thread.
        Returns:
        A newly created TreeDirectoryMonitor's instance.
        Throws:
        IOException - If an I/O error occurs.
      • addTopLevelDirectory

        public void addTopLevelDirectory​(Path dir)
        Adds a directory to watch. The directory will be added to the directory model and watched for changes.
        Parameters:
        dir - The directory to be watched and viewed.
      • addTopLevelDirectory

        public void addTopLevelDirectory​(Path dir,
                                         Consumer<? super TreeDirectoryItems.DirectoryItem<T>> onCollapse,
                                         Consumer<? super TreeDirectoryItems.DirectoryItem<T>> onExpand)
        Adds a directory to watch. The directory will be added to the directory model and watched for changes.
        Parameters:
        dir - The directory to be watched and viewed.
        onCollapse - A Consumer to be invoked when this item is collapsed. Can be null.
        onExpand - A Consumer to be invoked when this item is expanded. Can be null.
      • dispose

        public void dispose()
        Releases resources used by this TreeDirectoryMonitor instance. In particular, stops the I/O thread (used for I/O operations as well as directory watching).
        Specified by:
        dispose in interface io.reactivex.disposables.Disposable
      • errors

        public io.reactivex.Observable<Throwable> errors()
        Returns:
        The Observable of asynchronously thrown errors.
      • io

        public TreeDirectoryAsynchronousIO<I,​T> io()
        Returns:
        The asynchronous I/O facility. All I/O operations performed by this facility are performed on a single thread. It is the same thread that is used to watch the file-system for changes.
      • isDisposed

        public boolean isDisposed()
        Specified by:
        isDisposed in interface io.reactivex.disposables.Disposable