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.araneaframework.uilib.support.UiLibMessages;
018: import org.araneaframework.uilib.util.MessageUtil;
019:
020: /**
021: * This class represents controls, that have a value of type <code>String</code>
022: * and a single request parameter of same type.
023: *
024: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
025: *
026: */
027: public abstract class StringValueControl extends
028: EmptyStringNullableControl {
029:
030: private Long minLength;
031: private Long maxLength;
032:
033: private boolean trimValue = false;
034:
035: /**
036: * Sets the maximum length.
037: * @param maxLength maximum length.
038: */
039: public void setMaxLength(Long maxLength) {
040: this .maxLength = maxLength;
041: }
042:
043: /**
044: * Sets the minimum length.
045: * @param minLength minimum length.
046: */
047: public void setMinLength(Long minLength) {
048: this .minLength = minLength;
049: }
050:
051: /**
052: * Sets whether the value from request will be trimmed.
053: * @param trimValue whether the value from request will be trimmed.
054: */
055: public void setTrimValue(boolean trimValue) {
056: this .trimValue = trimValue;
057: }
058:
059: /**
060: * Returns "String".
061: * @return "String".
062: */
063: public String getRawValueType() {
064: return "String";
065: }
066:
067: //*********************************************************************
068: //* INTERNAL METHODS
069: //*********************************************************************
070:
071: /**
072: * Direct copy.
073: */
074: protected Object fromRequest(String parameterValue) {
075: if (parameterValue != null && trimValue)
076: return parameterValue.trim();
077:
078: return parameterValue;
079: }
080:
081: /**
082: * Direct copy.
083: */
084: protected String toResponse(Object controlValue) {
085: return (String) controlValue;
086: }
087:
088: /**
089: * Checks that the value (<code>String</code>) length is between the given values.
090: */
091: protected void validateNotNull() {
092: if (minLength != null
093: && ((String) getRawValue()).length() < minLength
094: .longValue()) {
095: addError(MessageUtil.localizeAndFormat(
096: UiLibMessages.STRING_TOO_SHORT, MessageUtil
097: .localize(getLabel(), getEnvironment()),
098: minLength.toString(), getEnvironment()));
099: }
100:
101: if (maxLength != null
102: && ((String) getRawValue()).length() > maxLength
103: .longValue()) {
104: addError(MessageUtil.localizeAndFormat(
105: UiLibMessages.STRING_TOO_LONG, MessageUtil
106: .localize(getLabel(), getEnvironment()),
107: maxLength.toString(), getEnvironment()));
108: }
109: }
110:
111: /**
112: * Returns {@link ViewModel}.
113: *
114: * @return {@link ViewModel}.
115: */
116: public Object getViewModel() {
117: return new ViewModel();
118: }
119:
120: //*********************************************************************
121: //* VIEW MODEL
122: //*********************************************************************
123:
124: /**
125: * @author <a href="mailto:olegm@webmedia.ee">Oleg Mürk</a>
126: */
127: public class ViewModel extends StringArrayRequestControl.ViewModel {
128:
129: private Long minLength;
130: private Long maxLength;
131:
132: /**
133: * Takes an outer class snapshot.
134: */
135: public ViewModel() {
136: this .minLength = StringValueControl.this .minLength;
137: this .maxLength = StringValueControl.this .maxLength;
138: }
139:
140: /**
141: * Returns the minimum length.
142: * @return the minimum length.
143: */
144: public Long getMinLength() {
145: return minLength;
146: }
147:
148: /**
149: * Returns the maximum length.
150: * @return the maximum length.
151: */
152: public Long getMaxLength() {
153: return maxLength;
154: }
155: }
156: }
|