diff options
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/gui/changemonitor/AbstractControlWrapper.java')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/changemonitor/AbstractControlWrapper.java | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/changemonitor/AbstractControlWrapper.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/changemonitor/AbstractControlWrapper.java index 4f7f2145..e43b5393 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/changemonitor/AbstractControlWrapper.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/changemonitor/AbstractControlWrapper.java @@ -1,6 +1,10 @@ package org.openslx.dozmod.gui.changemonitor; +import java.util.ArrayList; import java.util.Comparator; +import java.util.List; + +import org.openslx.dozmod.gui.changemonitor.DialogChangeMonitor.ValidationConstraint; /** * Control/element wrapper @@ -9,23 +13,19 @@ public abstract class AbstractControlWrapper<T> { private final DialogChangeMonitor dcm; protected boolean wasEverChanged; protected boolean isCurrentlyChanged; - protected boolean isValid = true; private T originalContent; - private final DialogChangeMonitor.ValidationConstraint<T> constraint; + private List<ValidationConstraint<T>> constraints; private final Comparator<T> cmp; - protected final String constraintErrorMessage; + protected String currentError = null; /** * If muted, this wrapper will not react to changes of the underlying * control. */ private boolean muted = false; - protected AbstractControlWrapper(DialogChangeMonitor dcm, String errorMessage, Comparator<T> comp, - DialogChangeMonitor.ValidationConstraint<T> cons) { + protected AbstractControlWrapper(DialogChangeMonitor dcm, Comparator<T> comp) { this.dcm = dcm; - this.constraintErrorMessage = errorMessage; this.cmp = comp; - this.constraint = cons; } public boolean hasChangedSinceInit() { @@ -37,6 +37,24 @@ public abstract class AbstractControlWrapper<T> { } /** + * Returns the current error message for the first failed constraint. If no + * constraint currently yields invalid, null is returned. + * + * @return + */ + public String currentConstraintError() { + return currentError; + } + + public AbstractControlWrapper<T> addConstraint(ValidationConstraint<T> constraint) { + if (constraints == null) { + constraints = new ArrayList<>(); + } + constraints.add(constraint); + return this; + } + + /** * Resets the change state of this control * and removes the "muted" flag, if set. */ @@ -48,7 +66,7 @@ public abstract class AbstractControlWrapper<T> { dcm.contentChanged(this); } } - + protected final void resetChangeState() { muted = false; isCurrentlyChanged = wasEverChanged = false; @@ -107,12 +125,18 @@ public abstract class AbstractControlWrapper<T> { } protected void checkValid(T text) { - if (muted) + if (muted || constraints == null || constraints.isEmpty()) return; - boolean valid = constraint == null || constraint.isValid(text); - if (isValid != valid) { - isValid = valid; + String error = null; + for (ValidationConstraint<T> i : constraints) { + error = i.checkStateValid(text); + if (error != null) + break; + } + if (error != currentError) { + currentError = error; dcm.validityChanged(this); } } + }
\ No newline at end of file |