diff --git a/client/src/main/java/ctbrec/ui/action/CheckModelAccountAction.java b/client/src/main/java/ctbrec/ui/action/CheckModelAccountAction.java index 5bdc9e04..d4ba90f1 100644 --- a/client/src/main/java/ctbrec/ui/action/CheckModelAccountAction.java +++ b/client/src/main/java/ctbrec/ui/action/CheckModelAccountAction.java @@ -1,15 +1,15 @@ package ctbrec.ui.action; +import ctbrec.GlobalThreadPool; +import ctbrec.Model; +import ctbrec.recorder.Recorder; +import ctbrec.ui.action.StopRecordingAction; +import ctbrec.ui.controls.Dialogs; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; - -import ctbrec.GlobalThreadPool; -import ctbrec.Model; -import ctbrec.recorder.Recorder; -import ctbrec.ui.controls.Dialogs; import javafx.application.Platform; import javafx.scene.control.Button; import lombok.extern.slf4j.Slf4j; @@ -17,9 +17,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class CheckModelAccountAction { private Button b; - private Recorder recorder; - private String buttonText; public CheckModelAccountAction(Button b, Recorder recorder) { @@ -30,30 +28,30 @@ public class CheckModelAccountAction { public void execute(Predicate filter) { b.setDisable(true); - Runnable checker = (() -> { - List deletedAccounts = new ArrayList<>(); + Runnable checker = () -> { + ArrayList deletedAccounts = new ArrayList(); try { checkModelAccounts(filter, deletedAccounts); - } finally { + } + finally { showResult(deletedAccounts); } - }); - GlobalThreadPool.submit(checker); + }; + GlobalThreadPool.submit((Runnable)checker); } private void showResult(List deletedAccounts) { Platform.runLater(() -> { b.setDisable(false); - b.setText(buttonText); + b.setText(this.buttonText); if (!deletedAccounts.isEmpty()) { - var sb = new StringBuilder(); + StringBuilder sb = new StringBuilder(); for (Model deletedModel : deletedAccounts) { - String name = deletedModel.getDisplayName() + " ".repeat(30); - name = name.substring(0, 30); - sb.append(name).append(' ').append('(').append(deletedModel.getUrl()).append(')').append('\n'); + Object name = deletedModel.getDisplayName() + " ".repeat(30); + name = ((String)name).substring(0, 30); + sb.append((String)name).append(' ').append('(').append(deletedModel.getUrl()).append(')').append('\n'); } - boolean remove = Dialogs.showConfirmDialog("Deleted Accounts", sb.toString(), - "The following accounts seem to have been deleted. Do you want to remove them?", b.getScene()); + boolean remove = Dialogs.showExpandedConfirmDialog("Deleted Accounts", "Accounts list:", sb.toString(), "The following accounts seem to have been deleted. Do you want to remove them?", b.getScene()); if (remove) { new StopRecordingAction(b, deletedAccounts, recorder).execute(); } @@ -62,21 +60,21 @@ public class CheckModelAccountAction { } private void checkModelAccounts(Predicate filter, List deletedAccounts) { - List models = recorder.getModels().stream() // - .filter(filter) // - .collect(Collectors.toList()); + List models = recorder.getModels().stream().filter(filter).collect(Collectors.toList()); int total = models.size(); - for (var i = 0; i < total; i++) { - final int counter = i+1; - Platform.runLater(() -> b.setText(buttonText + ' ' + counter + '/' + total)); - var modelToCheck = models.get(i); + for (int i = 0; i < total; ++i) { + int counter = i + 1; + Platform.runLater(() -> b.setText(buttonText + " " + counter + "/" + total)); + Model modelToCheck = (Model)models.get(i); try { - if (!modelToCheck.exists()) { - deletedAccounts.add(modelToCheck); - } - } catch (IOException e) { - log.warn("Couldn't check, if model account still exists", e); + if (modelToCheck.exists()) continue; + deletedAccounts.add(modelToCheck); + continue; + } + catch (IOException e) { + log.warn("Couldn't check, if model account still exists", (Throwable)e); } } } } + diff --git a/client/src/main/java/ctbrec/ui/controls/Dialogs.java b/client/src/main/java/ctbrec/ui/controls/Dialogs.java index 340cfca0..548b6586 100644 --- a/client/src/main/java/ctbrec/ui/controls/Dialogs.java +++ b/client/src/main/java/ctbrec/ui/controls/Dialogs.java @@ -1,6 +1,12 @@ package ctbrec.ui.controls; import ctbrec.ui.AutosizeAlert; +import java.io.InputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Collection; +import java.util.Objects; +import java.util.Optional; import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.geometry.Insets; @@ -9,6 +15,7 @@ import javafx.scene.control.*; import javafx.scene.control.Alert.AlertType; import javafx.scene.image.Image; import javafx.scene.layout.GridPane; +import javafx.scene.layout.Priority; import javafx.scene.layout.Region; import javafx.stage.Modality; import javafx.stage.Stage; @@ -29,26 +36,68 @@ public class Dialogs { } public static void showError(String header, String text, Throwable t) { - showError(scene, header, text, t); + if (Objects.nonNull(t)) { + Dialogs.showError(scene, header, text, t); + } else { + Dialogs.showError(scene, header, text); + } } - public static void showError(Scene parent, String header, String text, Throwable t) { + public static void showError(Scene parent, String header, String text) { Runnable r = () -> { - Alert alert = new AutosizeAlert(Alert.AlertType.ERROR, parent); + AutosizeAlert alert = new AutosizeAlert(Alert.AlertType.ERROR, parent); alert.setTitle("Error"); alert.setHeaderText(header); - String content = text; - if (t != null) { - content += " " + t.getLocalizedMessage(); - } - alert.setContentText(content); + alert.setContentText(text); if (parent != null) { - var stage = (Stage) alert.getDialogPane().getScene().getWindow(); - stage.getScene().getStylesheets().addAll(parent.getStylesheets()); + Stage stage = (Stage)alert.getDialogPane().getScene().getWindow(); + stage.getScene().getStylesheets().addAll((Collection)parent.getStylesheets()); } alert.showAndWait(); }; + if (Platform.isFxApplicationThread()) { + r.run(); + } else { + Platform.runLater(r); + } + } + public static void showError(Scene parent, String header, String text, Throwable t) { + if (Objects.isNull(t)) { + Dialogs.showError(parent, header, text); + return; + } + Runnable r = () -> { + AutosizeAlert alert = new AutosizeAlert(Alert.AlertType.ERROR, parent); + alert.setTitle("Error"); + alert.setHeaderText(header); + String content = text + "\n" + t.getLocalizedMessage(); + alert.setContentText(content); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + t.printStackTrace(pw); + String exceptionText = sw.toString(); + Label label = new Label("The exception stacktrace was:"); + TextArea textArea = new TextArea(exceptionText); + textArea.setEditable(false); + textArea.setWrapText(true); + textArea.setMaxWidth(Double.MAX_VALUE); + textArea.setMaxHeight(Double.MAX_VALUE); + GridPane.setVgrow(textArea, Priority.ALWAYS); + GridPane.setHgrow(textArea, Priority.ALWAYS); + GridPane expContent = new GridPane(); + expContent.setMaxWidth(Double.MAX_VALUE); + expContent.add(label, 0, 0); + expContent.add(textArea, 0, 1); + alert.getDialogPane().setExpandableContent(expContent); + alert.getDialogPane().setMinHeight(Double.NEGATIVE_INFINITY); + alert.getDialogPane().setMinWidth(Double.NEGATIVE_INFINITY); + if (parent != null) { + Stage stage = (Stage)alert.getDialogPane().getScene().getWindow(); + stage.getScene().getStylesheets().addAll((Collection)parent.getStylesheets()); + } + alert.showAndWait(); + }; if (Platform.isFxApplicationThread()) { r.run(); } else { @@ -121,6 +170,24 @@ public class Dialogs { return confirm.getResult() == YES; } + public static boolean showExpandedConfirmDialog(String title, String message, String expandedText, String header, Scene parent) { + AutosizeAlert confirm = new AutosizeAlert(Alert.AlertType.CONFIRMATION, message, parent, ButtonType.YES, ButtonType.NO); + confirm.setTitle(title); + confirm.setHeaderText(header); + TextArea textArea = new TextArea(expandedText); + textArea.setEditable(false); + textArea.setWrapText(true); + textArea.setMaxWidth(Double.MAX_VALUE); + textArea.setMaxHeight(Double.MAX_VALUE); + GridPane.setVgrow(textArea, Priority.ALWAYS); + GridPane.setHgrow(textArea, Priority.ALWAYS); + confirm.getDialogPane().setExpandableContent(textArea); + confirm.getDialogPane().setExpanded(true); + confirm.getDialogPane().setMinHeight(Double.NEGATIVE_INFINITY); + confirm.showAndWait(); + return confirm.getResult() == ButtonType.YES; + } + public static ButtonType showShutdownDialog(Scene parent) { var message = "There are recordings in progress"; var confirm = new AutosizeAlert(AlertType.CONFIRMATION, "", parent, YES, FINISH, NO);