001: /*
002: * @(#)ValidationResult.java
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.validation;
007:
008: /**
009: * ValidationResult is the object that returns from the {@link Validator#validating(ValidationObject)}.
010: * There are three things on the result.
011: * <ul>
012: * <li> valid: whether the result is valid. It can be true or false.
013: * <li> id: an int value of the result. It's better to reserve value 0 for valid result.
014: * For invalid result, you can use whatever value as long as it's consistent across your application.
015: * <li> message: a String value of result. You can use this string value to put a message to indicate why the validation
016: * failed.
017: * </ul>
018: * Users can extend this class to create their own ValidationResult to provide
019: * additional information that needed to be returned from Validator.
020: */
021: public class ValidationResult {
022: private int _id;
023: private boolean _valid;
024: private int _failBehavior = FAIL_BEHAVIOR_REVERT;
025: private String _message;
026:
027: /**
028: * When validation fails, reverts back to the previous valid value.
029: */
030: public final static int FAIL_BEHAVIOR_REVERT = 0;
031:
032: /**
033: * When validation fails, do not stop cell editing until user enters a valid value or press ESCAPE to cancel the editing.
034: */
035: public final static int FAIL_BEHAVIOR_PERSIST = 1;
036:
037: /**
038: * When validation fails, reset the value to null.
039: */
040: public final static int FAIL_BEHAVIOR_RESET = 2;
041:
042: /**
043: * The shared ValidationResult when the validation result is valid.
044: */
045: public static final ValidationResult OK = new ValidationResult(true);
046:
047: /**
048: * Creates an empty ValidationResult. The valid is set to false.
049: */
050: public ValidationResult() {
051: this (false);
052: }
053:
054: /**
055: * Creates an invalid ValidationResult with an id and no message.
056: *
057: * @param id
058: */
059: public ValidationResult(int id) {
060: this (id, false, null);
061: }
062:
063: /**
064: * Creates an empty ValidationResult.
065: *
066: * @param valid
067: */
068: public ValidationResult(boolean valid) {
069: this (0, valid, null);
070: }
071:
072: /**
073: * Creates an invalid ValidationResult with an id and a message.
074: *
075: * @param id
076: * @param message
077: */
078: public ValidationResult(int id, String message) {
079: this (id, false, message);
080: }
081:
082: /**
083: * Creates an ValidationResult with an id and a message.
084: *
085: * @param id
086: * @param valid
087: * @param message
088: */
089: public ValidationResult(int id, boolean valid, String message) {
090: _id = id;
091: _valid = valid;
092: _message = message;
093: }
094:
095: /**
096: * Creates an ValidationResult with an id and an error behavior.
097: *
098: * @param id
099: * @param valid
100: * @param failBehavoir
101: */
102: public ValidationResult(int id, boolean valid, int failBehavoir) {
103: _id = id;
104: _valid = valid;
105: _failBehavior = failBehavoir;
106: }
107:
108: /**
109: * Creates an ValidationResult with an id, a message and an error behavior.
110: *
111: * @param id
112: * @param valid
113: * @param failBehavoir
114: * @param message
115: */
116: public ValidationResult(int id, boolean valid, int failBehavoir,
117: String message) {
118: _id = id;
119: _valid = valid;
120: _failBehavior = failBehavoir;
121: _message = message;
122: }
123:
124: /**
125: * Gets the id of the ValidationResult.
126: *
127: * @return the id.
128: */
129: public int getId() {
130: return _id;
131: }
132:
133: /**
134: * Sets the id of the ValidationResult.
135: *
136: * @param id
137: */
138: public void setId(int id) {
139: _id = id;
140: }
141:
142: /**
143: * Checks if the validation state is valid.
144: *
145: * @return the validation state. True means valid. Otherwise, false.
146: */
147: public boolean isValid() {
148: return _valid;
149: }
150:
151: /**
152: * Sets the validation state.
153: *
154: * @param valid
155: */
156: public void setValid(boolean valid) {
157: _valid = valid;
158: }
159:
160: /**
161: * Gets the message associated with the ValidationResult.
162: *
163: * @return the message.
164: */
165: public String getMessage() {
166: return _message;
167: }
168:
169: /**
170: * Sets the message associated with the ValidationResult.
171: *
172: * @param message the new message.
173: */
174: public void setMessage(String message) {
175: _message = message;
176: }
177:
178: /**
179: * Gets the behavior if validation fails.
180: *
181: * @return the behavior if validation fails.
182: */
183: public int getFailBehavior() {
184: return _failBehavior;
185: }
186:
187: /**
188: * Sets the behavior if validation fails. Valid values are {@link #FAIL_BEHAVIOR_PERSIST}, {@link #FAIL_BEHAVIOR_REVERT}, and {@link #FAIL_BEHAVIOR_REVERT}.
189: *
190: * @param failBehavior
191: */
192: public void setFailBehavior(int failBehavior) {
193: _failBehavior = failBehavior;
194: }
195:
196: @Override
197: public String toString() {
198: String properties = " id=" + getId() + " message="
199: + getMessage() + " ";
200: return getClass().getName() + "[" + properties + "]";
201: }
202: }
|