KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > tree > renderkit > html > HtmlTreeCheckboxRenderer


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.custom.tree.renderkit.html;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import javax.faces.FacesException;
22 import javax.faces.component.UIComponent;
23 import javax.faces.component.UISelectMany;
24 import javax.faces.context.FacesContext;
25 import javax.faces.convert.Converter;
26
27 import org.apache.myfaces.custom.tree.HtmlTreeCheckbox;
28 import org.apache.myfaces.renderkit.RendererUtils;
29 import org.apache.myfaces.renderkit.html.HtmlCheckboxRendererBase;
30
31 /**
32  * @author <a HREF="mailto:dlestrat@apache.org">David Le Strat </a>
33  */

34 public class HtmlTreeCheckboxRenderer extends HtmlCheckboxRendererBase
35 {
36
37     /**
38      * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext,
39      * javax.faces.component.UIComponent)
40      */

41     public void encodeEnd(FacesContext context, UIComponent component) throws IOException JavaDoc
42     {
43         HtmlTreeCheckbox checkbox = (HtmlTreeCheckbox) component;
44
45         String JavaDoc forAttr = checkbox.getFor();
46         if (forAttr == null)
47         {
48             throw new IllegalStateException JavaDoc("Mandatory attribute 'for'");
49         }
50
51         UIComponent uiComponent = checkbox.findComponent(forAttr);
52         if (uiComponent == null)
53         {
54             throw new IllegalStateException JavaDoc("Could not find component '" + forAttr
55                     + "' (calling findComponent on component '" + checkbox.getClientId(context) + "')");
56         }
57         if (!(uiComponent instanceof UISelectMany))
58         {
59             throw new IllegalStateException JavaDoc("UISelectMany expected");
60         }
61
62         UISelectMany uiSelectMany = (UISelectMany) uiComponent;
63
64         Converter converter;
65         try
66         {
67             converter = RendererUtils.findUISelectManyConverter(context, uiSelectMany);
68         }
69         catch (FacesException e)
70         {
71             converter = null;
72         }
73         
74         Set JavaDoc lookupSet = RendererUtils.getSelectedValuesAsSet(context, component, converter, uiSelectMany);
75
76         Object JavaDoc itemValue = checkbox.getItemValue();
77         String JavaDoc itemStrValue = null;
78         if (converter == null)
79         {
80             if (null != itemValue)
81             {
82                 itemStrValue = itemValue.toString();
83             }
84         }
85         else
86         {
87             itemStrValue = converter.getAsString(context, uiSelectMany, itemValue);
88         }
89
90         renderCheckbox(context, uiSelectMany, itemStrValue, checkbox.getItemLabel(), lookupSet.contains(itemStrValue),
91                 true);
92     }
93
94     /**
95      * @see org.apache.myfaces.renderkit.html.HtmlCheckboxRendererBase#isDisabled(javax.faces.context.FacesContext,
96      * javax.faces.component.UIComponent)
97      */

98     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
99     {
100         return super.isDisabled(facesContext, uiComponent);
101     }
102
103     /**
104      * @see javax.faces.render.Renderer#decode(javax.faces.context.FacesContext,
105      * javax.faces.component.UIComponent)
106      */

107     public void decode(FacesContext facesContext, UIComponent uiComponent)
108     {
109         if (uiComponent instanceof HtmlTreeCheckbox)
110         {
111             //nothing to decode
112
}
113         else
114         {
115             super.decode(facesContext, uiComponent);
116         }
117     }
118 }
119
Popular Tags