1 | |
package org.apache.tapestry.dojo.form; |
2 | |
|
3 | |
import org.apache.tapestry.IMarkupWriter; |
4 | |
import org.apache.tapestry.IRequestCycle; |
5 | |
import org.apache.tapestry.IScript; |
6 | |
import org.apache.tapestry.TapestryUtils; |
7 | |
import org.apache.tapestry.form.TranslatedField; |
8 | |
import org.apache.tapestry.form.TranslatedFieldSupport; |
9 | |
import org.apache.tapestry.form.ValidatableFieldSupport; |
10 | |
import org.apache.tapestry.json.JSONLiteral; |
11 | |
import org.apache.tapestry.json.JSONObject; |
12 | |
import org.apache.tapestry.valid.ValidatorException; |
13 | |
|
14 | |
import java.util.*; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | 0 | public abstract class GTimePicker extends AbstractFormWidget implements TranslatedField |
21 | |
{ |
22 | |
|
23 | |
|
24 | |
|
25 | |
static final int TIME_SEGMENT_LENGTH = 48; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public abstract Object getValue(); |
32 | |
|
33 | |
public abstract void setValue(Object value); |
34 | |
|
35 | |
public abstract boolean isDisabled(); |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
protected void renderFormWidget(IMarkupWriter writer, IRequestCycle cycle) |
41 | |
{ |
42 | 0 | String value = getTranslatedFieldSupport().format(this, getValue()); |
43 | |
|
44 | 0 | renderDelegatePrefix(writer, cycle); |
45 | |
|
46 | 0 | writer.beginEmpty("input"); |
47 | 0 | writer.attribute("type", "text"); |
48 | 0 | writer.attribute("autocomplete", "off"); |
49 | 0 | writer.attribute("name", getName()); |
50 | |
|
51 | 0 | if (isDisabled()) |
52 | 0 | writer.attribute("disabled", "disabled"); |
53 | |
|
54 | 0 | if (value != null) |
55 | 0 | writer.attribute("value", value); |
56 | |
|
57 | 0 | renderIdAttribute(writer, cycle); |
58 | 0 | renderDelegateAttributes(writer, cycle); |
59 | |
|
60 | 0 | getTranslatedFieldSupport().renderContributions(this, writer, cycle); |
61 | 0 | getValidatableFieldSupport().renderContributions(this, writer, cycle); |
62 | |
|
63 | 0 | renderInformalParameters(writer, cycle); |
64 | |
|
65 | 0 | writer.closeTag(); |
66 | |
|
67 | 0 | renderDelegateSuffix(writer, cycle); |
68 | |
|
69 | |
|
70 | |
|
71 | 0 | Locale locale = getPage().getLocale(); |
72 | |
|
73 | 0 | GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(getPage().getLocale()); |
74 | 0 | cal.set(Calendar.HOUR, 0); |
75 | 0 | cal.set(Calendar.AM_PM, Calendar.AM); |
76 | |
|
77 | 0 | StringBuffer optStr = new StringBuffer("["); |
78 | 0 | int selectedIndex = -1; |
79 | |
|
80 | 0 | for(int i=0, hour=0; i < TIME_SEGMENT_LENGTH; i++) |
81 | |
{ |
82 | 0 | if (i != 0) |
83 | |
{ |
84 | 0 | optStr.append(","); |
85 | |
} |
86 | |
|
87 | 0 | if (i == 24) |
88 | |
{ |
89 | 0 | hour = 0; |
90 | 0 | cal.set(Calendar.AM_PM, Calendar.PM); |
91 | |
} |
92 | |
|
93 | 0 | cal.set(Calendar.HOUR, hour); |
94 | 0 | cal.set(Calendar.MINUTE, (i % 2 > 0) ? 30 : 0); |
95 | |
|
96 | 0 | String option = getTranslator().format(this, locale, cal.getTime()); |
97 | |
|
98 | 0 | optStr.append("\"").append(option).append("\""); |
99 | |
|
100 | 0 | if (selectedIndex < 0 && value != null && value.equals(option)) |
101 | |
{ |
102 | 0 | selectedIndex = i; |
103 | |
} |
104 | |
|
105 | 0 | if (i % 2 > 0) |
106 | |
{ |
107 | 0 | hour++; |
108 | |
} |
109 | |
} |
110 | |
|
111 | 0 | optStr.append("]"); |
112 | |
|
113 | |
|
114 | |
|
115 | 0 | JSONObject json = new JSONObject(); |
116 | 0 | json.put("inputNodeId", getClientId()); |
117 | 0 | json.put("optionValues", new JSONLiteral(optStr.toString())); |
118 | |
|
119 | 0 | if (selectedIndex > -1) |
120 | |
{ |
121 | 0 | json.put("selectedIndex", selectedIndex); |
122 | |
} |
123 | |
|
124 | 0 | Map parms = new HashMap(); |
125 | 0 | parms.put("clientId", getClientId()); |
126 | 0 | parms.put("props", json.toString()); |
127 | 0 | parms.put("widget", this); |
128 | |
|
129 | 0 | getScript().execute(this, cycle, TapestryUtils.getPageRenderSupport(cycle, this), parms); |
130 | 0 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
protected void rewindFormWidget(IMarkupWriter writer, IRequestCycle cycle) |
136 | |
{ |
137 | 0 | String value = cycle.getParameter(getName()); |
138 | |
|
139 | |
try |
140 | |
{ |
141 | 0 | Object translated = getTranslatedFieldSupport().parse(this, value); |
142 | |
|
143 | 0 | getValidatableFieldSupport().validate(this, writer, cycle, translated); |
144 | |
|
145 | 0 | setValue(translated); |
146 | |
} |
147 | 0 | catch (ValidatorException e) |
148 | |
{ |
149 | 0 | getForm().getDelegate().record(e); |
150 | 0 | } |
151 | 0 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public boolean isRequired() |
157 | |
{ |
158 | 0 | return getValidatableFieldSupport().isRequired(this); |
159 | |
} |
160 | |
|
161 | |
|
162 | |
public abstract IScript getScript(); |
163 | |
|
164 | |
|
165 | |
public abstract TranslatedFieldSupport getTranslatedFieldSupport(); |
166 | |
|
167 | |
|
168 | |
public abstract ValidatableFieldSupport getValidatableFieldSupport(); |
169 | |
|
170 | |
} |