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