use AutocompleteComboBox to create new payment method

This commit is contained in:
bvcxza 2024-08-01 08:46:38 -03:00 committed by GitHub
parent cf282fd930
commit 0f0b645f72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 7 deletions

View File

@ -72,6 +72,7 @@ import haveno.core.trade.HavenoUtils;
import haveno.core.util.FormattingUtils; import haveno.core.util.FormattingUtils;
import haveno.core.util.coin.CoinFormatter; import haveno.core.util.coin.CoinFormatter;
import haveno.desktop.common.view.FxmlView; import haveno.desktop.common.view.FxmlView;
import haveno.desktop.components.AutocompleteComboBox;
import haveno.desktop.components.TitledGroupBg; import haveno.desktop.components.TitledGroupBg;
import haveno.desktop.components.paymentmethods.AchTransferForm; import haveno.desktop.components.paymentmethods.AchTransferForm;
import haveno.desktop.components.paymentmethods.AdvancedCashForm; import haveno.desktop.components.paymentmethods.AdvancedCashForm;
@ -144,7 +145,6 @@ import java.util.stream.Collectors;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.ListView; import javafx.scene.control.ListView;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
@ -181,7 +181,7 @@ public class TraditionalAccountsView extends PaymentAccountsView<GridPane, Tradi
private final AdvancedCashValidator advancedCashValidator; private final AdvancedCashValidator advancedCashValidator;
private final TransferwiseValidator transferwiseValidator; private final TransferwiseValidator transferwiseValidator;
private final CoinFormatter formatter; private final CoinFormatter formatter;
private ComboBox<PaymentMethod> paymentMethodComboBox; private AutocompleteComboBox<PaymentMethod> paymentMethodComboBox;
private PaymentMethodForm paymentMethodForm; private PaymentMethodForm paymentMethodForm;
private TitledGroupBg accountTitledGroupBg; private TitledGroupBg accountTitledGroupBg;
private Button saveNewAccountButton; private Button saveNewAccountButton;
@ -463,14 +463,16 @@ public class TraditionalAccountsView extends PaymentAccountsView<GridPane, Tradi
removeAccountRows(); removeAccountRows();
addAccountButton.setDisable(true); addAccountButton.setDisable(true);
accountTitledGroupBg = addTitledGroupBg(root, ++gridRow, 2, Res.get("shared.createNewAccount"), Layout.GROUP_DISTANCE); accountTitledGroupBg = addTitledGroupBg(root, ++gridRow, 2, Res.get("shared.createNewAccount"), Layout.GROUP_DISTANCE);
paymentMethodComboBox = FormBuilder.addComboBox(root, gridRow, Res.get("shared.selectPaymentMethod"), Layout.FIRST_ROW_AND_GROUP_DISTANCE); paymentMethodComboBox = FormBuilder.addAutocompleteComboBox(
paymentMethodComboBox.setVisibleRowCount(11); root, gridRow, Res.get("shared.selectPaymentMethod"), Layout.FIRST_ROW_AND_GROUP_DISTANCE
);
paymentMethodComboBox.setVisibleRowCount(Math.min(paymentMethodComboBox.getItems().size(), 10));
paymentMethodComboBox.setPrefWidth(250); paymentMethodComboBox.setPrefWidth(250);
List<PaymentMethod> list = PaymentMethod.paymentMethods.stream() List<PaymentMethod> list = PaymentMethod.paymentMethods.stream()
.filter(PaymentMethod::isTraditional) .filter(PaymentMethod::isTraditional)
.sorted() .sorted()
.collect(Collectors.toList()); .collect(Collectors.toList());
paymentMethodComboBox.setItems(FXCollections.observableArrayList(list)); paymentMethodComboBox.setAutocompleteItems(FXCollections.observableArrayList(list));
paymentMethodComboBox.setConverter(new StringConverter<>() { paymentMethodComboBox.setConverter(new StringConverter<>() {
@Override @Override
public String toString(PaymentMethod paymentMethod) { public String toString(PaymentMethod paymentMethod) {
@ -479,10 +481,15 @@ public class TraditionalAccountsView extends PaymentAccountsView<GridPane, Tradi
@Override @Override
public PaymentMethod fromString(String s) { public PaymentMethod fromString(String s) {
return null; if (s.isEmpty())
return null;
return paymentMethodComboBox.getItems().stream()
.filter(item -> Res.get(item.getId()).equals(s))
.findAny().orElse(null);
} }
}); });
paymentMethodComboBox.setOnAction(e -> { paymentMethodComboBox.setOnChangeConfirmed(e -> {
if (paymentMethodForm != null) { if (paymentMethodForm != null) {
FormBuilder.removeRowsFromGridPane(root, 3, paymentMethodForm.getGridRow() + 1); FormBuilder.removeRowsFromGridPane(root, 3, paymentMethodForm.getGridRow() + 1);
GridPane.setRowSpan(accountTitledGroupBg, paymentMethodForm.getRowSpan() + 1); GridPane.setRowSpan(accountTitledGroupBg, paymentMethodForm.getRowSpan() + 1);
@ -550,6 +557,7 @@ public class TraditionalAccountsView extends PaymentAccountsView<GridPane, Tradi
} }
private PaymentMethodForm getPaymentMethodForm(PaymentMethod paymentMethod) { private PaymentMethodForm getPaymentMethodForm(PaymentMethod paymentMethod) {
if (paymentMethod == null) return null;
final PaymentAccount paymentAccount = PaymentAccountFactory.getPaymentAccount(paymentMethod); final PaymentAccount paymentAccount = PaymentAccountFactory.getPaymentAccount(paymentMethod);
paymentAccount.init(); paymentAccount.init();
return getPaymentMethodForm(paymentMethod, paymentAccount); return getPaymentMethodForm(paymentMethod, paymentAccount);

View File

@ -1405,6 +1405,24 @@ public class FormBuilder {
return comboBox; return comboBox;
} }
public static <T> AutocompleteComboBox<T> addAutocompleteComboBox(GridPane gridPane, int rowIndex, String title, double top) {
var comboBox = new AutocompleteComboBox<T>();
comboBox.setLabelFloat(true);
comboBox.setPromptText(title);
comboBox.setMaxWidth(Double.MAX_VALUE);
// Default ComboBox does not show promptText after clear selection.
// https://stackoverflow.com/questions/50569330/how-to-reset-combobox-and-display-prompttext?noredirect=1&lq=1
comboBox.setButtonCell(getComboBoxButtonCell(title, comboBox));
GridPane.setRowIndex(comboBox, rowIndex);
GridPane.setColumnIndex(comboBox, 0);
GridPane.setMargin(comboBox, new Insets(top + Layout.FLOATING_LABEL_DISTANCE, 0, 0, 0));
gridPane.getChildren().add(comboBox);
return comboBox;
}
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// Label + AutocompleteComboBox // Label + AutocompleteComboBox
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////