package ctbrec.ui.controls; import java.io.InputStream; import java.util.Optional; import ctbrec.ui.AutosizeAlert; import javafx.application.Platform; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; import javafx.scene.control.Dialog; import javafx.scene.control.TextArea; import javafx.scene.image.Image; import javafx.scene.layout.GridPane; import javafx.stage.Modality; import javafx.stage.Stage; public class Dialogs { // TODO reduce calls to this method and use Dialogs.showError(Scene parent, String header, String text, Throwable t) instead public static void showError(String header, String text, Throwable t) { showError(null, header, text, t); } public static void showError(Scene parent, String header, String text, Throwable t) { Runnable r = () -> { Alert alert = new AutosizeAlert(Alert.AlertType.ERROR); alert.setTitle("Error"); alert.setHeaderText(header); String content = text; if(t != null) { content += " " + t.getLocalizedMessage(); } alert.setContentText(content); if(parent != null) { Stage stage = (Stage) alert.getDialogPane().getScene().getWindow(); stage.getScene().getStylesheets().addAll(parent.getStylesheets()); } alert.showAndWait(); }; if(Platform.isFxApplicationThread()) { r.run(); } else { Platform.runLater(r); } } public static Optional showTextInput(Scene parent, String title, String header, String text) { Dialog dialog = new Dialog<>(); dialog.setTitle(title); dialog.setHeaderText(header); dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL); dialog.initModality(Modality.APPLICATION_MODAL); dialog.setResizable(true); InputStream icon = Dialogs.class.getResourceAsStream("/icon.png"); Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow(); stage.getIcons().add(new Image(icon)); if(parent != null) { stage.getScene().getStylesheets().addAll(parent.getStylesheets()); } GridPane grid = new GridPane(); grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(20, 150, 10, 10)); TextArea notes = new TextArea(text); notes.setPrefRowCount(3); grid.add(notes, 0, 0); dialog.getDialogPane().setContent(grid); Platform.runLater(() -> notes.requestFocus()); dialog.setResultConverter(dialogButton -> { if (dialogButton == ButtonType.OK) { return notes.getText(); } return null; }); return dialog.showAndWait(); } }