Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.dua3.utility.fx.controls;
import org.jspecify.annotations.Nullable;
import com.dua3.utility.fx.ValidationResult;
import com.dua3.utility.fx.icons.IconView;
import com.dua3.utility.lang.LangUtil;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.MapProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleMapProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.control.TextInputControl;
import javafx.scene.control.Tooltip;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* The Validator class is used for validating controls in a JavaFX application. It provides methods for setting
* validation rules for different types of controls, such as TextInputControl and Control. It also allows for
* custom validation rules to be set.
*
* The Validator class supports the use of a ResourceBundle for looking up message texts. If a ResourceBundle is
* provided during initialization, it will be used to retrieve the messages for validation errors. Otherwise,
* the message will be used as is.
*
* The Validator class also supports decorating nodes with invalid values. When a control fails validation, an
* icon and tooltip can be added to the control to indicate the validation error.
*
* The Validator class is meant to be used in a JavaFX application and should be initialized with a valid
* ResourceBundle if localized validation messages are required.
*/
public class Validator {
private static final Logger LOG = LogManager.getLogger(Validator.class);
private final @Nullable ResourceBundle resources;
private final LinkedHashMap>> controls = new LinkedHashMap<>();
private final MapProperty validationResultProperty = new SimpleMapProperty<>();
private final BooleanProperty validProperty = new SimpleBooleanProperty();
private final List disposeList = new ArrayList<>();
private int iconSize = (int) Math.round(Font.getDefault().getSize());
private String iconError = "fth-alert-triangle";
private boolean decorateNodes = false;
/**
* Creates a Validator instance without assigning a resource bundle.
*/
public Validator() {
this(null);
}
/**
* Run cleanup actions.
*/
public void dispose() {
disposeList.forEach(Runnable::run);
disposeList.clear();
}
/**^
* Creates a Validator instance.
*
* @param resources the resource bundle to look up message texts
*/
public Validator(@Nullable ResourceBundle resources) {
this.resources = resources;
}
/**
* Get list of rules for control. If the control is not registered yet, it will be assigned a newly generated
* empty list.
*
* @param c the control
* @return the list of rules
*/
private List> rules(Control c) {
return controls.computeIfAbsent(c, this::createRuleList);
}
/**
* Creates a new rule list and sets the control to be validated on focus change.
*
* @param control the control to create a rule list for
* @return new rule list for the control
*/
private List> createRuleList(Control control) {
control.setFocusTraversable(true);
ChangeListener