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

Last change on this file since 128 was 128, checked in by gav, 16 years ago

Installed templates with grails install-templates

File size: 9.3 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        if(!cp) {
144            return "<g:datePicker name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></g:datePicker>"
145        }
146        else {
147            if(!cp.editable) {
148                return "\${${domainInstance}?.${property.name}?.toString()}"
149            }
150            else {
151                def buf = new StringBuffer('<g:datePicker ')
152                if(cp.widget) buf << "widget=\"${cp.widget}\" "
153                if(cp.format) buf << "format=\"${cp.format}\" "
154                cp.attributes.each { k,v ->
155                    buf << "${k}=\"${v}\" "
156                }
157                buf << "name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" ${renderNoSelection(property)}></g:datePicker>"
158                return buf.toString()
159            }
160        }
161    }
162
163    private renderSelectTypeEditor(type,domainClass,property) {
164        if(!cp) {
165            return "<g:${type}Select name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\"></g:${type}Select>"
166        }
167        else {
168            def buf = new StringBuffer("<g:${type}Select ")
169            if(cp.widget) buf << "widget=\"${cp.widget}\" ";
170            cp.attributes.each { k,v ->
171                buf << "${k}=\"${v}\" "
172            }
173            buf << "name=\"${property.name}\" value=\"\${${domainInstance}?.${property.name}}\" ${renderNoSelection(property)}></g:${type}Select>"
174            return buf.toString()
175        }
176    }
177
178    private renderNoSelection(property) {
179        if(property.optional) {
180            if(property.manyToOne || property.oneToOne) {
181                return "noSelection=\"['null':'']\""                           
182            }
183            else {
184                return "noSelection=\"['':'']\""
185            }
186        }
187        return ""
188    }
189%>
Note: See TracBrowser for help on using the repository browser.