Skip to content

Commit

Permalink
Add Second Level
Browse files Browse the repository at this point in the history
* add second level to generate files
* extend File in IronFile
  • Loading branch information
brp14005 committed Feb 11, 2016
1 parent b249080 commit 00e0cee
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 30 deletions.
51 changes: 22 additions & 29 deletions src/main/java/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import java.util.ResourceBundle;

public class Controller{
private TreeItem<File> root = new TreeItem<>();
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;
@FXML private MenuItem fileExit;
@FXML private MenuItem editPreferences;
@FXML private TreeView<File> dirTree;
@FXML private TreeView<IronFile> dirTree;
@FXML private ResourceBundle resources;

@FXML private void initialize() {
Expand All @@ -37,13 +37,12 @@ public class Controller{

// Create the directory Tree
private void createTree() {
// Get the hard disk drives and 2 level down
File[] roots = File.listRoots();
for (File hdd : roots) {
TreeItem<File> parentTreeItem = new TreeItem<>(hdd);
// Get the hard disk drives and 2 levels down
IronFile[] roots = IronFile.listRoots();
for (IronFile hdd : roots) {
TreeItem<IronFile> parentTreeItem = new TreeItem<>(hdd);
parentTreeItem.setGraphic(new ImageView(hddIcon));
// setChildOfParent(parentTreeItem);
parentTreeItem.getChildren().add(new TreeItem<>(new File("Users"))); // test
setChildOfParent(hdd, parentTreeItem);
root.getChildren().add(parentTreeItem); // set hdd to root

}
Expand All @@ -57,27 +56,21 @@ public TreeCell<File> call(TreeView<File> param) {
}
});*/
}

private void setChildOfParent(TreeItem<File> parent) {
// String rootPath = parent.getPath();
try {
// Loop through all of the files/folders from parent at depth 2
Files.walkFileTree(Paths.get(parent.getValue().getAbsolutePath()), EnumSet.of(FileVisitOption.FOLLOW_LINKS), 2, new FolderViewManager()).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
File childFile = filePath.toFile(); // create a file from working path
TreeItem<File> itemChild = new TreeItem<>(childFile); // create a tree item from child file
parent.getChildren().add(itemChild);
itemChild.setExpanded(false);
System.out.println(filePath);
}
});
/*Files.walk(Paths.get("C:\\Users\\Brian")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
// System.out.println(filePath);
}
});*/
} catch (IOException e) {
e.printStackTrace();
/**
* Set the child of a given parent TreeItem
*
* @param parent file of the parent node
* @param parentTreeItem TreeItem of the parent node
* */
@SuppressWarnings("ConstantConditions")
private void setChildOfParent(final File parent, TreeItem<IronFile> parentTreeItem) {
for (final IronFile file : IronFile.convertFiles(parent.listFiles())) {
if (file.isDirectory()) {
System.out.println("This is a directory");
} else {
parentTreeItem.getChildren().add(new TreeItem<>(file));
System.out.println(file.getName());
}
}
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/java/IronFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

package main.java;

import java.io.File;

/**
* This class extends the java File class and returns the filename for toString()
*/
public class IronFile extends File {
private boolean isRoot = true;

public IronFile(String pathname) {
super(pathname);
isRoot = (getParent() == null);

}
public IronFile(File file) {
super(file.getPath());
isRoot = (getParent() == null);
}

@Override
public IronFile[] listFiles() {
return convertFiles(super.listFiles());
}

/**
* Return the available hard drive disks.
* */
public static IronFile[] listRoots() {
return convertFiles(File.listRoots());
}

public static IronFile[] convertFiles(File[] files) {
IronFile[] ironFiles = new IronFile[files.length];
for (int i = 0; i < files.length; i++) {
ironFiles[i] = new IronFile(files[i]);
}
return ironFiles;
}

public boolean isRoot() {
return isRoot;
}

public void setRoot(boolean root) {
isRoot = root;
}

@Override
public String toString() {
if (isRoot) {
return this.getAbsolutePath();
} else {
return this.getName();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void handle(ActionEvent event) {
// Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();

primaryStage.setTitle("Iron-gate!");
primaryStage.setScene(new Scene(root, 990, 785));
primaryStage.setScene(new Scene(root, 990, 700));

Scene scene = primaryStage.getScene(); // we get the scene from above
scene.getStylesheets().clear(); // clear any styles
Expand Down

0 comments on commit 00e0cee

Please sign in to comment.