forked from Querz/mcaselector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/net/querz/mcaselector/ui/dialog/ErrorDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters