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 org.apache.commons.lang.StringUtils;
018: import org.araneaframework.uilib.form.FilteredInputControl;
019: import org.araneaframework.uilib.form.control.inputfilter.InputFilter;
020: import org.araneaframework.uilib.support.TextType;
021: import org.araneaframework.uilib.support.UiLibMessages;
022: import org.araneaframework.uilib.util.MessageUtil;
023: import org.araneaframework.uilib.util.ValidationUtil;
024:
025: /**
026: * Class that represents a textbox control.
027: *
028: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
029: *
030: */
031: public class TextControl extends StringValueControl implements
032: FilteredInputControl {
033: private InputFilter inputFilter;
034: protected TextType textType = TextType.TEXT;
035:
036: /**
037: * Empty.
038: */
039: public TextControl() {
040: //Empty
041: }
042:
043: /**
044: * Makes a text control with specific type.
045: * @param textType specific type.
046: */
047: public TextControl(TextType textType) {
048: this .textType = textType;
049: }
050:
051: /**
052: * Makes a text control with specific type and minimum and maximum length constraints.
053: * @param minLength minimum permitted length.
054: * @param maxLength maximum permitted length.
055: */
056: public TextControl(Long minLength, Long maxLength) {
057: setMinLength(minLength);
058: setMaxLength(maxLength);
059: }
060:
061: /**
062: * Makes a text control with specific type and minimum and maximum length constraints.
063: * @param minLength minimum permitted length.
064: * @param maxLength maximum permitted length.
065: * @param trimValue whether the value from request will be trimmed.
066: */
067: public TextControl(Long minLength, Long maxLength, boolean trimValue) {
068: setMinLength(minLength);
069: setMaxLength(maxLength);
070: setTrimValue(trimValue);
071: }
072:
073: /**
074: * Makes a text control with specific type and minimum and maximum length constraints.
075: * @param textType specific type.
076: * @param minLength minimum permitted length.
077: * @param maxLength maximum permitted length.
078: */
079: public TextControl(TextType textType, Long minLength, Long maxLength) {
080: this .textType = textType;
081: setMinLength(minLength);
082: setMaxLength(maxLength);
083: }
084:
085: /**
086: * Sets the specific text type.
087: * @param textType the specific text type.
088: */
089: public void setTextType(TextType textType) {
090: this .textType = textType;
091: }
092:
093: /** @since 1.0.11 */
094: public InputFilter getInputFilter() {
095: return inputFilter;
096: }
097:
098: /** @since 1.0.11 */
099: public void setInputFilter(InputFilter inputFilter) {
100: this .inputFilter = inputFilter;
101: }
102:
103: //*********************************************************************
104: //* INTERNAL INTERFACE
105: //*********************************************************************
106: /**
107: * In case text control type is other than {@link TextType#TEXT} makes custom checks.
108: */
109: protected void validateNotNull() {
110: super .validateNotNull();
111:
112: if (textType.equals(TextType.NUMBER_ONLY)) {
113: for (int i = 0; i < ((String) getRawValue()).length(); i++) {
114: if (!Character.isDigit(((String) getRawValue())
115: .charAt(i))) {
116: addError(MessageUtil.localizeAndFormat(
117: UiLibMessages.NOT_A_NUMBER, MessageUtil
118: .localize(getLabel(),
119: getEnvironment()),
120: getEnvironment()));
121: break;
122: }
123: }
124: } else if (textType.equals(TextType.EMAIL)) {
125: if (!ValidationUtil.isEmail((String) getRawValue())) {
126: addError(MessageUtil.localizeAndFormat(
127: UiLibMessages.NOT_AN_EMAIL,
128: MessageUtil.localize(getLabel(),
129: getEnvironment()), getEnvironment()));
130: }
131: }
132:
133: if (getInputFilter() != null
134: && !StringUtils.containsOnly((String) value,
135: getInputFilter().getCharacterFilter())) {
136: addError(MessageUtil.localizeAndFormat(getInputFilter()
137: .getInvalidInputMessage(), MessageUtil.localize(
138: getLabel(), getEnvironment()), getInputFilter()
139: .getCharacterFilter(), getEnvironment()));
140: }
141: }
142:
143: /**
144: * Returns {@link ViewModel}.
145: *
146: * @return {@link ViewModel}.
147: */
148: public Object getViewModel() {
149: return new ViewModel();
150: }
151:
152: //*********************************************************************
153: //* VIEW MODEL
154: //*********************************************************************
155:
156: /**
157: * @author <a href="mailto:olegm@webmedia.ee">Oleg Mürk</a>
158: */
159: public class ViewModel extends StringValueControl.ViewModel {
160: protected String textType;
161: protected InputFilter inputFilter;
162:
163: protected ViewModel() {
164: this .textType = TextControl.this .textType.getName();
165: this .inputFilter = TextControl.this .getInputFilter();
166: }
167:
168: public String getTextType() {
169: return textType;
170: }
171:
172: public InputFilter getInputFilter() {
173: return inputFilter;
174: }
175: }
176: }
|