KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > frontend > templateone > form > A_CmsField


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/form/A_CmsField.java,v $
3  * Date : $Date: 2006/03/27 14:52:20 $
4  * Version: $Revision: 1.5 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.frontend.templateone.form;
33
34 import org.opencms.main.CmsLog;
35 import org.opencms.util.CmsStringUtil;
36
37 import java.util.ArrayList JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.regex.Pattern JavaDoc;
41 import java.util.regex.PatternSyntaxException JavaDoc;
42
43 import org.apache.commons.logging.Log;
44
45 /**
46  * Abstract base class for all input fields.<p>
47  *
48  * @author Andreas Zahner
49  * @author Thomas Weckert
50  * @author Jan Baudisch
51  * @version $Revision: 1.5 $
52  * @since 6.0.0
53  */

54 public abstract class A_CmsField implements I_CmsField {
55
56     /** The log object for this class. */
57     private static final Log LOG = CmsLog.getLog(CmsFormHandler.class);
58
59     private List JavaDoc m_items;
60     private String JavaDoc m_label;
61     private boolean m_mandatory;
62     private String JavaDoc m_name;
63     private String JavaDoc m_validationExpression;
64     private String JavaDoc m_value;
65     private String JavaDoc m_errorMessage;
66
67     /**
68      * Default constructor.<p>
69      */

70     public A_CmsField() {
71
72         super();
73
74         m_items = new ArrayList JavaDoc();
75         m_mandatory = false;
76         m_value = "";
77         m_validationExpression = "";
78     }
79
80     /**
81      * @see java.lang.Object#finalize()
82      */

83     protected void finalize() throws Throwable JavaDoc {
84
85         try {
86
87             if (m_items != null) {
88                 m_items.clear();
89             }
90
91             m_label = null;
92             m_name = null;
93             m_validationExpression = null;
94             m_value = null;
95         } catch (Throwable JavaDoc t) {
96             // ignore
97
}
98
99         super.finalize();
100     }
101
102     /**
103      * @see org.opencms.frontend.templateone.form.I_CmsField#getItems()
104      */

105     public List JavaDoc getItems() {
106
107         return m_items;
108     }
109
110     /**
111      * @see org.opencms.frontend.templateone.form.I_CmsField#getLabel()
112      */

113     public String JavaDoc getLabel() {
114
115         return m_label;
116     }
117
118     /**
119      * @see org.opencms.frontend.templateone.form.I_CmsField#getName()
120      */

121     public String JavaDoc getName() {
122
123         return m_name;
124     }
125
126     /**
127      * @see org.opencms.frontend.templateone.form.I_CmsField#getValidationExpression()
128      */

129     public String JavaDoc getValidationExpression() {
130
131         return m_validationExpression;
132     }
133
134     /**
135      * @see org.opencms.frontend.templateone.form.I_CmsField#getValue()
136      */

137     public String JavaDoc getValue() {
138
139         return m_value;
140     }
141
142     /**
143      * @see org.opencms.frontend.templateone.form.I_CmsField#isMandatory()
144      */

145     public boolean isMandatory() {
146
147         return m_mandatory;
148     }
149
150     /**
151      * @see org.opencms.frontend.templateone.form.I_CmsField#needsItems()
152      */

153     public boolean needsItems() {
154
155         return (CmsCheckboxField.class.isAssignableFrom(getClass())
156             || CmsSelectionField.class.isAssignableFrom(getClass()) || CmsRadioButtonField.class.isAssignableFrom(getClass()));
157     }
158
159     /**
160      * Sets the list of items for select boxes, radio buttons and checkboxes.<p>
161      *
162      * The list contains CmsFieldItem objects with the following information:
163      * <ol>
164      * <li>the value of the item</li>
165      * <li>the description of the item</li>
166      * <li>the selection flag of the item (true or false)</li>
167      * </ol>
168      *
169      * @param items the list of items for select boxes, radio buttons and checkboxes
170      */

171     protected void setItems(List JavaDoc items) {
172
173         m_items = items;
174     }
175
176     /**
177      * Sets the description text of the input field.<p>
178      *
179      * @param description the description text of the input field
180      */

181     protected void setLabel(String JavaDoc description) {
182
183         m_label = description;
184     }
185
186     /**
187      * Sets if this input field is mandatory.<p>
188      *
189      * @param mandatory true if this input field is mandatory, otherwise false
190      */

191     protected void setMandatory(boolean mandatory) {
192
193         m_mandatory = mandatory;
194     }
195
196     /**
197      * Sets the name of the input field.<p>
198      *
199      * @param name the name of the input field
200      */

201     protected void setName(String JavaDoc name) {
202
203         m_name = name;
204     }
205
206     /**
207      * Sets the regular expression that is used for validation of the field.<p>
208      *
209      * @param expression the regular expression that is used for validation of the field
210      */

211     protected void setValidationExpression(String JavaDoc expression) {
212
213         m_validationExpression = expression;
214     }
215
216     /**
217      * Sets the initial value of the field.<p>
218      *
219      * @param value the initial value of the field
220      */

221     protected void setValue(String JavaDoc value) {
222
223         m_value = value;
224     }
225
226     /**
227      * Validates the constraints if this field is mandatory.<p>
228      *
229      * @return {@link CmsFormHandler#ERROR_MANDATORY} if a constraint is violated
230      */

231     protected String JavaDoc validateConstraints() {
232
233         if (isMandatory()) {
234
235             // check if the field has a value
236
if (needsItems()) {
237
238                 // check if at least one item has been selected
239
Iterator JavaDoc k = m_items.iterator();
240                 boolean isSelected = false;
241                 while (k.hasNext()) {
242
243                     CmsFieldItem currentItem = (CmsFieldItem)k.next();
244                     if (currentItem.isSelected()) {
245                         isSelected = true;
246                         continue;
247                     }
248                 }
249
250                 if (!isSelected) {
251                     // no item has been selected, create an error message
252
return CmsFormHandler.ERROR_MANDATORY;
253                 }
254             } else {
255
256                 // check if the field has been filled out
257
if (CmsStringUtil.isEmpty(m_value)) {
258                     return CmsFormHandler.ERROR_MANDATORY;
259                 }
260             }
261         }
262
263         return null;
264     }
265
266     /**
267      * Validates the input value of this field.<p>
268      *
269      * @return {@link CmsFormHandler#ERROR_VALIDATION} if validation of the input value failed
270      */

271     protected String JavaDoc validateValue() {
272
273         // validate non-empty values with given regular expression
274
if (CmsStringUtil.isNotEmpty(m_value) && (!"".equals(m_validationExpression))) {
275
276             Pattern JavaDoc pattern = null;
277             try {
278
279                 pattern = Pattern.compile(m_validationExpression);
280                 if (!pattern.matcher(m_value).matches()) {
281                     return CmsFormHandler.ERROR_VALIDATION;
282                 }
283             } catch (PatternSyntaxException JavaDoc e) {
284
285                 // syntax error in regular expression, log to opencms.log
286
if (LOG.isErrorEnabled()) {
287                     LOG.error(Messages.get().getBundle().key(Messages.LOG_ERR_PATTERN_SYNTAX_0), e);
288                 }
289             }
290         }
291
292         return null;
293     }
294
295     /**
296      * @see org.opencms.frontend.templateone.form.I_CmsField#validate(CmsFormHandler)
297      */

298     public String JavaDoc validate(CmsFormHandler formHandler) {
299
300         // validate the constraints
301
String JavaDoc validationError = validateConstraints();
302         if (CmsStringUtil.isEmpty(validationError)) {
303
304             // no constraint error- validate the input value
305
validationError = validateValue();
306         }
307
308         return validationError;
309     }
310
311     /**
312      * Sets the error message if validation failed.<p>
313      *
314      * @param errorMessage the error message if validation failed
315      */

316     protected void setErrorMessage(String JavaDoc errorMessage) {
317
318         m_errorMessage = errorMessage;
319     }
320
321     /**
322      * @see org.opencms.frontend.templateone.form.I_CmsField#getErrorMessage()
323      */

324     public String JavaDoc getErrorMessage() {
325
326         return m_errorMessage;
327     }
328
329     /**
330      * Returns the field value as a String.<p>
331      * @see java.lang.Object#toString()
332      */

333     public String JavaDoc toString() {
334
335         String JavaDoc result;
336         if (needsItems()) {
337             // check which item has been selected
338
StringBuffer JavaDoc fieldValue = new StringBuffer JavaDoc(8);
339             Iterator JavaDoc k = getItems().iterator();
340             boolean isSelected = false;
341             while (k.hasNext()) {
342                 CmsFieldItem currentItem = (CmsFieldItem)k.next();
343                 if (currentItem.isSelected()) {
344                     if (isSelected) {
345                         fieldValue.append(", ");
346                     }
347                     fieldValue.append(currentItem.getLabel());
348                     isSelected = true;
349                 }
350             }
351             result = fieldValue.toString();
352         } else {
353             // for other field types, append value
354
result = getValue();
355         }
356
357         return result;
358     }
359 }
360
Popular Tags