source: trunk/src/templates/scaffolding/renderEditor.template @ 398

Last change on this file since 398 was 158, checked in by gav, 15 years ago

Update templates to Grails-1.1.1

File size: 9.4 KB
Line 
1<%  if(property.type == Boolean.class || property.type == boolean.class)
2        out << renderBooleanEditor(domainClass,property)       
3    else if(Number.class.isAssignableFrom(property.type) || (property.type.isPrimitive() && property.type != boolean.class))
4        out << renderNumberEditor(domainClass,property)
5    else if(property.type == String.class)
6        out << renderStringEditor(domainClass,property)
7    else if(property.type == Date.class || property.type == java.sql.Date.class || property.type == java.sql.Time.class)
8        out << renderDateEditor(domainClass,property)
9    else if(property.type == Calendar.class)
10        out << renderDateEditor(domainClass,property) 
11    else if(property.type == URL.class)
12        out << renderStringEditor(domainClass,property)
13    else if(property.isEnum())
14        out << renderEnumEditor(domainClass,property)
15    else if(property.type == TimeZone.class)
16        out << renderSelectTypeEditor("timeZone",domainClass,property)
17    else if(property.type == Locale.class)
18        out << renderSelectTypeEditor("locale",domainClass,property)
19    else if(property.type == Currency.class)
20        out << renderSelectTypeEditor("currency",domainClass,property)
21    else if(property.type==([] as Byte[]).class) //TODO: Bug in groovy means i have to do this :(
22        out << renderByteArrayEditor(domainClass,property)
23    else if(property.type==([] as byte[]).class) //TODO: Bug in groovy means i have to do this :(
24        out << renderByteArrayEditor(domainClass,property)               
25    else if(property.manyToOne || property.oneToOne)
26        out << renderManyToOne(domainClass,property)
27    else if((property.oneToMany && !property.bidirectional) || (property.manyToMany && property.isOwningSide()))
28        out << renderManyToMany(domainClass, property)
29    else if(property.oneToMany)
30        out << renderOneToMany(domainClass,property)
31
32    private renderEnumEditor(domainClass,property) {
33        if(property.isEnum()) {
34            return "<g:select  from=\"\${${property.type.name}?.values()}\" value=\"\${${domainInstance}?.${property.name}}\" name=\"${property.name}\" ${renderNoSelection(property)}></g:select>"
35        }
36    }
37
38    private renderStringEditor(domainClass, property) {
39        if(!cp) {
40            return "<input type=\"text\" name=\"${property.name}\" id=\"${property.name}\" value=\"\${fieldValue(bean:${domainInstance},field:'${property.name}')}\" />"
41        }
42        else {
43            if("textarea" == cp.widget || (cp.maxSize > 250 && !cp.password && !cp.inList)) {
44                return "<textarea rows=\"5\" cols=\"40\" name=\"${property.name}\">\${fieldValue(bean:${domainInstance}, field:'${property.name}')}</textarea>"
45            }
46             else {
47                if(cp.inList) {
48                    def sb = new StringBuffer('<g:select ')
49                    sb << "id=\"${property.name}\" name=\"${property.name}\" from=\"\${${domainInstance}.constraints.${property.name}.inList}\" value=\"\${${domainInstance}.${property?.name}}\" ${renderNoSelection(property)}>"
50                    sb << '</g:select>'
51                    return sb.toString()
52                }
53                else {
54                    def sb = new StringBuffer('<input ')
55                    cp.password ? sb << 'type="password" ' : sb << 'type="text" '
56                    if(!cp.editable) sb << 'readonly="readonly" '
57                    if(cp.maxSize) sb << "maxlength=\"${cp.maxSize}\" "
58                    sb << "id=\"${property.name}\" name=\"${property.name}\" value=\"\${fieldValue(bean:${domainInstance},field:'${property.name}')}\"/>"
59                    return sb.toString()
60                }
61            }
62        }
63    }
64
65    private renderByteArrayEditor(domainClass,property) {
66        return "<input type=\"file\" id=\"${property.name}\" name=\"${property.name}\" />"
67    }
68
69    private renderManyToOne(domainClass,property) {
70        if(property.association) {
71            return "<g:select optionKey=\"id\" from=\"\${${property.type.name}.list()}\" name=\"${property.name}.id\" value=\"\${${domainInstance}?.${property.name}?.id}\" ${renderNoSelection(property)}></g:select>"
72        }
73    }
74
75    private renderManyToMany(domainClass,property) {
76        def sw = new StringWriter()
77        def pw = new PrintWriter(sw)
78
79        pw.println "<g:select name=\"${property.name}\""
80        pw.println "from=\"\${${property.referencedDomainClass.fullName}.list()}\""
81        pw.println "size=\"5\" multiple=\"yes\" optionKey=\"id\""
82        pw.println "value=\"\${${domainInstance}?.${property.name}}\" />"
83
84        return sw.toString()         
85    }
86
87    private renderOneToMany(domainClass,property) {
88        def sw = new StringWriter()
89        def pw = new PrintWriter(sw)
90        pw.println()
91        pw.println "<ul>"
92        pw.println "<g:each var=\"${property.name[0]}\" in=\"\${${domainInstance}?.${property.name}?}\">"
93        pw.println "    <li><g:link controller=\"${property.referencedDomainClass.propertyName}\" action=\"show\" id=\"\${${property.name[0]}.id}\">\${${property.name[0]}?.encodeAsHTML()}</g:link></li>"
94        pw.println "</g:each>"
95        pw.println "</ul>"
96        pw.println "<g:link controller=\"${property.referencedDomainClass.propertyName}\" params=\"['${domainClass.propertyName}.id':${domainInstance}?.id]\" action=\"create\">Add ${property.referencedDomainClass.shortName}</g:link>"
97        return sw.toString()
98    }
99
100    private renderNumberEditor(domainClass,property) {
101        if(!cp) {
102            if(property.type == Byte.class) {
103                return "<g:select from=\"\${-128..127}\" name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></g:select>"
104            }
105            else {
106                return "<input type=\"text\" id=\"${property.name}\" name=\"${property.name}\" value=\"\${fieldValue(bean:${domainInstance},field:'${property.name}')}\" />"
107            }
108        }
109        else {
110            if(cp.range) {
111                return "<g:select from=\"\${${cp.range.from}..${cp.range.to}}\" id=\"${property.name}\" name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" ${renderNoSelection(property)}></g:select>"
112            }
113            else if(cp.inList) {
114                def sb = new StringBuffer('<g:select ')
115                sb << "id=\"${property.name}\" name=\"${property.name}\" from=\"\${${domainClass.propertyName}.constraints.${property.name}.inList}\" value=\"\${${domainClass.propertyName}.${property?.name}}\" ${renderNoSelection(property)}>"
116                sb << '</g:select>'
117                return sb.toString()
118            }           
119            else {
120                return "<input type=\"text\" id=\"${property.name}\" name=\"${property.name}\" value=\"\${fieldValue(bean:${domainInstance},field:'${property.name}')}\" />"
121            }
122        }
123     }
124
125    private renderBooleanEditor(domainClass,property) {
126        if(!cp) {
127            return "<g:checkBox name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></g:checkBox>"
128        }
129        else {
130            def buf = new StringBuffer('<g:checkBox ')
131            if(cp.widget) buf << "widget=\"${cp.widget}\"";
132
133            buf << "name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" "
134            cp.attributes.each { k,v ->
135                buf << "${k}=\"${v}\" "
136            }
137            buf << '></g:checkBox>'
138            return buf.toString()
139        }
140    }
141
142    private renderDateEditor(domainClass,property) {
143        def precision = property.type == java.sql.Date ? 'day' : 'minute';
144        if(!cp) {
145            return "<g:datePicker name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" precision=\"${precision}\"></g:datePicker>"
146        }
147        else {
148            if(!cp.editable) {
149                return "\${${domainInstance}?.${property.name}?.toString()}"
150            }
151            else {
152                def buf = new StringBuffer('<g:datePicker ')
153                if(cp.widget) buf << "widget=\"${cp.widget}\" "
154                if(cp.format) buf << "format=\"${cp.format}\" "
155                cp.attributes.each { k,v ->
156                    buf << "${k}=\"${v}\" "
157                }
158                buf << "name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" precision=\"${precision}\" ${renderNoSelection(property)}></g:datePicker>"
159                return buf.toString()
160            }
161        }
162    }
163
164    private renderSelectTypeEditor(type,domainClass,property) {
165        if(!cp) {
166            return "<g:${type}Select name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></g:${type}Select>"
167        }
168        else {
169            def buf = new StringBuffer("<g:${type}Select ")
170            if(cp.widget) buf << "widget=\"${cp.widget}\" ";
171            cp.attributes.each { k,v ->
172                buf << "${k}=\"${v}\" "
173            }
174            buf << "name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" ${renderNoSelection(property)}></g:${type}Select>"
175            return buf.toString()
176        }
177    }
178
179    private renderNoSelection(property) {
180        if(property.optional) {
181            if(property.manyToOne || property.oneToOne) {
182                return "noSelection=\"['null':'']\""                           
183            }
184            else {
185                return "noSelection=\"['':'']\""
186            }
187        }
188        return ""
189    }
190%>
Note: See TracBrowser for help on using the repository browser.