- java.lang.Object
-
- eu.ess.xaos.ui.control.tree.directory.TreeDirectoryMonitor<I,T>
-
- Type Parameters:
I
- Type of the external initiator of the I/O operation.T
- Type of the object returned byTreeItem.getValue()
.
- All Implemented Interfaces:
io.reactivex.disposables.Disposable
public class TreeDirectoryMonitor<I,T> extends Object implements io.reactivex.disposables.Disposable
TreeDirectoryMonitor combines aDirectoryWatcher
, aTreeDirectoryModel
, and aTreeDirectoryAsynchronousIO
. 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
TreeView
s.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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addTopLevelDirectory(Path dir)
Adds a directory to watch.void
addTopLevelDirectory(Path dir, Consumer<? super TreeDirectoryItems.DirectoryItem<T>> onCollapse, Consumer<? super TreeDirectoryItems.DirectoryItem<T>> onExpand)
Adds a directory to watch.static <I> TreeDirectoryMonitor<I,Path>
build(I externalInitiator)
Creates aTreeDirectoryMonitor
instance to be used from the JavaFX application thread.static <I> TreeDirectoryMonitor<I,Path>
build(I externalInitiator, Executor clientThreadExecutor)
Creates aTreeDirectoryMonitor
instance to be used from a designated thread.static <I,T>
TreeDirectoryMonitor<I,T>build(I externalInitiator, Function<T,Path> projector, Function<Path,T> injector, Executor clientThreadExecutor)
Creates aTreeDirectoryMonitor
instance to be used from a designated thread.void
dispose()
Releases resources used by thisTreeDirectoryMonitor
instance.io.reactivex.Observable<Throwable>
errors()
TreeDirectoryAsynchronousIO<I,T>
io()
boolean
isDisposed()
TreeDirectoryModel<I,T>
model()
-
-
-
Method Detail
-
build
public static <I> TreeDirectoryMonitor<I,Path> build(I externalInitiator) throws IOException
Creates aTreeDirectoryMonitor
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 aTreeDirectoryMonitor
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 aTreeDirectoryMonitor
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 byTreeItem.getValue()
.- Parameters:
externalInitiator
- Object to represent an initiator of an external file-system change.projector
- Converts the (T
)TreeItem.getValue()
into aPath
object.injector
- Converts a givenPath
object intoT
. The reverse ofprojector
.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.
-
dispose
public void dispose()
Releases resources used by thisTreeDirectoryMonitor
instance. In particular, stops the I/O thread (used for I/O operations as well as directory watching).- Specified by:
dispose
in interfaceio.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 interfaceio.reactivex.disposables.Disposable
-
model
public TreeDirectoryModel<I,T> model()
- Returns:
- The observable tree directory model.
-
-