|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DatePicker.java | 0% | 0% | 0% | 0% |
|
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;
|
|
18 |
import java.text.ParseException;
|
|
19 |
import java.text.SimpleDateFormat;
|
|
20 |
import java.util.Calendar;
|
|
21 |
import java.util.Date;
|
|
22 |
import java.util.HashMap;
|
|
23 |
import java.util.Locale;
|
|
24 |
import java.util.Map;
|
|
25 |
|
|
26 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
27 |
import org.apache.hivemind.Resource;
|
|
28 |
import org.apache.tapestry.IAsset;
|
|
29 |
import org.apache.tapestry.IEngine;
|
|
30 |
import org.apache.tapestry.IForm;
|
|
31 |
import org.apache.tapestry.IMarkupWriter;
|
|
32 |
import org.apache.tapestry.IRequestCycle;
|
|
33 |
import org.apache.tapestry.IScript;
|
|
34 |
import org.apache.tapestry.Tapestry;
|
|
35 |
import org.apache.tapestry.engine.IScriptSource;
|
|
36 |
import org.apache.tapestry.html.Body;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Provides a Form <tt>java.util.Date</tt> field component for selecting dates.
|
|
40 |
*
|
|
41 |
* [<a href="../../../../../ComponentReference/DatePicker.html">Component Reference</a>]
|
|
42 |
*
|
|
43 |
* @author Paul Geerts
|
|
44 |
* @author Malcolm Edgar
|
|
45 |
* @since 2.2
|
|
46 |
*
|
|
47 |
*/
|
|
48 |
|
|
49 |
public abstract class DatePicker extends AbstractFormComponent |
|
50 |
{ |
|
51 |
public abstract String getFormat();
|
|
52 |
|
|
53 |
public abstract Date getValue();
|
|
54 |
|
|
55 |
public abstract void setValue(Date value); |
|
56 |
|
|
57 |
public abstract boolean isDisabled(); |
|
58 |
|
|
59 |
public abstract boolean getIncludeWeek(); |
|
60 |
|
|
61 |
public abstract IAsset getIcon();
|
|
62 |
|
|
63 |
private IScript _script;
|
|
64 |
|
|
65 |
private static final String SYM_NAME = "name"; |
|
66 |
private static final String SYM_FORMNAME = "formName"; |
|
67 |
private static final String SYM_MONTHNAMES = "monthNames"; |
|
68 |
private static final String SYM_SHORT_MONTHNAMES = "shortMonthNames"; |
|
69 |
private static final String SYM_WEEKDAYNAMES = "weekDayNames"; |
|
70 |
private static final String SYM_SHORT_WEEKDAYNAMES = "shortWeekDayNames"; |
|
71 |
private static final String SYM_FIRSTDAYINWEEK = "firstDayInWeek"; |
|
72 |
private static final String SYM_MINDAYSINFIRSTWEEK = "minimalDaysInFirstWeek"; |
|
73 |
private static final String SYM_FORMAT = "format"; |
|
74 |
private static final String SYM_INCL_WEEK = "includeWeek"; |
|
75 |
private static final String SYM_VALUE = "value"; |
|
76 |
private static final String SYM_BUTTONONCLICKHANDLER = "buttonOnclickHandler"; |
|
77 |
|
|
78 |
// Output symbol
|
|
79 |
|
|
80 |
private static final String SYM_BUTTONNAME = "buttonName"; |
|
81 |
|
|
82 | 0 |
protected void finishLoad() |
83 |
{ |
|
84 | 0 |
IEngine engine = getPage().getEngine(); |
85 | 0 |
IScriptSource source = engine.getScriptSource(); |
86 |
|
|
87 | 0 |
Resource location = |
88 |
getSpecification().getSpecificationLocation().getRelativeResource("DatePicker.script");
|
|
89 |
|
|
90 | 0 |
_script = source.getScript(location); |
91 |
} |
|
92 |
|
|
93 | 0 |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
94 |
{ |
|
95 | 0 |
IForm form = getForm(cycle); |
96 |
|
|
97 | 0 |
String name = form.getElementId(this);
|
98 |
|
|
99 | 0 |
String format = getFormat(); |
100 |
|
|
101 | 0 |
if (format == null) |
102 | 0 |
format = "dd MMM yyyy";
|
103 |
|
|
104 | 0 |
SimpleDateFormat formatter = new SimpleDateFormat(format, getPage().getLocale());
|
105 |
|
|
106 | 0 |
boolean disabled = isDisabled();
|
107 |
|
|
108 | 0 |
if (!cycle.isRewinding())
|
109 |
{ |
|
110 | 0 |
Body body = Body.get(cycle); |
111 |
|
|
112 | 0 |
if (body == null) |
113 | 0 |
throw new ApplicationRuntimeException( |
114 |
Tapestry.format("must-be-contained-by-body", "DatePicker"), |
|
115 |
this,
|
|
116 |
null,
|
|
117 |
null);
|
|
118 |
|
|
119 | 0 |
Locale locale = getPage().getLocale(); |
120 | 0 |
DateFormatSymbols dfs = new DateFormatSymbols(locale);
|
121 | 0 |
Calendar cal = Calendar.getInstance(locale); |
122 |
|
|
123 | 0 |
Date value = getValue(); |
124 |
|
|
125 | 0 |
Map symbols = new HashMap();
|
126 |
|
|
127 | 0 |
symbols.put(SYM_NAME, name); |
128 | 0 |
symbols.put(SYM_FORMAT, format); |
129 | 0 |
symbols.put(SYM_INCL_WEEK, getIncludeWeek() ? Boolean.TRUE : Boolean.FALSE); |
130 |
|
|
131 | 0 |
symbols.put(SYM_MONTHNAMES, makeStringList(dfs.getMonths(), 0, 12)); |
132 | 0 |
symbols.put(SYM_SHORT_MONTHNAMES, makeStringList(dfs.getShortMonths(), 0, 12)); |
133 | 0 |
symbols.put(SYM_WEEKDAYNAMES, makeStringList(dfs.getWeekdays(), 1, 8)); |
134 | 0 |
symbols.put(SYM_SHORT_WEEKDAYNAMES, makeStringList(dfs.getShortWeekdays(), 1, 8)); |
135 | 0 |
symbols.put(SYM_FIRSTDAYINWEEK, new Integer(cal.getFirstDayOfWeek() - 1));
|
136 | 0 |
symbols.put(SYM_MINDAYSINFIRSTWEEK, new Integer(cal.getMinimalDaysInFirstWeek()));
|
137 | 0 |
symbols.put(SYM_FORMNAME, form.getName()); |
138 | 0 |
symbols.put(SYM_VALUE, value); |
139 |
|
|
140 | 0 |
_script.execute(cycle, body, symbols); |
141 |
|
|
142 | 0 |
writer.beginEmpty("input");
|
143 | 0 |
writer.attribute("type", "text"); |
144 | 0 |
writer.attribute("name", name);
|
145 | 0 |
writer.attribute("title", formatter.toLocalizedPattern());
|
146 |
|
|
147 | 0 |
if (value != null) |
148 | 0 |
writer.attribute("value", formatter.format(value));
|
149 |
|
|
150 | 0 |
if (disabled)
|
151 | 0 |
writer.attribute("disabled", "disabled"); |
152 |
|
|
153 | 0 |
renderInformalParameters(writer, cycle); |
154 |
|
|
155 | 0 |
writer.printRaw(" ");
|
156 |
|
|
157 | 0 |
if (!disabled)
|
158 |
{ |
|
159 | 0 |
writer.begin("a");
|
160 | 0 |
writer.attribute("href", (String) symbols.get(SYM_BUTTONONCLICKHANDLER));
|
161 |
} |
|
162 |
|
|
163 | 0 |
IAsset icon = getIcon(); |
164 |
|
|
165 | 0 |
writer.beginEmpty("img");
|
166 | 0 |
writer.attribute("src", icon.buildURL(cycle));
|
167 | 0 |
writer.attribute("border", 0);
|
168 |
|
|
169 | 0 |
if (!disabled)
|
170 | 0 |
writer.end(); // <a>
|
171 |
|
|
172 |
} |
|
173 |
|
|
174 | 0 |
if (form.isRewinding())
|
175 |
{ |
|
176 | 0 |
if (disabled)
|
177 | 0 |
return;
|
178 |
|
|
179 | 0 |
String textValue = cycle.getRequestContext().getParameter(name); |
180 |
|
|
181 | 0 |
if (Tapestry.isBlank(textValue))
|
182 | 0 |
return;
|
183 |
|
|
184 | 0 |
try
|
185 |
{ |
|
186 | 0 |
Date value = formatter.parse(textValue); |
187 |
|
|
188 | 0 |
setValue(value); |
189 |
} |
|
190 |
catch (ParseException ex)
|
|
191 |
{ |
|
192 |
} |
|
193 |
} |
|
194 |
|
|
195 |
} |
|
196 |
|
|
197 |
/**
|
|
198 |
* Create a list of quoted strings. The list is suitable for
|
|
199 |
* initializing a JavaScript array.
|
|
200 |
*/
|
|
201 | 0 |
private String makeStringList(String[] a, int offset, int length) |
202 |
{ |
|
203 | 0 |
StringBuffer b = new StringBuffer();
|
204 | 0 |
for (int i = offset; i < length; i++) |
205 |
{ |
|
206 |
// JavaScript is sensitive to some UNICODE characters. So for
|
|
207 |
// the sake of simplicity, we just escape everything
|
|
208 | 0 |
b.append('"');
|
209 | 0 |
char[] ch = a[i].toCharArray();
|
210 | 0 |
for (int j = 0; j < ch.length; j++) |
211 |
{ |
|
212 | 0 |
if (ch[j] < 128)
|
213 |
{ |
|
214 | 0 |
b.append(ch[j]); |
215 |
} |
|
216 |
else
|
|
217 |
{ |
|
218 | 0 |
b.append(escape(ch[j])); |
219 |
} |
|
220 |
} |
|
221 |
|
|
222 | 0 |
b.append('"');
|
223 | 0 |
if (i < length - 1)
|
224 |
{ |
|
225 | 0 |
b.append(", ");
|
226 |
} |
|
227 |
} |
|
228 | 0 |
return b.toString();
|
229 |
|
|
230 |
} |
|
231 |
|
|
232 |
/**
|
|
233 |
* Create an escaped Unicode character
|
|
234 |
* @param c
|
|
235 |
* @return The unicode character in escaped string form
|
|
236 |
*/
|
|
237 | 0 |
private static String escape(char c) |
238 |
{ |
|
239 | 0 |
StringBuffer b = new StringBuffer();
|
240 | 0 |
for (int i = 0; i < 4; i++) |
241 |
{ |
|
242 | 0 |
b.append(Integer.toHexString(c & 0x000F).toUpperCase()); |
243 | 0 |
c >>>= 4; |
244 |
} |
|
245 | 0 |
b.append("u\\");
|
246 | 0 |
return b.reverse().toString();
|
247 |
} |
|
248 |
|
|
249 |
} |
|
250 |
|
|