From 4a29e46812e471244e6f7b8ba8c008a0bd0969b2 Mon Sep 17 00:00:00 2001 From: Joshua Jeschek <64850647+joshuajeschek@users.noreply.github.com> Date: Sun, 16 Jan 2022 11:28:12 +0100 Subject: [PATCH] =?UTF-8?q?[feat]=20=C3=84nderungen=20f=C3=BCr=20AnalyseCl?= =?UTF-8?q?ient=20(#33)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [feat] getter in TrialData * [fix] datetime * [fix] startTime can be null * [feat] AbsScene und Log hinzugefügt * [fix] stage * [fix] resourcs directory name * [fix] controller name * [fix] log size Co-authored-by: TheDarkDesync <82680013+TheDarkDesync@users.noreply.github.com> --- pom.xml | 5 + .../weichware10/util/data/TrialData.java | 19 ++- .../github/weichware10/util/db/Trials.java | 3 +- .../github/weichware10/util/gui/AbsScene.java | 111 ++++++++++++++++++ .../util/gui/AbsSceneController.java | 21 ++++ .../java/github/weichware10/util/gui/Log.java | 87 ++++++++++++++ .../weichware10/util/gui/LogController.java | 75 ++++++++++++ .../github/weichware10/util/gui/Log.fxml | 52 ++++++++ 8 files changed, 371 insertions(+), 2 deletions(-) create mode 100644 src/main/java/github/weichware10/util/gui/AbsScene.java create mode 100644 src/main/java/github/weichware10/util/gui/AbsSceneController.java create mode 100644 src/main/java/github/weichware10/util/gui/Log.java create mode 100644 src/main/java/github/weichware10/util/gui/LogController.java create mode 100644 src/main/resources/github/weichware10/util/gui/Log.fxml diff --git a/pom.xml b/pom.xml index d9c1560..3d804d7 100644 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,11 @@ javafx-controls 17 + + org.openjfx + javafx-fxml + 17 + diff --git a/src/main/java/github/weichware10/util/data/TrialData.java b/src/main/java/github/weichware10/util/data/TrialData.java index e20e958..d7ce4e0 100644 --- a/src/main/java/github/weichware10/util/data/TrialData.java +++ b/src/main/java/github/weichware10/util/data/TrialData.java @@ -27,6 +27,7 @@ * @since v0.2 */ public class TrialData { + public final ToolType toolType; public final String trialId; public final String configId; @@ -109,6 +110,22 @@ public void setAnswer(String answer) { this.answer = answer; } + public ToolType getToolType() { + return toolType; + } + + public String getTrialId() { + return trialId; + } + + public String getConfigId() { + return configId; + } + + public DateTime getStartTime() { + return startTime; + } + /** * Add a DataPoint for CodeCharts. * @@ -224,7 +241,7 @@ public String toString() { toolType.toString(), trialId, configId, - startTime.toString(), + startTime != null ? startTime.toString() : "null", answer, dataPoints.size()); } diff --git a/src/main/java/github/weichware10/util/db/Trials.java b/src/main/java/github/weichware10/util/db/Trials.java index ffd06ce..8423dd9 100644 --- a/src/main/java/github/weichware10/util/db/Trials.java +++ b/src/main/java/github/weichware10/util/db/Trials.java @@ -332,11 +332,12 @@ public List getList(String configId, ToolType toolType, st = conn.createStatement(); rs = st.executeQuery(query); while (rs.next()) { + Timestamp ts = rs.getTimestamp("starttime"); TrialData td = new TrialData( ToolType.valueOf(rs.getString("tooltype")), rs.getString("trialid"), rs.getString("configid"), - new DateTime(rs.getTimestamp("starttime")), + ts != null ? new DateTime(ts) : null, rs.getString("answer"), Arrays.asList()); // leere Liste diff --git a/src/main/java/github/weichware10/util/gui/AbsScene.java b/src/main/java/github/weichware10/util/gui/AbsScene.java new file mode 100644 index 0000000..46cb69d --- /dev/null +++ b/src/main/java/github/weichware10/util/gui/AbsScene.java @@ -0,0 +1,111 @@ +package github.weichware10.util.gui; + +import github.weichware10.util.Logger; +import java.net.URL; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.control.MenuBar; +import javafx.scene.image.Image; +import javafx.scene.layout.BorderPane; +import javafx.stage.Stage; + +/** + * Abstrakte Klasse, von der alle Szenen erben. Diese Klasse lädt vorallem FXML-Dateien. + */ +public abstract class AbsScene { + + /** + * lädt die Szene intern und gibt die root-Instanz zurück. + * + * @param fxml - URL der FXML-Datei + * @return das geladene Parent Objekt und die Controller-Instanz. + */ + protected static InitResult initialize(URL fxml) { + + FXMLLoader loader = new FXMLLoader(fxml); + + Parent root = null; + try { + root = loader.load(); + } catch (Exception e) { + Logger.error("Error when loading " + fxml, e, true); + System.exit(-1); + } + + AbsSceneController controller = loader.getController(); + + return new InitResult(root, controller); + } + + /** + * Setzt die MenuBar, falls das in initialize geladene root-Objekt BorderPane ist. + * + * @param menuBar - die zu setzende Menüleiste + */ + public static void setMenuBar(MenuBar menuBar, Parent root) { + if (root != null && root instanceof BorderPane) { + BorderPane borderPane = (BorderPane) root; + borderPane.setTop(menuBar); + } + } + + /** + * Zeigt die Szene an. + * + * @param primaryStage - das Hauptfenster + * @return das Initialisierungsergebniss + */ + public static InitResult start(Stage primaryStage, URL fxml, Parent root, + AbsSceneController controller, String title, MenuBar menuBar, + Integer width, Integer height, String icon) { + if (primaryStage == null || title == null || fxml == null) { + throw new NullPointerException( + String.format("Stage primaryStage (%s), ", + (primaryStage != null) ? primaryStage.toString() : "null") + + String.format("String title (%s), ", + (title != null) ? title : "null") + + String.format("and URL fxml (%s) are required", + (fxml != null) ? fxml.toString() : "null")); + } + if (root == null) { + InitResult ir = initialize(fxml); + root = ir.root; + controller = ir.controller; + } + // Menüleiste setzen + if (menuBar != null) { + setMenuBar(menuBar, root); + } + // Größe einstellen + if (width != null && height != null) { + primaryStage.setWidth(width); + primaryStage.setHeight(height); + } + // auf primaryStage setzen + Scene existingScene = root.getScene(); + if (existingScene != null) { + primaryStage.setScene(existingScene); + } else { + primaryStage.setScene(new Scene(root)); + } + if (icon != null) { + primaryStage.getIcons().add(new Image(icon)); + } + primaryStage.setTitle(title); + return new InitResult(root, controller); + } + + /** + * root und controller des Lade-Vorgangs. + */ + public static class InitResult { + public final Parent root; + public final AbsSceneController controller; + + protected InitResult(Parent root, AbsSceneController controller) { + this.root = root; + this.controller = controller; + } + } +} diff --git a/src/main/java/github/weichware10/util/gui/AbsSceneController.java b/src/main/java/github/weichware10/util/gui/AbsSceneController.java new file mode 100644 index 0000000..66cd816 --- /dev/null +++ b/src/main/java/github/weichware10/util/gui/AbsSceneController.java @@ -0,0 +1,21 @@ +package github.weichware10.util.gui; + +import java.net.URL; +import java.util.ResourceBundle; +import javafx.fxml.FXML; + + +/** + * abstrakte Klasse für Controller-Klassen. + */ +public abstract class AbsSceneController { + + @FXML + protected ResourceBundle resources; + @FXML + protected URL location; + + @FXML + protected abstract void initialize(); + +} diff --git a/src/main/java/github/weichware10/util/gui/Log.java b/src/main/java/github/weichware10/util/gui/Log.java new file mode 100644 index 0000000..9225abf --- /dev/null +++ b/src/main/java/github/weichware10/util/gui/Log.java @@ -0,0 +1,87 @@ +package github.weichware10.util.gui; + +import github.weichware10.util.Logger; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.value.ObservableBooleanValue; +import javafx.scene.Parent; +import javafx.stage.Stage; + +/** + * Dialog zum Darstellen der Log Infos. + */ +public class Log extends AbsScene { + + private static Stage logStage; + private static Parent root; + + private static SimpleBooleanProperty visibleProperty = new SimpleBooleanProperty(false); + public static ObservableBooleanValue visible = visibleProperty; + + /** + * Startet das Log. + */ + public static void start(String icon) { + Integer width = null; + Integer height = null; + if (logStage == null) { + logStage = new Stage(); + width = 750; + height = 500; + } + root = start(logStage, + Log.class.getResource("Log.fxml"), + root, + null, + "Toolbox - Log", + null, + width, + height, + icon).root; + } + + public static boolean isVisible() { + return visible.get(); + } + + public static void show() { + logStage.show(); + visibleProperty.set(true); + } + + public static void hide() { + logStage.hide(); + visibleProperty.set(false); + } + + public static void close() { + logStage.close(); + } + + /** + * Loggt den Inhalt (falls vorhanden) mit dem gegebenen Typ. + * + * @param content - zu loggender Inhalt + * @param type - "DEBUG", "INFO", "WARN" oder "ERROR" + */ + public static void log(String content, String type) { + if (content.length() == 0) { + return; + } + switch (type) { + case "DEBUG": + Logger.debug(content); + break; + case "INFO": + Logger.info(content); + break; + case "WARN": + Logger.warn(content); + break; + case "ERROR": + Logger.error(content); + break; + default: + break; + } + } +} diff --git a/src/main/java/github/weichware10/util/gui/LogController.java b/src/main/java/github/weichware10/util/gui/LogController.java new file mode 100644 index 0000000..3ff800c --- /dev/null +++ b/src/main/java/github/weichware10/util/gui/LogController.java @@ -0,0 +1,75 @@ +package github.weichware10.util.gui; + +import github.weichware10.util.Logger; +import java.net.URL; +import java.util.ResourceBundle; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.MenuButton; +import javafx.scene.control.MenuItem; +import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; +import javafx.scene.layout.Pane; + +/** + * Controller für {@link Log}. + */ +public class LogController extends AbsSceneController { + + @FXML + private ResourceBundle resources; + @FXML + private URL location; + + + @FXML + private TextField logInput; + @FXML + private TextArea logArea; + @FXML + private Pane logPane; + @FXML + private MenuButton logMenuButton; + + @FXML + protected void initialize() { + assert logArea != null + : "fx:id=\"logArea\" not injected: check 'Log.fxml'."; + assert logPane != null + : "fx:id=\"logPane\" not injected: check 'Log.fxml'."; + assert logInput != null + : "fx:id=\"logInput\" not injected: check 'Log.fxml'."; + assert logMenuButton != null + : "fx:id=\"logMenuButton\" not injected: check 'Log.fxml'."; + logArea.minHeightProperty().bind(logPane.heightProperty()); + logArea.minWidthProperty().bind(logPane.widthProperty()); + Logger.setLogArea(logArea); + } + + @FXML + void setLogType(ActionEvent event) { + Logger.info("log:content Setting log type"); + // mit try/catch, da Abfrage nach Klasse von event.getSource() nicht gut funktioniert + try { + MenuItem type = (MenuItem) event.getSource(); + logMenuButton.setText(type.getText()); + } catch (Exception e) { + Logger.error("an error occured while changing the log type"); + } + } + + @FXML + void inputKeyPress(KeyEvent event) { + if (event.getCode() == KeyCode.ENTER) { + log(); + } + } + + @FXML + void log() { + Log.log(logInput.getText(), logMenuButton.getText()); + logInput.clear(); + } +} diff --git a/src/main/resources/github/weichware10/util/gui/Log.fxml b/src/main/resources/github/weichware10/util/gui/Log.fxml new file mode 100644 index 0000000..8fceb59 --- /dev/null +++ b/src/main/resources/github/weichware10/util/gui/Log.fxml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + +
+ + +