diff --git a/src/main/java/id/co/gtc/erhacam/AutoCloseAlert.java b/src/main/java/id/co/gtc/erhacam/AutoCloseAlert.java index 29b2c8a..d146344 100644 --- a/src/main/java/id/co/gtc/erhacam/AutoCloseAlert.java +++ b/src/main/java/id/co/gtc/erhacam/AutoCloseAlert.java @@ -3,6 +3,7 @@ package id.co.gtc.erhacam; import javafx.animation.PauseTransition; import javafx.application.Platform; import javafx.geometry.Pos; +import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.VBox; @@ -12,9 +13,13 @@ import javafx.stage.Stage; import javafx.stage.StageStyle; import javafx.util.Duration; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; import java.util.function.Consumer; +import static Config.SomeCodes.ValidString; + public class AutoCloseAlert { private static Stage currentAlertStage; @@ -37,6 +42,22 @@ public class AutoCloseAlert { clear(); } + /** + * Show an alert with a title, content, and automatically close after a few seconds + * @param title the title of the alert + * @param content the content of the alert + * @param seconds the number of seconds before the alert is closed, or put 0 to keep it open + * @param onClose What to do after auto close + */ + public static void show(String title, String content, int seconds, Consumer onClose){ + Platform.runLater(()->{ + Stage alertStage = _showtext(title, "", content); + closeAlertStage(seconds, onClose, alertStage); + }); + } + + + /** * Show an alert with a title, header, content, and automatically close after a few seconds * If called several times, the previous alert will be closed before showing a new one @@ -48,55 +69,82 @@ public class AutoCloseAlert { */ public static void show(String title, String header, String content, int seconds, Consumer onClose) { Platform.runLater(()->{ - // close previous alert before showing a new one - Optional.ofNullable(currentAlertStage).ifPresent(Stage::close); + Stage alertStage = _showtext(title, header, content); + closeAlertStage(seconds, onClose, alertStage); + }); + } - Stage alertStage = new Stage(); - alertStage.initModality(Modality.APPLICATION_MODAL); - alertStage.initStyle(StageStyle.UTILITY); - alertStage.setAlwaysOnTop(true); - alertStage.setResizable(false); + /** + * Create an alert with a title, header, and content + * @param title the title + * @param header the header + * @param content the content + * @return the alert stage + */ + private static Stage _showtext(String title, String header, String content){ + // close previous alert before showing a new one + close(); - double screenwidth = Screen.getPrimary().getVisualBounds().getWidth(); - double width = screenwidth/4; - double height = width * 9 / 16; + Stage alertStage = new Stage(); + alertStage.initModality(Modality.APPLICATION_MODAL); + alertStage.initStyle(StageStyle.UTILITY); + alertStage.setAlwaysOnTop(true); + alertStage.setResizable(false); + + double screenwidth = Screen.getPrimary().getVisualBounds().getWidth(); + double width = screenwidth/4; + double height = width * 9 / 16; + + List children = new ArrayList<>(); + if (ValidString(header)){ Label headerLabel = new Label(header); headerLabel.setStyle("-fx-font-weight: bold; -fx-font-size: 16px;"); headerLabel.setMinHeight(height*0.25); + children.add(headerLabel); + } + if (ValidString(content)){ Label contentLabel = new Label(content); contentLabel.setStyle("-fx-font-size: 12px;"); contentLabel.setMinHeight(height*0.75); + children.add(contentLabel); + } - VBox root = new VBox(10, headerLabel, contentLabel); - root.setPrefSize(width, height); - root.setAlignment(Pos.CENTER); + VBox root = new VBox(10, children.toArray(new Node[0])); + root.setPrefSize(width, height); + root.setAlignment(Pos.CENTER); - Scene scene = new Scene(root); - alertStage.setScene(scene); + Scene scene = new Scene(root); + alertStage.setScene(scene); - alertStage.setTitle(title); - currentAlertStage = alertStage; + alertStage.setTitle(title); - alertStage.show(); + alertStage.show(); + currentAlertStage = alertStage; + shownHeader = ValidString(header) ? header : ""; + shownContent = ValidString(content) ? content : ""; + shownTitle = ValidString(title) ? title : ""; - shownHeader = header; - shownContent = content; - shownTitle = title; - - if (seconds>0){ - PauseTransition delay = new PauseTransition(Duration.seconds(seconds)); - delay.setOnFinished(e -> { - alertStage.close(); - if (currentAlertStage == alertStage) { - currentAlertStage = null; - } - if (onClose!=null) onClose.accept(shownTitle); - clear(); - } ); - delay.play(); - } - - }); + return alertStage; + } + /** + * Close the alert after a few seconds + * @param seconds the number of seconds before the alert is closed, if 0, the alert will not be closed + * @param onClose What to do after auto close + * @param alertStage the alert stage to be closed + */ + private static void closeAlertStage(int seconds, Consumer onClose, Stage alertStage) { + if (seconds>0){ + PauseTransition delay = new PauseTransition(Duration.seconds(seconds)); + delay.setOnFinished(e -> { + alertStage.close(); + if (currentAlertStage == alertStage) { + currentAlertStage = null; + } + if (onClose!=null) onClose.accept(shownTitle); + clear(); + } ); + delay.play(); + } } }