001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.forms.element;
025:
026: import java.io.PrintWriter;
027: import java.util.regex.Pattern;
028:
029: import org.riotfamily.common.markup.DocumentWriter;
030: import org.riotfamily.common.markup.Html;
031: import org.riotfamily.forms.ErrorUtils;
032: import org.riotfamily.forms.MessageUtils;
033: import org.riotfamily.forms.request.FormRequest;
034: import org.springframework.util.ObjectUtils;
035: import org.springframework.util.StringUtils;
036:
037: /**
038: * A text input field.
039: */
040: public class TextField extends AbstractTextElement {
041:
042: private static final String CONFIRM_SUFFIX = "-confirm";
043:
044: private static final String DEFAULT_CONFIRM_MESSAGE_KEY = "label.textField.confirmInput";
045:
046: private static final String DEFAULT_REGEX_MISMATCH_MESSAGE_KEY = "error.textField.regexMismatch";
047:
048: private boolean confirm;
049:
050: private String confirmText = null;
051:
052: private String confirmMessageKey;
053:
054: private String confirmMessageText;
055:
056: private Pattern pattern;
057:
058: private String regexMismatchMessageKey = DEFAULT_REGEX_MISMATCH_MESSAGE_KEY;
059:
060: private String regexMismatchMessageText;
061:
062: public TextField() {
063: this ("text");
064: }
065:
066: public TextField(String s) {
067: super (s);
068: }
069:
070: public void setConfirm(boolean confirm) {
071: this .confirm = confirm;
072: }
073:
074: public void setConfirmMessageKey(String confirmMessageKey) {
075: this .confirmMessageKey = confirmMessageKey;
076: }
077:
078: public void setConfirmMessageText(String confirmMessageText) {
079: this .confirmMessageText = confirmMessageText;
080: }
081:
082: public void setRegex(String regex) {
083: this .pattern = Pattern.compile(regex);
084: setValidateOnChange(true);
085: }
086:
087: public void setRegexMismatchMessageKey(
088: String regexMismatchMessageKey) {
089: this .regexMismatchMessageKey = regexMismatchMessageKey;
090: }
091:
092: public void setRegexMismatchMessageText(
093: String regexMismatchMessageText) {
094: this .regexMismatchMessageText = regexMismatchMessageText;
095: }
096:
097: public void renderInternal(PrintWriter writer) {
098: if (confirm) {
099: DocumentWriter doc = new DocumentWriter(writer);
100: doc.start(Html.DIV).attribute(Html.COMMON_CLASS,
101: "confirm-text");
102: doc.body();
103: super .renderInternal(writer);
104: String msg = MessageUtils.getMessage(this ,
105: getConfirmMessage());
106: doc.start(Html.P).body(msg).end();
107:
108: doc.startEmpty(Html.INPUT).attribute(Html.INPUT_TYPE,
109: getType()).attribute(Html.COMMON_CLASS,
110: getStyleClass()).attribute(Html.INPUT_NAME,
111: getConfirmParamName()).attribute(Html.INPUT_VALUE,
112: confirmText != null ? confirmText : getText());
113:
114: doc.closeAll();
115: } else {
116: super .renderInternal(writer);
117: }
118: }
119:
120: public void processRequest(FormRequest request) {
121: if (confirm) {
122: confirmText = request.getParameter(getConfirmParamName());
123: }
124: super .processRequest(request);
125: }
126:
127: public void validate() {
128: super .validate();
129: if (confirm) {
130: if (!ObjectUtils.nullSafeEquals(getText(), confirmText)) {
131: ErrorUtils.reject(this ,
132: "error.textField.confirmationFailed");
133: }
134: }
135: }
136:
137: protected void validateSyntax() {
138: if (pattern != null && StringUtils.hasLength(getText())) {
139: if (!pattern.matcher(getText()).matches()) {
140: getForm().getErrors().rejectValue(getFieldName(),
141: regexMismatchMessageKey,
142: regexMismatchMessageText);
143: }
144: }
145: }
146:
147: protected String getConfirmParamName() {
148: return getParamName() + CONFIRM_SUFFIX;
149: }
150:
151: protected String getConfirmMessage() {
152: if (confirmMessageText != null) {
153: return confirmMessageText;
154: } else if (confirmMessageKey != null) {
155: return MessageUtils.getMessage(this , confirmMessageKey);
156: } else {
157: return MessageUtils.getMessage(this ,
158: getDefaultConfirmMessageKey());
159: }
160: }
161:
162: protected String getDefaultConfirmMessageKey() {
163: return DEFAULT_CONFIRM_MESSAGE_KEY;
164: }
165:
166: }
|