Skip to content

Commit

Permalink
add error dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Querz committed Jan 22, 2021
1 parent c09541c commit 00f8b16
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/java/net/querz/mcaselector/text/Translation.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public enum Translation {
DIALOG_PROGRESS_TITLE_EXPORTING_FILTERED_CHUNKS("dialog.progress.title.exporting_filtered_chunks"),
DIALOG_PROGRESS_TITLE_SELECTING_FILTERED_CHUNKS("dialog.progress.title.selecting_filtered_chunks"),
DIALOG_PROGRESS_TITLE_CHANGING_NBT_DATA("dialog.progress.title.changing_nbt_data"),
DIALOG_ERROR_BUTTON_COPY_TO_CLIPBOARD("dialog.error.button.copy_to_clipboard"),
DIALOG_ERROR_TITLE("dialog.error.title"),
DIALOG_ERROR_HEADER("dialog.error.header"),
DIALOG_ERROR_COPIED_TO_CLIPBOARD("dialog.error.copied_to_clipboard"),
BUTTON_CANCEL("button.cancel"),
BUTTON_OK("button.ok");

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/querz/mcaselector/ui/DialogHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.querz.mcaselector.ui.dialog.ChangeFieldsConfirmationDialog;
import net.querz.mcaselector.ui.dialog.ChangeNBTDialog;
import net.querz.mcaselector.ui.dialog.DeleteConfirmationDialog;
import net.querz.mcaselector.ui.dialog.ErrorDialog;
import net.querz.mcaselector.ui.dialog.ExportConfirmationDialog;
import net.querz.mcaselector.ui.dialog.FilterChunksDialog;
import net.querz.mcaselector.ui.dialog.GotoDialog;
Expand Down Expand Up @@ -427,7 +428,7 @@ public static void openWorld(TileMap tileMap, Stage primaryStage, OptionBar opti
if (file != null && file.isDirectory()) {
List<File> dimensions = detectDimensionDirectories(file);
if (dimensions.size() == 0) {
// TODO: show error dialog that we didn't find any dimension
new ErrorDialog(primaryStage, String.format("no dimensions found in %s", file.getAbsolutePath())).showAndWait();
Debug.dumpf("no dimensions found in %s", file.getAbsolutePath());
return;
}
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/net/querz/mcaselector/ui/dialog/ErrorDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package net.querz.mcaselector.ui.dialog;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import net.querz.mcaselector.text.TextHelper;
import net.querz.mcaselector.text.Translation;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;

public class ErrorDialog extends Alert {

private final VBox content = new VBox();

public ErrorDialog(Stage primaryStage, String errorMessage) {
super(AlertType.ERROR, null, ButtonType.CLOSE);
ButtonType copyToClipbaord = new ButtonType(Translation.DIALOG_ERROR_BUTTON_COPY_TO_CLIPBOARD.toString(), ButtonBar.ButtonData.BACK_PREVIOUS);
getDialogPane().getButtonTypes().add(0, copyToClipbaord);
initStyle(StageStyle.UTILITY);
getDialogPane().getStyleClass().add("error-dialog-pane");
titleProperty().bind(Translation.DIALOG_ERROR_TITLE.getProperty());
headerTextProperty().bind(Translation.DIALOG_ERROR_HEADER.getProperty());

TextArea errorText = new TextArea();
errorText.setEditable(false);
errorText.setText(errorMessage);

Label copiedToClipboard = new Label();
copiedToClipboard.getStyleClass().add("copied-to-clipboard-label");

getDialogPane().lookupButton(copyToClipbaord).addEventFilter(ActionEvent.ACTION, e -> {
copyTextToClipboard(errorMessage);
showLabelTextForXSeconds(copiedToClipboard, Translation.DIALOG_ERROR_COPIED_TO_CLIPBOARD.toString(), 3);
e.consume();
});

content.getChildren().addAll(errorText, copiedToClipboard);

getDialogPane().setContent(content);

getDialogPane().getStylesheets().addAll(primaryStage.getScene().getStylesheets());
}

public ErrorDialog(Stage primaryStage, Exception ex) {
this(primaryStage, TextHelper.getStacktraceAsString(ex));
}

private void copyTextToClipboard(String text) {
StringSelection stringSelection = new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
}

private boolean showingCopiedToClipboardText = false;

private void showLabelTextForXSeconds(Label label, String text, int seconds) {
if (showingCopiedToClipboardText) {
return;
}
showingCopiedToClipboardText = true;
label.setText(text);
Thread t = new Thread(() -> {
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(() -> {
label.setText(null);
showingCopiedToClipboardText = false;
});
});
t.start();
}
}
17 changes: 17 additions & 0 deletions src/main/resources/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@
-fx-padding: 0;
}

/*---------- TEXT AREA ----------*/

.text-area {
-fx-text-fill: -color-text-light;
-fx-pref-height: 75px;
}

.text-area .content {
-fx-background-color: -color-text-field-background;
}

/*---------- LISTVIEW ----------*/

.list-view {
Expand Down Expand Up @@ -372,6 +383,12 @@
-fx-text-fill: -color-text-light;
}

/*---------- ERROR DIALOG ----------*/

.error-dialog-pane .copied-to-clipboard-label {
-fx-padding: 10 0 -10 0;
}

/*---------- PROGRESS DIALOG ----------*/

.progress-dialog {
Expand Down

0 comments on commit 00f8b16

Please sign in to comment.