KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > element > TextField


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.element;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28
29 import org.riotfamily.common.markup.DocumentWriter;
30 import org.riotfamily.common.markup.Html;
31 import org.riotfamily.forms.ErrorUtils;
32 import org.riotfamily.forms.MessageUtils;
33 import org.riotfamily.forms.request.FormRequest;
34 import org.springframework.util.ObjectUtils;
35 import org.springframework.util.StringUtils;
36
37 /**
38  * A text input field.
39  */

40 public class TextField extends AbstractTextElement {
41
42     private static final String JavaDoc CONFIRM_SUFFIX = "-confirm";
43     
44     private static final String JavaDoc DEFAULT_CONFIRM_MESSAGE_KEY =
45             "label.textField.confirmInput";
46     
47     private static final String JavaDoc DEFAULT_REGEX_MISMATCH_MESSAGE_KEY =
48             "error.textField.regexMismatch";
49     
50     private boolean confirm;
51     
52     private String JavaDoc confirmText = null;
53     
54     private String JavaDoc confirmMessageKey;
55     
56     private String JavaDoc confirmMessageText;
57     
58     private Pattern JavaDoc pattern;
59     
60     private String JavaDoc regexMismatchMessageKey = DEFAULT_REGEX_MISMATCH_MESSAGE_KEY;
61     
62     private String JavaDoc regexMismatchMessageText;
63     
64     
65     public TextField() {
66         this("text");
67     }
68     
69     public TextField(String JavaDoc s) {
70         super(s);
71     }
72     
73     public void setConfirm(boolean confirm) {
74         this.confirm = confirm;
75     }
76
77     public void setConfirmMessageKey(String JavaDoc confirmMessageKey) {
78         this.confirmMessageKey = confirmMessageKey;
79     }
80
81     public void setConfirmMessageText(String JavaDoc confirmMessageText) {
82         this.confirmMessageText = confirmMessageText;
83     }
84
85     public void setRegex(String JavaDoc regex) {
86         this.pattern = Pattern.compile(regex);
87         setValidateOnChange(true);
88     }
89
90     public void setRegexMismatchMessageKey(String JavaDoc regexMismatchMessageKey) {
91         this.regexMismatchMessageKey = regexMismatchMessageKey;
92     }
93
94     public void setRegexMismatchMessageText(String JavaDoc regexMismatchMessageText) {
95         this.regexMismatchMessageText = regexMismatchMessageText;
96     }
97
98     public void renderInternal(PrintWriter JavaDoc writer) {
99         if (confirm) {
100             DocumentWriter doc = new DocumentWriter(writer);
101             doc.start(Html.DIV).attribute(Html.COMMON_CLASS, "confirm-text");
102             doc.body();
103             super.renderInternal(writer);
104             String JavaDoc msg = MessageUtils.getMessage(this, getConfirmMessage());
105             doc.start(Html.P).body(msg).end();
106             
107             doc.startEmpty(Html.INPUT)
108                     .attribute(Html.INPUT_TYPE, getType())
109                     .attribute(Html.COMMON_CLASS, getStyleClass())
110                     .attribute(Html.INPUT_NAME, getConfirmParamName())
111                     .attribute(Html.INPUT_VALUE,
112                     confirmText != null ? confirmText : getText());
113             
114             doc.closeAll();
115         }
116         else {
117             super.renderInternal(writer);
118         }
119     }
120     
121     public void processRequest(FormRequest request) {
122         if (confirm) {
123             confirmText = request.getParameter(getConfirmParamName());
124         }
125         super.processRequest(request);
126     }
127
128     protected void validate(boolean formSubmitted) {
129         super.validate(formSubmitted);
130         if (formSubmitted && confirm) {
131             if (!ObjectUtils.nullSafeEquals(getText(), confirmText)) {
132                 ErrorUtils.reject(this, "error.textField.confirmationFailed");
133             }
134         }
135         if (pattern != null && StringUtils.hasLength(getText())) {
136             if (!pattern.matcher(getText()).matches()) {
137                 getForm().getErrors().rejectValue(getFieldName(),
138                         regexMismatchMessageKey, regexMismatchMessageText);
139             }
140             
141         }
142     }
143
144     protected String JavaDoc getConfirmParamName() {
145         return getParamName() + CONFIRM_SUFFIX;
146     }
147     
148     protected String JavaDoc getConfirmMessage() {
149         if (confirmMessageText != null) {
150             return confirmMessageText;
151         }
152         else if (confirmMessageKey != null){
153             return MessageUtils.getMessage(this, confirmMessageKey);
154         }
155         else {
156             return MessageUtils.getMessage(this, getDefaultConfirmMessageKey());
157         }
158     }
159     
160     protected String JavaDoc getDefaultConfirmMessageKey() {
161         return DEFAULT_CONFIRM_MESSAGE_KEY;
162     }
163
164 }
165
Popular Tags