Extract logic from classes
This commit is contained in:
146
src/main/java/app/service/FiletreeOperations.java
Normal file
146
src/main/java/app/service/FiletreeOperations.java
Normal file
@@ -0,0 +1,146 @@
|
||||
package app.service;
|
||||
|
||||
import java.io.File;
|
||||
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 {
|
||||
|
||||
// FIXME: File specific icons not working properly
|
||||
// TODO: Clean up code that is not in use
|
||||
|
||||
// Creating the images for the icons.
|
||||
Image folder = new Image(getClass().getResourceAsStream("/graphics/folder.png"));
|
||||
Image md = new Image(getClass().getResourceAsStream("/graphics/md.png"));
|
||||
Image java = new Image(getClass().getResourceAsStream("/graphics/java.png"));
|
||||
Image placeholder = new Image(getClass().getResourceAsStream("/graphics/placeholder.png"));
|
||||
|
||||
// TODO: Error check for recursiveness, and files without icons
|
||||
|
||||
/**
|
||||
* 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()) {
|
||||
CheckBoxTreeItem<String> element = new CheckBoxTreeItem<>(file.getName(), new ImageView(folder));
|
||||
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 {
|
||||
checkExtensions(file, parent);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A helping function to sort the files/folders in the fileTree so that it shows
|
||||
* 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);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A help function to check if the extensions match .java or .md to insert the
|
||||
* specific icons and sending it to another help funtion createExtension to
|
||||
* create the new CheckboxTreeItem<String> that will add to the parent.
|
||||
*/
|
||||
private static void checkExtensions(File file, CheckBoxTreeItem<String> parent) {
|
||||
String name = file.getName();
|
||||
String ext = (name.substring(file.getName().lastIndexOf(".") + 1, file.getName().length()));
|
||||
|
||||
try {
|
||||
createExtension(name, getIconForFile(file), parent);
|
||||
} catch (Exception e) {
|
||||
System.err.println("[ERROR]: DEFAULT FILE ICON NOT FOUND");
|
||||
}
|
||||
|
||||
// if ("java".equals(ext))
|
||||
// createExtension(name, java, parent);
|
||||
// else if ("md".equals(ext))
|
||||
// createExtension(name, md, parent);
|
||||
// else
|
||||
// createExtension(name, placeholder, parent);
|
||||
}
|
||||
|
||||
private static Image getIconForFile(File file) throws IOException {
|
||||
Image icon;
|
||||
|
||||
try {
|
||||
String mimeType = Files.probeContentType(file.toPath()).replace('/', '-');
|
||||
|
||||
String iconPath = "/graphics/filetreeicons/" + mimeType + ".png";
|
||||
if (mimeType == null) throw new IOException();
|
||||
|
||||
InputStream imageData = FiletreeOperations.class.getResourceAsStream(iconPath);
|
||||
if (imageData == null) throw new IOException();
|
||||
|
||||
icon = new Image(imageData);
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("[ERROR]: ICON NOT FOUND: " + file.getPath());
|
||||
|
||||
// String iconPath = "/graphics/filetreeicons/file.png";
|
||||
String iconPath = "/graphics/placeholder.png";
|
||||
|
||||
InputStream imageData = FileOperations.class.getResourceAsStream(iconPath);
|
||||
if (imageData == null) throw new IOException();
|
||||
|
||||
icon = new Image(imageData);
|
||||
}
|
||||
|
||||
return icon;
|
||||
}
|
||||
|
||||
private static void createExtension(String name, Image image, CheckBoxTreeItem<String> parent) {
|
||||
CheckBoxTreeItem<String> element = new CheckBoxTreeItem<>(name, new ImageView(image));
|
||||
parent.getChildren().add(element);
|
||||
}
|
||||
|
||||
public static Path getPathOfTreeItem(TreeItem<String> item) {
|
||||
String root = Model.getProjectPath().getFileName().toString();
|
||||
String path = "";
|
||||
while (!root.equals(item.getValue())) {
|
||||
path = File.separator + item.getValue() + path;
|
||||
item = item.getParent();
|
||||
}
|
||||
|
||||
path = Model.getProjectPath().toString() + path;
|
||||
Path pathToString = Paths.get(path);
|
||||
|
||||
return pathToString;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user