Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] handshake fix & delete TempDir & dialog image error #20

Merged
merged 16 commits into from
Jan 9, 2022
Merged
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<!-- modules for custom JVM image -->
<!-- JAVAFX: media, controls, fxml -->
<!-- UTIL: sql, management, security.sasl -->
<jvm.modules>javafx.media,javafx.controls,javafx.fxml,java.logging,java.sql,java.management,java.security.sasl</jvm.modules>
<jvm.modules>javafx.media,javafx.controls,javafx.fxml,java.logging,java.sql,java.management,java.security.sasl,jdk.crypto.ec</jvm.modules>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
Expand All @@ -48,6 +48,13 @@
</repositories>

<dependencies>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>

<!-- .env Datei -->
<dependency>
<groupId>io.github.cdimascio</groupId>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/github/weichware10/toolbox/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void main(String[] args) {
String logfile = String.format(
Dotenv.load().get("LOGS") + "/%s.log", DateTime.now().toString("yMMdd-HHmmss"));
Logger.setLogfile(logfile);
Util.deleteTempDir();
launch(args);
}

Expand Down
72 changes: 47 additions & 25 deletions src/main/java/github/weichware10/toolbox/Util.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package github.weichware10.toolbox;

import github.weichware10.util.Logger;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -9,6 +10,9 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.FileUtils;

/**
* util.
Expand All @@ -23,8 +27,8 @@ public final class Util {
public static String createTempDir() {
String tmpdir = null;
try {
tmpdir = Files.createTempDirectory("tmpDirPrefix").toFile().getAbsolutePath();
Logger.info("created tempdir: " + tmpdir);
tmpdir = Files.createTempDirectory("weichware").toFile().getAbsolutePath();
Logger.info("created tempdir: " + tmpdir + "...");
} catch (Exception e) {
System.out.println(e);
}
Expand All @@ -36,42 +40,60 @@ public static String createTempDir() {
*
* @param imageUrl - URL vom Bild
* @return Pfad zum Bild
* @throws MalformedURLException
* @throws IllegalArgumentException
* @throws FileNotFoundException
* @throws IOException
*/
public static String saveImage(String imageUrl) {
public static String saveImage(String imageUrl) throws MalformedURLException,
IllegalArgumentException, FileNotFoundException, IOException {
if (tmpdir == null) {
tmpdir = createTempDir();
}
URL url = null;
try {
url = new URL(imageUrl);
} catch (MalformedURLException e) {
Logger.error("MalformedURLException while setting image URL", e, true);
return null;
}
url = new URL(imageUrl);

String fileName = url.getFile();
String destName = tmpdir + fileName.substring(fileName.lastIndexOf("/"));
System.out.println(destName);

try {
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destName);
byte[] b = new byte[2048];
int length;
String fileType = fileName.substring(fileName.lastIndexOf("."));

while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
List<String> supportedFileTypes = Arrays.asList(".jpg", ".png", ".jpeg");

if (!supportedFileTypes.contains(fileType)) {
throw new IllegalArgumentException("Filetype of given image is not supported");
}

is.close();
os.close();
} catch (FileNotFoundException e) {
Logger.error("FileNotFoundException while saving Image", e, true);
return null;
} catch (IOException e) {
Logger.error("IOException while saving Image", e, true);
return null;
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destName);
byte[] b = new byte[2048];
int length;

while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}

is.close();
os.close();

return destName;
}

