Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up all payment method forms #1821

Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@

package bisq.desktop.components.paymentmethods;

import bisq.desktop.components.InputTextField;
import bisq.desktop.util.FormBuilder;
import bisq.desktop.util.validation.AliPayValidator;

import bisq.core.locale.Res;
import bisq.core.locale.TradeCurrency;
import bisq.core.payment.AccountAgeWitnessService;
import bisq.core.payment.AliPayAccount;
import bisq.core.payment.PaymentAccount;
Expand All @@ -31,21 +28,13 @@
import bisq.core.util.BSFormatter;
import bisq.core.util.validation.InputValidator;

import org.apache.commons.lang3.StringUtils;

import javafx.scene.layout.GridPane;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static bisq.desktop.util.FormBuilder.addTopLabelTextFieldWithCopyIcon;

public class AliPayForm extends PaymentMethodForm {
private static final Logger log = LoggerFactory.getLogger(AliPayForm.class);
public class AliPayForm extends GeneralAccountNumberForm {

private final AliPayAccount aliPayAccount;
private final AliPayValidator aliPayValidator;
private InputTextField accountNrInputTextField;

public static int addFormForBuyer(GridPane gridPane, int gridRow, PaymentAccountPayload paymentAccountPayload) {
addTopLabelTextFieldWithCopyIcon(gridPane, ++gridRow, Res.get("payment.account.no"), ((AliPayAccountPayload) paymentAccountPayload).getAccountNr());
Expand All @@ -55,49 +44,15 @@ public static int addFormForBuyer(GridPane gridPane, int gridRow, PaymentAccount
public AliPayForm(PaymentAccount paymentAccount, AccountAgeWitnessService accountAgeWitnessService, AliPayValidator aliPayValidator, InputValidator inputValidator, GridPane gridPane, int gridRow, BSFormatter formatter) {
super(paymentAccount, accountAgeWitnessService, inputValidator, gridPane, gridRow, formatter);
this.aliPayAccount = (AliPayAccount) paymentAccount;
this.aliPayValidator = aliPayValidator;
}

@Override
public void addFormForAddAccount() {
gridRowFrom = gridRow + 1;

accountNrInputTextField = FormBuilder.addInputTextField(gridPane, ++gridRow, Res.get("payment.account.no"));
accountNrInputTextField.setValidator(aliPayValidator);
accountNrInputTextField.textProperty().addListener((ov, oldValue, newValue) -> {
aliPayAccount.setAccountNr(newValue);
updateFromInputs();
});

final TradeCurrency singleTradeCurrency = aliPayAccount.getSingleTradeCurrency();
final String nameAndCode = singleTradeCurrency != null ? singleTradeCurrency.getNameAndCode() : "";
FormBuilder.addTopLabelTextField(gridPane, ++gridRow, Res.get("shared.currency"), nameAndCode);
addLimitations();
addAccountNameTextFieldWithAutoFillToggleButton();
void setAccountNumber(String newValue) {
aliPayAccount.setAccountNr(newValue);
}

@Override
protected void autoFillNameTextField() {
if (useCustomAccountNameToggleButton != null && !useCustomAccountNameToggleButton.isSelected()) {
String accountNr = accountNrInputTextField.getText();
accountNr = StringUtils.abbreviate(accountNr, 9);
String method = Res.get(paymentAccount.getPaymentMethod().getId());
accountNameTextField.setText(method.concat(": ").concat(accountNr));
}
String getAccountNr() {
return aliPayAccount.getAccountNr();
}

@Override
public void addFormForDisplayAccount() {
addFormForAccountNumberDisplayAccount(aliPayAccount.getAccountName(),
aliPayAccount.getPaymentMethod(), aliPayAccount.getAccountNr(),
aliPayAccount.getSingleTradeCurrency());
}

@Override
public void updateAllInputsValid() {
allInputsValid.set(isAccountNameValid()
&& aliPayValidator.validate(aliPayAccount.getAccountNr()).isValid
&& aliPayAccount.getTradeCurrencies().size() > 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import bisq.core.util.BSFormatter;
import bisq.core.util.validation.InputValidator;

import org.apache.commons.lang3.StringUtils;

import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

Expand Down Expand Up @@ -85,12 +83,7 @@ public void addFormForAddAccount() {

@Override
protected void autoFillNameTextField() {
if (useCustomAccountNameToggleButton != null && !useCustomAccountNameToggleButton.isSelected()) {
String mobileNr = mobileNrInputTextField.getText();
mobileNr = StringUtils.abbreviate(mobileNr, 9);
String method = Res.get(paymentAccount.getPaymentMethod().getId());
accountNameTextField.setText(method.concat(": ").concat(mobileNr));
}
setAccountNameWithString(mobileNrInputTextField.getText());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package bisq.desktop.components.paymentmethods;

import bisq.desktop.components.InputTextField;
import bisq.desktop.util.FormBuilder;

import bisq.core.locale.Res;
import bisq.core.locale.TradeCurrency;
import bisq.core.payment.AccountAgeWitnessService;
import bisq.core.payment.PaymentAccount;
import bisq.core.util.BSFormatter;
import bisq.core.util.validation.InputValidator;

import javafx.scene.layout.GridPane;

abstract public class GeneralAccountNumberForm extends PaymentMethodForm {

private InputTextField accountNrInputTextField;

public GeneralAccountNumberForm(PaymentAccount paymentAccount, AccountAgeWitnessService accountAgeWitnessService, InputValidator inputValidator, GridPane gridPane, int gridRow, BSFormatter formatter) {
super(paymentAccount, accountAgeWitnessService, inputValidator, gridPane, gridRow, formatter);
}

@Override
public void addFormForAddAccount() {
gridRowFrom = gridRow + 1;

accountNrInputTextField = FormBuilder.addInputTextField(gridPane, ++gridRow, Res.get("payment.account.no"));
accountNrInputTextField.setValidator(inputValidator);
accountNrInputTextField.textProperty().addListener((ov, oldValue, newValue) -> {
setAccountNumber(newValue);
updateFromInputs();
});

addTradeCurrency();

addLimitations();
addAccountNameTextFieldWithAutoFillToggleButton();
}

public void addTradeCurrency() {
final TradeCurrency singleTradeCurrency = paymentAccount.getSingleTradeCurrency();
final String nameAndCode = singleTradeCurrency != null ? singleTradeCurrency.getNameAndCode() : "";
FormBuilder.addTopLabelTextField(gridPane, ++gridRow, Res.get("shared.currency"), nameAndCode);
}

@Override
protected void autoFillNameTextField() {
setAccountNameWithString(accountNrInputTextField.getText());
}

@Override
public void addFormForDisplayAccount() {
addFormForAccountNumberDisplayAccount(paymentAccount.getAccountName(), paymentAccount.getPaymentMethod(), getAccountNr(),
paymentAccount.getSingleTradeCurrency());
}

@Override
public void updateAllInputsValid() {
allInputsValid.set(isAccountNameValid()
&& inputValidator.validate(getAccountNr()).isValid
&& paymentAccount.getTradeCurrencies().size() > 0);
}

abstract void setAccountNumber(String newValue);

abstract String getAccountNr();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package bisq.desktop.components.paymentmethods;

import bisq.desktop.components.InputTextField;
import bisq.desktop.util.FormBuilder;

import bisq.core.locale.Country;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res;
import bisq.core.locale.TradeCurrency;
import bisq.core.payment.AccountAgeWitnessService;
import bisq.core.payment.CountryBasedPaymentAccount;
import bisq.core.payment.PaymentAccount;
import bisq.core.util.BSFormatter;
import bisq.core.util.validation.InputValidator;

import org.apache.commons.lang3.StringUtils;

import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXTextField;

import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;

import javafx.collections.FXCollections;

import javafx.util.StringConverter;

import java.util.ArrayList;
import java.util.List;

import static bisq.desktop.util.FormBuilder.addTopLabelWithVBox;

public abstract class GeneralSepaForm extends PaymentMethodForm {

static final String BIC = "BIC";
static final String IBAN = "IBAN";

final List<CheckBox> euroCountryCheckBoxes = new ArrayList<>();
final List<CheckBox> nonEuroCountryCheckBoxes = new ArrayList<>();
private TextField currencyTextField;
private ComboBox<TradeCurrency> currencyComboBox;
InputTextField ibanInputTextField;

GeneralSepaForm(PaymentAccount paymentAccount, AccountAgeWitnessService accountAgeWitnessService, InputValidator inputValidator, GridPane gridPane, int gridRow, BSFormatter formatter) {
super(paymentAccount, accountAgeWitnessService, inputValidator, gridPane, gridRow, formatter);
}

@Override
protected void autoFillNameTextField() {
if (useCustomAccountNameToggleButton != null && !useCustomAccountNameToggleButton.isSelected()) {
TradeCurrency singleTradeCurrency = this.paymentAccount.getSingleTradeCurrency();
String currency = singleTradeCurrency != null ? singleTradeCurrency.getCode() : null;
if (currency != null) {
String iban = ibanInputTextField.getText();
if (iban.length() > 9)
iban = StringUtils.abbreviate(iban, 9);
String method = Res.get(paymentAccount.getPaymentMethod().getId());
CountryBasedPaymentAccount countryBasedPaymentAccount = (CountryBasedPaymentAccount) this.paymentAccount;
String country = countryBasedPaymentAccount.getCountry() != null ?
countryBasedPaymentAccount.getCountry().code : null;
if (country != null)
accountNameTextField.setText(method.concat(" (").concat(currency).concat("/").concat(country)
.concat("): ").concat(iban));
}
}
}

void setCountryComboBoxAction(ComboBox<Country> countryComboBox, CountryBasedPaymentAccount paymentAccount) {
countryComboBox.setOnAction(e -> {
Country selectedItem = countryComboBox.getSelectionModel().getSelectedItem();
paymentAccount.setCountry(selectedItem);
TradeCurrency currency = CurrencyUtil.getCurrencyByCountryCode(selectedItem.code);
setupCurrency(selectedItem, currency);

updateCountriesSelection(euroCountryCheckBoxes);
updateCountriesSelection(nonEuroCountryCheckBoxes);
updateFromInputs();
});
}

void updateCurrencyFormElements(TradeCurrency currency, boolean isSepaCountry, CountryBasedPaymentAccount paymentAccount) {
if (isSepaCountry) {
currencyTextField.setVisible(true);
currencyTextField.setManaged(true);
currencyComboBox.setVisible(false);
currencyComboBox.setManaged(false);
paymentAccount.setSingleTradeCurrency(currency);
currencyTextField.setText(Res.get("payment.currencyWithSymbol", currency.getNameAndCode()));
} else {
currencyComboBox.setVisible(true);
currencyComboBox.setManaged(true);
currencyTextField.setVisible(false);
currencyTextField.setManaged(false);
currencyComboBox.setItems(FXCollections.observableArrayList(currency,
CurrencyUtil.getFiatCurrency("EUR").get()));
currencyComboBox.setOnAction(e2 -> {
paymentAccount.setSingleTradeCurrency(currencyComboBox.getSelectionModel().getSelectedItem());
updateCountriesSelection(euroCountryCheckBoxes);
autoFillNameTextField();
});
currencyComboBox.setConverter(new StringConverter<>() {
@Override
public String toString(TradeCurrency currency) {
return currency.getNameAndCode();
}

@Override
public TradeCurrency fromString(String string) {
return null;
}
});
currencyComboBox.getSelectionModel().select(0);
}
}

void addCountriesGrid(String title, List<CheckBox> checkBoxList,
List<Country> dataProvider) {
FlowPane flowPane = FormBuilder.addTopLabelFlowPane(gridPane, ++gridRow, title, 0).second;

flowPane.setId("flow-pane-checkboxes-bg");

dataProvider.forEach(country ->
fillUpFlowPaneWithCountries(checkBoxList, flowPane, country));
updateCountriesSelection(checkBoxList);
}

ComboBox<Country> addCountrySelection() {
HBox hBox = new HBox();

hBox.setSpacing(10);
ComboBox<Country> countryComboBox = new JFXComboBox<>();
currencyComboBox = new JFXComboBox<>();
currencyTextField = new JFXTextField("");
currencyTextField.setEditable(false);
currencyTextField.setMouseTransparent(true);
currencyTextField.setFocusTraversable(false);
currencyTextField.setMinWidth(300);

currencyTextField.setVisible(false);
currencyTextField.setManaged(false);
currencyComboBox.setVisible(false);
currencyComboBox.setManaged(false);

hBox.getChildren().addAll(countryComboBox, currencyTextField, currencyComboBox);

addTopLabelWithVBox(gridPane, ++gridRow, Res.get("payment.bank.country"), hBox, 0);

countryComboBox.setPromptText(Res.get("payment.select.bank.country"));
countryComboBox.setConverter(new StringConverter<>() {
@Override
public String toString(Country country) {
return country.name + " (" + country.code + ")";
}

@Override
public Country fromString(String s) {
return null;
}
});
return countryComboBox;
}

abstract void setupCurrency(Country country, TradeCurrency currency);

abstract void updateCountriesSelection(List<CheckBox> checkBoxList);

}