001: /**
002: * Copyright 2006 Webmedia Group Ltd.
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: **/package org.araneaframework.uilib.form.control;
016:
017: import java.math.BigInteger;
018: import org.apache.commons.lang.StringUtils;
019: import org.araneaframework.uilib.form.FilteredInputControl;
020: import org.araneaframework.uilib.form.control.inputfilter.InputFilter;
021: import org.araneaframework.uilib.support.UiLibMessages;
022: import org.araneaframework.uilib.util.MessageUtil;
023:
024: /**
025: * This class represents a textbox control that accepts only valid
026: * integer numbers.
027: *
028: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
029: *
030: */
031: public class NumberControl extends EmptyStringNullableControl implements
032: FilteredInputControl {
033: private InputFilter inputFilter;
034: private BigInteger minValue;
035: private BigInteger maxValue;
036:
037: /**
038: * Empty.
039: */
040: public NumberControl() {
041: //Empty
042: }
043:
044: /**
045: * Makes a number control that has minimum and maximum value.
046: *
047: * @param minValue minimum permitted value.
048: * @param maxValue maximum permitted value.
049: */
050: public NumberControl(BigInteger minValue, BigInteger maxValue) {
051: super ();
052: this .minValue = minValue;
053: this .maxValue = maxValue;
054: }
055:
056: /**
057: * Sets the maximum permitted value.
058: * @param maxValue maximum permitted value.
059: */
060: public void setMaxValue(BigInteger maxValue) {
061: this .maxValue = maxValue;
062: }
063:
064: /**
065: * Sets the minimum permitted value.
066: * @param minValue minimum permitted value.
067: */
068: public void setMinValue(BigInteger minValue) {
069: this .minValue = minValue;
070: }
071:
072: /**
073: * Returns the maximum permitted value.
074: * @return the maximum permitted value.
075: */
076: public BigInteger getMaxValue() {
077: return maxValue;
078: }
079:
080: /**
081: * Returns the minimum permitted value.
082: * @return the minimum permitted value.
083: */
084: public BigInteger getMinValue() {
085: return minValue;
086: }
087:
088: /**
089: * Returns "BigInteger".
090: * @return "BigInteger".
091: */
092: public String getRawValueType() {
093: return "BigInteger";
094: }
095:
096: /** @since 1.0.11 */
097: public InputFilter getInputFilter() {
098: return inputFilter;
099: }
100:
101: /** @since 1.0.11 */
102: public void setInputFilter(InputFilter inputFilter) {
103: this .inputFilter = inputFilter;
104: }
105:
106: //*********************************************************************
107: //* INTERNAL METHODS
108: //*********************************************************************
109:
110: /**
111: * Trims request parameter.
112: */
113: protected String preprocessRequestParameter(String parameterValue) {
114: String result = super
115: .preprocessRequestParameter(parameterValue);
116: return (result == null ? null : result.trim());
117: }
118:
119: /**
120: * Checks that the submitted data is a valid integer number.
121: */
122: protected Object fromRequest(String parameterValue) {
123: BigInteger result = null;
124:
125: try {
126: result = parameterValue == null ? null : new BigInteger(
127: parameterValue);
128: } catch (NumberFormatException e) {
129: addError(MessageUtil.localizeAndFormat(
130: UiLibMessages.NOT_INTEGER, MessageUtil.localize(
131: getLabel(), getEnvironment()),
132: getEnvironment()));
133: }
134:
135: if (getInputFilter() != null
136: && !StringUtils.containsOnly(parameterValue,
137: getInputFilter().getCharacterFilter())) {
138: addError(MessageUtil.localizeAndFormat(getInputFilter()
139: .getInvalidInputMessage(), MessageUtil.localize(
140: getLabel(), getEnvironment()), getInputFilter()
141: .getCharacterFilter(), getEnvironment()));
142: }
143:
144: return result;
145: }
146:
147: /**
148: *
149: */
150: protected String toResponse(Object controlValue) {
151: return ((BigInteger) controlValue).toString();
152: }
153:
154: /**
155: * Checks that the submitted value is in permitted range.
156: *
157: */
158: protected void validateNotNull() {
159: if (minValue != null
160: && maxValue != null
161: && ((((BigInteger) getRawValue()).compareTo(minValue) == -1) || ((BigInteger) getRawValue())
162: .compareTo(maxValue) == 1)) {
163: addError(MessageUtil.localizeAndFormat(
164: UiLibMessages.NUMBER_NOT_BETWEEN, new Object[] {
165: MessageUtil.localize(getLabel(),
166: getEnvironment()),
167: minValue.toString(), maxValue.toString() },
168: getEnvironment()));
169: } else if (minValue != null
170: && ((BigInteger) getRawValue()).compareTo(minValue) == -1) {
171: addError(MessageUtil.localizeAndFormat(
172: UiLibMessages.NUMBER_NOT_GREATER, new Object[] {
173: MessageUtil.localize(getLabel(),
174: getEnvironment()),
175: minValue.toString(), }, getEnvironment()));
176: } else if (maxValue != null
177: && ((BigInteger) getRawValue()).compareTo(maxValue) == 1) {
178: addError(MessageUtil.localizeAndFormat(
179: UiLibMessages.NUMBER_NOT_LESS, new Object[] {
180: MessageUtil.localize(getLabel(),
181: getEnvironment()),
182: maxValue.toString(), }, getEnvironment()));
183: }
184: }
185:
186: /**
187: * Returns {@link ViewModel}.
188: * @return {@link ViewModel}.
189: */
190: public Object getViewModel() {
191: return new ViewModel();
192: }
193:
194: //*********************************************************************
195: //* VIEW MODEL
196: //*********************************************************************
197:
198: /**
199: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
200: *
201: */
202: public class ViewModel extends StringArrayRequestControl.ViewModel {
203: private InputFilter inputFilter;
204: private BigInteger maxValue;
205: private BigInteger minValue;
206:
207: /**
208: * Takes an outer class snapshot.
209: */
210: public ViewModel() {
211: this .maxValue = NumberControl.this .getMaxValue();
212: this .minValue = NumberControl.this .getMinValue();
213: this .inputFilter = NumberControl.this .getInputFilter();
214: }
215:
216: /**
217: * Returns maximum permitted value.
218: * @return maximum permitted value.
219: */
220: public BigInteger getMaxValue() {
221: return this .maxValue;
222: }
223:
224: /**
225: * Returns minimum permitted value.
226: * @return minimum permitted value.
227: */
228: public BigInteger getMinValue() {
229: return this .minValue;
230: }
231:
232: public InputFilter getInputFilter() {
233: return inputFilter;
234: }
235: }
236: }
|