Skip to content

Commit

Permalink
Add Icon to Root
Browse files Browse the repository at this point in the history
* add recursive finder for all files/directories (testing)
* extend FolderViewManager.java
* add hdd icons
  • Loading branch information
brp14005 committed Feb 11, 2016
1 parent 612a697 commit b249080
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 37 deletions.
52 changes: 43 additions & 9 deletions src/main/java/Controller.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
package main.java;

import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.util.Callback;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import sun.misc.Resource;
import sun.reflect.generics.tree.Tree;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.ResourceBundle;

public class Controller{
private TreeItem<File> 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 ResourceBundle resources;

@FXML private void initialize() {
Expand All @@ -31,14 +37,19 @@ public class Controller{

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

}
dirTree.setRoot(root);
dirTree.setShowRoot(false);

/*dirTree.setCellFactory(new Callback<TreeView<File>, TreeCell<File>>() {
@Override
public TreeCell<File> call(TreeView<File> param) {
Expand All @@ -47,6 +58,29 @@ 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();
}
}

private class TreeFieldImpl extends TreeCell<File> {
public TreeFieldImpl() {
// MenuItem
Expand Down
59 changes: 35 additions & 24 deletions src/main/java/FolderViewManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,67 @@
import javafx.scene.control.TreeView;

import java.io.File;

/**
* Created by kristopherguzman on 2/7/16.
*/
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

/**
This class handles manipulation of the Folder View. This includes
directory searches, displaying directories, and all things directly changing
the Folder View.
*/
public class FolderViewManager {

public static TreeView<String> treeView;
private static TreeItem<String> root;
private static int NEST_COUNT = 0; //tracks number of nested calls when searching through files, TEMPORARY
private FolderViewManager() { }
Additionally, this class extends SimpleFileVisitor which uses java 8.
public static void setRootDirectory(File file) {
@author kristopherguzman
@author Brian Patino
*/
public class FolderViewManager extends SimpleFileVisitor<Path>{
// public static TreeView<String> treeView;
// private static TreeItem<String> root;
// private static int NEST_COUNT = 0; //tracks number of nested calls when searching through files, TEMPORARY

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// System.out.printf("Visiting file %s\n", file);
return FileVisitResult.CONTINUE;
}
/**
* {@inheritDoc}
* This method must be overridden so that walking the tree can continue.
* */
@Override
public FileVisitResult visitFileFailed(Path file, IOException e) throws IOException {
System.err.printf("Visiting failed for %s\n", file);
return FileVisitResult.SKIP_SUBTREE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
// System.out.printf("About to visit directory %s\n", dir);
return FileVisitResult.CONTINUE;
}

/*public static void setRootDirectory(File file) {
root = new TreeItem<String>(file.getName());
createCellsFromRoot(file, root);
treeView.setRoot(root);
}
private static void createCellsFromRoot(File rootFile, TreeItem<String> rootNode) {

NEST_COUNT++;

if(NEST_COUNT > 6000) { //fixed value, TEMPORARY, must come up with better way to optimize load time of files

return;
}
for(File f : rootFile.listFiles()) {

if(!f.getName().startsWith(".")) { //don't show files that start with dot (ex: .filename .pythonfile)

TreeItem<String> fileNode = new TreeItem<String>(f.getName());
rootNode.getChildren().add(fileNode);

if (f.isDirectory()) {

createCellsFromRoot(f, fileNode);

}
}

}

}

}
*/}
13 changes: 9 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import javafx.stage.Stage;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main extends Application {

Expand All @@ -26,16 +28,19 @@ public void handle(ActionEvent event) {
}
});*/
Parent root = FXMLLoader.load(getClass().getResource("/main/resources/StartPage.fxml"));
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
// Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();

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

Scene scene = primaryStage.getScene(); // we get the scene from above
scene.getStylesheets().clear(); // clear any styles
scene.getStylesheets().add("/main/resources/mainStyle.css"); // absolute path
// scene.getStylesheets().add("/main/resources/mainStyle.css"); // absolute path

try { //try to set the tree view in the FolderViewManager class
/**
* Temporally omitted, needs to be transfered to the Controller Class
*/
/*try { //try to set the tree view in the FolderViewManager class
FolderViewManager.treeView = (TreeView<String>) scene.lookup("#folder-view-pane");
File homeDir = new File(System.getProperty("user.home"));
Expand All @@ -45,7 +50,7 @@ public void handle(ActionEvent event) {
} catch (Exception e) {
e.printStackTrace();
}
}*/

primaryStage.show();
}
Expand Down
Binary file added src/main/resources/icons/hdd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b249080

Please sign in to comment.