From ad3806d0f3a21d2c7c9e542a7d7e17700def13b5 Mon Sep 17 00:00:00 2001 From: LasaBir Date: Sun, 17 Jan 2021 06:58:35 -0800 Subject: [PATCH] Rework error and warning dialogs and group warnings. (#782) * Reworked Error and Warning boxes. * Error box now puts itself ontop of the chunky window. (if possible, won't let it be put behind anything else) * Group Warnings together and use ChunkyErrorDialog object. * Having warnings up no longer prevents the main UI from being interacted with (side effect of using Stage instead of Alert) * Pressing escape to dismiss ChunkyErrorDialog only triggers on KeyReleased, so you wont accidentally dismiss all errors and warnings when meaning to only dismiss one of the two. * Log.Level enum now holds its name in a final string. * Other little bits of code cleanup. * Apply code formatting by @NotStirred Co-authored-by: Tom Martin * Rename error and warning windows, move the log level name into the ui code. * Fix warning dialog being created as "Information Summary" if the first message has level INFO. Co-authored-by: Maik Marschner Co-authored-by: Tom Martin Co-authored-by: Maik Marschner --- .../se/llbit/chunky/ui/ChunkyErrorDialog.java | 53 ++++++++++++++++--- .../se/llbit/chunky/ui/UILogReceiver.java | 41 +++++++------- .../res/se/llbit/chunky/ui/WarningDialog.fxml | 18 +++++++ 3 files changed, 86 insertions(+), 26 deletions(-) create mode 100644 chunky/src/res/se/llbit/chunky/ui/WarningDialog.fxml diff --git a/chunky/src/java/se/llbit/chunky/ui/ChunkyErrorDialog.java b/chunky/src/java/se/llbit/chunky/ui/ChunkyErrorDialog.java index d18f617902..782fff2443 100644 --- a/chunky/src/java/se/llbit/chunky/ui/ChunkyErrorDialog.java +++ b/chunky/src/java/se/llbit/chunky/ui/ChunkyErrorDialog.java @@ -33,6 +33,7 @@ import javafx.scene.layout.BorderPane; import javafx.scene.layout.Region; import javafx.stage.Stage; +import se.llbit.log.Level; import java.io.IOException; import java.net.URL; @@ -49,20 +50,23 @@ public class ChunkyErrorDialog extends Stage implements Initializable { @FXML TabPane tabPane; - private int errorCount = 0; + private int messageCount = 0; + private final Level type; /** * Initialize the error dialog. */ - public ChunkyErrorDialog() throws IOException { - FXMLLoader loader = new FXMLLoader(getClass().getResource("ErrorDialog.fxml")); + public ChunkyErrorDialog(Level errorType) throws IOException { + type = errorType; + + FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlName(errorType))); loader.setController(this); Parent root = loader.load(); - setTitle("Error Summary"); + setTitle(getLevelName(errorType) + " Summary"); getIcons().add(new Image(getClass().getResourceAsStream("/chunky-icon.png"))); setScene(new Scene(root)); addEventFilter(KeyEvent.ANY, e -> { - if (e.getCode() == KeyCode.ESCAPE) { + if (e.getCode() == KeyCode.ESCAPE && e.getEventType() == KeyEvent.KEY_RELEASED) { e.consume(); close(); } @@ -75,8 +79,18 @@ public ChunkyErrorDialog() throws IOException { /** * Add a log record to be displayed by this error dialog. */ - public synchronized void addErrorMessage(String message) { - errorCount += 1; + public synchronized void addMessageAndShow(String message) { + addMessage(message); + this.show(); + this.setAlwaysOnTop(true); // will probably fail. + this.toFront(); + } + + /** + * Add a log record to be displayed by this error dialog. + */ + private synchronized void addMessage(String message) { + messageCount += 1; BorderPane.setMargin(tabPane, new Insets(10, 0, 0, 0)); @@ -95,7 +109,7 @@ public synchronized void addErrorMessage(String message) { BorderPane.setMargin(dismiss, new Insets(10, 0, 0, 0)); pane.setBottom(dismiss); - Tab tab = new Tab("Error " + errorCount, pane); + Tab tab = new Tab(getLevelName(type) + " " + messageCount, pane); tabPane.getTabs().add(tab); dismiss.setOnAction(event -> { @@ -106,4 +120,27 @@ public synchronized void addErrorMessage(String message) { }); } + private String fxmlName(Level level) + { + switch (level) { + case WARNING: + return "WarningDialog.fxml"; + case ERROR: + default: // To prevent crash should this happen (it shouldn't), use Error. + return "ErrorDialog.fxml"; + } + } + + private String getLevelName(Level level) { + switch (level) { + case WARNING: + return "Warning"; + case ERROR: + return "Error"; + case INFO: + return "Info"; + default: + return ""; // should never happen + } + } } diff --git a/chunky/src/java/se/llbit/chunky/ui/UILogReceiver.java b/chunky/src/java/se/llbit/chunky/ui/UILogReceiver.java index cf20ca07b7..398dc0bbb6 100644 --- a/chunky/src/java/se/llbit/chunky/ui/UILogReceiver.java +++ b/chunky/src/java/se/llbit/chunky/ui/UILogReceiver.java @@ -34,31 +34,36 @@ public class UILogReceiver extends Receiver { private ChunkyErrorDialog errorDialog = null; + private ChunkyErrorDialog warningDialog = null; @Override public void logEvent(Level level, final String message) { + Platform.runLater(() -> { + createOrGetDialogContainer(level).addMessageAndShow(message); + }); + } + + private ChunkyErrorDialog createOrGetDialogContainer(Level level) { switch (level) { case INFO: case WARNING: - Platform.runLater(() -> { - Alert warning = Dialogs.createAlert(Alert.AlertType.WARNING); - warning.setContentText(message); - warning.show(); - }); - break; + if (warningDialog == null) { + try { + warningDialog = new ChunkyErrorDialog(Level.WARNING); + } catch (IOException e) { + throw new Error("Failed to create warning dialog", e); + } + } + return warningDialog; case ERROR: - Platform.runLater(() -> { - if (errorDialog == null) { - try { - errorDialog = new ChunkyErrorDialog(); - } catch (IOException e) { - throw new Error("Failed to create error dialog", e); - } + default: + if (errorDialog == null) { + try { + errorDialog = new ChunkyErrorDialog(Level.ERROR); + } catch (IOException e) { + throw new Error("Failed to create error dialog", e); } - errorDialog.addErrorMessage(message); - errorDialog.show(); - }); - break; + } + return errorDialog; } } - } diff --git a/chunky/src/res/se/llbit/chunky/ui/WarningDialog.fxml b/chunky/src/res/se/llbit/chunky/ui/WarningDialog.fxml new file mode 100644 index 0000000000..7f2918ddfc --- /dev/null +++ b/chunky/src/res/se/llbit/chunky/ui/WarningDialog.fxml @@ -0,0 +1,18 @@ + + + + + + + + + + +
+ +
+ + + +