KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > HtmlCheckboxRendererBase


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.renderkit.html;
17
18 import org.apache.myfaces.renderkit.JSFAttr;
19 import org.apache.myfaces.renderkit.RendererUtils;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.faces.FacesException;
25 import javax.faces.component.UIComponent;
26 import javax.faces.component.UISelectBoolean;
27 import javax.faces.component.UISelectMany;
28 import javax.faces.component.html.HtmlSelectBooleanCheckbox;
29 import javax.faces.component.html.HtmlSelectManyCheckbox;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32 import javax.faces.convert.Converter;
33 import javax.faces.convert.ConverterException;
34 import javax.faces.model.SelectItem;
35 import java.io.IOException JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Set JavaDoc;
38
39 /**
40  * @author Thomas Spiegl (latest modification by $Author: mmarinschek $)
41  * @author Anton Koinov
42  * @version $Revision: 1.16 $ $Date: 2005/02/11 16:03:00 $
43  * $Log: HtmlCheckboxRendererBase.java,v $
44  * Revision 1.16 2005/02/11 16:03:00 mmarinschek
45  * solve bug in tabbed panel when datatable was displayed not on tab, but at the bottom of the datatable...
46  *
47  * Revision 1.15 2005/02/08 18:01:01 svieujot
48  * Typo.
49  *
50  * Revision 1.14 2005/01/18 22:43:05 svieujot
51  * Fix some bugs where converter wasn't used to determine selected values.
52  * This caused for examples the list, checkbox and radio based components to bug when the backing bean value type is a primitive.
53  *
54  * Revision 1.13 2005/01/07 01:54:36 svieujot
55  * radioRenderer wasn't looking at the submitted value.
56  *
57  * Revision 1.9 2004/10/13 11:51:01 matze renamed packages to org.apache
58  *
59  * Revision 1.8 2004/07/01 22:00:56 mwessendorf ASF switch
60  *
61  * Revision 1.7 2004/06/21 23:23:37 o_rossmueller fix #976411: removed </input>
62  * tag for selectMultiCheckbox fix #972165: also check for readonly and also
63  * check for selectMultiCheckbox
64  *
65  * Revision 1.6 2004/06/04 00:26:16 o_rossmueller modified renderes to comply
66  * with JSF 1.1
67  *
68  * Revision 1.5 2004/05/18 14:31:39 manolito user role support completely moved
69  * to components source tree
70  *
71  * Revision 1.4 2004/05/05 18:03:00 o_rossmueller fix #948110: render span
72  * element for styleClass
73  *
74  * Revision 1.3 2004/04/02 13:57:13 manolito extended HtmlSelectManyCheckbox
75  * with layout "spread" and custom Checkbox component
76  *
77  * Revision 1.2 2004/04/02 13:37:49 manolito no message
78  *
79  */

