I made a Tree object which is loaded with files from a directory.
preVisistDirectory iterates faster then visitFile can follow to fill the tree.
I tried to build a variable which is called after the files are read and the treeitem created to read the next directory but didn’t work.
Anyone?
public static void main(String[] args) {
// getfiles(filepath,"*html");
System.out.println("Help -> setHelp() -> Path = " + filepath);
Display display = new Display();
Shell shell = new Shell(display, SWT.CLOSE | SWT.RESIZE);
shell.setText("Tree Object. ");
shell.setSize(400, 300);
final Tree tree = new Tree(shell, SWT.BORDER | SWT.SINGLE);
tree.setSize(290, 290);
try {
Files.walkFileTree(filepath, new SimpleFileVisitor<Path>() {
TreeItem parent, child;
int baselevel = filepath.getNameCount();
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) {
System.out.println("Help -> FileVisitResults -> DirectoryName = " + dir.getFileName().toString());
System.out.println("Help -> FileVisitResults -> Find files and Directories : " + dir.getName(0));
System.out.println("Help -> FileVisitResults -> nameCount : " + (dir.getNameCount() - baselevel));
System.out.println("Help -> FileVisitResults -> baselevel = : " + (dir.getNameCount() - baselevel));
if (dir.getNameCount() - baselevel + 1 > 1) {
if (dir.getNameCount() - baselevel == 1) {
parent = null;
}
if (parent != null) {
child = new TreeItem(parent, 0);
child.setText(dir.getFileName().toString());
parent = child;
}
if (parent == null) {
child = new TreeItem(tree, 0);
child.setText(dir.getFileName().toString());
parent = child;
}
}
return FileVisitResult.CONTINUE;
}
public FileVisitResult visitFile(Path dir,
BasicFileAttributes attrs) {
System.out.println("Help -> FileVisitResults -> FileName = " + dir);
if (dir.getNameCount() - baselevel + 1 > 1) {
if (dir.getNameCount() - baselevel == 1) {
parent = null;
}
if (parent != null) {
child = new TreeItem(parent, 0);
child.setText(dir.getFileName().toString());
}
if (parent == null) {
child = new TreeItem(tree, 0);
child.setText(dir.getFileName().toString());
}
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Answer:
There are no exceptions, but loops faster through the directories then the files can be added to the directories. So when there are 5 files to be added by vistitFile after 2 files the preDirectoryVisit sets parent to the next directory so visitFile skips the remaining files.
Answer 2:
The tree loads the directories with preVisitDirectory method wich itterates faster over over the directories then visitFile iterates over the files. Because the found directory is stored in the parent variable which is used ass the parent treeitem to a add the file as a child treeitem.
Your concept of
parentandchildnode doesn’t really work out. I managed to get it working using twoHashMaps. One for the directories and one for the files. This way, you can easily find theparentof your current file/directory:As you can read here the order of traversal is not guaranteed to be the same as in your file manager. This is why I collect the children of a directory before adding them to the tree. The directories needn’t be sorted though.
Here is how the tree looks:
And this is the folder structure: