Skip to content

Commit

Permalink
Add password validations to UnlockView
Browse files Browse the repository at this point in the history
Adapted validation controls from JFoenix
  • Loading branch information
namloan committed Aug 12, 2023
1 parent 1341914 commit 26c3fe6
Show file tree
Hide file tree
Showing 11 changed files with 472 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
package bisq.desktop.common.validation;


import bisq.desktop.components.controls.validator.ValidatorBase;
import bisq.i18n.Res;

import java.math.BigInteger;
import java.util.Objects;
import java.util.function.Function;

/**
* Deprecated. Use {@link ValidatorBase} instead.
*/
@Deprecated
public class InputValidator {

public ValidationResult validate(String input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
package bisq.desktop.common.validation;


import bisq.desktop.components.controls.validator.ValidatorBase;
import bisq.i18n.Res;

/**
* Deprecated. Use an instance of {@link ValidatorBase} instead.
*/
@Deprecated
public class PasswordValidator extends InputValidator {
public ValidationResult validate(CharSequence value) {
//todo trim
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public MaterialPasswordField(@Nullable String description, @Nullable String prom
if ((newValue == null || newValue.length() == 0) && !textProperty().isBound()) {
setText("");
}
validate();
}).get());

password.set(getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
import bisq.desktop.common.threading.UIThread;
import bisq.desktop.common.utils.ClipboardUtil;
import bisq.desktop.common.validation.InputValidator;
import bisq.desktop.components.controls.validator.ValidationControl;
import bisq.desktop.components.controls.validator.ValidatorBase;
import bisq.i18n.Res;
import de.jensd.fx.fontawesome.AwesomeIcon;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
Expand All @@ -42,6 +46,8 @@
import javax.annotation.Nullable;
import java.lang.ref.WeakReference;

import static bisq.desktop.components.controls.validator.ValidatorBase.PSEUDO_CLASS_ERROR;

@Slf4j
public class MaterialTextField extends Pane {
protected final Region bg = new Region();
Expand Down Expand Up @@ -80,7 +86,7 @@ public MaterialTextField(@Nullable String description, @Nullable String prompt,

selectionLine.setPrefWidth(0);
selectionLine.setPrefHeight(2);
selectionLine.getStyleClass().add("bisq-green-line");
selectionLine.getStyleClass().add("material-text-field-selection-line");
selectionLine.setMouseTransparent(true);

descriptionLabel.setLayoutX(16);
Expand Down Expand Up @@ -142,10 +148,45 @@ public MaterialTextField(@Nullable String description, @Nullable String prompt,
textInputControl.setOnMouseEntered(e -> onMouseEntered());
textInputControl.setOnMouseExited(e -> onMouseExited());

validationControl = new ValidationControl(this.textInputControl);

doLayout();
update();
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Validation
///////////////////////////////////////////////////////////////////////////////////////////////////

protected ValidationControl validationControl;

public ValidatorBase getActiveValidator() {
return validationControl.getActiveValidator();
}

public ReadOnlyObjectProperty<ValidatorBase> activeValidatorProperty() {
return validationControl.activeValidatorProperty();
}

public ObservableList<ValidatorBase> getValidators() {
return validationControl.getValidators();
}

public void setValidators(ValidatorBase... validators) {
validationControl.setValidators(validators);
}

public boolean validate() {
var isValid = validationControl.validate();
selectionLine.pseudoClassStateChanged(PSEUDO_CLASS_ERROR, !isValid);
descriptionLabel.pseudoClassStateChanged(PSEUDO_CLASS_ERROR, !isValid);
return isValid;
}

public void resetValidation() {
validationControl.resetValidation();
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// Description
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package bisq.desktop.components.controls.validator;

import javafx.scene.control.TextInputControl;

public class RequiredFieldValidator extends ValidatorBase {

public RequiredFieldValidator(String message) {
super(message);
}

@Override
protected void eval() {
TextInputControl textField = (TextInputControl) srcControl.get();
hasErrors.set(textField.getText() == null || textField.getText().isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package bisq.desktop.components.controls.validator;

import javafx.scene.control.TextInputControl;

public class TextMinLengthValidator extends ValidatorBase {

private static final int DEFAULT_MIN_LENGTH = 8;
private final int minLength;

public TextMinLengthValidator(String message, int minLength) {
super(message);
this.minLength = minLength;
}

public TextMinLengthValidator(String message) {
this(message, DEFAULT_MIN_LENGTH);
}

@Override
protected void eval() {
var textField = (TextInputControl) srcControl.get();
hasErrors.set(textField.getText().length() < minLength);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package bisq.desktop.components.controls.validator;

import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Control;

public class ValidationControl {

private final ReadOnlyObjectWrapper<ValidatorBase> activeValidator = new ReadOnlyObjectWrapper<>();

private final Control control;

public ValidationControl(Control control) {
this.control = control;
}

public ValidatorBase getActiveValidator() {
return activeValidator.get();
}

public ReadOnlyObjectProperty<ValidatorBase> activeValidatorProperty() {
return this.activeValidator.getReadOnlyProperty();
}

ReadOnlyObjectWrapper<ValidatorBase> activeValidatorWritableProperty() {
return this.activeValidator;
}

/**
* list of validators that will validate the text value upon calling
* {{@link #validate()}
*/
private final ObservableList<ValidatorBase> validators = FXCollections.observableArrayList();

public ObservableList<ValidatorBase> getValidators() {
return validators;
}

public void setValidators(ValidatorBase... validators) {
this.validators.addAll(validators);
}

/**
* validates the text value using the list of validators provided by the user
* {{@link #setValidators(ValidatorBase...)}
*
* @return true if the value is valid else false
*/
public boolean validate() {
for (ValidatorBase validator : validators) {
validator.setSrcControl(control);
validator.validate();
if (validator.getHasErrors()) {
activeValidator.set(validator);
return false;
}
}
activeValidator.set(null);
return true;
}

public void resetValidation() {
control.pseudoClassStateChanged(ValidatorBase.PSEUDO_CLASS_ERROR, false);
activeValidator.set(null);
}
}
Loading

0 comments on commit 26c3fe6

Please sign in to comment.