LiveDirsFX is a combination of a directory watcher, a directory-tree model (for TreeView) and a simple asynchronous file I/O facility. The extra benefits of this combination are:
enum ChangeSource {
INTERNAL, // indicates a change made by this application
EXTERNAL, // indicates an external change
}
// create LiveDirs to watch a directory
LiveDirs<ChangeSource> liveDirs = new LiveDirs<>(EXTERNAL);
Path dir = Paths.get("/path/to/watched/directory/");
liveDirs.addTopLevelDirectory(dir);
// use LiveDirs as a TreeView model
TreeView<Path> treeView = new TreeView<>(liveDirs.model().getRoot());
treeView.setShowRoot(false);
// handle external changes
liveDirs.model().modifications().subscribe(m -> {
if(m.getInitiator() == EXTERNAL) {
// handle external modification, e.g. reload the modified file
reload(m.getPath());
} else {
// modification done by this application, no extra action needed
}
});
// Use LiveDirs's I/O facility to write to the filesystem,
// in order to be able to distinguish between internal and external changes.
Path file = dir.resolve("some/file.txt");
liveDirs.io().saveUTF8File(file, "Hello text file!", INTERNAL);
// clean up
liveDirs.dispose();
Snapshot releases are deployed to Sonatype snapshot repository with these Maven coordinates
Group ID | Artifact ID | Version |
---|---|---|
org.fxmisc.livedirs | livedirsfx | 1.0.0-SNAPSHOT |
repositories {
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
compile group: 'org.fxmisc.livedirs', name: 'livedirsfx', version: '1.0.0-SNAPSHOT'
}
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
libraryDependencies += "org.fxmisc.livedirs" % "livedirsfx" % "1.0.0-SNAPSHOT"
Download the latest JAR or fat JAR (including dependencies) and place it on your classpath.