123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- /*
- @ITMillApache2LicenseForJavaFiles@
- */
-
- package com.itmill.toolkit.data;
-
- import com.itmill.toolkit.terminal.ErrorMessage;
- import com.itmill.toolkit.terminal.PaintException;
- import com.itmill.toolkit.terminal.PaintTarget;
-
- /**
- * Object validator interface. Implementors of this class can be added to any
- * {@link com.itmill.toolkit.data.Validatable} object to verify its value. The
- * <code>Validatable#isValid(Object)</code> iterates all registered
- * <code>Validator</code>s, calling their {@link #validate(Object)} methods.
- * <code>validate(Object)</code> should throw the
- * {@link Validator.InvalidValueException} if the given value is not valid by
- * its standards.
- *
- * @author IT Mill Ltd.
- * @version
- * @VERSION@
- * @since 3.0
- */
- public interface Validator {
-
- /**
- * Checks the given value against this validator. If the value is valid this
- * method should do nothing, and if it's not valid, it should throw
- * <code>Validator.InvalidValueException</code>
- *
- * @param value
- * the value to check
- * @throws Validator.InvalidValueException
- * if the value is not valid
- */
- public void validate(Object value) throws Validator.InvalidValueException;
-
- /**
- * Tests if the given value is valid.
- *
- * @param value
- * the value to check
- * @return <code>true</code> for valid value, otherwise <code>false</code>.
- */
- public boolean isValid(Object value);
-
- /**
- * Adds the proposing functionality to a {@link Validator}. A
- * <code>Suggestive</code> validator can propose a valid value for the
- * object it is attached to validate. This way the {@link Validatable}
- * object may avoid situations where it contains a value that could lead to
- * a error.
- *
- * @author IT Mill Ltd.
- * @version
- * @VERSION@
- * @since 3.0
- */
- public interface Suggestive extends Validator {
-
- /**
- * Suggests another value that can be used instead of the proposedValue
- * if it is invalid. If it is valid in the opinion of this validator,
- * however, it is returned as is.
- *
- * @param proposedValue
- * Originally proposed value that could be invalid.
- * @return Suggested value that's not invalid against this validator
- */
- public Object suggestValidValue(Object proposedValue);
- }
-
- /**
- * Invalid value exception can be thrown by {@link Validator} when a given
- * value is not valid.
- *
- * @author IT Mill Ltd.
- * @version
- * @VERSION@
- * @since 3.0
- */
- public class InvalidValueException extends RuntimeException implements
- ErrorMessage {
-
- /**
- * Serial generated by eclipse.
- */
- private static final long serialVersionUID = 3689073941163422257L;
-
- /** Array of validation errors that are causing the problem. */
- private InvalidValueException[] causes = null;
-
- /**
- * Constructs a new <code>InvalidValueException</code> with the
- * specified detail message.
- *
- * @param message
- * The detail message of the problem.
- */
- public InvalidValueException(String message) {
- this(message, new InvalidValueException[] {});
- }
-
- /**
- * Constructs a new <code>InvalidValueException</code> with a set of
- * causing validation exceptions. The error message contains first the
- * given message and then a list of validation errors in the given
- * validatables.
- *
- * @param message
- * The detail message of the problem.
- * @param causes
- * Array of validatables whos invalidities are possiblity
- * causing the invalidity.
- */
- public InvalidValueException(String message,
- InvalidValueException[] causes) {
- super(message);
- if (causes == null) {
- throw new NullPointerException(
- "Possible causes array must not be null");
- }
- this.causes = causes;
- }
-
- public final int getErrorLevel() {
- return ErrorMessage.ERROR;
- }
-
- public void paint(PaintTarget target) throws PaintException {
- target.startTag("error");
- target.addAttribute("level", "error");
-
- // Error message
- final String message = getLocalizedMessage();
- if (message != null) {
- target.addText(message);
- }
-
- // Paint all the causes
- for (int i = 0; i < causes.length; i++) {
- causes[i].paint(target);
- }
-
- target.endTag("error");
- }
-
- /* Documented in super interface */
- public void addListener(RepaintRequestListener listener) {
- }
-
- /* Documented in super interface */
- public void removeListener(RepaintRequestListener listener) {
- }
-
- /* Documented in super interface */
- public void requestRepaint() {
- }
-
- /* Documented in super interface */
- public void requestRepaintRequests() {
- }
-
- }
- }
|