| 1 |
/* |
| 2 |
* @(#)ValidationResult.java |
| 3 |
* |
| 4 |
* Copyright 2002 - 2005 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.validation; |
| 7 |
|
| 8 |
/** |
| 9 |
* ValidationResult is the object that returns from the {@link Validator#validating(ValidationObject)}. There are three |
| 10 |
* things on the result. <ul> <li> valid: whether the result is valid. It can be true or false. <li> id: an int value of |
| 11 |
* the result. It's better to reserve value 0 for valid result. For invalid result, you can use whatever value as long |
| 12 |
* as it's consistent across your application. <li> message: a String value of result. You can use this string value to |
| 13 |
* put a message to indicate why the validation failed. </ul> Users can extend this class to create their own |
| 14 |
* ValidationResult to provide additional information that needed to be returned from Validator. |
| 15 |
* <p/> |
| 16 |
* If you used a constructor without failBehavior parameter, the default value for behavior is {@link |
| 17 |
* #FAIL_BEHAVIOR_REVERT}, which means JideTable will not accept the new input and just revert the |
| 18 |
* value in the cell to the old value and stop cell editing as normal. |
| 19 |
*/ |
| 20 |
public class ValidationResult { |
| 21 |
private int _id; |
| 22 |
private boolean _valid; |
| 23 |
private Object _newValue; |
| 24 |
private int _failBehavior = FAIL_BEHAVIOR_REVERT; |
| 25 |
/** |
| 26 |
* If you want to change the new value in the validation result to null, please return NULL_VALUE so that JideTable |
| 27 |
* will be able to change it automatically to null after validation. |
| 28 |
* |
| 29 |
* @since 3.4.3 |
| 30 |
*/ |
| 31 |
public static final Object NULL_VALUE = new NullValue(); |
| 32 |
private String _message; |
| 33 |
|
| 34 |
private static class NullValue { |
| 35 |
public NullValue() { |
| 36 |
super(); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* When validation fails, reverts back to the previous valid value and stop cell editting as normal. |
| 42 |
*/ |
| 43 |
public static final int FAIL_BEHAVIOR_REVERT = 0; |
| 44 |
|
| 45 |
/** |
| 46 |
* When validation fails, do not stop cell editing until user enters a valid value or press ESCAPE to cancel the |
| 47 |
* editing. |
| 48 |
*/ |
| 49 |
public static final int FAIL_BEHAVIOR_PERSIST = 1; |
| 50 |
|
| 51 |
/** |
| 52 |
* When validation fails, reset the value to null. |
| 53 |
*/ |
| 54 |
public static final int FAIL_BEHAVIOR_RESET = 2; |
| 55 |
|
| 56 |
/** |
| 57 |
* The shared ValidationResult when the validation result is valid. {@link #getNewValue()} will always return null |
| 58 |
* even if you ever invoked {@link #setNewValue(Object)} to avoid potential wrong value assignment. |
| 59 |
*/ |
| 60 |
public static final ValidationResult OK = new ValidationResult() { |
| 61 |
@Override |
| 62 |
public Object getNewValue() { |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public boolean isValid() { |
| 68 |
return true; |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
/** |
| 73 |
* Creates an empty ValidationResult. The valid is set to false. |
| 74 |
*/ |
| 75 |
public ValidationResult() { |
| 76 |
this(false); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Creates an invalid ValidationResult with an id and no message. |
| 81 |
* |
| 82 |
* @param id the result id. You can create the id by your definition |
| 83 |
*/ |
| 84 |
public ValidationResult(int id) { |
| 85 |
this(id, false, null); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Creates an empty ValidationResult. |
| 90 |
* |
| 91 |
* @param valid a boolean value to indicate if the value input is valid |
| 92 |
*/ |
| 93 |
public ValidationResult(boolean valid) { |
| 94 |
this(0, valid, null); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Creates an invalid ValidationResult with an id and a message. |
| 99 |
* |
| 100 |
* @param id the result id. You can create the id by your definition |
| 101 |
* @param message the message you want to display to the customer |
| 102 |
*/ |
| 103 |
public ValidationResult(int id, String message) { |
| 104 |
this(id, false, message); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Creates an ValidationResult with an id and a message. |
| 109 |
* |
| 110 |
* @param id the result id. You can create the id by your definition |
| 111 |
* @param valid a boolean value to indicate if the value input is valid |
| 112 |
* @param message the message you want to display to the customer |
| 113 |
*/ |
| 114 |
public ValidationResult(int id, boolean valid, String message) { |
| 115 |
_id = id; |
| 116 |
_valid = valid; |
| 117 |
_message = message; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Creates an ValidationResult with an id and an error behavior. |
| 122 |
* |
| 123 |
* @param id the result id. You can create the id by your definition |
| 124 |
* @param valid a boolean value to indicate if the value input is valid |
| 125 |
* @param failBehavoir the behavior |
| 126 |
*/ |
| 127 |
public ValidationResult(int id, boolean valid, int failBehavoir) { |
| 128 |
_id = id; |
| 129 |
_valid = valid; |
| 130 |
_failBehavior = failBehavoir; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Creates an ValidationResult with an id, a message and an error behavior. |
| 135 |
* |
| 136 |
* @param id the result id. You can create the id by your definition |
| 137 |
* @param valid a boolean value to indicate if the value input is valid |
| 138 |
* @param failBehavoir the behavior |
| 139 |
* @param message the message you want to display to the customer |
| 140 |
*/ |
| 141 |
public ValidationResult(int id, boolean valid, int failBehavoir, String message) { |
| 142 |
_id = id; |
| 143 |
_valid = valid; |
| 144 |
_failBehavior = failBehavoir; |
| 145 |
_message = message; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Gets the id of the ValidationResult. |
| 150 |
* |
| 151 |
* @return the id. |
| 152 |
*/ |
| 153 |
public int getId() { |
| 154 |
return _id; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Sets the id of the ValidationResult. |
| 159 |
* |
| 160 |
* @param id the result id. You can create the id by your definition |
| 161 |
*/ |
| 162 |
public void setId(int id) { |
| 163 |
_id = id; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Gets the new value of the ValidationResult. |
| 168 |
* |
| 169 |
* @return the new value. |
| 170 |
*/ |
| 171 |
public Object getNewValue() { |
| 172 |
return _newValue; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Sets the new value of the ValidationResult. |
| 177 |
* |
| 178 |
* @param newValue the new value. |
| 179 |
*/ |
| 180 |
public void setNewValue(Object newValue) { |
| 181 |
_newValue = newValue; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Checks if the validation state is valid. |
| 186 |
* |
| 187 |
* @return the validation state. True means valid. Otherwise, false. |
| 188 |
*/ |
| 189 |
public boolean isValid() { |
| 190 |
return _valid; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Sets the validation state. |
| 195 |
* |
| 196 |
* @param valid a boolean value to indicate if the value input is valid |
| 197 |
*/ |
| 198 |
public void setValid(boolean valid) { |
| 199 |
_valid = valid; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Gets the message associated with the ValidationResult. |
| 204 |
* |
| 205 |
* @return the message. |
| 206 |
*/ |
| 207 |
public String getMessage() { |
| 208 |
return _message; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Sets the message associated with the ValidationResult. |
| 213 |
* |
| 214 |
* @param message the new message. |
| 215 |
*/ |
| 216 |
public void setMessage(String message) { |
| 217 |
_message = message; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Gets the behavior if validation fails. |
| 222 |
* |
| 223 |
* @return the behavior if validation fails. |
| 224 |
*/ |
| 225 |
public int getFailBehavior() { |
| 226 |
return _failBehavior; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Sets the behavior if validation fails. Valid values are {@link #FAIL_BEHAVIOR_PERSIST}, {@link |
| 231 |
* #FAIL_BEHAVIOR_REVERT}, and {@link #FAIL_BEHAVIOR_RESET}. |
| 232 |
* <p/> |
| 233 |
* If you used a constructor without this parameter, the default value for behavior is {@link |
| 234 |
* #FAIL_BEHAVIOR_REVERT}, which means JideTable will not accept the new input and just revert the |
| 235 |
* value in the cell to the old value and stop cell editing as normal. |
| 236 |
* |
| 237 |
* @param failBehavior the behavior |
| 238 |
*/ |
| 239 |
public void setFailBehavior(int failBehavior) { |
| 240 |
_failBehavior = failBehavior; |
| 241 |
} |
| 242 |
|
| 243 |
@Override |
| 244 |
public String toString() { |
| 245 |
String properties = |
| 246 |
" id=" + getId() + " message=" + getMessage() + " "; |
| 247 |
return getClass().getName() + "[" + properties + "]"; |
| 248 |
} |
| 249 |
} |