001: package org.jreform.internal;
002:
003: import java.util.HashMap;
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.Map;
007: import java.util.Set;
008:
009: import org.jreform.Checkbox;
010: import org.jreform.Criterion;
011: import org.jreform.DuplicateNameException;
012: import org.jreform.Input;
013: import org.jreform.InputCollection;
014: import org.jreform.InputControl;
015: import org.jreform.InputDataType;
016: import org.jreform.MultiCheckbox;
017: import org.jreform.MultiSelect;
018: import org.jreform.Radio;
019: import org.jreform.Select;
020: import org.jreform.UndefinedInputControlException;
021:
022: /**
023: * @author armandino (at) gmail.com
024: */
025: public abstract class AbstractInputCollection implements
026: InputCollection {
027: private final Map<String, InputControl<?>> inputs;
028: private final Set<String> errors;
029: private boolean isValid;
030:
031: AbstractInputCollection() {
032: inputs = new HashMap<String, InputControl<?>>();
033: errors = new HashSet<String>();
034: isValid = false;
035: }
036:
037: /**
038: * Adds the specified input to the collection.
039: *
040: * @throws DuplicateNameException if there is an existing input
041: * with the same name in this collection
042: */
043: <T> void add(InputControl<T> input) {
044: String name = input.getInputName();
045:
046: if (inputs.containsKey(name))
047: throw new DuplicateNameException(
048: "Duplicate input name within the same form: "
049: + name);
050:
051: inputs.put(name, input);
052: }
053:
054: final InputControl<?> getInputControl(String name) {
055: InputControl<?> input = inputs.get(name);
056:
057: if (input == null)
058: throw new UndefinedInputControlException(
059: "Undefined input control: " + name);
060:
061: return input;
062: }
063:
064: public final Set<String> getErrors() {
065: return errors;
066: }
067:
068: public final void addError(String errorKey) {
069: errors.add(errorKey);
070: }
071:
072: public final boolean isValid() {
073: return isValid;
074: }
075:
076: final Map<String, InputControl<?>> getInputs() {
077: return inputs;
078: }
079:
080: final void setValid(boolean isValid) {
081: this .isValid = isValid;
082: }
083:
084: /**
085: * Perform additional validation of form data where necessary.
086: */
087: protected void additionalValidate() {
088: }
089:
090: //
091: // ----------------------------------------------------------------
092: // ------------------InputCollection methods ----------------------
093: // ----------------------------------------------------------------
094: //
095:
096: //
097: // Getters
098: //
099:
100: @SuppressWarnings("unchecked")
101: public final <T> Input<T> getInput(String name) {
102: return (Input<T>) getInputControl(name);
103: }
104:
105: @SuppressWarnings("unchecked")
106: public final <T> Checkbox<T> getCheckbox(String name) {
107: return (Checkbox<T>) getInputControl(name);
108: }
109:
110: @SuppressWarnings("unchecked")
111: public final <T> MultiCheckbox<T> getMultiCheckbox(String name) {
112: return (MultiCheckbox<T>) getInputControl(name);
113: }
114:
115: @SuppressWarnings("unchecked")
116: public final <T> Radio<T> getRadio(String name) {
117: return (Radio<T>) getInputControl(name);
118: }
119:
120: @SuppressWarnings("unchecked")
121: public final <T> Select<T> getSelect(String name) {
122: return (Select<T>) getInputControl(name);
123: }
124:
125: @SuppressWarnings("unchecked")
126: public final <T> MultiSelect<T> getMultiSelect(String name) {
127: return (MultiSelect<T>) getInputControl(name);
128: }
129:
130: //
131: // Add methods
132: //
133:
134: public final <T> InputControlModifier<T> input(
135: InputDataType<T> type, String name,
136: Criterion<T>... criteria) {
137: return create(new InputImpl<T>(type, name, criteria));
138: }
139:
140: public final <T> InputControlModifier<T> checkbox(
141: InputDataType<T> type, String name,
142: Criterion<T>... criteria) {
143: return create(new CheckboxImpl<T>(type, name, criteria));
144: }
145:
146: public final <T> InputControlModifier<T> multiCheckbox(
147: InputDataType<T> type, String name,
148: Criterion<T>... criteria) {
149: return create(new MultiCheckboxImpl<T>(type, name, criteria));
150: }
151:
152: public final <T> InputControlModifier<T> radio(
153: InputDataType<T> type, String name,
154: Criterion<T>... criteria) {
155: return create(new RadioImpl<T>(type, name, criteria));
156: }
157:
158: public final <T> InputControlModifier<T> select(
159: InputDataType<T> type, String name,
160: Criterion<T>... criteria) {
161: return create(new SelectImpl<T>(type, name, criteria));
162: }
163:
164: public final <T> InputControlModifier<T> multiSelect(
165: InputDataType<T> type, String name,
166: Criterion<T>... criteria) {
167: return create(new MultiSelectImpl<T>(type, name, criteria));
168: }
169:
170: private <T> InputControlModifier<T> create(
171: AbstractInputControl<T> input) {
172: add(input);
173: return new InputControlModifier<T>(input);
174: }
175:
176: @Override
177: public String toString() {
178: StringBuilder sb = new StringBuilder();
179: Iterator<String> iter = inputs.keySet().iterator();
180:
181: while (iter.hasNext()) {
182: String inputName = iter.next();
183: InputControl<?> input = inputs.get(inputName);
184: sb.append(inputName).append(": ").append(
185: input.getStringValue()).append(
186: System.getProperty("line.separator"));
187: }
188:
189: return sb.toString();
190: }
191:
192: }
|