Skip to content

Commit

Permalink
Rework error and warning dialogs and group warnings. (#782)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>
Co-authored-by: Tom Martin <[email protected]>
Co-authored-by: Maik Marschner <[email protected]>
  • Loading branch information
4 people authored Jan 17, 2021
1 parent 309950f commit ad3806d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 26 deletions.
53 changes: 45 additions & 8 deletions chunky/src/java/se/llbit/chunky/ui/ChunkyErrorDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand All @@ -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));

Expand All @@ -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 -> {
Expand All @@ -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
}
}
}
41 changes: 23 additions & 18 deletions chunky/src/java/se/llbit/chunky/ui/UILogReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

}
18 changes: 18 additions & 0 deletions chunky/src/res/se/llbit/chunky/ui/WarningDialog.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TabPane?>

<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<top>
<Label text="A warning occurred!&#10;&#10;Below is more information.&#10;" />
</top>
<center>
<TabPane fx:id="tabPane" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" />
</center>
<padding>
<Insets left="10.0" right="10.0" top="10.0" />
</padding>
</BorderPane>

0 comments on commit ad3806d

Please sign in to comment.