KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > DatePicker


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form;
16
17 import java.text.DateFormatSymbols JavaDoc;
18 import java.text.SimpleDateFormat JavaDoc;
19 import java.util.Calendar JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.hivemind.Resource;
26 import org.apache.tapestry.IAsset;
27 import org.apache.tapestry.IMarkupWriter;
28 import org.apache.tapestry.IRequestCycle;
29 import org.apache.tapestry.IScript;
30 import org.apache.tapestry.PageRenderSupport;
31 import org.apache.tapestry.TapestryUtils;
32 import org.apache.tapestry.engine.IScriptSource;
33 import org.apache.tapestry.form.translator.DateTranslator;
34
35 /**
36  * Provides a Form <tt>java.util.Date</tt> field component for selecting dates. [ <a
37  * HREF="../../../../../ComponentReference/DatePicker.html">Component Reference </a>] As of 4.0,
38  * DatePicker can indicate that it is required, use a custom translator (e.g. for java.sql.Date),
39  * and perform validation on the submitted date.
40  *
41  * @author Paul Geerts
42  * @author Malcolm Edgar
43  * @author Paul Ferraro
44  * @since 2.2
45  */

46
47 public abstract class DatePicker extends AbstractValidatableField
48 {
49     public abstract Date JavaDoc getValue();
50
51     public abstract void setValue(Date JavaDoc value);
52
53     public abstract boolean isDisabled();
54
55     public abstract boolean getIncludeWeek();
56
57     public abstract IAsset getIcon();
58
59     private IScript _script;
60
61     private static final String JavaDoc SYM_NAME = "name";
62
63     private static final String JavaDoc SYM_FORMNAME = "formName";
64
65     private static final String JavaDoc SYM_MONTHNAMES = "monthNames";
66
67     private static final String JavaDoc SYM_SHORT_MONTHNAMES = "shortMonthNames";
68
69     private static final String JavaDoc SYM_WEEKDAYNAMES = "weekDayNames";
70
71     private static final String JavaDoc SYM_SHORT_WEEKDAYNAMES = "shortWeekDayNames";
72
73     private static final String JavaDoc SYM_FIRSTDAYINWEEK = "firstDayInWeek";
74
75     private static final String JavaDoc SYM_MINDAYSINFIRSTWEEK = "minimalDaysInFirstWeek";
76
77     private static final String JavaDoc SYM_FORMAT = "format";
78
79     private static final String JavaDoc SYM_INCL_WEEK = "includeWeek";
80
81     private static final String JavaDoc SYM_VALUE = "value";
82
83     private static final String JavaDoc SYM_BUTTONONCLICKHANDLER = "buttonOnclickHandler";
84
85     /**
86      * Injected
87      *
88      * @since 4.0
89      */

90     public abstract IScriptSource getScriptSource();
91
92     /**
93      * @see org.apache.tapestry.AbstractComponent#finishLoad()
94      */

95     protected void finishLoad()
96     {
97         super.finishLoad();
98
99         IScriptSource source = getScriptSource();
100
101         Resource location = getSpecification().getSpecificationLocation().getRelativeResource(
102                 "DatePicker.script");
103
104         _script = source.getScript(location);
105     }
106
107     /**
108      * @see org.apache.tapestry.form.validator.AbstractValidatableField#render(org.apache.tapestry.IMarkupWriter,
109      * org.apache.tapestry.IRequestCycle, java.lang.String)
110      */

111     public void render(IMarkupWriter writer, IRequestCycle cycle, String JavaDoc value)
112     {
113         PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
114
115         boolean disabled = isDisabled();
116         DateTranslator translator = (DateTranslator) getTranslator();
117         Locale JavaDoc locale = getPage().getLocale();
118         SimpleDateFormat JavaDoc format = translator.getDateFormat(locale);
119
120         DateFormatSymbols JavaDoc dfs = format.getDateFormatSymbols();
121         Calendar JavaDoc cal = Calendar.getInstance(locale);
122
123         String JavaDoc name = getName();
124
125         Map JavaDoc symbols = new HashMap JavaDoc();
126
127         symbols.put(SYM_NAME, name);
128         symbols.put(SYM_FORMAT, format.toPattern());
129         symbols.put(SYM_INCL_WEEK, getIncludeWeek() ? Boolean.TRUE : Boolean.FALSE);
130
131         symbols.put(SYM_MONTHNAMES, makeStringList(dfs.getMonths(), 0, 12));
132         symbols.put(SYM_SHORT_MONTHNAMES, makeStringList(dfs.getShortMonths(), 0, 12));
133         symbols.put(SYM_WEEKDAYNAMES, makeStringList(dfs.getWeekdays(), 1, 8));
134         symbols.put(SYM_SHORT_WEEKDAYNAMES, makeStringList(dfs.getShortWeekdays(), 1, 8));
135         symbols.put(SYM_FIRSTDAYINWEEK, new Integer JavaDoc(cal.getFirstDayOfWeek() - 1));
136         symbols.put(SYM_MINDAYSINFIRSTWEEK, new Integer JavaDoc(cal.getMinimalDaysInFirstWeek()));
137         symbols.put(SYM_FORMNAME, getForm().getName());
138         symbols.put(SYM_VALUE, getValue());
139
140         _script.execute(cycle, pageRenderSupport, symbols);
141
142         renderDelegatePrefix(writer, cycle);
143
144         writer.beginEmpty("input");
145         writer.attribute("type", "text");
146         writer.attribute("name", name);
147         writer.attribute("value", value);
148         writer.attribute("title", format.toLocalizedPattern());
149
150         if (disabled)
151             writer.attribute("disabled", "disabled");
152
153         renderIdAttribute(writer, cycle);
154
155         renderDelegateAttributes(writer, cycle);
156
157         renderContributions(writer, cycle);
158
159         renderInformalParameters(writer, cycle);
160
161         writer.printRaw("&nbsp;");
162
163         if (!disabled)
164         {
165             writer.begin("a");
166             writer.attribute("href", (String JavaDoc) symbols.get(SYM_BUTTONONCLICKHANDLER));
167         }
168
169         IAsset icon = getIcon();
170
171         writer.beginEmpty("img");
172         writer.attribute("src", icon.buildURL(cycle));
173         writer.attribute("border", 0);
174
175         if (!disabled)
176             writer.end();
177
178         renderDelegateSuffix(writer, cycle);
179     }
180
181     /**
182      * @see org.apache.tapestry.form.AbstractValidatableField#readValue()
183      */

184     public Object JavaDoc readValue()
185     {
186         return getValue();
187     }
188
189     /**
190      * @see org.apache.tapestry.form.AbstractValidatableField#updateValue(java.lang.Object)
191      */

192     public void writeValue(Object JavaDoc value)
193     {
194         setValue((Date JavaDoc) value);
195     }
196
197     /**
198      * Create a list of quoted strings. The list is suitable for initializing a JavaScript array.
199      */

200     private String JavaDoc makeStringList(String JavaDoc[] a, int offset, int length)
201     {
202         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
203         for (int i = offset; i < length; i++)
204         {
205             // JavaScript is sensitive to some UNICODE characters. So for
206
// the sake of simplicity, we just escape everything
207
b.append('"');
208             char[] ch = a[i].toCharArray();
209             for (int j = 0; j < ch.length; j++)
210             {
211                 if (ch[j] < 128)
212                 {
213                     b.append(ch[j]);
214                 }
215                 else
216                 {
217                     b.append(escape(ch[j]));
218                 }
219             }
220
221             b.append('"');
222             if (i < length - 1)
223             {
224                 b.append(", ");
225             }
226         }
227         return b.toString();
228
229     }
230
231     /**
232      * Create an escaped Unicode character
233      *
234      * @param c
235      * @return The unicode character in escaped string form
236      */

237     private static String JavaDoc escape(char c)
238     {
239         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
240         for (int i = 0; i < 4; i++)
241         {
242             b.append(Integer.toHexString(c & 0x000F).toUpperCase());
243             c >>>= 4;
244         }
245         b.append("u\\");
246         return b.reverse().toString();
247     }
248
249 }
Popular Tags