1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.render.html; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.util.Iterator; |
20 | |
|
21 | |
import javax.faces.component.UIComponent; |
22 | |
import javax.faces.component.UIOutput; |
23 | |
import javax.faces.context.FacesContext; |
24 | |
import javax.faces.context.ResponseWriter; |
25 | |
import javax.faces.convert.ConverterException; |
26 | |
import javax.faces.internal.IgnoreAttribute; |
27 | |
import javax.faces.internal.SelectItemsIterator; |
28 | |
import javax.faces.internal.UIComponentUtil; |
29 | |
import javax.faces.model.SelectItem; |
30 | |
import javax.faces.model.SelectItemGroup; |
31 | |
|
32 | |
import org.seasar.framework.util.ArrayIterator; |
33 | |
import org.seasar.framework.util.ArrayUtil; |
34 | |
import org.seasar.framework.util.StringUtil; |
35 | |
import org.seasar.teeda.core.JsfConstants; |
36 | |
import org.seasar.teeda.core.render.AbstractInputRenderer; |
37 | |
import org.seasar.teeda.core.util.RendererUtil; |
38 | |
import org.seasar.teeda.core.util.ValueHolderUtil; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 88 | public class HtmlSelectManyCheckboxRenderer extends AbstractInputRenderer { |
44 | |
|
45 | |
public static final String COMPONENT_FAMILY = "javax.faces.SelectMany"; |
46 | |
|
47 | |
public static final String RENDERER_TYPE = "javax.faces.Checkbox"; |
48 | |
|
49 | 88 | private final IgnoreAttribute ignoreComponent = new IgnoreAttribute(); |
50 | |
{ |
51 | 88 | ignoreComponent.addAttributeName(JsfConstants.ID_ATTR); |
52 | 88 | ignoreComponent.addAttributeName(JsfConstants.VALUE_ATTR); |
53 | 88 | ignoreComponent.addAttributeName(JsfConstants.LAYOUT_ATTR); |
54 | 88 | ignoreComponent.addAttributeName(JsfConstants.DISABLED_ATTR); |
55 | 88 | ignoreComponent.addAttributeName(JsfConstants.DISABLED_CLASS_ATTR); |
56 | 88 | ignoreComponent.addAttributeName(JsfConstants.ENABLED_CLASS_ATTR); |
57 | 88 | ignoreComponent.addAttributeName(JsfConstants.BORDER_ATTR); |
58 | 88 | ignoreComponent.addAttributeName(JsfConstants.STYLE_ATTR); |
59 | 88 | ignoreComponent.addAttributeName(JsfConstants.STYLE_CLASS_ATTR); |
60 | 88 | ignoreComponent.addAttributeName(JsfConstants.NAME_ATTR); |
61 | 88 | ignoreComponent.addAttributeName(JsfConstants.TYPE_ATTR); |
62 | 88 | ignoreComponent.addAttributeName(JsfConstants.CHECKED_ATTR); |
63 | 88 | ignoreComponent.addAttributeName("selectedValues"); |
64 | 88 | } |
65 | |
|
66 | |
public void encodeEnd(FacesContext context, UIComponent component) |
67 | |
throws IOException { |
68 | 36 | assertNotNull(context, component); |
69 | 32 | if (!component.isRendered()) { |
70 | 2 | return; |
71 | |
} |
72 | 30 | encodeHtmlSelectManyCheckboxEnd(context, component); |
73 | 30 | } |
74 | |
|
75 | 1 | protected static final String[] TABLE_ATTRIBUTES = new String[] { |
76 | |
JsfConstants.BORDER_ATTR, JsfConstants.STYLE_ATTR, |
77 | |
JsfConstants.STYLE_CLASS_ATTR }; |
78 | |
|
79 | |
protected void encodeHtmlSelectManyCheckboxEnd(FacesContext context, |
80 | |
UIComponent htmlSelectManyCheckbox) throws IOException { |
81 | |
|
82 | 30 | final Iterator it = new SelectItemsIterator(htmlSelectManyCheckbox); |
83 | 30 | if (!it.hasNext()) { |
84 | 2 | return; |
85 | |
} |
86 | 28 | ResponseWriter writer = context.getResponseWriter(); |
87 | 28 | boolean noneLayout = isNoneLayout(htmlSelectManyCheckbox); |
88 | 28 | if (!noneLayout) { |
89 | 26 | writer |
90 | |
.startElement(JsfConstants.TABLE_ELEM, |
91 | |
htmlSelectManyCheckbox); |
92 | |
|
93 | 26 | RendererUtil.renderIdAttributeIfNecessary(writer, |
94 | |
htmlSelectManyCheckbox, getIdForRender(context, |
95 | |
htmlSelectManyCheckbox)); |
96 | 26 | RendererUtil.renderAttributes(writer, htmlSelectManyCheckbox, |
97 | |
TABLE_ATTRIBUTES); |
98 | |
} |
99 | 28 | String[] selectedValues = getValuesForRender(context, |
100 | |
htmlSelectManyCheckbox); |
101 | |
|
102 | 28 | final boolean pageDirectionLayout = isPageDirectionLayout(htmlSelectManyCheckbox); |
103 | 28 | if (!noneLayout) { |
104 | 26 | if (!pageDirectionLayout) { |
105 | 24 | writer.startElement(JsfConstants.TR_ELEM, |
106 | |
htmlSelectManyCheckbox); |
107 | |
} |
108 | |
} |
109 | 28 | renderSelectItems(context, htmlSelectManyCheckbox, writer, it, |
110 | |
selectedValues, pageDirectionLayout, noneLayout); |
111 | 28 | if (!noneLayout) { |
112 | 26 | if (!pageDirectionLayout) { |
113 | 24 | writer.endElement(JsfConstants.TR_ELEM); |
114 | |
} |
115 | 26 | writer.endElement(JsfConstants.TABLE_ELEM); |
116 | |
} |
117 | 28 | } |
118 | |
|
119 | |
protected String[] getValuesForRender(FacesContext context, |
120 | |
UIComponent htmlSelectManyCheckbox) { |
121 | 15 | return ValueHolderUtil.getValuesForRender(context, |
122 | |
htmlSelectManyCheckbox); |
123 | |
} |
124 | |
|
125 | |
protected void renderSelectItems(FacesContext context, |
126 | |
UIComponent htmlSelectManyCheckbox, ResponseWriter writer, |
127 | |
Iterator it, String[] selectedValues, |
128 | |
final boolean pageDirectionLayout, final boolean noneLayout) |
129 | |
throws IOException { |
130 | |
|
131 | 77 | while (it.hasNext()) { |
132 | 47 | final SelectItem selectItem = (SelectItem) it.next(); |
133 | |
|
134 | 47 | if (!noneLayout) { |
135 | 44 | if (pageDirectionLayout) { |
136 | 4 | writer.startElement(JsfConstants.TR_ELEM, |
137 | |
htmlSelectManyCheckbox); |
138 | |
} |
139 | 44 | writer.startElement(JsfConstants.TD_ELEM, |
140 | |
htmlSelectManyCheckbox); |
141 | |
} |
142 | 47 | if (selectItem instanceof SelectItemGroup) { |
143 | 2 | renderSelectItemGroup(context, htmlSelectManyCheckbox, writer, |
144 | |
selectedValues, selectItem, pageDirectionLayout, |
145 | |
noneLayout); |
146 | |
} else { |
147 | 45 | renderSelectItem(context, htmlSelectManyCheckbox, writer, |
148 | |
selectedValues, selectItem); |
149 | |
} |
150 | 47 | if (!noneLayout) { |
151 | 44 | writer.endElement(JsfConstants.TD_ELEM); |
152 | 44 | if (pageDirectionLayout) { |
153 | 4 | writer.endElement(JsfConstants.TR_ELEM); |
154 | |
} |
155 | |
} |
156 | |
} |
157 | 30 | } |
158 | |
|
159 | |
protected void renderSelectItemGroup(FacesContext context, |
160 | |
UIComponent htmlSelectManyCheckbox, ResponseWriter writer, |
161 | |
String[] selectedValues, final SelectItem selectItem, |
162 | |
final boolean pageDirectionLayout, final boolean noneLayout) |
163 | |
throws IOException { |
164 | 2 | SelectItemGroup selectItemGroup = (SelectItemGroup) selectItem; |
165 | 2 | SelectItem[] selectItems = selectItemGroup.getSelectItems(); |
166 | 2 | Iterator selectItemsIt = new ArrayIterator(selectItems); |
167 | 2 | if (!noneLayout) { |
168 | 2 | writer |
169 | |
.startElement(JsfConstants.TABLE_ELEM, |
170 | |
htmlSelectManyCheckbox); |
171 | 2 | if (!pageDirectionLayout) { |
172 | 2 | writer.startElement(JsfConstants.TR_ELEM, |
173 | |
htmlSelectManyCheckbox); |
174 | |
} |
175 | |
} |
176 | 2 | renderSelectItems(context, htmlSelectManyCheckbox, writer, |
177 | |
selectItemsIt, selectedValues, pageDirectionLayout, noneLayout); |
178 | 2 | if (!noneLayout) { |
179 | 2 | if (!pageDirectionLayout) { |
180 | 2 | writer.endElement(JsfConstants.TR_ELEM); |
181 | |
} |
182 | 2 | writer.endElement(JsfConstants.TABLE_ELEM); |
183 | |
} |
184 | 2 | } |
185 | |
|
186 | |
protected void renderSelectItem(FacesContext context, |
187 | |
UIComponent htmlSelectManyCheckbox, ResponseWriter writer, |
188 | |
String[] selectedValues, final SelectItem selectItem) |
189 | |
throws IOException { |
190 | |
|
191 | 45 | writer.startElement(JsfConstants.LABEL_ELEM, htmlSelectManyCheckbox); |
192 | 45 | final boolean disabled = UIComponentUtil |
193 | |
.isDisabled(htmlSelectManyCheckbox) || |
194 | |
selectItem.isDisabled(); |
195 | 45 | final String labelClass = getLabelStyleClass(htmlSelectManyCheckbox, |
196 | |
disabled); |
197 | 45 | if (labelClass != null) { |
198 | 6 | RendererUtil.renderAttribute(writer, JsfConstants.CLASS_ATTR, |
199 | |
labelClass); |
200 | |
} |
201 | |
|
202 | 45 | renderInputElement(context, htmlSelectManyCheckbox, writer, |
203 | |
selectedValues, selectItem, disabled); |
204 | |
|
205 | 45 | final String label = selectItem.getLabel(); |
206 | 45 | if (!StringUtil.isEmpty(label)) { |
207 | 45 | writer.writeText(label, null); |
208 | |
} |
209 | 45 | writer.endElement(JsfConstants.LABEL_ELEM); |
210 | 45 | } |
211 | |
|
212 | |
protected void renderInputElement(FacesContext context, |
213 | |
UIComponent htmlSelectManyCheckbox, ResponseWriter writer, |
214 | |
String[] selectedValues, final SelectItem selectItem, |
215 | |
final boolean disabled) throws IOException { |
216 | |
|
217 | 24 | writer.startElement(JsfConstants.INPUT_ELEM, htmlSelectManyCheckbox); |
218 | 24 | RendererUtil.renderAttribute(writer, JsfConstants.TYPE_ATTR, |
219 | |
JsfConstants.CHECKBOX_VALUE); |
220 | 24 | RendererUtil.renderAttribute(writer, JsfConstants.NAME_ATTR, |
221 | |
htmlSelectManyCheckbox.getClientId(context)); |
222 | 24 | final Object value = selectItem.getValue(); |
223 | 24 | RendererUtil.renderAttribute(writer, JsfConstants.VALUE_ATTR, value); |
224 | 24 | renderRemainAttributes(htmlSelectManyCheckbox, writer, ignoreComponent); |
225 | 24 | if (isChecked(selectedValues, value.toString())) { |
226 | 2 | renderCheckedAttribute(writer); |
227 | |
} |
228 | 24 | if (disabled) { |
229 | 7 | renderDisabledAttribute(writer); |
230 | |
} |
231 | 24 | writer.endElement(JsfConstants.INPUT_ELEM); |
232 | 24 | } |
233 | |
|
234 | |
protected boolean isPageDirectionLayout(UIComponent htmlSelectManyCheckbox) { |
235 | 28 | return JsfConstants.PAGE_DIRECTION_VALUE.equals(UIComponentUtil |
236 | |
.getStringAttribute(htmlSelectManyCheckbox, |
237 | |
JsfConstants.LAYOUT_ATTR)); |
238 | |
} |
239 | |
|
240 | |
protected boolean isNoneLayout(UIComponent htmlSelectManyCheckbox) { |
241 | 28 | return JsfConstants.NONE_VALUE.equals(UIComponentUtil |
242 | |
.getStringAttribute(htmlSelectManyCheckbox, |
243 | |
JsfConstants.LAYOUT_ATTR)); |
244 | |
} |
245 | |
|
246 | |
protected boolean isChecked(String[] selectedValues, final String value) { |
247 | 45 | return ArrayUtil.contains(selectedValues, value); |
248 | |
} |
249 | |
|
250 | |
public void decode(FacesContext context, UIComponent component) { |
251 | 4 | assertNotNull(context, component); |
252 | 2 | getDecoder().decodeMany(context, component); |
253 | 2 | } |
254 | |
|
255 | |
public Object getConvertedValue(FacesContext context, |
256 | |
UIComponent component, Object submittedValue) |
257 | |
throws ConverterException { |
258 | 3 | assertNotNull(context, component); |
259 | 1 | return RendererUtil.getConvertedUIOutputValues(context, |
260 | |
(UIOutput) component, submittedValue); |
261 | |
} |
262 | |
|
263 | |
public void addIgnoreAttributeName(final String name) { |
264 | 33 | ignoreComponent.addAttributeName(name); |
265 | 33 | } |
266 | |
|
267 | |
} |