01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.uilib.form.constraint;
16:
17: import java.math.BigInteger;
18: import org.araneaframework.uilib.form.FormElement;
19: import org.araneaframework.uilib.support.UiLibMessages;
20: import org.araneaframework.uilib.util.MessageUtil;
21:
22: /**
23: * This constraint checks that the value is between two others.
24: *
25: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
26: *
27: */
28: public class NumberInRangeConstraint extends BaseFieldConstraint {
29: private BigInteger rangeStart;
30: private BigInteger rangeEnd;
31:
32: public NumberInRangeConstraint() {
33: }
34:
35: public NumberInRangeConstraint(FormElement field) {
36: super (field);
37: }
38:
39: public NumberInRangeConstraint(BigInteger start, BigInteger end) {
40: rangeStart = start;
41: rangeEnd = end;
42: }
43:
44: /**
45: * Checks that the value is between two others.
46: */
47: protected void validateConstraint() {
48: if (getValue() == null) {
49: addError(MessageUtil.localizeAndFormat(
50: UiLibMessages.NUMBER_NOT_BETWEEN, new Object[] {
51: t(getLabel()), rangeStart.toString(),
52: rangeEnd.toString() }, getEnvironment()));
53: return;
54: }
55:
56: BigInteger value = new BigInteger(getValue().toString());
57:
58: if (rangeStart != null
59: && rangeEnd != null
60: && ((value.compareTo(rangeStart) == -1) || value
61: .compareTo(rangeEnd) == 1)) {
62: addError(MessageUtil.localizeAndFormat(
63: UiLibMessages.NUMBER_NOT_BETWEEN, new Object[] {
64: t(getLabel()), rangeStart.toString(),
65: rangeEnd.toString() }, getEnvironment()));
66: } else if (rangeStart != null
67: && value.compareTo(rangeStart) == -1) {
68: addError(MessageUtil.localizeAndFormat(
69: UiLibMessages.NUMBER_NOT_GREATER, new Object[] {
70: t(getLabel()), rangeStart.toString(), },
71: getEnvironment()));
72: } else if (rangeEnd != null && value.compareTo(rangeEnd) == 1) {
73: addError(MessageUtil.localizeAndFormat(
74: UiLibMessages.NUMBER_NOT_LESS, new Object[] {
75: t(getLabel()), rangeEnd.toString() },
76: getEnvironment()));
77: }
78: }
79:
80: /**
81: * Start of range.
82: * @param rangeStart Start of range.
83: */
84: public void setRangeStart(BigInteger rangeStart) {
85: this .rangeStart = rangeStart;
86: }
87:
88: /**
89: * End of range.
90: * @param rangeEnd End of range.
91: */
92: public void setRangeEnd(BigInteger rangeEnd) {
93: this.rangeEnd = rangeEnd;
94: }
95: }
|