KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > ext > HtmlCheckboxRenderer


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.ext;
17
18 import org.apache.myfaces.component.UserRoleUtils;
19 import org.apache.myfaces.custom.checkbox.HtmlCheckbox;
20 import org.apache.myfaces.renderkit.RendererUtils;
21 import org.apache.myfaces.renderkit.html.HtmlCheckboxRendererBase;
22
23 import javax.faces.FacesException;
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.UISelectMany;
26 import javax.faces.context.FacesContext;
27 import javax.faces.convert.Converter;
28 import javax.faces.model.SelectItem;
29 import java.io.IOException JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32
33
34 /**
35  * @author Manfred Geiler (latest modification by $Author: svieujot $)
36  * @version $Revision: 1.8 $ $Date: 2005/01/18 22:43:05 $
37  * $Log: HtmlCheckboxRenderer.java,v $
38  * Revision 1.8 2005/01/18 22:43:05 svieujot
39  * Fix some bugs where converter wasn't used to determine selected values.
40  * This caused for examples the list, checkbox and radio based components to bug when the backing bean value type is a primitive.
41  *
42  * Revision 1.7 2004/10/13 11:50:59 matze
43  * renamed packages to org.apache
44  *
45  * Revision 1.6 2004/08/13 15:47:08 manolito
46  * No decode for spread checkbox or radio
47  *
48  * Revision 1.5 2004/07/01 21:53:06 mwessendorf
49  * ASF switch
50  *
51  * Revision 1.4 2004/06/04 00:26:16 o_rossmueller
52  * modified renderes to comply with JSF 1.1
53  *
54  * Revision 1.3 2004/05/18 14:31:38 manolito
55  * user role support completely moved to components source tree
56  *
57  * Revision 1.2 2004/04/05 09:11:03 manolito
58  * extended exception messages
59  *
60  * Revision 1.1 2004/04/02 13:57:11 manolito
61  * extended HtmlSelectManyCheckbox with layout "spread" and custom Checkbox component
62  *
63  */

64 public class HtmlCheckboxRenderer
65         extends HtmlCheckboxRendererBase
66 {
67     //private static final Log log = LogFactory.getLog(HtmlRadioRenderer.class);
68

69     private static final String JavaDoc LAYOUT_SPREAD = "spread";
70
71     public void encodeEnd(FacesContext context, UIComponent component) throws IOException JavaDoc
72     {
73         if (context == null) throw new NullPointerException JavaDoc("context");
74         if (component == null) throw new NullPointerException JavaDoc("component");
75
76         if (component instanceof HtmlCheckbox)
77         {
78             renderSingleCheckbox(context, (HtmlCheckbox)component);
79         }
80         else if (component instanceof UISelectMany)
81         {
82             String JavaDoc layout = getLayout((UISelectMany)component);
83             if (layout != null && layout.equals(LAYOUT_SPREAD))
84             {
85                 return; //checkbox inputs are rendered by spread checkbox components
86
}
87             else
88             {
89                 super.encodeEnd(context, component);
90             }
91         }
92         else
93         {
94             throw new IllegalArgumentException JavaDoc("Unsupported component class " + component.getClass().getName());
95         }
96     }
97
98
99     private void renderSingleCheckbox(FacesContext facesContext, HtmlCheckbox checkbox) throws IOException JavaDoc
100     {
101         String JavaDoc forAttr = checkbox.getFor();
102         if (forAttr == null)
103         {
104             throw new IllegalStateException JavaDoc("mandatory attribute 'for'");
105         }
106         int index = checkbox.getIndex();
107         if (index < 0)
108         {
109             throw new IllegalStateException JavaDoc("positive index must be given");
110         }
111
112         UIComponent uiComponent = checkbox.findComponent(forAttr);
113         if (uiComponent == null)
114         {
115             throw new IllegalStateException JavaDoc("Could not find component '" + forAttr + "' (calling findComponent on component '" + checkbox.getClientId(facesContext) + "')");
116         }
117         if (!(uiComponent instanceof UISelectMany))
118         {
119             throw new IllegalStateException JavaDoc("UISelectMany expected");
120         }
121
122         UISelectMany uiSelectMany = (UISelectMany)uiComponent;
123         Converter converter;
124         List JavaDoc selectItemList = RendererUtils.getSelectItemList(uiSelectMany);
125         if (index >= selectItemList.size())
126         {
127             throw new IndexOutOfBoundsException JavaDoc("index " + index + " >= " + selectItemList.size());
128         }
129
130         try
131         {
132             converter = RendererUtils.findUISelectManyConverter(facesContext, uiSelectMany);
133         }
134         catch (FacesException e)
135         {
136             converter = null;
137         }
138
139         SelectItem selectItem = (SelectItem)selectItemList.get(index);
140         Object JavaDoc itemValue = selectItem.getValue();
141         String JavaDoc itemStrValue;
142         if (converter == null)
143         {
144             itemStrValue = itemValue.toString();
145         }
146         else
147         {
148             itemStrValue = converter.getAsString(facesContext, uiSelectMany, itemValue);
149         }
150
151         //TODO: we must cache this Set!
152
Set JavaDoc lookupSet = RendererUtils.getSelectedValuesAsSet(facesContext, uiComponent, converter, uiSelectMany);
153
154         renderCheckbox(facesContext,
155                        uiSelectMany,
156                        itemStrValue,
157                        selectItem.getLabel(),
158                        lookupSet.contains(itemStrValue), true);
159     }
160
161
162     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
163     {
164         if (!UserRoleUtils.isEnabledOnUserRole(uiComponent))
165         {
166             return false;
167         }
168         else
169         {
170             return super.isDisabled(facesContext, uiComponent);
171         }
172     }
173
174
175     public void decode(FacesContext facesContext, UIComponent uiComponent)
176     {
177         if (uiComponent instanceof HtmlCheckbox)
178         {
179             //nothing to decode
180
}
181         else
182         {
183             super.decode(facesContext, uiComponent);
184         }
185     }
186 }
187
Popular Tags