first version of DirectoryScanner

This commit is contained in:
Mateusz Kowalczyk 2020-01-12 19:56:44 +01:00
parent 1b4b540c78
commit 1d4dc9602f
3 changed files with 168 additions and 5 deletions

View File

@ -0,0 +1,70 @@
package dev.mateuszkowalczyk.ffm.app;
import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
import dev.mateuszkowalczyk.ffm.utils.Property;
import javafx.application.Platform;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
public class DirectoryScanner implements Runnable {
private WorkspaceService workspaceService = WorkspaceService.getInstance();
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
@Override
public void run() {
System.out.println("Run photos reader");
this.readImages();
}
private ArrayList<ImageView> readImages() {
ArrayList<ImageView> imageViews = new ArrayList<>();
try {
Files.walk(Paths.get(this.propertiesLoader.get(Property.PATH_TO_DIRECTORY)))
.filter(Files::isRegularFile)
.forEach(path -> {
String filename = path.getFileName().toString();
if(this.isImagePath(filename)) {
ImageView imageView = new ImageView();
InputStream inputStream = null;
Image image1 = new Image("file:" + path.toString(), 200, 200, false, true);
imageView.setImage(image1);
imageView.setFitHeight(200);
imageView.setFitWidth(200);
imageView.setSmooth(true);
imageView.setCache(true);
imageViews.add(imageView);
Platform.runLater(() -> WorkspaceService.getInstance().getMainPageController().addImage(imageView));
}
});
} catch (IOException e) {
e.printStackTrace();
}
return imageViews;
}
private boolean isImagePath(String filename) {
switch (filename.substring(filename.length() - 3).toLowerCase()) {
case "jpg":
return true;
default:
return false;
}
}
private void clearImages() {
this.workspaceService.getMainPageController().clearImages();
}
}

View File

@ -5,19 +5,38 @@ import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
import dev.mateuszkowalczyk.ffm.utils.Property;
import dev.mateuszkowalczyk.ffm.view.SceneEnum;
import dev.mateuszkowalczyk.ffm.view.StageController;
import dev.mateuszkowalczyk.ffm.view.workspace.MainPageController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class WorkspaceService {
private static final WorkspaceService instance = new WorkspaceService();
private PropertiesLoader propertiesLoader = new PropertiesLoader();
private MainPageController mainPageController;
private WorkspaceService(){}
private WorkspaceService() {
}
public static WorkspaceService getInstance() {
return instance;
}
public MainPageController getMainPageController() {
return mainPageController;
}
public void setMainPageController(MainPageController mainPageController) {
this.mainPageController = mainPageController;
}
public void setupWorkspace() {
Chooser chooser = new Chooser();
String path = chooser.chooseDirectory();
@ -30,6 +49,54 @@ public class WorkspaceService {
}
public void refreshWorkspace() {
System.out.println("Refreshing workspace");
this.getMainPageController().clearImages();
var t = new Thread(new DirectoryScanner());
t.start();
}
private void readImages() {
try {
Files.walk(Paths.get(this.propertiesLoader.get(Property.PATH_TO_DIRECTORY)))
.filter(Files::isRegularFile)
.forEach(path -> {
String filename = path.getFileName().toString();
if(this.isImagePath(filename)) {
System.out.println("read image");
ImageView imageView = new ImageView();
InputStream inputStream = null;
try {
inputStream = new FileInputStream(path.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Image image = new Image(inputStream);
Image image1 = new Image("file:" + path.toString(), 200, 200, false, true);
imageView.setImage(image1);
imageView.setFitHeight(200);
imageView.setFitWidth(200);
imageView.setSmooth(true);
imageView.setCache(true);
this.mainPageController.addImage(imageView);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean isImagePath(String filename) {
switch (filename.substring(filename.length() - 3).toLowerCase()) {
case "jpg":
return true;
default:
return false;
}
}
}

View File

@ -1,10 +1,36 @@
package dev.mateuszkowalczyk.ffm.view.workspace;
import dev.mateuszkowalczyk.ffm.app.WorkspaceService;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
public class MainPageController {
import java.net.URL;
import java.util.ResourceBundle;
public class MainPageController implements Initializable {
private WorkspaceService workspaceService = WorkspaceService.getInstance();
@FXML
private FlowPane imagesContainer;
public MainPageController() {
WorkspaceService.getInstance().refreshWorkspace();
this.workspaceService.setMainPageController(this);
}
public void clearImages() {
if (this.imagesContainer != null) {
this.imagesContainer.getChildren().clear();
}
}
public void addImage(ImageView imageView) {
this.imagesContainer.getChildren().add(imageView);
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
this.workspaceService.refreshWorkspace();
}
}