public abstract class GroupValidator
extends java.lang.Object
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.
ValidationGroup
ValidationGroup
needs to be prepared at creation time
with an instance of GroupValidator
.
GroupValidator
, the method performGroupValidation(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:
Problem
in any of the UI components within the ValidationGroup
, the validation in
the GroupValidator
will be invoked as well.
Problem
more
severe (i.e strictly worse) than any other Problem
in the
ValidationGroup
, then this Problem
will become the
lead problem in the group.
ValidationUI
(s) of the ValidationGroup
.
Problem
happens to be the one caused by the
GroupValidator
, then the default behavior is that this
Problem
will cause all UI components within the
ValidationGroup to be decorated. This behavior can however be
disabled by passing false
to the constructor GroupValidator(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()));
Modifier | Constructor and Description |
---|---|
protected |
GroupValidator()
Default constructor, calls
this(true) |
protected |
GroupValidator(boolean shallShowProblemInChildrenUIs) |
Modifier and Type | Method and Description |
---|---|
protected abstract void |
performGroupValidation(Problems problems)
Validate the state of the combination of the UI components
within the ValidationGroup.
|
protected GroupValidator()
this(true)
protected GroupValidator(boolean shallShowProblemInChildrenUIs)
shallShowProblemInChildrenUIs
- specifies whether a
Problem generated by the GroupValidator
(if it happens
to be the lead Problem
) should cause the UI-components
in the ValidationGroup
to be decorated (showing the
Problem
) or notprotected abstract void performGroupValidation(Problems problems)
Problem
s to
the passed list.problems
- A list of problems.