182 lines
6.3 KiB
Java
182 lines
6.3 KiB
Java
|
import javafx.application.Platform;
|
||
|
import javafx.stage.Stage;
|
||
|
import javafx.stage.StageStyle;
|
||
|
import javafx.fxml.FXML;
|
||
|
import javafx.stage.FileChooser;
|
||
|
import javafx.stage.DirectoryChooser;
|
||
|
import javafx.scene.control.ScrollPane;
|
||
|
import javafx.scene.layout.TilePane;
|
||
|
import javafx.event.ActionEvent;
|
||
|
import javafx.scene.control.Button;
|
||
|
import javafx.scene.control.Label;
|
||
|
import javafx.scene.control.TextField;
|
||
|
import javafx.scene.input.KeyEvent;
|
||
|
import javafx.scene.input.KeyCode;
|
||
|
import javafx.scene.input.MouseEvent;
|
||
|
import javafx.scene.image.ImageView;
|
||
|
import javafx.concurrent.Task;
|
||
|
import java.io.*;
|
||
|
import java.lang.Runtime;
|
||
|
import javafx.scene.control.Tooltip;
|
||
|
import javafx.application.Platform;
|
||
|
|
||
|
|
||
|
public class Controller {
|
||
|
@FXML private ScrollPane stackPane;
|
||
|
@FXML private TextField pathTxt;
|
||
|
@FXML private Button fileBttn, clearBttn;
|
||
|
@FXML private TilePane tilePane;
|
||
|
@FXML private Label dir;
|
||
|
private DirectoryChooser folderChooser = new DirectoryChooser();
|
||
|
private FileChooser fileChooser = new FileChooser();
|
||
|
private Stage fileChrStage = new Stage(), dirChrStage = new Stage();
|
||
|
private File file, loaded, directory = new File("."), thumbs = new File("thumbs/");
|
||
|
private File[] filesList, thumbsList;
|
||
|
private Opener open = new Opener(); // Open book with proper program
|
||
|
private CleanPath cleaner = new CleanPath(); // Cleans thumbnail names
|
||
|
private String textAreaPth, filePth;
|
||
|
private ImageView thumbImgView;
|
||
|
private Process pb; // Process runner
|
||
|
private Tooltip tooltip;
|
||
|
|
||
|
@FXML void initialize() {
|
||
|
folderChooser.setTitle("Open Directory");
|
||
|
fileChooser.setTitle("Open Epub or Pdf File");
|
||
|
}
|
||
|
|
||
|
@FXML void Enter(KeyEvent event) { // on enter in txt field
|
||
|
if (event.getCode().equals(KeyCode.ENTER))
|
||
|
getFiles();
|
||
|
}
|
||
|
|
||
|
@FXML void setNewDir(MouseEvent event) {
|
||
|
scanDir();
|
||
|
}
|
||
|
|
||
|
@FXML void setJarUse(ActionEvent event) {
|
||
|
open.setWinSetting();
|
||
|
}
|
||
|
|
||
|
void scanDir() {
|
||
|
tilePane.getChildren().clear();
|
||
|
textAreaPth = pathTxt.getText();
|
||
|
|
||
|
if (textAreaPth.isEmpty()) {
|
||
|
directory = folderChooser.showDialog(dirChrStage);
|
||
|
if (directory != null) {
|
||
|
System.out.println("Directory: " + directory);
|
||
|
filesList = directory.listFiles();
|
||
|
thumbsList = thumbs.listFiles();
|
||
|
pathTxt.setText("" + directory);
|
||
|
|
||
|
Task getThumb = new Task<Void>() {
|
||
|
@Override public Void call() {
|
||
|
scanDirP2();
|
||
|
return null;
|
||
|
}};
|
||
|
new Thread(getThumb).start();
|
||
|
} else {
|
||
|
System.out.println("No directory selected....");
|
||
|
tilePane.getChildren().add(dir);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
directory = new File(textAreaPth);
|
||
|
}
|
||
|
|
||
|
void scanDirP2() {
|
||
|
for (int i=0; i<filesList.length; i++) {
|
||
|
int found = 0, success = 0;
|
||
|
final String file = "" + filesList[i],
|
||
|
fileName = "" + filesList[i].getName();
|
||
|
|
||
|
cleaner.cleanThumbPth(fileName);
|
||
|
String thumbImg = cleaner.returnThumbPth();
|
||
|
|
||
|
for (int j=0; j<thumbsList.length; j++) {
|
||
|
String ref = thumbImg + ".png";
|
||
|
if (ref.contains(thumbsList[j].getName())) {
|
||
|
found = 1;
|
||
|
success = 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (found == 0) {
|
||
|
try {
|
||
|
if (filesList[i].getName().toLowerCase().contains(".epub")) {
|
||
|
ProcessBuilder pb = new ProcessBuilder("python", "resources/bin/epub-thumbnailer",
|
||
|
file, "thumbs/" + thumbImg + ".png", "300");
|
||
|
Process p = pb.start();
|
||
|
p.waitFor();
|
||
|
success = 1;
|
||
|
}
|
||
|
else if (filesList[i].getName().toLowerCase().contains(".pdf")) {
|
||
|
ProcessBuilder pb = new ProcessBuilder("resources/bin/pdf-thumbnailer", "-scale", "x300",
|
||
|
file + "[0]", "thumbs/" + thumbImg + ".png");
|
||
|
Process p = pb.start();
|
||
|
p.waitFor();
|
||
|
success = 1;
|
||
|
}
|
||
|
else { System.out.println("Not an understood file type."); }
|
||
|
|
||
|
} catch(Throwable imgIOErr) {
|
||
|
System.out.println(imgIOErr);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (success == 1) {
|
||
|
String imgPth = "thumbs/" + thumbImg + ".png";
|
||
|
thumbImgView = new ImageView(imgPth);
|
||
|
tooltip = new Tooltip(fileName);
|
||
|
tooltip.minHeight(800);
|
||
|
tooltip.minWidth(800);
|
||
|
Tooltip.install(thumbImgView, tooltip);
|
||
|
|
||
|
Platform.runLater(new Runnable() {
|
||
|
@Override public void run() {
|
||
|
tilePane.getChildren().add(thumbImgView);
|
||
|
}
|
||
|
});
|
||
|
thumbImgView.setFitHeight(300);
|
||
|
thumbImgView.setFitWidth(200);
|
||
|
thumbImgView.setCache(true);
|
||
|
thumbImgView.setOnMouseClicked(mouse -> {
|
||
|
if (mouse.getClickCount() == 2 && !mouse.isConsumed()) {
|
||
|
mouse.consume();
|
||
|
pathTxt.setText(file);
|
||
|
getFiles();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@FXML void openFile(ActionEvent event) { // file bttn click
|
||
|
getFiles();
|
||
|
}
|
||
|
|
||
|
void getFiles() { // opens epub/pdf
|
||
|
textAreaPth = pathTxt.getText();
|
||
|
if (textAreaPth.isEmpty()) {
|
||
|
file = fileChooser.showOpenDialog(fileChrStage);
|
||
|
pathTxt.setText(textAreaPth = "" + file);
|
||
|
filePth = pathTxt.getText();
|
||
|
dir.setText(":Reading:\n" + filePth);
|
||
|
} else {
|
||
|
filePth = textAreaPth;
|
||
|
}
|
||
|
open.fileOpener(filePth);
|
||
|
}
|
||
|
|
||
|
@FXML void clearBttnClick(ActionEvent event) {
|
||
|
dir.setText("Choose Dir");
|
||
|
pathTxt.setText("");
|
||
|
tilePane.getChildren().clear();
|
||
|
tilePane.getChildren().add(dir);
|
||
|
}
|
||
|
|
||
|
@FXML void close(ActionEvent event) {
|
||
|
Platform.exit();
|
||
|
}
|
||
|
}
|