-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderEditor.template
281 lines (263 loc) · 12.7 KB
/
renderEditor.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<% if(property.type == Boolean.class || property.type == boolean.class)
out << renderBooleanEditor(domainClass,property)
else if(Number.class.isAssignableFrom(property.type) || (property.type.isPrimitive() && property.type != boolean.class))
out << renderNumberEditor(domainClass,property)
else if(property.type == String.class)
out << renderStringEditor(domainClass,property)
else if(property.type == Date.class || property.type == java.sql.Date.class || property.type == java.sql.Time.class)
out << renderDateEditor(domainClass,property)
else if(property.type == Calendar.class)
out << renderDateEditor(domainClass,property)
else if(org.joda.time.ReadableInstant.class.isAssignableFrom(property.type) || property.type == org.joda.time.LocalDateTime.class)
out << renderJodaDateTimeEditor(domainClass,property)
else if(property.type == org.joda.time.LocalDate.class)
out << renderJodaDateEditor(domainClass,property)
else if(property.type == org.joda.time.LocalTime.class)
out << renderJodaTimeEditor(domainClass,property)
else if(property.type == org.joda.time.Period.class || property.type == org.joda.time.Duration.class)
out << renderJodaPeriodEditor(domainClass,property)
else if(property.type == URL.class)
out << renderStringEditor(domainClass,property)
else if(property.isEnum())
out << renderEnumEditor(domainClass,property)
else if(property.type == TimeZone.class)
out << renderSelectTypeEditor("timeZone",domainClass,property)
else if(property.type == Locale.class)
out << renderSelectTypeEditor("locale",domainClass,property)
else if(property.type == Currency.class)
out << renderSelectTypeEditor("currency",domainClass,property)
else if(property.type==([] as Byte[]).class) //TODO: Bug in groovy means i have to do this :(
out << renderByteArrayEditor(domainClass,property)
else if(property.type==([] as byte[]).class) //TODO: Bug in groovy means i have to do this :(
out << renderByteArrayEditor(domainClass,property)
else if(property.manyToOne || property.oneToOne)
out << renderManyToOne(domainClass,property)
else if((property.oneToMany && !property.bidirectional) || (property.manyToMany && property.isOwningSide()))
out << renderManyToMany(domainClass, property)
else if(property.oneToMany)
out << renderOneToMany(domainClass,property)
private renderEnumEditor(domainClass,property) {
if(property.isEnum()) {
return "<g:select from=\"\${${property.type.name}?.values()}\" value=\"\${${domainInstance}?.${property.name}}\" name=\"${property.name}\" ${renderNoSelection(property)}></g:select>"
}
}
private renderStringEditor(domainClass, property) {
if(!cp) {
return "<input type=\"text\" name=\"${property.name}\" id=\"${property.name}\" value=\"\${fieldValue(bean:${domainInstance},field:'${property.name}')}\" />"
}
else {
if("textarea" == cp.widget || (cp.maxSize > 250 && !cp.password && !cp.inList)) {
return "<textarea rows=\"5\" cols=\"40\" name=\"${property.name}\">\${fieldValue(bean:${domainInstance}, field:'${property.name}')}</textarea>"
}
else {
if(cp.inList) {
def sb = new StringBuffer('<g:select ')
sb << "id=\"${property.name}\" name=\"${property.name}\" from=\"\${${domainInstance}.constraints.${property.name}.inList}\" value=\"\${${domainInstance}.${property?.name}}\" ${renderNoSelection(property)}>"
sb << '</g:select>'
return sb.toString()
}
else {
def sb = new StringBuffer('<input ')
cp.password ? sb << 'type="password" ' : sb << 'type="text" '
if(!cp.editable) sb << 'readonly="readonly" '
if(cp.maxSize) sb << "maxlength=\"${cp.maxSize}\" "
sb << "id=\"${property.name}\" name=\"${property.name}\" value=\"\${fieldValue(bean:${domainInstance},field:'${property.name}')}\"/>"
return sb.toString()
}
}
}
}
private renderByteArrayEditor(domainClass,property) {
return "<input type=\"file\" id=\"${property.name}\" name=\"${property.name}\" />"
}
private renderManyToOne(domainClass,property) {
if(property.association) {
return "<g:select optionKey=\"id\" from=\"\${${property.type.name}.list()}\" name=\"${property.name}.id\" value=\"\${${domainInstance}?.${property.name}?.id}\" ${renderNoSelection(property)}></g:select>"
}
}
private renderManyToMany(domainClass,property) {
def sw = new StringWriter()
def pw = new PrintWriter(sw)
pw.println "<g:select name=\"${property.name}\""
pw.println "from=\"\${${property.referencedDomainClass.fullName}.list()}\""
pw.println "size=\"5\" multiple=\"yes\" optionKey=\"id\""
pw.println "value=\"\${${domainInstance}?.${property.name}}\" />"
return sw.toString()
}
private renderOneToMany(domainClass,property) {
def sw = new StringWriter()
def pw = new PrintWriter(sw)
pw.println()
pw.println "<ul>"
pw.println "<g:each var=\"${property.name[0]}\" in=\"\${${domainInstance}?.${property.name}?}\">"
pw.println " <li><g:link controller=\"${property.referencedDomainClass.propertyName}\" action=\"show\" id=\"\${${property.name[0]}.id}\">\${${property.name[0]}?.encodeAsHTML()}</g:link></li>"
pw.println "</g:each>"
pw.println "</ul>"
pw.println "<g:link controller=\"${property.referencedDomainClass.propertyName}\" params=\"['${domainClass.propertyName}.id':${domainInstance}?.id]\" action=\"create\">Add ${property.referencedDomainClass.shortName}</g:link>"
return sw.toString()
}
private renderNumberEditor(domainClass,property) {
if(!cp) {
if(property.type == Byte.class) {
return "<g:select from=\"\${-128..127}\" name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></g:select>"
}
else {
return "<input type=\"text\" id=\"${property.name}\" name=\"${property.name}\" value=\"\${fieldValue(bean:${domainInstance},field:'${property.name}')}\" />"
}
}
else {
if(cp.range) {
return "<g:select from=\"\${${cp.range.from}..${cp.range.to}}\" id=\"${property.name}\" name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" ${renderNoSelection(property)}></g:select>"
}
else if(cp.inList) {
def sb = new StringBuffer('<g:select ')
sb << "id=\"${property.name}\" name=\"${property.name}\" from=\"\${${domainClass.propertyName}.constraints.${property.name}.inList}\" value=\"\${${domainClass.propertyName}.${property?.name}}\" ${renderNoSelection(property)}>"
sb << '</g:select>'
return sb.toString()
}
else {
return "<input type=\"text\" id=\"${property.name}\" name=\"${property.name}\" value=\"\${fieldValue(bean:${domainInstance},field:'${property.name}')}\" />"
}
}
}
private renderBooleanEditor(domainClass,property) {
if(!cp) {
return "<g:checkBox name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></g:checkBox>"
}
else {
def buf = new StringBuffer('<g:checkBox ')
if(cp.widget) buf << "widget=\"${cp.widget}\"";
buf << "name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" "
cp.attributes.each { k,v ->
buf << "${k}=\"${v}\" "
}
buf << '></g:checkBox>'
return buf.toString()
}
}
private renderDateEditor(domainClass,property) {
if(!cp) {
return "<g:datePicker name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></g:datePicker>"
}
else {
if(!cp.editable) {
return "\${${domainInstance}?.${property.name}?.toString()}"
}
else {
def buf = new StringBuffer('<g:datePicker ')
if(cp.widget) buf << "widget=\"${cp.widget}\" "
if(cp.format) buf << "format=\"${cp.format}\" "
cp.attributes.each { k,v ->
buf << "${k}=\"${v}\" "
}
buf << "name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" ${renderNoSelection(property)}></g:datePicker>"
return buf.toString()
}
}
}
private renderJodaDateTimeEditor(domainClass,property) {
if(!cp) {
return "<joda:dateTimePicker name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></joda:dateTimePicker>"
}
else {
if(!cp.editable) {
return "\${${domainInstance}?.${property.name}?.toString()}"
}
else {
def buf = new StringBuffer('<joda:dateTimePicker ')
if(cp.widget) buf << "widget=\"${cp.widget}\" "
if(cp.format) buf << "format=\"${cp.format}\" "
cp.attributes.each { k,v ->
buf << "${k}=\"${v}\" "
}
buf << "name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" ${renderNoSelection(property)}></joda:dateTimePicker>"
return buf.toString()
}
}
}
private renderJodaDateEditor(domainClass,property) {
if(!cp) {
return "<joda:datePicker name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></joda:datePicker>"
}
else {
if(!cp.editable) {
return "\${${domainInstance}?.${property.name}?.toString()}"
}
else {
def buf = new StringBuffer('<joda:datePicker ')
if(cp.widget) buf << "widget=\"${cp.widget}\" "
if(cp.format) buf << "format=\"${cp.format}\" "
cp.attributes.each { k,v ->
buf << "${k}=\"${v}\" "
}
buf << "name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" ${renderNoSelection(property)}></joda:datePicker>"
return buf.toString()
}
}
}
private renderJodaTimeEditor(domainClass,property) {
if(!cp) {
return "<joda:timePicker name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></joda:timePicker>"
}
else {
if(!cp.editable) {
return "\${${domainInstance}?.${property.name}?.toString()}"
}
else {
def buf = new StringBuffer('<joda:timePicker ')
if(cp.widget) buf << "widget=\"${cp.widget}\" "
if(cp.format) buf << "format=\"${cp.format}\" "
cp.attributes.each { k,v ->
buf << "${k}=\"${v}\" "
}
buf << "name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" ${renderNoSelection(property)}></joda:timePicker>"
return buf.toString()
}
}
}
private renderJodaPeriodEditor(domainClass,property) {
if(!cp) {
return "<joda:periodPicker name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></joda:periodPicker>"
}
else {
if(!cp.editable) {
return "\${${domainInstance}?.${property.name}?.toString()}"
}
else {
def buf = new StringBuffer('<joda:periodPicker ')
if(cp.widget) buf << "widget=\"${cp.widget}\" "
if(cp.format) buf << "format=\"${cp.format}\" "
cp.attributes.each { k,v ->
buf << "${k}=\"${v}\" "
}
buf << "name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" ${renderNoSelection(property)}></joda:periodPicker>"
return buf.toString()
}
}
}
private renderSelectTypeEditor(type,domainClass,property) {
if(!cp) {
return "<g:${type}Select name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></g:${type}Select>"
}
else {
def buf = new StringBuffer("<g:${type}Select ")
if(cp.widget) buf << "widget=\"${cp.widget}\" ";
cp.attributes.each { k,v ->
buf << "${k}=\"${v}\" "
}
buf << "name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" ${renderNoSelection(property)}></g:${type}Select>"
return buf.toString()
}
}
private renderNoSelection(property) {
if(property.optional) {
if(property.manyToOne || property.oneToOne) {
return "noSelection=\"['null':'']\""
}
else {
return "noSelection=\"['':'']\""
}
}
return ""
}
%>