Skip to content

Commit

Permalink
Added IronFileFilter, OSDetection, and CmdExecutor. Added feature for…
Browse files Browse the repository at this point in the history
… tagging files and retrieving tags from files using the tool bar and the tree view. Can execute commands in shell, and detect operation system.
  • Loading branch information
kag12017 committed Feb 25, 2016
1 parent 673797c commit 4fbd297
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 24 deletions.
48 changes: 48 additions & 0 deletions src/main/java/CmdExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* Created by kristopherguzman on 2/19/16.
*/
public class CmdExecutor {

public CmdExecutor() { }

public String run(String cmd) throws IOException {

System.out.println("executing command: " + cmd);


String[] args = new String[] {"sh", "-c", "cd / && " + cmd};
Process process = Runtime.getRuntime().exec(args);
BufferedReader outputReader = new BufferedReader(new InputStreamReader(process.getInputStream())); //efficiently reads chars
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

String line = outputReader.readLine();
String error = errorReader.readLine();

System.out.println("Below is command output: \n ");
while(line != null) {

System.out.println("cmd output: " + line);
line = outputReader.readLine();

}

while(error != null) {

System.out.println("error output: " + error);
error = errorReader.readLine();

}

outputReader.close();
errorReader.close();
return null;

}

}
44 changes: 41 additions & 3 deletions src/main/java/Controller.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package main.java;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.MenuItem;
Expand All @@ -8,6 +14,7 @@
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import sun.misc.Resource;
import sun.reflect.generics.tree.Tree;

Expand All @@ -26,14 +33,45 @@ public class Controller{
@FXML private MenuItem fileExit;
@FXML private MenuItem editPreferences;
@FXML private TreeView<IronFile> dirTree;
@FXML private MenuItem toolsTagFiles;
@FXML private ResourceBundle resources;

@FXML private void initialize() {
FolderViewManager manager = new FolderViewManager(dirTree); // 2 statements in 1 line is best
final 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);

dirTree.setOnMouseClicked(new EventHandler<MouseEvent>() {

@Override
public void handle(MouseEvent args) {

ObservableList<TreeItem<IronFile>> selectedItems = dirTree.getSelectionModel().getSelectedItems();

manager.setSelectedFiles(selectedItems);

}

});

/**
Testing tagging. Be careful with this, tag removal is not implemented yet.
*/
toolsTagFiles.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {


//manager.setFileAttrForSelected();
manager.getFileAttrForSelected();

}


});


}
}

