01: package org.jreform.internal;
02:
03: import javax.servlet.http.HttpServletRequest;
04:
05: import org.jreform.Criterion;
06: import org.jreform.InputControl;
07: import org.jreform.InputDataType;
08:
09: /**
10: * Base class for single- and multi-value input controls.
11: * Validation rules are implemented by subclasses.
12: *
13: * @author armandino (at) gmail.com
14: */
15: abstract class AbstractInputControl<T> implements InputControl<T> {
16: private InputDataType<T> type;
17: private String name;
18: private String messageOnError;
19: private boolean isRequired;
20: private boolean isValid;
21: private boolean isGroupInput;
22: private Criterion<T>[] criteria;
23:
24: /**
25: * Constructor.
26: *
27: * @param type of this input's data.
28: * @param name of this input.
29: * @param isRequired is this a required or optional input field.
30: * @param criteria this input's data must satisfy.
31: */
32: AbstractInputControl(InputDataType<T> type, String name,
33: Criterion<T>... criteria) {
34: this .type = type;
35: this .name = name;
36: this .criteria = criteria;
37: this .isRequired = true;
38: this .isValid = true;
39: this .isGroupInput = false;
40: }
41:
42: /**
43: * Validates this input's <tt>value</tt> attribute(s).
44: */
45: abstract boolean validate(HttpServletRequest req);
46:
47: public final InputDataType<T> getType() {
48: return type;
49: }
50:
51: public final String getInputName() {
52: return name;
53: }
54:
55: public final String getOnError() {
56: return isValid() ? "" : messageOnError;
57: }
58:
59: public final void setOnError(String message) {
60: if (messageOnError == null)
61: messageOnError = message;
62: }
63:
64: public final boolean isRequired() {
65: return isRequired;
66: }
67:
68: final void setRequired(boolean isRequired) {
69: this .isRequired = isRequired;
70: }
71:
72: public final boolean isValid() {
73: return isValid;
74: }
75:
76: final void setValid(boolean isValid) {
77: this .isValid = isValid;
78: }
79:
80: /**
81: * Returns <code>true</code> if this input belongs to a group.
82: */
83: final boolean isGroupInput() {
84: return isGroupInput;
85: }
86:
87: final void setGroupInput(boolean isGroupInput) {
88: this .isGroupInput = isGroupInput;
89: }
90:
91: protected final Criterion<T>[] getCriteria() {
92: return criteria;
93: }
94:
95: }
|