KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
27 import java.text.DateFormat JavaDoc;
28 import java.text.ParseException JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.regex.Matcher JavaDoc;
32 import java.util.regex.Pattern JavaDoc;
33
34 import org.riotfamily.common.util.FormatUtils;
35 import org.riotfamily.forms.DHTMLElement;
36 import org.riotfamily.forms.ErrorUtils;
37 import org.riotfamily.forms.MessageUtils;
38 import org.riotfamily.forms.TemplateUtils;
39 import org.riotfamily.forms.resource.FormResource;
40 import org.riotfamily.forms.resource.ResourceElement;
41 import org.riotfamily.forms.resource.ScriptResource;
42 import org.riotfamily.forms.resource.StylesheetResource;
43 import org.springframework.beans.propertyeditors.CustomDateEditor;
44 import org.springframework.util.StringUtils;
45
46 /**
47  * A DHTML calendar widget.
48  * http://www.dynarch.com/projects/calendar/
49  */

50 public class Calendar extends AbstractTextElement implements ResourceElement,
51         DHTMLElement {
52
53     /**
54      * Rules to translate {@link SimpleDateFormat SimpleDateFormat patterns}
55      * to the syntax used by the JavaScript calendar widget.
56      * */

57     private static String JavaDoc[] conversions = new String JavaDoc[] {
58             "'(.)", "$1",
59             "%", "%%",
60             "(?<!%)MMMM", "%B",
61             "(?<!%)MMM", "%b",
62             "(?<!%)MM?", "%m",
63             "(?<!%)dd", "%d",
64             "(?<!%)d", "%e",
65             "(?<!%)yyyy", "%Y",
66             "(?<!%)yy", "%y",
67             "(?<!%)hh", "%I",
68             "(?<!%)h", "%i",
69             "(?<!%)HH", "%H",
70             "(?<!%)H", "%h",
71             "(?<!%)mm?", "%M",
72             "(?<!%)ss?", "%S",
73             "(?<!%)EE?", "%a",
74             "(?<!%)w", "%W",
75             "(?<!%)a", "%P"};
76
77     /** List of compiled conversion patterns */
78     private static Pattern JavaDoc[] patterns;
79
80     static {
81         patterns = new Pattern JavaDoc[conversions.length / 2];
82         for (int i = 0; i < patterns.length; i++) {
83             patterns[i] = Pattern.compile(conversions[i * 2]);
84         }
85     }
86
87     private String JavaDoc formatPattern = "yyyy-MM-dd";
88
89     private String JavaDoc formatKey;
90
91     private String JavaDoc defaultValue;
92
93     private String JavaDoc jsFormatPattern;
94
95     private DateFormat JavaDoc dateFormat;
96
97     private ScriptResource resource;
98
99
100     public Calendar() {
101         setStyleClass("text calendar-input");
102     }
103
104     public String JavaDoc getFormatPattern() {
105         return formatPattern;
106     }
107
108     /**
109      * Sets the format pattern to use.
110      * @see SimpleDateFormat
111      */

112     public void setFormatPattern(String JavaDoc formatPattern) {
113         this.formatPattern = formatPattern;
114     }
115
116     public String JavaDoc getJsFormatPattern() {
117         return jsFormatPattern;
118     }
119
120     /**
121      * Sets a message-key that is used to look-up the actual format pattern.
122      */

123     public void setFormatKey(String JavaDoc formatKey) {
124         this.formatKey = formatKey;
125     }
126
127     public void setDefaultValue(String JavaDoc defaultValue) {
128         this.defaultValue = defaultValue;
129     }
130
131     protected void afterFormSet() {
132         if (formatKey != null) {
133             formatPattern = MessageUtils.getMessage(this, formatKey);
134         }
135         jsFormatPattern = formatPattern;
136         for (int i = 0; i < patterns.length; i++) {
137             Matcher JavaDoc matcher = patterns[i].matcher(jsFormatPattern);
138             jsFormatPattern = matcher.replaceAll(conversions[i * 2 + 1]);
139         }
140         dateFormat = new SimpleDateFormat JavaDoc(formatPattern);
141         setPropertyEditor(new CustomDateEditor(dateFormat, false));
142     }
143
144     public boolean isShowTime() {
145         return formatPattern != null && (formatPattern.indexOf('H') != -1 ||
146                 formatPattern.indexOf('h') != -1);
147     }
148
149     public void setValue(Object JavaDoc value) {
150         if (value == null && defaultValue != null) {
151             value = getDefaultDate();
152         }
153         super.setValue(value);
154     }
155
156     protected void validate(boolean formSubmitted) {
157         super.validate(formSubmitted);
158         if (StringUtils.hasText(getText())) {
159             try {
160                 dateFormat.parse(getText());
161             }
162             catch (ParseException JavaDoc e) {
163                 ErrorUtils.reject(this, "error.calendar.invalidDateFormat");
164             }
165         }
166     }
167
168     protected void afterFormContextSet() {
169         String JavaDoc lang = getFormContext().getLocale().getLanguage().toLowerCase();
170         URL JavaDoc languageScript = getClass().getResource(
171                 "/org/riotfamily/resources/jscalendar/lang/calendar-"
172                 + lang + ".js");
173
174         if (languageScript == null) {
175             lang = "en";
176         }
177
178         resource = new ScriptResource("jscalendar/calendar-setup.js", "Calendar.setup", new FormResource[] {
179             new ScriptResource("jscalendar/lang/calendar-" + lang + ".js", "Calendar._DN",
180                 new ScriptResource("jscalendar/calendar.js", "Calendar")
181             ),
182             new StylesheetResource("jscalendar/calendar.css")
183         });
184     }
185
186     public FormResource getResource() {
187         return resource;
188     }
189
190     public String JavaDoc getInitScript() {
191         return TemplateUtils.getInitScript(this);
192     }
193
194     protected Date JavaDoc getDefaultDate() {
195         Date JavaDoc date = FormatUtils.parseDate(defaultValue);
196         if (date == null) {
197             try {
198                 date = dateFormat.parse(defaultValue);
199             }
200             catch (ParseException JavaDoc e) {
201             }
202         }
203         return date;
204     }
205 }
206
Popular Tags