|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.tapestry.form; |
|
16 |
| |
|
17 |
| import java.text.DateFormatSymbols; |
|
18 |
| import java.text.SimpleDateFormat; |
|
19 |
| import java.util.Calendar; |
|
20 |
| import java.util.Date; |
|
21 |
| import java.util.HashMap; |
|
22 |
| import java.util.Locale; |
|
23 |
| import java.util.Map; |
|
24 |
| |
|
25 |
| import org.apache.tapestry.IAsset; |
|
26 |
| import org.apache.tapestry.IMarkupWriter; |
|
27 |
| import org.apache.tapestry.IRequestCycle; |
|
28 |
| import org.apache.tapestry.IScript; |
|
29 |
| import org.apache.tapestry.PageRenderSupport; |
|
30 |
| import org.apache.tapestry.TapestryUtils; |
|
31 |
| import org.apache.tapestry.form.translator.DateTranslator; |
|
32 |
| import org.apache.tapestry.valid.ValidatorException; |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| public abstract class DatePicker extends AbstractFormComponent implements TranslatedField |
|
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 static final String SYM_NAME = "name"; |
|
61 |
| |
|
62 |
| private static final String SYM_FORMNAME = "formName"; |
|
63 |
| |
|
64 |
| private static final String SYM_MONTHNAMES = "monthNames"; |
|
65 |
| |
|
66 |
| private static final String SYM_SHORT_MONTHNAMES = "shortMonthNames"; |
|
67 |
| |
|
68 |
| private static final String SYM_WEEKDAYNAMES = "weekDayNames"; |
|
69 |
| |
|
70 |
| private static final String SYM_SHORT_WEEKDAYNAMES = "shortWeekDayNames"; |
|
71 |
| |
|
72 |
| private static final String SYM_FIRSTDAYINWEEK = "firstDayInWeek"; |
|
73 |
| |
|
74 |
| private static final String SYM_MINDAYSINFIRSTWEEK = "minimalDaysInFirstWeek"; |
|
75 |
| |
|
76 |
| private static final String SYM_FORMAT = "format"; |
|
77 |
| |
|
78 |
| private static final String SYM_INCL_WEEK = "includeWeek"; |
|
79 |
| |
|
80 |
| private static final String SYM_CLEAR_BUTTON_LABEL = "clearButtonLabel"; |
|
81 |
| |
|
82 |
| private static final String SYM_VALUE = "value"; |
|
83 |
| |
|
84 |
| private static final String SYM_BUTTONONCLICKHANDLER = "buttonOnclickHandler"; |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| public abstract IScript getScript(); |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
0
| protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
|
98 |
| { |
|
99 |
0
| PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
|
|
100 |
| |
|
101 |
0
| boolean disabled = isDisabled();
|
|
102 |
0
| DateTranslator translator = (DateTranslator) getTranslator();
|
|
103 |
0
| Locale locale = getPage().getLocale();
|
|
104 |
0
| SimpleDateFormat format = translator.getDateFormat(locale);
|
|
105 |
| |
|
106 |
0
| DateFormatSymbols dfs = format.getDateFormatSymbols();
|
|
107 |
0
| Calendar cal = Calendar.getInstance(locale);
|
|
108 |
| |
|
109 |
0
| String name = getName();
|
|
110 |
| |
|
111 |
0
| String value = getTranslatedFieldSupport().format(this, getValue());
|
|
112 |
| |
|
113 |
0
| Map symbols = new HashMap();
|
|
114 |
| |
|
115 |
0
| symbols.put(SYM_NAME, name);
|
|
116 |
0
| symbols.put(SYM_FORMAT, format.toPattern());
|
|
117 |
0
| symbols.put(SYM_INCL_WEEK, getIncludeWeek() ? Boolean.TRUE : Boolean.FALSE);
|
|
118 |
| |
|
119 |
0
| symbols.put(SYM_MONTHNAMES, makeStringList(dfs.getMonths(), 0, 12));
|
|
120 |
0
| symbols.put(SYM_SHORT_MONTHNAMES, makeStringList(dfs.getShortMonths(), 0, 12));
|
|
121 |
0
| symbols.put(SYM_WEEKDAYNAMES, makeStringList(dfs.getWeekdays(), 1, 8));
|
|
122 |
0
| symbols.put(SYM_SHORT_WEEKDAYNAMES, makeStringList(dfs.getShortWeekdays(), 1, 8));
|
|
123 |
0
| symbols.put(SYM_FIRSTDAYINWEEK, new Integer(cal.getFirstDayOfWeek() - 1));
|
|
124 |
0
| symbols.put(SYM_MINDAYSINFIRSTWEEK, new Integer(cal.getMinimalDaysInFirstWeek()));
|
|
125 |
0
| symbols.put(SYM_CLEAR_BUTTON_LABEL, getMessages().getMessage("clear"));
|
|
126 |
0
| symbols.put(SYM_FORMNAME, getForm().getName());
|
|
127 |
0
| symbols.put(SYM_VALUE, getValue());
|
|
128 |
| |
|
129 |
0
| getScript().execute(cycle, pageRenderSupport, symbols);
|
|
130 |
| |
|
131 |
0
| renderDelegatePrefix(writer, cycle);
|
|
132 |
| |
|
133 |
0
| writer.beginEmpty("input");
|
|
134 |
0
| writer.attribute("type", "text");
|
|
135 |
0
| writer.attribute("name", name);
|
|
136 |
0
| writer.attribute("value", value);
|
|
137 |
0
| writer.attribute("title", format.toLocalizedPattern());
|
|
138 |
| |
|
139 |
0
| if (disabled)
|
|
140 |
0
| writer.attribute("disabled", "disabled");
|
|
141 |
| |
|
142 |
0
| renderIdAttribute(writer, cycle);
|
|
143 |
| |
|
144 |
0
| renderDelegateAttributes(writer, cycle);
|
|
145 |
| |
|
146 |
0
| getTranslatedFieldSupport().renderContributions(this, writer, cycle);
|
|
147 |
0
| getValidatableFieldSupport().renderContributions(this, writer, cycle);
|
|
148 |
| |
|
149 |
0
| renderInformalParameters(writer, cycle);
|
|
150 |
| |
|
151 |
0
| writer.printRaw(" ");
|
|
152 |
| |
|
153 |
0
| if (!disabled)
|
|
154 |
| { |
|
155 |
0
| writer.begin("a");
|
|
156 |
0
| writer.attribute("href", (String) symbols.get(SYM_BUTTONONCLICKHANDLER));
|
|
157 |
| } |
|
158 |
| |
|
159 |
0
| IAsset icon = getIcon();
|
|
160 |
| |
|
161 |
0
| writer.beginEmpty("img");
|
|
162 |
0
| writer.attribute("src", icon.buildURL());
|
|
163 |
0
| writer.attribute("border", 0);
|
|
164 |
| |
|
165 |
0
| if (!disabled)
|
|
166 |
0
| writer.end();
|
|
167 |
| |
|
168 |
0
| renderDelegateSuffix(writer, cycle);
|
|
169 |
| } |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
0
| protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
|
176 |
| { |
|
177 |
0
| String value = cycle.getParameter(getName());
|
|
178 |
| |
|
179 |
0
| try
|
|
180 |
| { |
|
181 |
0
| Date date = (Date) getTranslatedFieldSupport().parse(this, value);
|
|
182 |
| |
|
183 |
0
| getValidatableFieldSupport().validate(this, writer, cycle, date);
|
|
184 |
| |
|
185 |
0
| setValue(date);
|
|
186 |
| } |
|
187 |
| catch (ValidatorException e) |
|
188 |
| { |
|
189 |
0
| getForm().getDelegate().record(e);
|
|
190 |
| } |
|
191 |
| } |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
0
| private String makeStringList(String[] a, int offset, int length)
|
|
197 |
| { |
|
198 |
0
| StringBuffer b = new StringBuffer();
|
|
199 |
0
| for (int i = offset; i < length; i++)
|
|
200 |
| { |
|
201 |
| |
|
202 |
| |
|
203 |
0
| b.append('"');
|
|
204 |
0
| char[] ch = a[i].toCharArray();
|
|
205 |
0
| for (int j = 0; j < ch.length; j++)
|
|
206 |
| { |
|
207 |
0
| if (ch[j] < 128)
|
|
208 |
| { |
|
209 |
0
| b.append(ch[j]);
|
|
210 |
| } |
|
211 |
| else |
|
212 |
| { |
|
213 |
0
| b.append(escape(ch[j]));
|
|
214 |
| } |
|
215 |
| } |
|
216 |
| |
|
217 |
0
| b.append('"');
|
|
218 |
0
| if (i < length - 1)
|
|
219 |
| { |
|
220 |
0
| b.append(", ");
|
|
221 |
| } |
|
222 |
| } |
|
223 |
0
| return b.toString();
|
|
224 |
| |
|
225 |
| } |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
0
| private static String escape(char c)
|
|
234 |
| { |
|
235 |
0
| StringBuffer b = new StringBuffer();
|
|
236 |
0
| for (int i = 0; i < 4; i++)
|
|
237 |
| { |
|
238 |
0
| b.append(Integer.toHexString(c & 0x000F).toUpperCase());
|
|
239 |
0
| c >>>= 4;
|
|
240 |
| } |
|
241 |
0
| b.append("u\\");
|
|
242 |
0
| return b.reverse().toString();
|
|
243 |
| } |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| public abstract ValidatableFieldSupport getValidatableFieldSupport(); |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| public abstract TranslatedFieldSupport getTranslatedFieldSupport(); |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
0
| public boolean isRequired()
|
|
259 |
| { |
|
260 |
0
| return getValidatableFieldSupport().isRequired(this);
|
|
261 |
| } |
|
262 |
| } |