/**
* Löscht angelegten temporären Ordner beim Beenden der App.
*/
public static void deleteTempDir() {
Thread deleterHook = new Thread(() -> {
try {
Logger.info("deleting tmp folder...");
FileUtils.deleteDirectory(new File(tmpdir));
} catch (IOException e) {
Logger.error("IOException while deleting tmpdir", e, true);
} catch (IllegalArgumentException e) {
Logger.error("IllegalArgumentException while deleting tmpdir", e, true);
}
});
Runtime.getRuntime().addShutdownHook(deleterHook);
}
}
6 changes: 2 additions & 4 deletions src/main/java/github/weichware10/toolbox/gui/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ protected void createZoomMapsTestTrial(TextField trialIdInput) {
createTestTrial(new Configuration(
"dunno yet",
"Von wem werden Froot Loops hergestellt?",
/*Util.saveImage("https://scotchaddict.com/wp-content/uploads/2014/01/illusion-of-choice.jpg"),*/
App.class.getClassLoader().getResource("test-image.jpg").toString(),
"https://scotchaddict.com/wp-content/uploads/2014/01/illusion-of-choice.jpg",
"Willkommen zu unserem ZoomMaps Versuch Illusion der Auswahl!",
"Vielen Dank für die Teilnahme. Sie sind jetzt ein Froot Loops Connoisseur!",
zoomMapsConfiguration),
Expand Down Expand Up @@ -188,8 +187,7 @@ protected void createCodeChartsTestTrial(TextField trialIdInput) {
createTestTrial(new Configuration(
"dunno yet",
"Test Question?",
/*Util.saveImage("https://scotchaddict.com/wp-content/uploads/2014/01/illusion-of-choice.jpg"),*/
App.class.getClassLoader().getResource("test-image.jpg").toString(),
"https://scotchaddict.com/wp-content/uploads/2014/01/illusion-of-choice.jpg",
"Welcome to this magnificent CodeCharts Trial",
"Thanks for participating in this extraordinary CodeCharts Trial!",
codeChartsConfiguration),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package github.weichware10.toolbox.gui.dialogs;

import github.weichware10.util.Logger;
import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.image.Image;
import javafx.stage.Stage;

/**
* Dialogfenster, wenn Bild nicht geladen werden kann.
*/
public class ImageNotFoundDialog {

/**
* Dialogfenster, wenn Bild nicht geladen werden kann.
*
* @param error - Errornachicht
*/
public void showImageNotFoundDialog(Exception error) {
Logger.info("showing ImageNotFoundDialog...");

Dialog<Void> dialog = new Dialog<>();

dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
dialog.setTitle("Image could not be loaded");

FXMLLoader loader = new FXMLLoader(
ImageNotFoundDialog.class.getResource("ImageNotFoundDialog.fxml"));
Parent root = null;
try {
root = loader.load();
} catch (IOException e) {
return;
}

ImageNotFoundDialogController controller = loader.getController();

// set icon
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(getClass().getResource("noimage.png").toString()));

controller.setErrorMessage(error.toString());

dialog.getDialogPane().setContent(root);

dialog.showAndWait();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package github.weichware10.toolbox.gui.dialogs;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;

/**
* Kontroller für {@link ImageNotFoundDialog}.
*/
public class ImageNotFoundDialogController {

@FXML
private ResourceBundle resources;

@FXML
private URL location;

@FXML
private TextArea errorMessage;

protected void setErrorMessage(String error) {
errorMessage.setText(error);
}

@FXML
void initialize() {
assert errorMessage != null
: "fx:id=\"errorMessage\" not injected: check 'ImageNotFoundDialog.fxml'.";

}

}
17 changes: 13 additions & 4 deletions src/main/java/github/weichware10/toolbox/zoommaps/ZoomBild.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package github.weichware10.toolbox.zoommaps;

import github.weichware10.toolbox.Util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.image.Image;
Expand All @@ -20,12 +24,17 @@ public class ZoomBild {
* Instanziiert ein neues ZoomBild.
*
* @param location - die Quelle der zu benutzenden Bilddatei
* @throws IllegalArgumentException falls die Location falsch ist
* @throws MalformedURLException
* @throws IllegalArgumentException
* @throws FileNotFoundException
* @throws IOException
*/
public ZoomBild(String location, ImageView imageView, ZoomCalculator zoomCalculator) {
public ZoomBild(String location, ImageView imageView, ZoomCalculator zoomCalculator)
throws MalformedURLException, IllegalArgumentException, FileNotFoundException,
IOException {
this.imageView = imageView;
// TODO auf errors reagiern
Image image = new Image(location);
String imgLocation = Util.saveImage(location);
Image image = new Image(imgLocation);
imageSize = new double[] { image.getWidth(), image.getHeight() };
imageView.setImage(image);
Rectangle2D viewport = new Rectangle2D(0, 0, imageSize[0], imageSize[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import github.weichware10.util.Logger;
import github.weichware10.util.config.ConfigClient;
import github.weichware10.util.data.TrialData;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -29,12 +32,17 @@ public class ZoomCalculator {
/**
* Erstelt einen neues ZoomCalculator.
*
* @param data -
* @param configClient -
* @param controller -
* @param data - ein (leeres) {@link TrialData}-Objekt
* @param configClient - der ConfigClient der App
* @param controller - die ControllerKlasse der Szene
* @throws IOException
* @throws FileNotFoundException
* @throws IllegalArgumentException
* @throws MalformedURLException
*/
public ZoomCalculator(TrialData data, ConfigClient configClient,
ZoomMapsController controller) {
public ZoomCalculator(TrialData data, ConfigClient configClient, ZoomMapsController controller)
throws MalformedURLException, IllegalArgumentException, FileNotFoundException,
IOException {
this.data = data;
this.configClient = configClient;
this.controller = controller;
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/github/weichware10/toolbox/zoommaps/ZoomMaps.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package github.weichware10.toolbox.zoommaps;

import github.weichware10.toolbox.gui.End;
import github.weichware10.toolbox.gui.dialogs.ImageNotFoundDialog;
import github.weichware10.toolbox.gui.util.Log;
import github.weichware10.util.Logger;
import github.weichware10.util.ToolType;
import github.weichware10.util.config.ConfigClient;
Expand Down Expand Up @@ -62,7 +64,14 @@ public ZoomMaps(Stage primaryStage, ConfigClient configClient, DataBaseClient da
primaryStage.setScene(scene);
primaryStage.setMaximized(true);

new ZoomCalculator(trialData, configClient, controller);
try {
new ZoomCalculator(trialData, configClient, controller);
} catch (Exception e) {
Logger.error("Error while loading image", e, false);
primaryStage.close();
Log.close();
new ImageNotFoundDialog().showImageNotFoundDialog(e);
}

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Text?>

<DialogPane minWidth="-Infinity" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="github.weichware10.toolbox.gui.dialogs.ImageNotFoundDialogController">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<opaqueInsets>
<Insets />
</opaqueInsets>
<content>
<GridPane vgap="10.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Image location in the configuration can not be resolved to an compatible image. Please contact the admin." wrappingWidth="350.0" />
<TextArea fx:id="errorMessage" editable="false" focusTraversable="false" prefHeight="60.0" prefWidth="200.0" promptText="no details available" wrapText="true" GridPane.rowIndex="1">
<opaqueInsets>
<Insets />
</opaqueInsets>
</TextArea>
</children>
</GridPane>
</content>
</DialogPane>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading