use of java.nio.file.NotDirectoryException in project sldeditor by robward-scisys.
the class FileTreeNode method populateDirectories.
/**
* Populate directories. For display purposes, we return our own name public String toString() {
* return name; } If we are a directory, scan our contents and populate with children. In
* addition, populate those children if the "descend" flag is true. We only descend once, to
* avoid recursing the whole subtree.
*
* @param descend the descend
* @return true, if successful
*/
public boolean populateDirectories(boolean descend) {
boolean addedNodes = false;
if (!isRoot || (isRoot && descend)) {
// Do this only once
if (!populated) {
if (interim) {
// We have had a quick look here before:
// remove the dummy node that we added last time
removeAllChildren();
interim = false;
}
// Get list of contents
List<Path> names = new ArrayList<>();
DirectoryStream<Path> stream = null;
try {
Path pathPath = Paths.get(path);
stream = Files.newDirectoryStream(pathPath);
if (stream != null) {
for (Path localPath : stream) {
names.add(localPath.getFileName());
}
}
} catch (AccessDeniedException e) {
// Access was denied
} catch (NotDirectoryException e) {
// Ignore
} catch (IOException e) {
ConsoleManager.getInstance().exception(this, e);
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
ConsoleManager.getInstance().exception(this, e);
}
}
// Process the directories
for (Path filename : names) {
Path d = Paths.get(path, filename.toString());
try {
if (d.toFile().isDirectory()) {
addFolder(descend, filename.toString());
addedNodes = true;
if (!descend) {
// Only add one node if not descending
break;
}
} else if (d.toFile().isFile()) {
if (validFile(filename.toString())) {
addFile(filename.toString());
}
}
} catch (Exception e) {
// Ignore phantoms or access problems
}
}
// so that we look again in the future if we need to
if (descend || !addedNodes) {
populated = true;
if (isDir() && !fileWatcherSet) {
Path pathPath = Paths.get(path);
FileSystemWatcher.getInstance().addWatch(this, pathPath);
fileWatcherSet = true;
}
} else {
// Just set interim state
interim = true;
}
}
}
return addedNodes;
}
Aggregations