Class GroupValidator
java.lang.Object
org.netbeans.validation.api.ui.GroupValidator
Encapsulates validation of the combination of several UI
components within a
ValidationGroup.
UI-components whose validity depends not only on their own state but on the state of each other as well, can be said to have validity interdependencies. In such cases not only the state of each component needs to be validated as a singular, but the combination of components needs to be validated as well.
The following items outline what needs to be done to achieve this.
- The UI components with interdependencies need to be added to
the same
ValidationGroup - This
ValidationGroupneeds to be prepared at creation time with an instance ofGroupValidator. - In this
GroupValidator, the methodperformGroupValidation(org.netbeans.validation.api.Problems)needs to be overridden to perform the custom interdependency validation.
When a UI component is changed (either programmatically or by having been interacted with by the user) the following will happen:
- As usual, the UI component will be revalidated using its connected validators.
- Now, only if there is no fatal
Problemin any of the UI components within theValidationGroup, the validation in theGroupValidatorwill be invoked as well. - If it turns out that the latter yields a
Problemmore severe (i.e strictly worse) than any otherProblemin theValidationGroup, then thisProblemwill become the lead problem in the group. - The lead problem of the group (whichever it may be) is shown
as usual in the
ValidationUI(s) of theValidationGroup. - If the lead
Problemhappens to be the one caused by theGroupValidator, then the default behavior is that thisProblemwill cause all UI components within the ValidationGroup to be decorated. This behavior can however be disabled by passingfalseto the constructorGroupValidator(boolean)
The following code example illustrates how this class can be used.
// Given three text fields, aField, bField and cField, this class validates
// that the sum of the numbers in them equals a number given in a combo box.
class SumValidation extends GroupValidator {
SumValidation() {
// The boolean specifies whether a Problem generated by the
// GroupValidator should cause the UI-components in the
// ValidationGroup to be decorated or not
super(true);
}
@Override
protected void performGroupValidation(Problems problems) {
try {
int desiredSum = Integer.parseInt(sumComboBox.getModel().getSelectedItem().toString());
int val1 = Integer.parseInt(aField.getText());
int val2 = Integer.parseInt(bField.getText());
int val3 = Integer.parseInt(cField.getText());
int sum = val1 + val2 + val3;
if (sum != desiredSum) {
problems.add( new Problem (val1 + "+" + val2 + "+" + val3 +
" equals " + sum + ", not " + desiredSum, Severity.FATAL));
} else if (val1 == desiredSum || val2 == desiredSum || val3 == desiredSum) {
problems.add( new Problem ("Hey...that's cheating!",
Severity.WARNING) );
}
} catch (NumberFormatException e) {
//do nothing, the other validators would have taken care of the bad entry
}
}
}
// The GroupValidator can be used as follows:
// Create ValidationGroup that will contain UI component with validity
// interdependencies. Pass a GroupValidator -- SumValidation -- to the
// ValidationGroup creator.
SwingValidationGroup bunch = SwingValidationGroup.create(new SumValidation());
// Create a Validator that can be reused for individual validation of
// the three text fields
Validator<String> fieldValidator =
StringValidators.trimString(StringValidators.REQUIRE_NON_EMPTY_STRING,
StringValidators.NO_WHITESPACE,
StringValidators.REQUIRE_VALID_NUMBER,
StringValidators.REQUIRE_VALID_INTEGER,
StringValidators.REQUIRE_NON_NEGATIVE_NUMBER);
bunch.add(aField, fieldValidator);
bunch.add(bField, fieldValidator);
bunch.add(cField, fieldValidator);
// Add the combo box as well so that the additional group
// validation is triggered whenever the combo box is interacted with. Note
// that there are no validators added for the combo box alone. Also, ValidationUI.NoOp.get()
// is passed, so that the combo box will not be decorated when there's a problem.
bunch.add(SwingValidationListenerFactory.createJComboBoxValidationListener(sumComboBox, ValidationUI.NoOp.get()));
- Author:
- Hugo Heden
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDefault constructor, callsthis(true)protectedGroupValidator(boolean shallShowProblemInChildrenUIs) -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidperformGroupValidation(Problems problems) Validate the state of the combination of the UI components within the ValidationGroup.
-
Constructor Details
-
GroupValidator
protected GroupValidator()Default constructor, callsthis(true) -
GroupValidator
protected GroupValidator(boolean shallShowProblemInChildrenUIs) - Parameters:
shallShowProblemInChildrenUIs- specifies whether a Problem generated by theGroupValidator(if it happens to be the leadProblem) should cause the UI-components in theValidationGroupto be decorated (showing theProblem) or not
-
-
Method Details
-
performGroupValidation
Validate the state of the combination of the UI components within the ValidationGroup. If invalid this method shall add one or moreProblems to the passed list.- Parameters:
problems- A list of problems.
-