Skip to content

Commit

Permalink
Fork File Visitor Method
Browse files Browse the repository at this point in the history
* recursively search through directories with FileVisitor class instead of a recursive method
* misc reformat
  • Loading branch information
brp14005 committed Feb 13, 2016
1 parent a52e847 commit 51387bc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 28 deletions.
9 changes: 4 additions & 5 deletions src/main/java/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.util.ResourceBundle;

public class Controller{
private TreeItem<IronFile> root = new TreeItem<>();
private final Image hddIcon = new Image("/main/resources/icons/hdd.png");
@FXML private MenuItem fileOpen;
@FXML private MenuItem fileNew;
@FXML private MenuItem fileTag;
Expand All @@ -31,9 +29,10 @@ public class Controller{
@FXML private ResourceBundle resources;

@FXML private void initialize() {
//createTree();
FolderViewManager manager = new FolderViewManager(root, dirTree);
IronFile homeDir = new IronFile(System.getProperty("user.home"));
FolderViewManager manager = new FolderViewManager(dirTree); // 2 statements in 1 line is best
// IronFile[] hardDrives = IronFile.listRoots(); // an array of hard drives
// manager.setRootDirectory(hardDrives);
IronFile homeDir = new IronFile(System.getProperty("user.home")); // use this for specific directory
manager.setRootDirectory(homeDir);
}
}
Expand Down
65 changes: 47 additions & 18 deletions src/main/java/FolderViewManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
import java.util.concurrent.ExecutionException;

/**
Expand All @@ -21,37 +24,63 @@
@author Brian Patino
*/
public class FolderViewManager {
private TreeView<IronFile> view;
private TreeItem<IronFile> root;
private final Image hddIcon = new Image("/main/resources/icons/hdd.png");
private int NEST_COUNT = 0; //tracks number of nested calls when searching through files, TEMPORARY
private IronFileVisitor ironVisitor;
private TreeView<IronFile> view;

public FolderViewManager(TreeItem<IronFile> root, TreeView<IronFile> dirTree) {
this.root = root;
this.view = dirTree;
public FolderViewManager(TreeView<IronFile> dirTree) {
ironVisitor = new IronFileVisitor();
ironVisitor.setRoot(new TreeItem<>());
view = dirTree;
}

/**
* Original method to set a specified directory as root.
* */
public void setRootDirectory(IronFile file) {
root = new TreeItem<>(file);
view.setRoot(root);
createCellsFromRoot(file, root);
ironVisitor.setRoot(new TreeItem<>(file));
// createCellsFromRoot(file, root);
createTree(new TreeItem<>(file));
}
/**
* Overloaded method to set multiple hard drives as root.
* */
public void setRootDirectory(IronFile[] hardDrives) {
for (IronFile hdd : hardDrives) { // loop through all hard drives
TreeItem<IronFile> hddTreeItem = new TreeItem<>(hdd);
hddTreeItem.setGraphic(new ImageView(hddIcon));
createTree(hddTreeItem);
// createCellsFromRoot(hdd, parentTreeItem);
// ironVisitor.getRoot().getChildren().add(parentTreeItem); // set hdd to root
}
}

private void createCellsFromRoot(File rootFile, TreeItem<IronFile> rootNode) {
NEST_COUNT++;
if(NEST_COUNT > 1000) { //fixed value, TEMPORARY optimization
return;
}

for(IronFile file : IronFile.convertFiles(rootFile.listFiles())) {
if(!file.getName().startsWith(".")) { //don't show system files that start with dot (ex: .filename .pythonfile)
TreeItem<IronFile> fileNode = new TreeItem<>(file);
rootNode.getChildren().add(fileNode);
if (file.isDirectory()) {
createCellsFromRoot(file, fileNode);
if(NEST_COUNT <= 6000) { //fixed value, TEMPORARY optimization
for(IronFile file : IronFile.convertFiles(rootFile.listFiles())) {
if(!file.getName().startsWith(".") && !file.getName().startsWith("$")) { //don't show system files that start with dot (ex: .filename .pythonfile)
TreeItem<IronFile> fileNode = new TreeItem<>(file);
rootNode.getChildren().add(fileNode);
if (file.isDirectory()) {
createCellsFromRoot(file, fileNode);
}
}
}
}
}

private void createTree(TreeItem<IronFile> rootItem) {
Path pathDir = Paths.get(rootItem.getValue().getAbsolutePath()); // get the path of the TreeItem
try {
Files.walkFileTree(pathDir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), 2, ironVisitor);
} catch (IOException e) {
e.printStackTrace();
}
view.setRoot(ironVisitor.getRoot()); // set multiple hard drives as root
view.setShowRoot(false); // do not show blank
}
/**
* Set the child of a given parent TreeItem
*
Expand Down
28 changes: 23 additions & 5 deletions src/main/java/IronFileVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
@author Brian Patino
*/
public class IronFileVisitor extends SimpleFileVisitor<Path>{

private TreeItem<IronFile> root;
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// System.out.printf("Visiting file %s\n", file);
public FileVisitResult visitFile(Path pathFile, BasicFileAttributes attrs) throws IOException {
IronFile current = new IronFile(pathFile.toFile());
// System.out.println(current.getAbsolutePath());
// root.getChildren().add(new TreeItem<>(new IronFile(current.getAbsolutePath())));
System.out.printf("Visiting file %s\n", pathFile);
return FileVisitResult.CONTINUE;
}
/**
Expand All @@ -39,7 +42,22 @@ public FileVisitResult visitFileFailed(Path file, IOException e) throws IOExcept
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
// System.out.printf("About to visit directory %s\n", dir);
return FileVisitResult.CONTINUE;
IronFile currentFile = new IronFile(dir.toAbsolutePath().toString());
if (currentFile.getName().startsWith(".")) {
System.out.printf("Skipped directory: %s\n", dir.getFileName());
return FileVisitResult.SKIP_SUBTREE;
} else {
System.out.printf("About to visit directory: %s\n", dir.getFileName());
root.getChildren().add(new TreeItem<>(currentFile));
return FileVisitResult.CONTINUE;
}
}

public TreeItem<IronFile> getRoot() {
return root;
}

public void setRoot(TreeItem<IronFile> root) {
this.root = root;
}
}

0 comments on commit 51387bc

Please sign in to comment.