1 | |
package org.seasar.cubby.tags; |
2 | |
|
3 | |
import java.io.IOException; |
4 | |
import java.util.Collection; |
5 | |
import java.util.Map; |
6 | |
import java.util.Map.Entry; |
7 | |
|
8 | |
import javax.servlet.jsp.JspException; |
9 | |
import javax.servlet.jsp.JspTagException; |
10 | |
import javax.servlet.jsp.JspWriter; |
11 | |
import javax.servlet.jsp.PageContext; |
12 | |
|
13 | |
import org.seasar.cubby.util.CubbyFunctions; |
14 | |
import org.seasar.cubby.util.CubbyHelperFunctions; |
15 | |
import org.seasar.framework.log.Logger; |
16 | |
import org.seasar.framework.message.MessageFormatter; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | 86 | public class SelectTag extends DynamicAttributesTagSupport { |
25 | |
|
26 | 1 | private static final Logger logger = Logger.getLogger(SelectTag.class); |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
private Object items; |
32 | |
|
33 | |
|
34 | |
|
35 | |
private String labelProperty; |
36 | |
|
37 | |
|
38 | |
|
39 | |
private String valueProperty; |
40 | |
|
41 | |
|
42 | |
|
43 | 13 | private boolean emptyOption = true; |
44 | |
|
45 | |
|
46 | |
|
47 | |
private String emptyOptionLabel; |
48 | |
|
49 | |
public Object getItems() { |
50 | |
return items; |
51 | |
} |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public void setItems(final Object items) { |
60 | 13 | this.items = items; |
61 | 13 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public void setLabelProperty(final String labelProperty) { |
70 | 6 | this.labelProperty = labelProperty; |
71 | 6 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public void setValueProperty(final String valueProperty) { |
80 | 7 | this.valueProperty = valueProperty; |
81 | 7 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public void setEmptyOption(final boolean emptyOption) { |
90 | 2 | this.emptyOption = emptyOption; |
91 | 2 | } |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public void setEmptyOptionLabel(final String emptyOptionLabel) { |
100 | 3 | this.emptyOptionLabel = emptyOptionLabel; |
101 | 3 | } |
102 | |
|
103 | |
|
104 | |
|
105 | |
@SuppressWarnings("unchecked") |
106 | |
@Override |
107 | |
public void doTag() throws JspException, IOException { |
108 | 13 | final Object form = getJspContext().getAttribute("__form", |
109 | |
PageContext.REQUEST_SCOPE); |
110 | 13 | final Object value = CubbyHelperFunctions.formValue( |
111 | |
getDynamicAttribute(), form, getJspContext(), "value"); |
112 | 13 | getJspContext().setAttribute("value", value, PageContext.PAGE_SCOPE); |
113 | 13 | final Map<?, ?> fieldErros = (Map<?, ?>) getJspContext().getAttribute( |
114 | |
"fieldErrors", PageContext.REQUEST_SCOPE); |
115 | 13 | if (fieldErros.get(getDynamicAttribute().get("name")) != null) { |
116 | |
CubbyHelperFunctions.addClassName(getDynamicAttribute(), |
117 | |
"fieldError"); |
118 | |
} |
119 | 13 | final JspWriter out = getJspContext().getOut(); |
120 | 13 | out.write("<select "); |
121 | 13 | out.write(CubbyHelperFunctions.toAttr(getDynamicAttribute())); |
122 | 13 | out.write(">\n"); |
123 | |
|
124 | 13 | if (emptyOption) { |
125 | 11 | out.write("<option value=\"\">"); |
126 | 11 | out.write(CubbyFunctions.out(emptyOptionLabel)); |
127 | 11 | out.write("</option>\n"); |
128 | |
} |
129 | |
|
130 | 13 | if (items.getClass().isArray()) { |
131 | |
final OptionWriter optionWriter = new OptionWriter( |
132 | |
new BeanItemAdaptor()); |
133 | |
for (final Object item : (Object[]) items) { |
134 | |
optionWriter.write(out, item, value); |
135 | |
} |
136 | |
} else { |
137 | |
final OptionWriter optionWriter; |
138 | |
final Collection<?> collection; |
139 | 13 | if (items instanceof Collection) { |
140 | 6 | optionWriter = new OptionWriter(new BeanItemAdaptor()); |
141 | 5 | collection = (Collection<?>) items; |
142 | 5 | } else if (items instanceof Map) { |
143 | 7 | optionWriter = new OptionWriter(new EntryItemAdaptor()); |
144 | 7 | collection = ((Map<?, ?>) items).entrySet(); |
145 | 7 | } else { |
146 | |
throw new JspTagException(MessageFormatter.getMessage( |
147 | |
"ECUB1001", new Object[] { "items", items.getClass() })); |
148 | |
} |
149 | 12 | for (final Object item : collection) { |
150 | 36 | optionWriter.write(out, item, value); |
151 | 36 | } |
152 | |
} |
153 | |
|
154 | 12 | out.write("</select>\n"); |
155 | 12 | } |
156 | |
|
157 | |
static class OptionWriter { |
158 | |
|
159 | |
private final ItemAdaptor itemAdaptor; |
160 | |
|
161 | 12 | OptionWriter(final ItemAdaptor itemAdaptor) { |
162 | 12 | this.itemAdaptor = itemAdaptor; |
163 | 12 | } |
164 | |
|
165 | |
public void write(final JspWriter out, final Object item, |
166 | |
final Object value) throws IOException { |
167 | 36 | out.write("<option value=\""); |
168 | 36 | final String itemValue = DynamicAttributesTagSupport |
169 | |
.toString(itemAdaptor.getItemValue(item)); |
170 | 36 | final String labelValue = DynamicAttributesTagSupport |
171 | |
.toString(itemAdaptor.getLabelValue(item)); |
172 | 36 | out.write(CubbyFunctions.out(itemValue)); |
173 | 36 | out.write("\" "); |
174 | 36 | out.write(selected(itemValue, value)); |
175 | 36 | out.write(">"); |
176 | 36 | out.write(CubbyFunctions.out(labelValue)); |
177 | 36 | out.write("</option>\n"); |
178 | 36 | } |
179 | |
|
180 | |
private String selected(final String value, final Object values) { |
181 | 36 | if (value == null || values == null) { |
182 | |
return ""; |
183 | |
} |
184 | 36 | if (CubbyHelperFunctions.isChecked(value, values)) { |
185 | 14 | return "selected=\"true\""; |
186 | |
} else { |
187 | 22 | return ""; |
188 | |
} |
189 | |
} |
190 | |
} |
191 | |
|
192 | |
interface ItemAdaptor { |
193 | |
|
194 | |
Object getItemValue(Object item); |
195 | |
|
196 | |
Object getLabelValue(Object item); |
197 | |
|
198 | |
} |
199 | |
|
200 | |
class BeanItemAdaptor implements ItemAdaptor { |
201 | |
|
202 | 6 | public BeanItemAdaptor() throws JspTagException { |
203 | 6 | if (valueProperty == null) { |
204 | 1 | throw new JspTagException(MessageFormatter.getMessage( |
205 | |
"ECUB1002", new Object[] { "items", "valueProperty" })); |
206 | |
} |
207 | 5 | } |
208 | |
|
209 | |
public Object getItemValue(final Object item) { |
210 | 18 | return CubbyHelperFunctions.property(item, valueProperty); |
211 | |
} |
212 | |
|
213 | |
public Object getLabelValue(final Object item) { |
214 | |
final Object labelValue; |
215 | 15 | if (labelProperty == null) { |
216 | 3 | labelValue = getItemValue(item); |
217 | 3 | } else { |
218 | 12 | labelValue = CubbyHelperFunctions.property(item, labelProperty); |
219 | |
} |
220 | 15 | return labelValue; |
221 | |
} |
222 | |
|
223 | |
} |
224 | |
|
225 | 13 | class EntryItemAdaptor implements ItemAdaptor { |
226 | |
|
227 | 7 | public EntryItemAdaptor() { |
228 | 7 | if (valueProperty != null) { |
229 | 2 | logger.log("WCUB1001", new Object[] { "items", |
230 | |
Map.class.getSimpleName(), "valueProperty", |
231 | |
valueProperty, |
232 | |
Entry.class.getSimpleName() + "#getKey()" }); |
233 | |
} |
234 | 7 | if (labelProperty != null) { |
235 | 2 | logger.log("WCUB1002", new Object[] { "items", |
236 | |
Map.class.getSimpleName(), "labelProperty", |
237 | |
labelProperty, |
238 | |
Entry.class.getSimpleName() + "#getValue()" }); |
239 | |
} |
240 | 7 | } |
241 | |
|
242 | |
public Object getItemValue(final Object item) { |
243 | 21 | return ((Entry<?, ?>) item).getKey(); |
244 | |
} |
245 | |
|
246 | |
public Object getLabelValue(final Object item) { |
247 | 21 | return ((Entry<?, ?>) item).getValue(); |
248 | |
} |
249 | |
|
250 | |
} |
251 | |
|
252 | |
} |