in kit/src/main/java/com/oracle/javafx/scenebuilder/kit/library/user/LibraryFolderWatcher.java [163:257]
private void runWatching() throws InterruptedException {
while (true) {
final Path folder = Paths.get(library.getPath());
WatchService watchService = null;
while (watchService == null) {
try {
watchService = folder.getFileSystem().newWatchService();
} catch(IOException x) {
System.out.println("FileSystem.newWatchService() failed"); //NOI18N
System.out.println("Sleeping..."); //NOI18N
Thread.sleep(1000 /* ms */);
}
}
WatchKey watchKey = null;
while ((watchKey == null) || (watchKey.isValid() == false)) {
try {
watchKey = folder.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
WatchKey wk;
do {
wk = watchService.take();
assert wk == watchKey;
boolean isDirty = false;
for (WatchEvent<?> e: wk.pollEvents()) {
final WatchEvent.Kind<?> kind = e.kind();
final Object context = e.context();
if (kind == StandardWatchEventKinds.ENTRY_CREATE
|| kind == StandardWatchEventKinds.ENTRY_DELETE
|| kind == StandardWatchEventKinds.ENTRY_MODIFY) {
assert context instanceof Path;
if (LibraryUtil.isJarPath((Path) context)) {
if (!hasJarBeenAdded((Path) context)) {
isDirty = true;
}
} else if (LibraryUtil.isFxmlPath((Path)context)){
isDirty = true;
} else if (LibraryUtil.isFolderMarkerPath((Path)context)) {
isDirty = true;
}
} else {
assert kind == StandardWatchEventKinds.OVERFLOW;
}
}
// We reconstruct a full set from scratch as soon as the
// dirty flag is set.
if (isDirty) {
// First put the builtin items in the library
library.setExploring(true);
try {
library.setItems(BuiltinLibrary.getLibrary().getItems());
// Now attempts to add the maven jars
List<Path> currentMavenJars = library.getAdditionalJarPaths().get();
final Set<Path> fxmls = new HashSet<>();
fxmls.addAll(getAllFiles(FILE_TYPE.FXML));
updateLibrary(fxmls);
final Set<Path> jarsAndFolders = new HashSet<>(currentMavenJars);
jarsAndFolders.addAll(getAllFiles(FILE_TYPE.JAR));
Set<Path> foldersMarkers = getAllFiles(FILE_TYPE.FOLDER_MARKER);
for (Path path : foldersMarkers) {
// open folders marker file: every line should be a single folder entry
// we scan the file and add the path to currentJarsOrFolders
List<Path> folderPaths = LibraryUtil.getFolderPaths(path);
for (Path f : folderPaths) {
jarsAndFolders.add(f);
}
}
exploreAndUpdateLibrary(jarsAndFolders);
library.updateExplorationCount(library.getExplorationCount()+1);
}
finally {
library.setExploring(false);
}
}
} while (wk.reset());
} catch(IOException x) {
Thread.sleep(1000 /* ms */);
}
}
}
}