1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.tags; |
17 | |
|
18 | |
import static org.seasar.cubby.internal.util.LogMessages.format; |
19 | |
import static org.seasar.cubby.tags.TagUtils.addCSSClassName; |
20 | |
import static org.seasar.cubby.tags.TagUtils.contains; |
21 | |
import static org.seasar.cubby.tags.TagUtils.errors; |
22 | |
import static org.seasar.cubby.tags.TagUtils.formValue; |
23 | |
import static org.seasar.cubby.tags.TagUtils.getFormWrapper; |
24 | |
import static org.seasar.cubby.tags.TagUtils.multipleFormValues; |
25 | |
import static org.seasar.cubby.tags.TagUtils.toAttr; |
26 | |
|
27 | |
import java.io.IOException; |
28 | |
import java.util.Collections; |
29 | |
import java.util.HashSet; |
30 | |
import java.util.Map; |
31 | |
import java.util.Set; |
32 | |
|
33 | |
import javax.servlet.jsp.JspContext; |
34 | |
import javax.servlet.jsp.JspException; |
35 | |
import javax.servlet.jsp.JspTagException; |
36 | |
import javax.servlet.jsp.JspWriter; |
37 | |
|
38 | |
import org.seasar.cubby.action.ActionErrors; |
39 | |
import org.seasar.cubby.controller.FormWrapper; |
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 | 19 | public class InputTag extends DynamicAttributesSimpleTagSupport { |
70 | |
|
71 | |
private static final Set<String> MULTIPLE_VALUE_TYPES; |
72 | |
static { |
73 | 1 | final Set<String> set = new HashSet<String>(); |
74 | 1 | set.add("checkbox"); |
75 | 1 | set.add("radio"); |
76 | 1 | MULTIPLE_VALUE_TYPES = Collections.unmodifiableSet(set); |
77 | 1 | } |
78 | |
|
79 | |
|
80 | |
private String type; |
81 | |
|
82 | |
|
83 | |
private String name; |
84 | |
|
85 | |
|
86 | |
private Object value; |
87 | |
|
88 | |
|
89 | |
private String checkedValue; |
90 | |
|
91 | |
|
92 | |
private Integer index; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public void setType(final String type) { |
101 | 18 | this.type = type; |
102 | 18 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public void setName(final String name) { |
111 | 18 | this.name = name; |
112 | 18 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public void setCheckedValue(final String checkedValue) { |
121 | 5 | this.checkedValue = checkedValue; |
122 | 5 | } |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public void setValue(final Object value) { |
131 | 13 | this.value = value; |
132 | 13 | } |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public void setIndex(final Integer index) { |
141 | 0 | this.index = index; |
142 | 0 | } |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
@Override |
148 | |
public void doTag() throws JspException, IOException { |
149 | 18 | final JspContext context = this.getJspContext(); |
150 | 18 | final JspWriter out = context.getOut(); |
151 | 18 | final ActionErrors errors = errors(context); |
152 | 18 | final Map<String, Object> dyn = this.getDynamicAttributes(); |
153 | 18 | final FormWrapper formWrapper = getFormWrapper(this); |
154 | |
|
155 | |
|
156 | 18 | if (this.index == null) { |
157 | 18 | if (!errors.getFields().get(this.name).isEmpty()) { |
158 | 0 | addCSSClassName(dyn, "fieldError"); |
159 | |
} |
160 | |
} else { |
161 | 0 | if (!errors.getIndexedFields().get(this.name).get(index).isEmpty()) { |
162 | 0 | addCSSClassName(dyn, "fieldError"); |
163 | |
} |
164 | |
} |
165 | |
|
166 | 18 | if (MULTIPLE_VALUE_TYPES.contains(this.type)) { |
167 | 13 | if (this.value == null) { |
168 | 2 | throw new JspTagException(format("ECUB1003", |
169 | |
MULTIPLE_VALUE_TYPES, "value")); |
170 | |
} |
171 | 11 | final Object[] values = multipleFormValues(context, formWrapper, |
172 | |
this.name, this.checkedValue); |
173 | |
|
174 | 11 | out.write("<input type=\""); |
175 | 11 | out.write(type); |
176 | 11 | out.write("\" name=\""); |
177 | 11 | out.write(this.name); |
178 | 11 | out.write("\" value=\""); |
179 | 11 | out.write(CubbyFunctions.out(this.value)); |
180 | 11 | out.write("\" "); |
181 | 11 | out.write(toAttr(dyn)); |
182 | 11 | out.write(" "); |
183 | 11 | out.write(checked(TagUtils.toString(this.value), values)); |
184 | 11 | out.write("/>"); |
185 | 11 | } else { |
186 | 5 | final Object value = formValue(context, formWrapper, this.name, |
187 | |
this.index, this.value); |
188 | |
|
189 | 5 | out.write("<input type=\""); |
190 | 5 | out.write(type); |
191 | 5 | out.write("\" name=\""); |
192 | 5 | out.write(this.name); |
193 | 5 | out.write("\" value=\""); |
194 | 5 | out.write(CubbyFunctions.out(TagUtils.toString(value))); |
195 | 5 | out.write("\" "); |
196 | 5 | out.write(toAttr(dyn)); |
197 | 5 | out.write("/>"); |
198 | |
} |
199 | 16 | } |
200 | |
|
201 | |
private static String checked(final String value, final Object values) { |
202 | 11 | if (value == null || values == null) { |
203 | 0 | return ""; |
204 | |
} |
205 | 11 | if (contains(values, value)) { |
206 | 5 | return "checked=\"checked\""; |
207 | |
} else { |
208 | 6 | return ""; |
209 | |
} |
210 | |
} |
211 | |
|
212 | |
} |