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.isChecked; |
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 | 17 | public class InputTag extends DynamicAttributesTagSupport { |
55 | |
|
56 | 1 | private static final String[] MULTIPLE_VALUE_TYPES = new String[] { |
57 | |
"checkbox", "radio" }; |
58 | |
|
59 | |
private String type; |
60 | |
|
61 | |
private String name; |
62 | |
|
63 | |
private Object value; |
64 | |
|
65 | |
private String checkedValue; |
66 | |
|
67 | |
private Integer index; |
68 | |
|
69 | |
public void setType(final String type) { |
70 | 17 | this.type = type; |
71 | 17 | } |
72 | |
|
73 | |
public void setName(final String name) { |
74 | 17 | this.name = name; |
75 | 17 | } |
76 | |
|
77 | |
public void setCheckedValue(final String checkedValue) { |
78 | 5 | this.checkedValue = checkedValue; |
79 | 5 | } |
80 | |
|
81 | |
public void setValue(final Object value) { |
82 | 13 | this.value = value; |
83 | 13 | } |
84 | |
|
85 | |
public void setIndex(final Integer index) { |
86 | 0 | this.index = index; |
87 | 0 | } |
88 | |
|
89 | |
@Override |
90 | |
public void doTag() throws JspException, IOException { |
91 | 17 | final JspContext context = this.getJspContext(); |
92 | 17 | final JspWriter out = context.getOut(); |
93 | 17 | final ActionErrors errors = errors(context); |
94 | 17 | final Map<String, Object> dyn = this.getDynamicAttribute(); |
95 | 17 | final Map<String, String[]> outputValues = outputValues(context); |
96 | |
|
97 | 17 | if (this.index == null) { |
98 | 17 | if (!errors.getFields().get(this.name).isEmpty()) { |
99 | 0 | addClassName(dyn, "fieldError"); |
100 | |
} |
101 | |
} else { |
102 | 0 | if (!errors.getIndexedFields().get(this.name).get(index).isEmpty()) { |
103 | 0 | addClassName(dyn, "fieldError"); |
104 | |
} |
105 | |
} |
106 | |
|
107 | 17 | if (ArrayUtil.contains(MULTIPLE_VALUE_TYPES, this.type)) { |
108 | 13 | if (this.value == null) { |
109 | 2 | throw new JspTagException(MessageFormatter.getMessage( |
110 | |
"ECUB1003", new Object[] { |
111 | |
Arrays.deepToString(MULTIPLE_VALUE_TYPES), |
112 | |
"value" })); |
113 | |
} |
114 | 11 | final Object[] values = multipleFormValues(context, outputValues, |
115 | |
this.name, this.checkedValue); |
116 | |
|
117 | 11 | out.write("<input type=\""); |
118 | 11 | out.write(type); |
119 | 11 | out.write("\" name=\""); |
120 | 11 | out.write(this.name); |
121 | 11 | out.write("\" value=\""); |
122 | 11 | out.write(CubbyFunctions.out(this.value)); |
123 | 11 | out.write("\" "); |
124 | 11 | out.write(toAttr(dyn)); |
125 | 11 | out.write(" "); |
126 | 11 | out.write(checked(toString(this.value), values)); |
127 | 11 | out.write("/>\n"); |
128 | 11 | } else { |
129 | 4 | final Object value = formValue(context, outputValues, this.name, |
130 | |
this.index, this.value); |
131 | |
|
132 | 4 | out.write("<input type=\""); |
133 | 4 | out.write(type); |
134 | 4 | out.write("\" name=\""); |
135 | 4 | out.write(this.name); |
136 | 4 | out.write("\" value=\""); |
137 | 4 | out.write(CubbyFunctions.out(toString(value))); |
138 | 4 | out.write("\" "); |
139 | 4 | out.write(toAttr(dyn)); |
140 | 4 | out.write("/>\n"); |
141 | |
} |
142 | 15 | } |
143 | |
|
144 | |
private static String checked(final String value, final Object values) { |
145 | 11 | if (value == null || values == null) { |
146 | 0 | return ""; |
147 | |
} |
148 | 11 | if (isChecked(value, values)) { |
149 | 5 | return "checked=\"true\""; |
150 | |
} else { |
151 | 6 | return ""; |
152 | |
} |
153 | |
} |
154 | |
|
155 | |
} |