001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.forms.impl;
017:
018: import java.util.Iterator;
019:
020: import javax.servlet.http.HttpServletRequest;
021:
022: import de.odysseus.calyxo.base.Message;
023: import de.odysseus.calyxo.forms.FormData;
024:
025: /**
026: *
027: * @author Christoph Beck
028: */
029: public class InputFieldResult {
030: private ValidatorSequenceValue value;
031: private InputField field;
032:
033: /**
034: *
035: */
036: public InputFieldResult(InputField field,
037: ValidatorSequenceValue value) {
038: super ();
039:
040: this .field = field;
041: this .value = value;
042: }
043:
044: public void populate(FormData bean) throws Exception {
045: if (isValid()) {
046: bean._setProperty(getProperty(), getValue());
047: }
048: }
049:
050: public InputField getField() {
051: return field;
052: }
053:
054: public String getProperty() {
055: return field.getFieldConfig().getProperty();
056: }
057:
058: public int getSize() {
059: return value.getCurrentIndex() + 1;
060: }
061:
062: public Object format(HttpServletRequest request) {
063: if (field.isArray()) {
064: Object[] values = (Object[]) value.getValue();
065: String[] strings = new String[values.length];
066: for (int i = 0; i < values.length; i++) {
067: if (isValid(i)) {
068: strings[i] = field.formatScalar(request, values[i]);
069: } else {
070: strings[i] = this .value.getInput(i);
071: }
072:
073: }
074: return strings;
075: } else {
076: if (value.isValid()) {
077: return field.formatScalar(request, value.getValue());
078: } else {
079: return value.getInput();
080: }
081: }
082: }
083:
084: /* (non-Javadoc)
085: * @see de.odysseus.calyxo.forms.FormInputResult#isValid()
086: */
087: public boolean isValid() {
088: return value.isValid();
089: }
090:
091: public boolean isValid(int index) {
092: return value.isValid(index);
093: }
094:
095: /* (non-Javadoc)
096: * @see de.odysseus.calyxo.forms.FormInputResult#format(javax.servlet.http.HttpServletRequest, int)
097: */
098: public String format(HttpServletRequest request, int index) {
099: if (isValid(index)) {
100: return field.formatScalar(request, getValue(index));
101: }
102: return value.getInput(index);
103: }
104:
105: /* (non-Javadoc)
106: * @see de.odysseus.calyxo.forms.FormInputResult#getValue()
107: */
108: public Object getValue() {
109: Object result = value.getValue();
110: if (result == null) {
111: result = field.getFieldConfig().getNull();
112: }
113: return result;
114: }
115:
116: /* (non-Javadoc)
117: * @see de.odysseus.calyxo.forms.FormInputResult#getValue(int)
118: */
119: public Object getValue(int index) {
120: return value.getValue(index);
121: }
122:
123: /* (non-Javadoc)
124: * @see de.odysseus.calyxo.forms.FormInputResult#getInputValue()
125: */
126: public Object getInputValue() {
127: return value.getInput();
128: }
129:
130: /* (non-Javadoc)
131: * @see de.odysseus.calyxo.forms.FormInputResult#getInputValue(int)
132: */
133: public String getInputValue(int index) {
134: return value.getInput(index);
135: }
136:
137: public Message getMessage(int index) {
138: if (!isValid(index)) {
139: return value.getMessage(index);
140: }
141: return null;
142: }
143:
144: /* (non-Javadoc)
145: * @see de.odysseus.calyxo.forms.FormInputResult#getMessages()
146: */
147: public Iterator getLastMessages() {
148: return value.getLastMessages();
149: }
150:
151: public Message getFirstMessage() {
152: if (!value.isValid()) {
153: return value.getFirstMessage();
154: }
155: return null;
156: }
157: }
|