001: package net.xoetrope.xui.validation;
002:
003: import java.awt.Component;
004:
005: import net.xoetrope.xml.XmlElement;
006: import net.xoetrope.xui.helper.NumberFormatter;
007:
008: /**
009: * <p>Performs a min/max check, reading the minimum and maximum values from the
010: * configuration file</p>
011: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
012: * License: see license.txt</p>
013: * $Revision: 1.23 $
014: */
015: public class XMinMaxValidator extends XBaseValidator {
016: private float min, max;
017:
018: /**
019: * Constructors the XMinMaxValidator
020: * @param name Name of the validation in the xml file
021: * @param min The min valid value.
022: * @param max The max valid value
023: * @param message The message to be displayed if validation fails
024: * @param mask The event mask to trigger the validation.
025: */
026: public XMinMaxValidator(String name, int mask) {
027: super (name, mask);
028: }
029:
030: /**
031: * Set the validation parameters
032: * @param element the validator parameters
033: */
034: public void setup(XmlElement element) {
035: super .setup(element);
036: min = Float.valueOf(element.getAttribute("min")).floatValue();
037: max = Float.valueOf(element.getAttribute("max")).floatValue();
038: if (element.getAttribute("mandatory") != null)
039: mandatory = Boolean.valueOf(
040: element.getAttribute("mandatory")).booleanValue();
041: String msg = element.getAttribute("msg");
042: }
043:
044: /**
045: * Carries out the min-max validation on the value entered. If the Method
046: * validationMethod is not null we have to invoke it in order to get it's
047: * return value. If the Method invocation returns a null we do not validate.
048: * If a problem is found we format the message using the messageHelper.
049: * @param c The component triggering the validation
050: * @throws Exception Contains the details of the message
051: */
052: public void validate(Component c, boolean forceMandatory)
053: throws Exception {
054: errorLevel = LEVEL_IGNORE;
055: formattedMessage = message;
056: String text = "";
057:
058: if (validationMethod == null)
059: text = getText(c);
060: else {
061: Object funcResult = invokeMethod();
062: if (funcResult != null)
063: text = funcResult.toString();
064: }
065:
066: Object ret = null;
067: Object funcResult = null;
068: boolean failed = false;
069: if ((text.length() > 0) || (mandatory)) {
070: float value = 0;
071: String tempValue = null;
072: if (validationMethod == null) {
073: if (text.length() > 0)
074: tempValue = text;
075: } else {
076: /* We need to get the value being compared from the function call */
077: funcResult = invokeMethod();
078: if (funcResult != null)
079: tempValue = funcResult.toString();
080: }
081:
082: try {
083: value = getValue(tempValue);
084: } catch (Exception ex) {
085: failed = true;
086: }
087: /* We are either validating a basic value or the results of the Method
088: invocation if it is not null.
089: */
090: if ((validationMethod == null) || (funcResult != null)) {
091: this .value = String.valueOf(value);
092: if (text.length() == 0 && mandatory && !forceMandatory) {
093: failed = false;
094: } else if (text.length() == 0 && mandatory
095: && forceMandatory) {
096: failed = true;
097: } else if (value < min) {
098: failed = true;
099: } else if (value > max) {
100: failed = true;
101: }
102: }
103: } else if (mandatory && forceMandatory)
104: failed = true;
105:
106: if (failed) {
107: replaceToken("{min}", String.valueOf(getMin()).replace('.',
108: NumberFormatter.getDecimalSeparator()));
109: replaceToken("{max}", String.valueOf(getMax()).replace('.',
110: NumberFormatter.getDecimalSeparator()));
111: if (funcResult != null)
112: replaceToken("{value}", funcResult.toString());
113: else
114: replaceToken("{value}", text);
115:
116: errorLevel = forceMandatory ? LEVEL_ERROR : LEVEL_WARNING;
117: throwException();
118: }
119: }
120:
121: /**
122: * Used to get converted unit values when working with subclasses
123: * @param in the value being compared
124: * @return the converted value
125: */
126: protected float getValue(String in) {
127: return NumberFormatter.parseFloat(in);
128: }
129:
130: /**
131: * @return The minimum value being validated against
132: */
133: public float getMin() {
134: return min;
135: }
136:
137: /**
138: * @return The maximum value being validated against
139: */
140: public float getMax() {
141: return max;
142: }
143:
144: /**
145: * Set the minimum value being validated against
146: */
147: public void setMin(float f) {
148: min = f;
149: }
150:
151: /**
152: * Set the maximum value being validated against
153: */
154: public void setMax(float f) {
155: max = f;
156: }
157: }
|