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 org.araneaframework.uilib.form.FormElement;
18: import org.araneaframework.uilib.support.UiLibMessages;
19: import org.araneaframework.uilib.util.MessageUtil;
20:
21: /**
22: * {@link org.araneaframework.uilib.form.Constraint} that allows constraining
23: * input length in a {@link FormElement}.
24: *
25: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
26: */
27: public class StringLengthInRangeConstraint extends BaseFieldConstraint {
28: private int rangeStart;
29: private int rangeEnd;
30:
31: public StringLengthInRangeConstraint() {
32: }
33:
34: public StringLengthInRangeConstraint(FormElement field) {
35: super (field);
36: }
37:
38: /**
39: * Creates the constraint, initializing the corresponding fields.
40: * @param rangeStart start of the length range.
41: * @param rangeEnd end of the length range.
42: */
43: public StringLengthInRangeConstraint(int rangeStart, int rangeEnd) {
44: setRangeStart(rangeStart);
45: setRangeEnd(rangeEnd);
46: }
47:
48: /**
49: * Checks that the length of string belongs in constraint boundaries.
50: */
51: protected void validateConstraint() {
52: String value = (String) getValue();
53:
54: if (value == null) {
55: if (rangeStart == 0)
56: return;
57: addValidationError();
58: return;
59: }
60:
61: if (value != null
62: && (value.length() < rangeStart || value.length() > rangeEnd))
63: addValidationError();
64: }
65:
66: private void addValidationError() {
67: addError(MessageUtil.localizeAndFormat(
68: UiLibMessages.STRING_NOT_IN_RANGE, new Object[] {
69: t(getLabel()), Integer.toString(rangeStart),
70: Integer.toString(rangeEnd) }, getEnvironment()));
71: }
72:
73: public void setRangeEnd(int rangeEnd) {
74: this .rangeEnd = rangeEnd;
75: }
76:
77: public void setRangeStart(int rangeStart) {
78: this.rangeStart = rangeStart;
79: }
80: }
|