TDT4100-project/src/main/java/app/service/FiletreeOperations.java

139 lines
3.9 KiB
Java
Raw Normal View History

2021-04-20 11:28:45 +02:00
package app.service;
import java.io.File;
2021-04-25 00:03:48 +02:00
import java.io.FileNotFoundException;
2021-04-20 11:28:45 +02:00
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import app.model.Model;
import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.TreeItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class FiletreeOperations {
2021-04-23 00:10:33 +02:00
private static int iconSize = 20;
private FiletreeOperations() {}
2021-04-20 11:28:45 +02:00
/**
* The method to generate the fileTree recursively. If it is a directory a
* CheckBoxStringItem is created and the method is called again. It goes through
* all until every directory or file inside the orginal CheckBoxItem is made. If
* the item is a file it sends it to the help function checkExtension which is
* described below.
*/
public static void generateTree(File file, CheckBoxTreeItem<String> parent) {
Image folder = new Image(FiletreeOperations.class.getResourceAsStream("/graphics/folder.png"));
if (file.isDirectory()) {
2021-04-23 00:10:33 +02:00
ImageView icon = new ImageView(folder);
icon.setFitHeight(iconSize);
icon.setFitWidth(iconSize);
CheckBoxTreeItem<String> element = new CheckBoxTreeItem<>(file.getName(), icon);
2021-04-20 11:28:45 +02:00
parent.getChildren().add(element);
List<File> dirList = new ArrayList<>();
List<File> fileList = new ArrayList<>();
sortFiles(dirList, fileList, file);
for (File f : dirList) {
generateTree(f, element);
}
} else {
2021-04-21 19:01:51 +02:00
try {
2021-04-23 00:10:33 +02:00
ImageView icon = new ImageView(getIconForFile(file));
icon.setFitHeight(iconSize);
icon.setFitWidth(iconSize);
CheckBoxTreeItem<String> element = new CheckBoxTreeItem<>(file.getName(), icon);
2021-04-21 19:01:51 +02:00
parent.getChildren().add(element);
2021-04-23 00:10:33 +02:00
2021-04-21 19:01:51 +02:00
} catch (Exception e) {
2021-04-25 22:46:07 +02:00
System.err.println("[ERROR] Default file icon not found");
System.err.println(e);
2021-04-21 19:01:51 +02:00
}
2021-04-20 11:28:45 +02:00
}
}
/**
2021-04-26 21:41:11 +02:00
* A helping function to sort the files/directories in the fileTree so that it shows
2021-04-20 11:28:45 +02:00
* in the correct order.
*/
private static void sortFiles(List<File> dirList, List<File> fileList, File file) {
for (File f : file.listFiles()) {
if (f.isDirectory())
dirList.add(f);
else
fileList.add(f);
}
dirList.addAll(fileList);
}
private static Image getIconForFile(File file) throws IOException {
Image icon;
try {
2021-04-23 00:10:33 +02:00
String mimeType = Files.probeContentType(file.toPath());
2021-04-20 11:28:45 +02:00
if (mimeType == null) throw new IOException();
2021-04-23 00:10:33 +02:00
String iconPath = "/graphics/filetreeicons/" + mimeType.replace('/', '-') + ".png";
2021-04-20 11:28:45 +02:00
InputStream imageData = FiletreeOperations.class.getResourceAsStream(iconPath);
if (imageData == null) throw new IOException();
icon = new Image(imageData);
} catch (IOException e) {
2021-04-25 22:46:07 +02:00
System.err.println("[WARNING] Icon not found: " + file.getPath());
2021-04-20 11:28:45 +02:00
// String iconPath = "/graphics/filetreeicons/file.png";
2021-04-23 00:10:33 +02:00
String iconPath = "/graphics/filetreeicons/unknown.png";
2021-04-20 11:28:45 +02:00
InputStream imageData = FileOperations.class.getResourceAsStream(iconPath);
if (imageData == null) throw new IOException();
icon = new Image(imageData);
}
return icon;
}
2021-04-25 00:03:48 +02:00
public static Path getPathOfTreeItem(TreeItem<String> item) throws FileNotFoundException {
Path projectPath =
2021-04-21 19:01:51 +02:00
Model
.getProjectPath()
2021-04-25 00:03:48 +02:00
.orElseThrow(() -> new IllegalStateException());
2021-04-26 21:46:54 +02:00
final String rootDirName =
2021-04-25 00:03:48 +02:00
projectPath
2021-04-21 19:01:51 +02:00
.getFileName()
.toString();
String path = "";
2021-04-26 21:46:54 +02:00
while (!rootDirName.equals(item.getValue())) {
2021-04-21 19:01:51 +02:00
path = File.separator + item.getValue() + path;
item = item.getParent();
2021-04-25 00:03:48 +02:00
if (item == null)
throw new FileNotFoundException();
2021-04-21 19:01:51 +02:00
}
2021-04-20 11:28:45 +02:00
2021-04-25 00:03:48 +02:00
path = projectPath.toString() + path;
2021-04-20 11:28:45 +02:00
2021-04-21 19:01:51 +02:00
Path pathToString = Paths.get(path);
return pathToString;
}
2021-04-20 11:28:45 +02:00
}