1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.shared_impl.renderkit.html; |
20 | |
|
21 | |
import org.apache.commons.logging.Log; |
22 | |
import org.apache.commons.logging.LogFactory; |
23 | |
import org.apache.myfaces.shared_impl.renderkit.JSFAttr; |
24 | |
|
25 | |
import javax.faces.FacesException; |
26 | |
import javax.faces.component.NamingContainer; |
27 | |
import javax.faces.component.UIComponent; |
28 | |
import javax.faces.component.UISelectBoolean; |
29 | |
import javax.faces.component.UISelectMany; |
30 | |
import javax.faces.component.html.HtmlSelectBooleanCheckbox; |
31 | |
import javax.faces.component.html.HtmlSelectManyCheckbox; |
32 | |
import javax.faces.context.FacesContext; |
33 | |
import javax.faces.context.ResponseWriter; |
34 | |
import javax.faces.convert.Converter; |
35 | |
import javax.faces.convert.ConverterException; |
36 | |
import javax.faces.model.SelectItem; |
37 | |
import javax.faces.model.SelectItemGroup; |
38 | |
import java.io.IOException; |
39 | |
import java.util.Iterator; |
40 | |
import java.util.Set; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public class HtmlCheckboxRendererBase extends HtmlRenderer { |
48 | 0 | private static final Log log = LogFactory |
49 | |
.getLog(HtmlCheckboxRendererBase.class); |
50 | |
|
51 | |
private static final String PAGE_DIRECTION = "pageDirection"; |
52 | |
|
53 | |
private static final String LINE_DIRECTION = "lineDirection"; |
54 | |
|
55 | |
private static final String EXTERNAL_TRUE_VALUE = "true"; |
56 | |
|
57 | |
public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) |
58 | |
throws IOException { |
59 | 0 | org.apache.myfaces.shared_impl.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, null); |
60 | 0 | if (uiComponent instanceof UISelectBoolean) { |
61 | 0 | Boolean value = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getBooleanValue( uiComponent ); |
62 | 0 | boolean isChecked = value != null ? value.booleanValue() : false; |
63 | 0 | renderCheckbox(facesContext, uiComponent, EXTERNAL_TRUE_VALUE, false,isChecked, true, null); |
64 | 0 | } else if (uiComponent instanceof UISelectMany) { |
65 | 0 | renderCheckboxList(facesContext, (UISelectMany) uiComponent); |
66 | |
} else { |
67 | 0 | throw new IllegalArgumentException("Unsupported component class " |
68 | |
+ uiComponent.getClass().getName()); |
69 | |
} |
70 | 0 | } |
71 | |
|
72 | |
public void renderCheckboxList(FacesContext facesContext, |
73 | |
UISelectMany selectMany) throws IOException { |
74 | |
|
75 | 0 | String layout = getLayout(selectMany); |
76 | 0 | boolean pageDirectionLayout = false; |
77 | 0 | if (layout != null) { |
78 | 0 | if (layout.equals(PAGE_DIRECTION)) { |
79 | 0 | pageDirectionLayout = true; |
80 | 0 | } else if (layout.equals(LINE_DIRECTION)) { |
81 | 0 | pageDirectionLayout = false; |
82 | |
} else { |
83 | 0 | log.error("Wrong layout attribute for component " |
84 | |
+ selectMany.getClientId(facesContext) + ": " + layout); |
85 | |
} |
86 | |
} |
87 | |
|
88 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
89 | |
|
90 | 0 | writer.startElement(HTML.TABLE_ELEM, selectMany); |
91 | 0 | HtmlRendererUtils.renderHTMLAttributes(writer, selectMany, |
92 | |
HTML.SELECT_TABLE_PASSTHROUGH_ATTRIBUTES); |
93 | 0 | HtmlRendererUtils.writeIdIfNecessary(writer, selectMany, facesContext); |
94 | |
|
95 | 0 | if (!pageDirectionLayout) |
96 | 0 | writer.startElement(HTML.TR_ELEM, selectMany); |
97 | |
|
98 | |
Converter converter; |
99 | |
try { |
100 | 0 | converter = org.apache.myfaces.shared_impl.renderkit.RendererUtils.findUISelectManyConverter(facesContext, |
101 | |
selectMany); |
102 | 0 | } catch (FacesException e) { |
103 | 0 | log.error("Error finding Converter for component with id " |
104 | |
+ selectMany.getClientId(facesContext)); |
105 | 0 | converter = null; |
106 | 0 | } |
107 | |
|
108 | 0 | Set lookupSet = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getSubmittedValuesAsSet(facesContext, selectMany, converter, selectMany); |
109 | 0 | boolean useSubmittedValues = lookupSet != null; |
110 | |
|
111 | 0 | if (!useSubmittedValues) { |
112 | 0 | lookupSet = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getSelectedValuesAsSet(facesContext, selectMany, converter, selectMany); |
113 | |
} |
114 | |
|
115 | 0 | int itemNum = 0; |
116 | |
|
117 | 0 | for (Iterator it = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getSelectItemList(selectMany) |
118 | 0 | .iterator(); it.hasNext();) { |
119 | 0 | SelectItem selectItem = (SelectItem) it.next(); |
120 | |
|
121 | 0 | renderGroupOrItemCheckbox(facesContext, selectMany, |
122 | |
selectItem, useSubmittedValues, lookupSet, |
123 | |
converter, pageDirectionLayout, new Integer(itemNum)); |
124 | |
|
125 | 0 | itemNum++; |
126 | 0 | } |
127 | |
|
128 | 0 | if (!pageDirectionLayout) |
129 | 0 | writer.endElement(HTML.TR_ELEM); |
130 | 0 | writer.endElement(HTML.TABLE_ELEM); |
131 | 0 | } |
132 | |
|
133 | |
protected String getLayout(UISelectMany selectMany) { |
134 | 0 | if (selectMany instanceof HtmlSelectManyCheckbox) { |
135 | 0 | return ((HtmlSelectManyCheckbox) selectMany).getLayout(); |
136 | |
} |
137 | |
|
138 | 0 | return (String) selectMany.getAttributes().get(JSFAttr.LAYOUT_ATTR); |
139 | |
} |
140 | |
|
141 | |
protected void renderGroupOrItemCheckbox(FacesContext facesContext, |
142 | |
UIComponent uiComponent, SelectItem selectItem, |
143 | |
boolean useSubmittedValues, Set lookupSet, |
144 | |
Converter converter, boolean pageDirectionLayout, Integer itemNum) throws IOException |
145 | |
{ |
146 | |
|
147 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
148 | |
|
149 | 0 | boolean isSelectItemGroup = (selectItem instanceof SelectItemGroup); |
150 | |
|
151 | 0 | Object itemValue = selectItem.getValue(); |
152 | |
|
153 | 0 | UISelectMany selectMany = (UISelectMany) uiComponent; |
154 | |
|
155 | 0 | String itemStrValue = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getConvertedStringValue(facesContext, selectMany, converter, itemValue); |
156 | |
|
157 | 0 | if (isSelectItemGroup) |
158 | |
{ |
159 | 0 | if (pageDirectionLayout) |
160 | 0 | writer.startElement(HTML.TR_ELEM, selectMany); |
161 | |
|
162 | 0 | writer.startElement(HTML.TD_ELEM, selectMany); |
163 | 0 | writer.write(selectItem.getLabel()); |
164 | 0 | writer.endElement(HTML.TD_ELEM); |
165 | |
|
166 | 0 | if (pageDirectionLayout) |
167 | |
{ |
168 | 0 | writer.endElement(HTML.TR_ELEM); |
169 | 0 | writer.startElement(HTML.TR_ELEM, selectMany); |
170 | |
} |
171 | 0 | writer.startElement(HTML.TD_ELEM, selectMany); |
172 | |
|
173 | 0 | writer.startElement(HTML.TABLE_ELEM, selectMany); |
174 | 0 | writer.writeAttribute(HTML.BORDER_ATTR, "0", null); |
175 | |
|
176 | 0 | SelectItemGroup group = (SelectItemGroup) selectItem; |
177 | 0 | SelectItem[] selectItems = group.getSelectItems(); |
178 | |
|
179 | 0 | for (int i = 0; i < selectItems.length; i++) |
180 | |
{ |
181 | 0 | renderGroupOrItemCheckbox(facesContext, selectMany, selectItems[i], useSubmittedValues, lookupSet, converter, pageDirectionLayout, new Integer(i)); |
182 | |
} |
183 | |
|
184 | 0 | writer.endElement(HTML.TD_ELEM); |
185 | 0 | writer.endElement(HTML.TR_ELEM); |
186 | 0 | writer.endElement(HTML.TABLE_ELEM); |
187 | 0 | writer.endElement(HTML.TD_ELEM); |
188 | |
|
189 | 0 | if (pageDirectionLayout) |
190 | 0 | writer.endElement(HTML.TR_ELEM); |
191 | |
|
192 | 0 | } |
193 | |
else |
194 | |
{ |
195 | |
|
196 | 0 | writer.write("\t\t"); |
197 | 0 | if (pageDirectionLayout) |
198 | 0 | writer.startElement(HTML.TR_ELEM, selectMany); |
199 | 0 | writer.startElement(HTML.TD_ELEM, selectMany); |
200 | |
|
201 | 0 | boolean checked = lookupSet.contains(itemStrValue); |
202 | |
|
203 | 0 | boolean disabled = selectItem.isDisabled(); |
204 | |
|
205 | 0 | String itemId = renderCheckbox(facesContext, selectMany, itemStrValue, disabled, checked, false, itemNum); |
206 | |
|
207 | |
|
208 | 0 | boolean componentDisabled = isDisabled(facesContext, selectMany); |
209 | 0 | boolean itemDisabled = (componentDisabled || disabled); |
210 | |
|
211 | 0 | HtmlRendererUtils.renderLabel(writer, selectMany, itemId, selectItem, itemDisabled); |
212 | |
|
213 | 0 | writer.endElement(HTML.TD_ELEM); |
214 | 0 | if (pageDirectionLayout) |
215 | 0 | writer.endElement(HTML.TR_ELEM); |
216 | |
} |
217 | 0 | } |
218 | |
|
219 | |
@Deprecated |
220 | |
protected void renderCheckbox(FacesContext facesContext, |
221 | |
UIComponent uiComponent, String value, String label, |
222 | |
boolean disabled, boolean checked, boolean renderId) throws IOException { |
223 | 0 | renderCheckbox(facesContext, uiComponent, value, disabled, checked, renderId, 0); |
224 | 0 | } |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
protected String renderCheckbox(FacesContext facesContext, |
231 | |
UIComponent uiComponent, String value, boolean disabled, boolean checked, boolean renderId, Integer itemNum) throws IOException { |
232 | 0 | String clientId = uiComponent.getClientId(facesContext); |
233 | |
|
234 | 0 | String itemId = (itemNum == null)? null : clientId + NamingContainer.SEPARATOR_CHAR + itemNum; |
235 | |
|
236 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
237 | |
|
238 | 0 | writer.startElement(HTML.INPUT_ELEM, uiComponent); |
239 | |
|
240 | 0 | if (itemId != null) |
241 | |
{ |
242 | 0 | writer.writeAttribute(HTML.ID_ATTR, itemId, null); |
243 | |
} |
244 | 0 | writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_CHECKBOX, null); |
245 | 0 | writer.writeAttribute(HTML.NAME_ATTR, clientId, null); |
246 | |
|
247 | 0 | if (renderId) { |
248 | 0 | HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext); |
249 | |
} |
250 | |
|
251 | 0 | if (checked) { |
252 | 0 | writer.writeAttribute(HTML.CHECKED_ATTR, org.apache.myfaces.shared_impl.renderkit.html.HTML.CHECKED_ATTR, null); |
253 | |
} |
254 | |
|
255 | 0 | if (disabled) { |
256 | 0 | writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, null); |
257 | |
} |
258 | |
|
259 | 0 | if ((value != null) && (value.length() > 0)) { |
260 | 0 | writer.writeAttribute(HTML.VALUE_ATTR, value, null); |
261 | |
} |
262 | |
|
263 | 0 | if (uiComponent instanceof UISelectBoolean) |
264 | |
{ |
265 | 0 | HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, |
266 | |
HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED); |
267 | |
} |
268 | |
else |
269 | |
{ |
270 | 0 | HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, |
271 | |
HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_STYLE); |
272 | |
} |
273 | 0 | if (isDisabled(facesContext, uiComponent)) { |
274 | 0 | writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null); |
275 | |
} |
276 | |
|
277 | 0 | writer.endElement(HTML.INPUT_ELEM); |
278 | |
|
279 | 0 | return itemId; |
280 | |
} |
281 | |
|
282 | |
protected boolean isDisabled(FacesContext facesContext, |
283 | |
UIComponent component) { |
284 | |
|
285 | |
|
286 | 0 | if (component instanceof HtmlSelectBooleanCheckbox) { |
287 | 0 | return ((HtmlSelectBooleanCheckbox) component).isDisabled(); |
288 | 0 | } else if (component instanceof HtmlSelectManyCheckbox) { |
289 | 0 | return ((HtmlSelectManyCheckbox) component).isDisabled(); |
290 | |
} else { |
291 | 0 | return org.apache.myfaces.shared_impl.renderkit.RendererUtils.getBooleanAttribute(component, |
292 | |
HTML.DISABLED_ATTR, false); |
293 | |
} |
294 | |
} |
295 | |
|
296 | |
public void decode(FacesContext facesContext, UIComponent component) { |
297 | 0 | org.apache.myfaces.shared_impl.renderkit.RendererUtils.checkParamValidity(facesContext, component, null); |
298 | 0 | if (component instanceof UISelectBoolean) { |
299 | 0 | HtmlRendererUtils.decodeUISelectBoolean(facesContext, component); |
300 | 0 | } else if (component instanceof UISelectMany) { |
301 | 0 | HtmlRendererUtils.decodeUISelectMany(facesContext, component); |
302 | |
} else { |
303 | 0 | throw new IllegalArgumentException("Unsupported component class " |
304 | |
+ component.getClass().getName()); |
305 | |
} |
306 | 0 | } |
307 | |
|
308 | |
public Object getConvertedValue(FacesContext facesContext, |
309 | |
UIComponent component, Object submittedValue) |
310 | |
throws ConverterException { |
311 | 0 | org.apache.myfaces.shared_impl.renderkit.RendererUtils.checkParamValidity(facesContext, component, null); |
312 | 0 | if (component instanceof UISelectBoolean) { |
313 | 0 | return submittedValue; |
314 | 0 | } else if (component instanceof UISelectMany) { |
315 | 0 | return org.apache.myfaces.shared_impl.renderkit.RendererUtils.getConvertedUISelectManyValue(facesContext, |
316 | |
(UISelectMany) component, submittedValue); |
317 | |
} else { |
318 | 0 | throw new IllegalArgumentException("Unsupported component class " |
319 | |
+ component.getClass().getName()); |
320 | |
} |
321 | |
} |
322 | |
} |