Procházet zdrojové kódy

:sparkles: Added ChainMode feature to MFXValidator and Constraint

Signed-off-by: palexdev <alessandro.parisi406@gmail.com>
palexdev před 3 roky
rodič
revize
fcff3303db

+ 6 - 0
materialfx/src/main/java/io/github/palexdev/materialfx/enums/ChainMode.java

@@ -19,4 +19,10 @@ public enum ChainMode {
         return this == AND && useAlternativeAnd ? "and" : this.text;
     }
 
+    /**
+     * Chains the given two boolean values according to the given {@link ChainMode}.
+     */
+    public static boolean chain(ChainMode mode, boolean first, boolean second) {
+        return (mode == AND) ? first && second : first || second;
+    }
 }

+ 26 - 1
materialfx/src/main/java/io/github/palexdev/materialfx/validation/Constraint.java

@@ -1,5 +1,6 @@
 package io.github.palexdev.materialfx.validation;
 
+import io.github.palexdev.materialfx.enums.ChainMode;
 import javafx.beans.binding.BooleanExpression;
 
 /**
@@ -7,7 +8,10 @@ import javafx.beans.binding.BooleanExpression;
  * for the validator' state to be valid.
  * <p></p>
  * This bean allows to specify the {@link Severity} of the condition, the message
- * to show when invalid, and of course the {@link BooleanExpression} that is the condition itself
+ * to show when invalid, and of course the {@link BooleanExpression} that is the condition itself.
+ * <p>
+ * It also allows to specify how this constraint should chained with others by setting its {@link ChainMode},
+ * {@link #setChainMode(ChainMode)}.
  * <p></p>
  * To build a constraint you can use the offered static methods, the {@link Builder}, or the parameterized constructors.
  */
@@ -18,6 +22,7 @@ public class Constraint {
 	private Severity severity;
 	private String message;
 	private BooleanExpression condition;
+	private ChainMode chainMode = ChainMode.AND;
 
 	//================================================================================
 	// Constructors
@@ -112,6 +117,21 @@ public class Constraint {
 		this.condition = condition;
 	}
 
+	/**
+	 * @return the mode defining how this constraint will be chained to other constraints
+	 */
+	public ChainMode getChainMode() {
+		return chainMode;
+	}
+
+	/**
+	 * Sets the mode defining how this constraint will be chained to other constraints.
+	 */
+	public Constraint setChainMode(ChainMode chainMode) {
+		this.chainMode = chainMode;
+		return this;
+	}
+
 	//================================================================================
 	// Builder Class
 	//================================================================================
@@ -137,6 +157,11 @@ public class Constraint {
 			return this;
 		}
 
+		public Builder setChainMode(ChainMode mode) {
+			constraint.setChainMode(mode);
+			return this;
+		}
+
 		public Constraint get() {
 			checkConstraint();
 			return constraint;

+ 5 - 1
materialfx/src/main/java/io/github/palexdev/materialfx/validation/MFXValidator.java

@@ -1,5 +1,6 @@
 package io.github.palexdev.materialfx.validation;
 
+import io.github.palexdev.materialfx.enums.ChainMode;
 import io.github.palexdev.materialfx.utils.others.observables.When;
 import javafx.beans.InvalidationListener;
 import javafx.beans.binding.BooleanExpression;
@@ -157,6 +158,9 @@ public class MFXValidator {
 	 * This is the method responsible for updating the validator' state.
 	 * Despite being public it should not be necessary to call it automatically as the
 	 * constraints and the dependencies automatically trigger this method.
+	 * <p>
+	 * Note that constraints are evaluated in order of insertion and according to their
+	 * {@link Constraint#getChainMode()}, so be careful with OR modes.
 	 * <p></p>
 	 * At the end invokes {@link #onUpdated()}.
 	 */
@@ -166,7 +170,7 @@ public class MFXValidator {
 			valid = valid && dependency.isValid();
 		}
 		for (Constraint constraint : constraints) {
-			valid = valid && constraint.isValid();
+			valid = ChainMode.chain(constraint.getChainMode(), valid, constraint.isValid());
 		}
 		setValid(valid);
 		onUpdated();