TomasMikula / LiveDirsFX

Directory tree model for JavaFX that watches the filesystem for changes.
BSD 2-Clause "Simplified" License
48 stars 10 forks source link

LiveDirsFX

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:

  1. Automatic synchronization of the directory model with the filesystem.
  2. Ability to distinguish directory and file modifications made by the application (through the I/O facility) from external modifications.

Example

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();

Use LiveDirsFX in your project

Method 1: as a managed dependency (recommended)

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

Gradle example

repositories {
    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots/' 
    }
}

dependencies {
    compile group: 'org.fxmisc.livedirs', name: 'livedirsfx', version: '1.0.0-SNAPSHOT'
}

Sbt example

resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"

libraryDependencies += "org.fxmisc.livedirs" % "livedirsfx" % "1.0.0-SNAPSHOT"

Method 2: as an unmanaged dependency

Download the latest JAR or fat JAR (including dependencies) and place it on your classpath.

Links

Javadoc