10 changes: 8 additions & 2 deletions src/main/java/FileTreeItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public FileTreeItem(IronFile rootFile) {

@Override
public ObservableList<TreeItem<IronFile>> getChildren() {
if (isFirstTimeChildren) {
if (isFirstTimeChildren) { //if children of node have not been created, then do so
isFirstTimeChildren = false;
super.getChildren().setAll(buildChildren(this));
}
Expand All @@ -39,12 +39,18 @@ public boolean isLeaf() {
}
private ObservableList<FileTreeItem> buildChildren(TreeItem<IronFile> ironTreeItem) {
IronFile f = ironTreeItem.getValue();

System.out.println("file's parent: " + f.getParent());

if (f != null && f.isDirectory()) {
IronFile[] files = f.listFiles();
if (files != null) {
ObservableList<FileTreeItem> children = FXCollections.observableArrayList();

for (IronFile childFile : files) {
children.add(new FileTreeItem(childFile));
if(childFile.filter.accept(childFile, childFile.getName())) {
children.add(new FileTreeItem(childFile));
}
}
return children;
}
Expand Down
178 changes: 160 additions & 18 deletions src/main/java/FolderViewManager.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,193 @@
package main.java;

import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.ArrayList;
import java.util.List;

/**
This class handles manipulation of the Folder View. This includes
directory searches, displaying directories, and all things directly changing
the Folder View.
Additionally, this class extends SimpleFileVisitor which uses java 8.
@author kristopherguzman
@author Kristopher Guzman kristopherguz@gmail.com
@author Brian Patino patinobrian@gmail.com
*/
public class FolderViewManager {

//private IronFileVisitor ironVisitor; // might be used later
private final Image hddIcon = new Image("/main/resources/icons/hdd.png");
// private IronFileVisitor ironVisitor; // might be used later
private TreeView<IronFile> view;
private CmdExecutor command;
private List<TreeItem<IronFile>> selectedFiles;

public FolderViewManager(TreeView<IronFile> dirTree) {
/*ironVisitor = new IronFileVisitor(); // save this for later
ironVisitor.setRoot(new TreeItem<>());*/ // save this for later

OSDetection.getOS();
view = dirTree;
command = new CmdExecutor();

}

/**
* Sets the root directory of the file browser to the specified folder.
*
* @param file root folder/file to start browser view
**/
public void setRootDirectory(IronFile file) {
FileTreeItem rootItem = new FileTreeItem(file);
view.setRoot(rootItem);
}
/**
* Overloaded method that sets a collection of folders/files as file browser view.
*
* @param hardDrives a collection of hard drives to being from root
* */

public void setRootDirectory(IronFile[] hardDrives) {
view.setRoot(new FileTreeItem(new IronFile(""))); // needs a blank file as root
for (IronFile hdd : hardDrives) {
System.out.println(hdd.getName());
FileTreeItem diskTreeItem = new FileTreeItem(hdd);
diskTreeItem.setGraphic(new ImageView(hddIcon));
view.getRoot().getChildren().add(diskTreeItem);
}
view.setShowRoot(false); // hide the blank file
}

public void setSelectedFiles(List<TreeItem<IronFile>> files) {

selectedFiles = files;

}

public void setFileAttrForSelected() {

for(TreeItem<IronFile> item : selectedFiles) {

setFileAttr(item.getValue(), "test_attr_key", "test_attr_value");

}

}

public void getFileAttrForSelected() {

for(TreeItem<IronFile> item : selectedFiles) {

getFileAttr(item.getValue(), "test_attr_key");

}

}

public void setFileAttr(IronFile file, String key, String value) {

if(OSDetection.OSType == OSDetection.OS.WINDOWS) {

try {

UserDefinedFileAttributeView view = Files.getFileAttributeView(file.toPath(), UserDefinedFileAttributeView.class);

//might want to give unique prefix to tag keys to avoid collision with system metadata
view.write(key, Charset.defaultCharset().encode(value));

} catch(IOException e) { e.printStackTrace(); }

} else if(OSDetection.OSType == OSDetection.OS.MAC) {

String option = "";

if(file.isDirectory()) {
option = "-r";
}


String cmd = "xattr -w " + option + " " + key + " " + value + " " + file.getAbsolutePath();

try {

String output = command.run(cmd);

} catch(IOException e) { e.printStackTrace(); }


}

}

public String getFileAttr(IronFile file, String key) {

if(OSDetection.OSType == OSDetection.OS.WINDOWS) {

try {
UserDefinedFileAttributeView view = Files.getFileAttributeView(file.toPath(), UserDefinedFileAttributeView.class);

ByteBuffer buf = ByteBuffer.allocate(view.size(key));
view.read(key, buf);
buf.flip();

return Charset.defaultCharset().decode(buf).toString();

} catch(IOException e) { e.printStackTrace(); }

} else if(OSDetection.OSType == OSDetection.OS.MAC) {

System.out.println("file path: " + file.getAbsolutePath());

String option = "";

if(file.isDirectory()) {
//option = "-r";
}


String cmd = "xattr -p " + option + " " + key + " " + file.getAbsolutePath(); //then append the attr command

try {

String output = command.run(cmd);

} catch(IOException e) { e.printStackTrace(); }

}

return null;

}

public String removeFileAttr(IronFile file, String key) {

if(OSDetection.OSType == OSDetection.OS.WINDOWS) {

try {

return (String) Files.getAttribute(file.toPath(), key);

} catch(IOException e) { e.printStackTrace(); }

} else if(OSDetection.OSType == OSDetection.OS.MAC) {

System.out.println("file path: " + file.getAbsolutePath());

String option = "";

if(file.isDirectory()) {
//option = "-r";
}


String cmd = "xattr -p " + option + " " + key + " " + file.getAbsolutePath(); //then append the attr command

try {

String output = command.run(cmd);

} catch(IOException e) { e.printStackTrace(); }

}

return null;

}

}
5 changes: 5 additions & 0 deletions src/main/java/IronFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
* This class extends the java File class and returns the filename for toString()
*/
public class IronFile extends File {

private boolean isRoot = true;
public IronFileFilter filter;

public IronFile(String pathname) {
super(pathname);
isRoot = (getParent() == null);
filter = new IronFileFilter();
}
public IronFile(File file) {
super(file.getPath());
isRoot = (getParent() == null);
filter = new IronFileFilter();

}

@Override
Expand Down
Loading

0 comments on commit 4fbd297

Please sign in to comment.