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.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 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| public abstract class DatePicker extends AbstractValidatableField |
48 |
| { |
49 |
| public abstract Date getValue(); |
50 |
| |
51 |
| public abstract void setValue(Date 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 SYM_NAME = "name"; |
62 |
| |
63 |
| private static final String SYM_FORMNAME = "formName"; |
64 |
| |
65 |
| private static final String SYM_MONTHNAMES = "monthNames"; |
66 |
| |
67 |
| private static final String SYM_SHORT_MONTHNAMES = "shortMonthNames"; |
68 |
| |
69 |
| private static final String SYM_WEEKDAYNAMES = "weekDayNames"; |
70 |
| |
71 |
| private static final String SYM_SHORT_WEEKDAYNAMES = "shortWeekDayNames"; |
72 |
| |
73 |
| private static final String SYM_FIRSTDAYINWEEK = "firstDayInWeek"; |
74 |
| |
75 |
| private static final String SYM_MINDAYSINFIRSTWEEK = "minimalDaysInFirstWeek"; |
76 |
| |
77 |
| private static final String SYM_FORMAT = "format"; |
78 |
| |
79 |
| private static final String SYM_INCL_WEEK = "includeWeek"; |
80 |
| |
81 |
| private static final String SYM_VALUE = "value"; |
82 |
| |
83 |
| private static final String SYM_BUTTONONCLICKHANDLER = "buttonOnclickHandler"; |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| public abstract IScriptSource getScriptSource(); |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
0
| protected void finishLoad()
|
96 |
| { |
97 |
0
| super.finishLoad();
|
98 |
| |
99 |
0
| IScriptSource source = getScriptSource();
|
100 |
| |
101 |
0
| Resource location = getSpecification().getSpecificationLocation().getRelativeResource(
|
102 |
| "DatePicker.script"); |
103 |
| |
104 |
0
| _script = source.getScript(location);
|
105 |
| } |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
0
| public void render(IMarkupWriter writer, IRequestCycle cycle, String value)
|
112 |
| { |
113 |
0
| PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
|
114 |
| |
115 |
0
| boolean disabled = isDisabled();
|
116 |
0
| DateTranslator translator = (DateTranslator) getTranslator();
|
117 |
0
| Locale locale = getPage().getLocale();
|
118 |
0
| SimpleDateFormat format = translator.getDateFormat(locale);
|
119 |
| |
120 |
0
| DateFormatSymbols dfs = format.getDateFormatSymbols();
|
121 |
0
| Calendar cal = Calendar.getInstance(locale);
|
122 |
| |
123 |
0
| String name = getName();
|
124 |
| |
125 |
0
| Map symbols = new HashMap();
|
126 |
| |
127 |
0
| symbols.put(SYM_NAME, name);
|
128 |
0
| symbols.put(SYM_FORMAT, format.toPattern());
|
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, getForm().getName());
|
138 |
0
| symbols.put(SYM_VALUE, getValue());
|
139 |
| |
140 |
0
| _script.execute(cycle, pageRenderSupport, symbols);
|
141 |
| |
142 |
0
| renderDelegatePrefix(writer, cycle);
|
143 |
| |
144 |
0
| writer.beginEmpty("input");
|
145 |
0
| writer.attribute("type", "text");
|
146 |
0
| writer.attribute("name", name);
|
147 |
0
| writer.attribute("value", value);
|
148 |
0
| writer.attribute("title", format.toLocalizedPattern());
|
149 |
| |
150 |
0
| if (disabled)
|
151 |
0
| writer.attribute("disabled", "disabled");
|
152 |
| |
153 |
0
| renderIdAttribute(writer, cycle);
|
154 |
| |
155 |
0
| renderDelegateAttributes(writer, cycle);
|
156 |
| |
157 |
0
| renderContributions(writer, cycle);
|
158 |
| |
159 |
0
| renderInformalParameters(writer, cycle);
|
160 |
| |
161 |
0
| writer.printRaw(" ");
|
162 |
| |
163 |
0
| if (!disabled)
|
164 |
| { |
165 |
0
| writer.begin("a");
|
166 |
0
| writer.attribute("href", (String) symbols.get(SYM_BUTTONONCLICKHANDLER));
|
167 |
| } |
168 |
| |
169 |
0
| IAsset icon = getIcon();
|
170 |
| |
171 |
0
| writer.beginEmpty("img");
|
172 |
0
| writer.attribute("src", icon.buildURL(cycle));
|
173 |
0
| writer.attribute("border", 0);
|
174 |
| |
175 |
0
| if (!disabled)
|
176 |
0
| writer.end();
|
177 |
| |
178 |
0
| renderDelegateSuffix(writer, cycle);
|
179 |
| } |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
0
| public Object readValue()
|
185 |
| { |
186 |
0
| return getValue();
|
187 |
| } |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
0
| public void writeValue(Object value)
|
193 |
| { |
194 |
0
| setValue((Date) value);
|
195 |
| } |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
0
| private String makeStringList(String[] a, int offset, int length)
|
201 |
| { |
202 |
0
| StringBuffer b = new StringBuffer();
|
203 |
0
| for (int i = offset; i < length; i++)
|
204 |
| { |
205 |
| |
206 |
| |
207 |
0
| b.append('"');
|
208 |
0
| char[] ch = a[i].toCharArray();
|
209 |
0
| for (int j = 0; j < ch.length; j++)
|
210 |
| { |
211 |
0
| if (ch[j] < 128)
|
212 |
| { |
213 |
0
| b.append(ch[j]);
|
214 |
| } |
215 |
| else |
216 |
| { |
217 |
0
| b.append(escape(ch[j]));
|
218 |
| } |
219 |
| } |
220 |
| |
221 |
0
| b.append('"');
|
222 |
0
| if (i < length - 1)
|
223 |
| { |
224 |
0
| b.append(", ");
|
225 |
| } |
226 |
| } |
227 |
0
| return b.toString();
|
228 |
| |
229 |
| } |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
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 |
| } |