Skip to content

Commit

Permalink
replace default textinput dialog with custom textfield dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
craigraw committed Mar 1, 2023
1 parent 5fe6a71 commit fb40d99
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand All @@ -17,7 +18,6 @@
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;
Expand Down Expand Up @@ -110,11 +110,11 @@ public Server fromString(String string) {
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<String> 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<String> optUrl = textfieldDialog.showAndWait();
if(optUrl.isPresent() && !optUrl.get().isEmpty()) {
try {
Server server = getBlockExplorer(optUrl.get());
Expand Down

0 comments on commit fb40d99

Please sign in to comment.