The ValueParamValidator
class handles
the verification of the Value parameter in the HowTo action. This
lesson describes in detail how to create the code to demonstrate the
parameter verification and validation features of the com.ibm.carma.ui.parameterValidators
extension
point.
About this task
For this sample, the following are a list of possible inputs
from the user and the informational messages that are displayed.
- When the user enters a 0, an informational message is displayed
- When the user enters a 1, a warning message is displayed
- When the user enters a value of 2 or greater, an error message
is displayed
- When the user enters a non-numeric character, the input will not
be allowed
Procedure
- In the Package Explorer view, open the
ValueParamValidator
class
by navigating com.ibm.carma.plugin.howto
> src
> com.ibm.carma.plugin.howto.action
and
double-clicking on the class to open it in the editor.
- You will first implement the
verifyInput
method.
This method checks for any invalid characters in the input text. For
this sample, this method will only accept numeric characters. The
following pseudocode demonstrates this: if the length of input is greater than 0
then allow input if the input characters are 0-9
else
do not allow input
Use the following example sample
code to implement the
verifyInput
method:
/*Accept only numeric characters as valid. */
public void verifyInput(ParameterValidationEvent event)
{
if(event.text.length() > 0)
event.allowInput = (event.text.matches("[0-9]"));
}
- Next, you will override the
validateParameter
method
to display the appropriate methods depending on the values entered,
as described above. The following pseudo code demonstrates
this:
if input = 0
return an informational message
else if input = 1
return a warning message
else
return an error message
Use the following example sample
code to override this method:
/* Returns an informational message if 0 is entered,
* Returns a warning message if 1 is entered,
* Returns an error message if a numeric value greater than 1 is entered
*/
public ValidationResult validateParameter(ParameterValidationevent event)
{
ValidationResult result = new ValidationResult();
if(event.text.contains("0"))
{
//Display informational message.
result.severity = ValidationResult.INFO;
result.message = "You entered a 0!";
}
else if(event.text.contains("1"))
{
//Display a warning message
result.severity = ValidationResult.WARNING;
result.message = "Values greater than 1 will result in an error!";
}
else
{
//Display an error message.
result.severity = ValidationResult.ERROR;
result.message = "Value is too great, enter a lower value.";
}
return result;
}
- Save the source and debug any errors.