80 public class HtmlCheckboxRendererBase extends HtmlRenderer {
81     private static final Log log = LogFactory
82             .getLog(HtmlCheckboxRendererBase.class);
83
84     private static final String JavaDoc PAGE_DIRECTION = "pageDirection";
85
86     private static final String JavaDoc LINE_DIRECTION = "lineDirection";
87
88     private static final String JavaDoc EXTERNAL_TRUE_VALUE = "true";
89
90     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
91             throws IOException JavaDoc {
92         RendererUtils.checkParamValidity(facesContext, uiComponent, null);
93         if (uiComponent instanceof UISelectBoolean) {
94             Boolean JavaDoc value = RendererUtils.getBooleanValue( uiComponent );
95             boolean isChecked = value != null ? value.booleanValue() : false;
96             renderCheckbox(facesContext, uiComponent, EXTERNAL_TRUE_VALUE,
97                     null, isChecked, true);
98         } else if (uiComponent instanceof UISelectMany) {
99             renderCheckboxList(facesContext, (UISelectMany) uiComponent);
100         } else {
101             throw new IllegalArgumentException JavaDoc("Unsupported component class "
102                     + uiComponent.getClass().getName());
103         }
104     }
105
106     public void renderCheckboxList(FacesContext facesContext,
107             UISelectMany selectMany) throws IOException JavaDoc {
108
109         String JavaDoc layout = getLayout(selectMany);
110         boolean pageDirectionLayout = false; //Default to lineDirection
111
if (layout != null) {
112             if (layout.equals(PAGE_DIRECTION)) {
113                 pageDirectionLayout = true;
114             } else if (layout.equals(LINE_DIRECTION)) {
115                 pageDirectionLayout = false;
116             } else {
117                 log.error("Wrong layout attribute for component "
118                         + selectMany.getClientId(facesContext) + ": " + layout);
119             }
120         }
121
122         ResponseWriter writer = facesContext.getResponseWriter();
123
124         writer.startElement(HTML.TABLE_ELEM, selectMany);
125         HtmlRendererUtils.renderHTMLAttributes(writer, selectMany,
126                 HTML.SELECT_TABLE_PASSTHROUGH_ATTRIBUTES);
127         HtmlRendererUtils.writeIdIfNecessary(writer, selectMany, facesContext);
128
129         if (!pageDirectionLayout)
130             writer.startElement(HTML.TR_ELEM, selectMany);
131
132         Converter converter;
133         try {
134             converter = RendererUtils.findUISelectManyConverter(facesContext,
135                     selectMany);
136         } catch (FacesException e) {
137             log.error("Error finding Converter for component with id "
138                     + selectMany.getClientId(facesContext));
139             converter = null;
140         }
141
142         Set JavaDoc lookupSet = RendererUtils.getSubmittedValuesAsSet(facesContext, selectMany, converter, selectMany);
143         boolean useSubmittedValues = lookupSet != null;
144
145         if (!useSubmittedValues) {
146             lookupSet = RendererUtils.getSelectedValuesAsSet(facesContext, selectMany, converter, selectMany);
147         }
148
149         for (Iterator JavaDoc it = RendererUtils.getSelectItemList(selectMany)
150                 .iterator(); it.hasNext();) {
151             SelectItem selectItem = (SelectItem) it.next();
152             Object JavaDoc itemValue = selectItem.getValue(); // TODO : Check here for getSubmittedValue. Look at RendererUtils.getValue
153

154             String JavaDoc itemStrValue;
155             if (converter == null) {
156                 itemStrValue = itemValue.toString();
157             } else {
158                 itemStrValue = converter.getAsString(facesContext, selectMany,
159                         itemValue);
160             }
161
162             writer.write("\t\t");
163             if (pageDirectionLayout)
164                 writer.startElement(HTML.TR_ELEM, selectMany);
165             writer.startElement(HTML.TD_ELEM, selectMany);
166             writer.startElement(HTML.LABEL_ELEM, selectMany);
167
168             boolean checked = (useSubmittedValues && lookupSet
169                     .contains(itemStrValue))
170                     || (!useSubmittedValues && lookupSet.contains(itemValue));
171
172             renderCheckbox(facesContext, selectMany, itemStrValue, selectItem
173                     .getLabel(), checked, false);
174             writer.endElement(HTML.LABEL_ELEM);
175             writer.endElement(HTML.TD_ELEM);
176             if (pageDirectionLayout)
177                 writer.endElement(HTML.TR_ELEM);
178         }
179
180         if (!pageDirectionLayout)
181             writer.endElement(HTML.TR_ELEM);
182         writer.endElement(HTML.TABLE_ELEM);
183     }
184
185     protected String JavaDoc getLayout(UISelectMany selectMany) {
186         if (selectMany instanceof HtmlSelectManyCheckbox) {
187             return ((HtmlSelectManyCheckbox) selectMany).getLayout();
188         } else {
189             return (String JavaDoc) selectMany.getAttributes().get(JSFAttr.LAYOUT_ATTR);
190         }
191     }
192
193     protected void renderCheckbox(FacesContext facesContext,
194             UIComponent uiComponent, String JavaDoc value, String JavaDoc label,
195             boolean checked, boolean renderId) throws IOException JavaDoc {
196         String JavaDoc clientId = uiComponent.getClientId(facesContext);
197
198         ResponseWriter writer = facesContext.getResponseWriter();
199
200         writer.startElement(HTML.INPUT_ELEM, uiComponent);
201         writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_CHECKBOX, null);
202         writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
203         if (renderId) {
204             HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
205         }
206
207         if (checked) {
208             writer.writeAttribute(HTML.CHECKED_ATTR, HTML.CHECKED_ATTR, null);
209         }
210
211         if ((value != null) && (value.length() > 0)) {
212             writer.writeAttribute(HTML.VALUE_ATTR, value, null);
213         }
214
215         HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
216                 HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
217         if (isDisabled(facesContext, uiComponent)) {
218             writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null);
219         }
220         writer.writeText("", null); // close input
221
if ((label != null) && (label.length() > 0)) {
222             writer.write(HTML.NBSP_ENTITY);
223             writer.writeText(label, null);
224         }
225     }
226
227     protected boolean isDisabled(FacesContext facesContext,
228             UIComponent component) {
229         //TODO: overwrite in extended HtmlCheckboxRenderer and check for
230
// enabledOnUserRole
231
if (component instanceof HtmlSelectBooleanCheckbox) {
232             return ((HtmlSelectBooleanCheckbox) component).isDisabled();
233         } else if (component instanceof HtmlSelectManyCheckbox) {
234             return ((HtmlSelectManyCheckbox) component).isDisabled();
235         } else {
236             return RendererUtils.getBooleanAttribute(component,
237                     HTML.DISABLED_ATTR, false);
238         }
239     }
240
241     public void decode(FacesContext facesContext, UIComponent component) {
242         RendererUtils.checkParamValidity(facesContext, component, null);
243         if (component instanceof UISelectBoolean) {
244             HtmlRendererUtils.decodeUISelectBoolean(facesContext, component);
245         } else if (component instanceof UISelectMany) {
246             HtmlRendererUtils.decodeUISelectMany(facesContext, component);
247         } else {
248             throw new IllegalArgumentException JavaDoc("Unsupported component class "
249                     + component.getClass().getName());
250         }
251     }
252
253     public Object JavaDoc getConvertedValue(FacesContext facesContext,
254             UIComponent component, Object JavaDoc submittedValue)
255             throws ConverterException {
256         RendererUtils.checkParamValidity(facesContext, component, null);
257         if (component instanceof UISelectBoolean) {
258             return submittedValue;
259         } else if (component instanceof UISelectMany) {
260             return RendererUtils.getConvertedUISelectManyValue(facesContext,
261                     (UISelectMany) component, submittedValue);
262         } else {
263             throw new IllegalArgumentException JavaDoc("Unsupported component class "
264                     + component.getClass().getName());
265         }
266     }
267 }
Popular Tags