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