|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.fedmest.cw.keyboard.validators.CharChoiceValidator
public final class CharChoiceValidator
This class checks for single-character input within a range of allowed characters. It is suited for simple text menus based on
one-character choices, but it can generically be used to limit the characters accepted as input in all sorts of contexts. Create
the class by specifying the characters that are allowed as input and simply let your KeyboardUtils instance get the input
for you. The returned value is the character that was selected. If the user canceled input on a cancelable KeyboardUtils,
the class will return an empty character '\0', so you can safely expect it to always return a valid char
primitive type (as opposed to null.
| Constructor Summary | |
|---|---|
CharChoiceValidator(char[] allowedChars)
Creates a new instance of this validator with an array of characters to be allowed as input. |
|
CharChoiceValidator(java.lang.String allowedChars)
Creates a new instance of this validator with a String containing the characters to be allowed as input. |
|
| Method Summary | |
|---|---|
java.lang.Character |
cancelValue()
Allows you to specify the return value to offer when the user cancels input on a cancelable instance of KeyboardUtils
. |
java.lang.Character |
checkInput(java.lang.String txt)
Takes the input string, checks its validity and converts it to the data type specified by the type argument. |
boolean |
isCaseSensitive()
Tells you whether this instance of the validator will take case into account when matching characters. |
void |
resetErrorMessages()
This method resets the error messages that will be displayed to users on validation failure to the default text ("Invalid choice - retry"). |
void |
setCaseSensitive(boolean caseSensitive)
Specifies whether the validation of the characters should take case into consideration. |
void |
setErrorMessages(java.lang.String errorMessage)
Sets the error messages that will be displayed to the user if validation fails either because the input string was longer than one character or because the input character is not among the allowed ones. |
void |
setErrorMessages(java.lang.String errorMessage1,
java.lang.String errorMessage2)
Sets the error messages that will be displayed to the user if validation fails either because the input string was longer than one character or because the input character is not among the allowed ones. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public CharChoiceValidator(char[] allowedChars)
allowedChars - an array of the allowed characterspublic CharChoiceValidator(java.lang.String allowedChars)
String containing the characters to be allowed as input.
allowedChars - a string whose characters will be used as allowed characters| Method Detail |
|---|
public void resetErrorMessages()
setErrorMessages(String, String),
setErrorMessages(String)public void setErrorMessages(java.lang.String errorMessage)
errorMessage - the new error messages to display to users when validation fails for any reason.setErrorMessages(String, String),
resetErrorMessages()
public void setErrorMessages(java.lang.String errorMessage1,
java.lang.String errorMessage2)
errorMessage1 - the new error message to display to users when the input string is longer than one charactererrorMessage2 - the new error message to display to users when the input character is not among the ones allowedsetErrorMessages(String),
resetErrorMessages()public void setCaseSensitive(boolean caseSensitive)
caseSensitive - set his to true if case should be taken into account in the validation procedure, or to false if case should not make a difference.public boolean isCaseSensitive()
true if the instance is case sensitive,false if it is case insensitive
public java.lang.Character checkInput(java.lang.String txt)
throws KeyboardInputException
KeyboardInputValidatorKeyboardInputException is raised and the user will have to type their input again. If a null
reference is passed into the method, the method may throw a NullPointerException, so programmers do not need to worry
about checking for null pointers.
checkInput in interface KeyboardInputValidator<java.lang.Character>txt - the input text. If the string is null, NullPointerException will be thrown.
KeyboardInputException - if validation against the input string failedpublic java.lang.Character cancelValue()
KeyboardInputValidatorKeyboardUtils
. Though null should be returned in most cases, this method is useful when - for example - you want to return a
primitive type from your validator and can't use null as a return value without causing a bit of inconvenienve in
your application code (explicit unboxing and checks for null values).
cancelValue in interface KeyboardInputValidator<java.lang.Character>
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||