195 lines
6.8 KiB
Java
195 lines
6.8 KiB
Java
package de.emmerts.fotoverwaltung;
|
|
|
|
import javafx.beans.value.ChangeListener;
|
|
import javafx.beans.value.ObservableValue;
|
|
import javafx.fxml.FXML;
|
|
import javafx.scene.control.CheckBox;
|
|
import javafx.scene.control.Label;
|
|
import javafx.scene.control.TreeItem;
|
|
import javafx.scene.control.TreeView;
|
|
import javafx.stage.DirectoryChooser;
|
|
|
|
import java.awt.*;
|
|
import java.io.File;
|
|
import java.io.FileFilter;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.Map;
|
|
import java.util.TreeMap;
|
|
|
|
|
|
public class FileTreeController{
|
|
@FXML
|
|
public TreeView<String> treeView;
|
|
public Label label;
|
|
|
|
protected String id;
|
|
File selectedDirectory;
|
|
@FXML
|
|
private CheckBox allowAllFileExtensions;
|
|
|
|
@FXML
|
|
void initialize()
|
|
{
|
|
//label.setText(label.getParent().getAccessibleText());
|
|
treeView.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() {
|
|
|
|
@Override
|
|
public void changed(ObservableValue observable, Object oldValue,
|
|
Object newValue) {
|
|
|
|
FileTreeItemComponent selectedItem = (FileTreeItemComponent) newValue;
|
|
if(selectedItem != null)
|
|
{
|
|
selectedItem.selectFileTreeItem();
|
|
MainApplication.currentFile = selectedItem.content;
|
|
}
|
|
|
|
// do what ever you want
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
@FXML
|
|
void allowAllFileExtensionsClick()
|
|
{
|
|
|
|
}
|
|
@FXML
|
|
protected void openDirectoryInOS()
|
|
{
|
|
//selectedDirectory
|
|
Desktop desktop = Desktop.getDesktop();
|
|
try {
|
|
desktop.open(selectedDirectory);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
@FXML
|
|
protected void chooseDirectory(){
|
|
DirectoryChooser dChooser = new DirectoryChooser();
|
|
String defaultDirectory = MainApplication.config.getString("directories."+id);
|
|
if(defaultDirectory != null)
|
|
dChooser.setInitialDirectory(new File(defaultDirectory));
|
|
selectedDirectory = dChooser.showDialog(MainApplication.controller.rootStage);
|
|
if(selectedDirectory == null){
|
|
return;
|
|
}
|
|
if(id.equals("fileTreeJPG"))
|
|
{
|
|
String dir = selectedDirectory.getParentFile().getName();
|
|
File cr2Dir = MainApplication.controller.fileTreeCR2.getDirectory();
|
|
while(!cr2Dir.getName().equals(dir))
|
|
{
|
|
cr2Dir = cr2Dir.getParentFile();
|
|
}
|
|
cr2Dir = new File(cr2Dir+"\\"+selectedDirectory.getName());
|
|
MainApplication.controller.fileTreeCR2.openDirectory(cr2Dir);
|
|
}
|
|
openDirectory();
|
|
}
|
|
|
|
protected void openDirectory(){
|
|
MainApplication.config.setProperty("directories."+id, selectedDirectory.getAbsolutePath());
|
|
treeView.setRoot(getFileList(selectedDirectory, true));
|
|
}
|
|
|
|
FileTreeItemComponent getFileList(File root, boolean expand)
|
|
{
|
|
ArrayList<FileTreeItemComponent> files = new ArrayList<>();
|
|
ArrayList<FileTreeItemComponent> directories = new ArrayList<>();
|
|
File[] allFiles;
|
|
if(allowAllFileExtensions.isSelected())
|
|
allFiles = root.listFiles();
|
|
else
|
|
allFiles = root.listFiles(ff);
|
|
for (File file : allFiles) {
|
|
if(file.isDirectory())
|
|
directories.add(getFileList(file, false));
|
|
else
|
|
files.add(new FileTreeItemComponent(file));
|
|
}
|
|
FileTreeItemComponent temp = new FileTreeItemComponent(root);
|
|
temp.setExpanded(expand);
|
|
temp.getChildren().addAll(directories);
|
|
temp.getChildren().addAll(files);
|
|
return temp;
|
|
}
|
|
|
|
FileFilter ff = fileForFilter -> {
|
|
if(fileForFilter.isDirectory())
|
|
return true;
|
|
ArrayList<String> allowedExtensions = new ArrayList<>();
|
|
allowedExtensions.add(".cr2");
|
|
allowedExtensions.add(".jpg");
|
|
allowedExtensions.add(".jpeg");
|
|
allowedExtensions.add(".gif");
|
|
allowedExtensions.add(".png");
|
|
for (String allowedExtension : allowedExtensions) {
|
|
if (fileForFilter.getName().endsWith(allowedExtension) || fileForFilter.getName().endsWith(allowedExtension.toUpperCase()))
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
public void displayTreeMap(TreeMap<String, File> files) {
|
|
FileTreeItemComponent temp = new FileTreeItemComponent(files.firstEntry().getValue().getParentFile().getParentFile());
|
|
treeView.setRoot(temp);
|
|
temp.setExpanded(true);
|
|
Map.Entry<String,File> entry = files.pollFirstEntry();
|
|
while(entry != null)
|
|
{
|
|
File toBeAdded = entry.getValue();
|
|
temp.getChildren().add(new FileTreeItemComponent(toBeAdded));
|
|
entry = files.pollFirstEntry();
|
|
}
|
|
}
|
|
|
|
private FileTreeItemComponent getOrCreateDirectory(File directory)
|
|
{
|
|
FileTreeItemComponent blubb = (FileTreeItemComponent) treeView.getRoot();
|
|
for (TreeItem<String> child : blubb.getChildren()) {
|
|
FileTreeItemComponent childFTIC = (FileTreeItemComponent) child;
|
|
if(childFTIC.content.getAbsolutePath().equals(directory.getAbsolutePath())) {
|
|
return childFTIC;
|
|
}
|
|
}
|
|
FileTreeItemComponent newDirectory = new FileTreeItemComponent(directory);
|
|
blubb.getChildren().add(newDirectory);
|
|
newDirectory.setExpanded(true);
|
|
return newDirectory;
|
|
}
|
|
|
|
public void displayTreeMapOld(TreeMap<String, File> files) {
|
|
FileTreeItemComponent temp = new FileTreeItemComponent(files.firstEntry().getValue().getParentFile().getParentFile());
|
|
temp.setExpanded(true);
|
|
ArrayList<FileTreeItemComponent> images = new ArrayList<>();
|
|
Map.Entry<String, File> entry = files.pollFirstEntry();
|
|
FileTreeItemComponent lastDirectory = null;
|
|
while(entry != null) {
|
|
File f = entry.getValue();
|
|
if(lastDirectory == null)
|
|
{
|
|
lastDirectory = new FileTreeItemComponent(f.getParentFile());
|
|
images.add(new FileTreeItemComponent(f));
|
|
}else if(!lastDirectory.content.getAbsolutePath().equals(f.getParentFile().getAbsolutePath()))
|
|
{
|
|
lastDirectory.getChildren().addAll(images);
|
|
temp.getChildren().add(lastDirectory);
|
|
lastDirectory = new FileTreeItemComponent(f.getParentFile());
|
|
images = new ArrayList<>();
|
|
images.add(new FileTreeItemComponent(f));
|
|
|
|
}
|
|
else {
|
|
images.add(new FileTreeItemComponent(f));
|
|
entry = files.pollFirstEntry();
|
|
}
|
|
|
|
}
|
|
treeView.setRoot(temp);
|
|
}
|
|
}
|