From fb40d991bb15c460d636635f242a27d1f114c99b Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 1 Mar 2023 08:14:25 +0200 Subject: [PATCH] replace default textinput dialog with custom textfield dialog --- .../sparrow/control/TextfieldDialog.java | 73 +++++++++++++++++++ .../GeneralPreferencesController.java | 12 +-- 2 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/sparrow/control/TextfieldDialog.java diff --git a/src/main/java/com/sparrowwallet/sparrow/control/TextfieldDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/TextfieldDialog.java new file mode 100644 index 00000000..6b4ce7d7 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/control/TextfieldDialog.java @@ -0,0 +1,73 @@ +package com.sparrowwallet.sparrow.control; + +import com.sparrowwallet.sparrow.AppServices; +import javafx.application.Platform; +import javafx.beans.NamedArg; +import javafx.scene.control.*; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Priority; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TextfieldDialog extends Dialog { + private static final Logger log = LoggerFactory.getLogger(TextAreaDialog.class); + + private final TextField textField; + private final String defaultValue; + + public TextfieldDialog() { + this(""); + } + + public TextfieldDialog(String defaultValue) { + this(defaultValue, true); + } + + public TextfieldDialog(@NamedArg("defaultValue") String defaultValue, @NamedArg("editable") boolean editable) { + final DialogPane dialogPane = getDialogPane(); + setDialogPane(dialogPane); + + Image image = new Image("/image/sparrow-small.png"); + dialogPane.setGraphic(new ImageView(image)); + + HBox hbox = new HBox(); + this.textField = new TextField(defaultValue); + this.textField.setMaxWidth(Double.MAX_VALUE); + this.textField.setEditable(editable); + hbox.getChildren().add(textField); + HBox.setHgrow(this.textField, Priority.ALWAYS); + + this.defaultValue = defaultValue; + + dialogPane.setContent(hbox); + dialogPane.getStylesheets().add(AppServices.class.getResource("general.css").toExternalForm()); + AppServices.setStageIcon(dialogPane.getScene().getWindow()); + + dialogPane.getStyleClass().add("text-input-dialog"); + dialogPane.getButtonTypes().add(ButtonType.OK); + if(editable) { + dialogPane.getButtonTypes().add(ButtonType.CANCEL); + } + + Platform.runLater(textField::requestFocus); + + setResultConverter((dialogButton) -> { + ButtonBar.ButtonData data = dialogButton == null ? null : dialogButton.getButtonData(); + return data == ButtonBar.ButtonData.OK_DONE ? textField.getText() : null; + }); + + dialogPane.setPrefWidth(600); + dialogPane.setPrefHeight(230); + AppServices.moveToActiveWindowScreen(this); + } + + public final TextField getEditor() { + return textField; + } + + public final String getDefaultValue() { + return defaultValue; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/preferences/GeneralPreferencesController.java b/src/main/java/com/sparrowwallet/sparrow/preferences/GeneralPreferencesController.java index 428e885c..739a34c1 100644 --- a/src/main/java/com/sparrowwallet/sparrow/preferences/GeneralPreferencesController.java +++ b/src/main/java/com/sparrowwallet/sparrow/preferences/GeneralPreferencesController.java @@ -3,6 +3,7 @@ package com.sparrowwallet.sparrow.preferences; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.EventManager; +import com.sparrowwallet.sparrow.control.TextfieldDialog; import com.sparrowwallet.sparrow.control.UnlabeledToggleSwitch; import com.sparrowwallet.sparrow.event.*; import com.sparrowwallet.sparrow.io.Config; @@ -17,7 +18,6 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.ComboBox; -import javafx.scene.control.TextInputDialog; import javafx.util.StringConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -110,11 +110,11 @@ public class GeneralPreferencesController extends PreferencesDetailController { blockExplorers.valueProperty().addListener((observable, oldValue, newValue) -> { if(newValue != null) { if(newValue == CUSTOM_BLOCK_EXPLORER) { - TextInputDialog textInputDialog = new TextInputDialog(); - textInputDialog.setTitle("Enter Block Explorer URL"); - textInputDialog.setHeaderText("Enter the URL of the block explorer.\n\nIf present, the characters {0} will be replaced with the txid.\nFor example, https://localhost or https://localhost/tx/{0}\n"); - textInputDialog.getEditor().setPromptText("https://localhost"); - Optional optUrl = textInputDialog.showAndWait(); + TextfieldDialog textfieldDialog = new TextfieldDialog(); + textfieldDialog.setTitle("Enter Block Explorer URL"); + textfieldDialog.setHeaderText("Enter the URL of the block explorer.\n\nIf present, the characters {0} will be replaced with the txid.\nFor example, https://localhost or https://localhost/tx/{0}\n"); + textfieldDialog.getEditor().setPromptText("https://localhost"); + Optional optUrl = textfieldDialog.showAndWait(); if(optUrl.isPresent() && !optUrl.get().isEmpty()) { try { Server server = getBlockExplorer(optUrl.get());