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.tags.TagUtils.addClassName; |
19 | |
import static org.seasar.cubby.tags.TagUtils.errors; |
20 | |
import static org.seasar.cubby.tags.TagUtils.formValue; |
21 | |
import static org.seasar.cubby.tags.TagUtils.contains; |
22 | |
import static org.seasar.cubby.tags.TagUtils.multipleFormValues; |
23 | |
import static org.seasar.cubby.tags.TagUtils.outputValues; |
24 | |
import static org.seasar.cubby.tags.TagUtils.toAttr; |
25 | |
|
26 | |
import java.io.IOException; |
27 | |
import java.util.Arrays; |
28 | |
import java.util.Map; |
29 | |
|
30 | |
import javax.servlet.jsp.JspContext; |
31 | |
import javax.servlet.jsp.JspException; |
32 | |
import javax.servlet.jsp.JspTagException; |
33 | |
import javax.servlet.jsp.JspWriter; |
34 | |
|
35 | |
import org.seasar.cubby.action.ActionErrors; |
36 | |
import org.seasar.cubby.util.CubbyFunctions; |
37 | |
import org.seasar.framework.message.MessageFormatter; |
38 | |
import org.seasar.framework.util.ArrayUtil; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 17 | public class InputTag extends DynamicAttributesTagSupport { |
56 | |
|
57 | 1 | private static final String[] MULTIPLE_VALUE_TYPES = new String[] { |
58 | |
"checkbox", "radio" }; |
59 | |
|
60 | |
private String type; |
61 | |
|
62 | |
private String name; |
63 | |
|
64 | |
private Object value; |
65 | |
|
66 | |
private String checkedValue; |
67 | |
|
68 | |
private Integer index; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public void setType(final String type) { |
77 | 17 | this.type = type; |
78 | 17 | } |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public void setName(final String name) { |
87 | 17 | this.name = name; |
88 | 17 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public void setCheckedValue(final String checkedValue) { |
97 | 5 | this.checkedValue = checkedValue; |
98 | 5 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void setValue(final Object value) { |
107 | 13 | this.value = value; |
108 | 13 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public void setIndex(final Integer index) { |
117 | 0 | this.index = index; |
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
@Override |
124 | |
public void doTag() throws JspException, IOException { |
125 | 17 | final JspContext context = this.getJspContext(); |
126 | 17 | final JspWriter out = context.getOut(); |
127 | 17 | final ActionErrors errors = errors(context); |
128 | 17 | final Map<String, Object> dyn = this.getDynamicAttribute(); |
129 | 17 | final Map<String, String[]> outputValues = outputValues(context); |
130 | |
|
131 | 17 | if (this.index == null) { |
132 | 17 | if (!errors.getFields().get(this.name).isEmpty()) { |
133 | 0 | addClassName(dyn, "fieldError"); |
134 | |
} |
135 | |
} else { |
136 | 0 | if (!errors.getIndexedFields().get(this.name).get(index).isEmpty()) { |
137 | 0 | addClassName(dyn, "fieldError"); |
138 | |
} |
139 | |
} |
140 | |
|
141 | 17 | if (ArrayUtil.contains(MULTIPLE_VALUE_TYPES, this.type)) { |
142 | 13 | if (this.value == null) { |
143 | 2 | throw new JspTagException(MessageFormatter.getMessage( |
144 | |
"ECUB1003", new Object[] { |
145 | |
Arrays.deepToString(MULTIPLE_VALUE_TYPES), |
146 | |
"value" })); |
147 | |
} |
148 | 11 | final Object[] values = multipleFormValues(context, outputValues, |
149 | |
this.name, this.checkedValue); |
150 | |
|
151 | 11 | out.write("<input type=\""); |
152 | 11 | out.write(type); |
153 | 11 | out.write("\" name=\""); |
154 | 11 | out.write(this.name); |
155 | 11 | out.write("\" value=\""); |
156 | 11 | out.write(CubbyFunctions.out(this.value)); |
157 | 11 | out.write("\" "); |
158 | 11 | out.write(toAttr(dyn)); |
159 | 11 | out.write(" "); |
160 | 11 | out.write(checked(toString(this.value), values)); |
161 | 11 | out.write("/>\n"); |
162 | 11 | } else { |
163 | 4 | final Object value = formValue(context, outputValues, this.name, |
164 | |
this.index, this.value); |
165 | |
|
166 | 4 | out.write("<input type=\""); |
167 | 4 | out.write(type); |
168 | 4 | out.write("\" name=\""); |
169 | 4 | out.write(this.name); |
170 | 4 | out.write("\" value=\""); |
171 | 4 | out.write(CubbyFunctions.out(toString(value))); |
172 | 4 | out.write("\" "); |
173 | 4 | out.write(toAttr(dyn)); |
174 | 4 | out.write("/>\n"); |
175 | |
} |
176 | 15 | } |
177 | |
|
178 | |
private static String checked(final String value, final Object values) { |
179 | 11 | if (value == null || values == null) { |
180 | 0 | return ""; |
181 | |
} |
182 | 11 | if (contains(values, value)) { |
183 | 5 | return "checked=\"true\""; |
184 | |
} else { |
185 | 6 | return ""; |
186 | |
} |
187 | |
} |
188 | |
|
189 | |
} |