01: package net.xoetrope.xui.validation;
02:
03: import java.awt.Component;
04:
05: import net.xoetrope.xml.XmlElement;
06:
07: /**
08: * <p>Implements a validator that must succeed for event handling to continue</p>
09: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
10: * License: see license.txt</p>
11: * @version $Revision: 1.11 $
12: */
13: public class XMandatoryValidator extends XBaseValidator {
14:
15: public XMandatoryValidator(String name, int mask) {
16: super (name, mask);
17: }
18:
19: /**
20: * Set the validation parameters
21: * @param element the validator parameters
22: */
23: public void setup(XmlElement element) {
24: super .setup(element);
25: mandatory = true;
26: }
27:
28: /**
29: * Carries out the mandatory validation on the component. If the Method
30: * validationMethod is not null we have to invoke it in order to get it's
31: * return value. If a problem is found we format the message using the
32: * messageHelper.
33: * @param c The component triggering the validation
34: * @throws Exception Contains the details of the message
35: */
36: public void validate(Component c, boolean forceMandatory)
37: throws Exception {
38: errorLevel = LEVEL_IGNORE;
39: if (forceMandatory) {
40: formattedMessage = message;
41: Object funcResult = null;
42: boolean failed = false;
43: String text = getText(c);
44: if (validationMethod != null) {
45: /* We need to get the value being compared from the function call */
46: funcResult = invokeMethod();
47: if (funcResult != null)
48: text = funcResult.toString();
49: else
50: text = "";
51: }
52: if (text.length() == 0) {
53: failed = true;
54: }
55: if (failed) {
56: errorLevel = LEVEL_ERROR;
57: throwException();
58: }
59: }
60: }
61: }
|