View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.tree.renderkit.html;
20  
21  import java.io.IOException;
22  import java.util.Set;
23  
24  import javax.faces.FacesException;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UISelectMany;
27  import javax.faces.context.FacesContext;
28  import javax.faces.convert.Converter;
29  
30  import org.apache.myfaces.custom.tree.HtmlTreeCheckbox;
31  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
32  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlCheckboxRendererBase;
33  
34  /**
35   * @JSFRenderer
36   *   renderKitId = "HTML_BASIC" 
37   *   family = "org.apache.myfaces.HtmlTreeCheckbox"
38   *   type = "org.apache.myfaces.HtmlTreeCheckbox"
39   *   
40   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
41   */
42  public class HtmlTreeCheckboxRenderer extends HtmlCheckboxRendererBase
43  {
44  
45      /**
46       * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext,
47       *      javax.faces.component.UIComponent)
48       */
49      public void encodeEnd(FacesContext context, UIComponent component) throws IOException
50      {
51          HtmlTreeCheckbox checkbox = (HtmlTreeCheckbox) component;
52  
53          String forAttr = checkbox.getFor();
54          if (forAttr == null)
55          {
56              throw new IllegalStateException("Mandatory attribute 'for'");
57          }
58  
59          UIComponent uiComponent = checkbox.findComponent(forAttr);
60          if (uiComponent == null)
61          {
62              throw new IllegalStateException("Could not find component '" + forAttr
63                                              + "' (calling findComponent on component '" + checkbox.getClientId(context) + "')");
64          }
65          if (!(uiComponent instanceof UISelectMany))
66          {
67              throw new IllegalStateException("UISelectMany expected");
68          }
69  
70          UISelectMany uiSelectMany = (UISelectMany) uiComponent;
71  
72          Converter converter;
73          try
74          {
75              converter = RendererUtils.findUISelectManyConverter(context, uiSelectMany);
76          }
77          catch (FacesException e)
78          {
79              converter = null;
80          }
81  
82          Set lookupSet = RendererUtils.getSelectedValuesAsSet(context, component, converter, uiSelectMany);
83  
84          Object itemValue = checkbox.getItemValue();
85          String itemStrValue = null;
86          if (converter == null)
87          {
88              if (null != itemValue)
89              {
90                  itemStrValue = itemValue.toString();
91              }
92          }
93          else
94          {
95              itemStrValue = converter.getAsString(context, uiSelectMany, itemValue);
96          }
97  
98          renderCheckbox(context,
99                         uiSelectMany,
100                        itemStrValue,
101                        checkbox.getItemLabel(),
102                        isDisabled(context,uiSelectMany),
103                        lookupSet.contains(itemStrValue),
104                        true);
105     }
106 
107     /**
108      * @see org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlCheckboxRendererBase#isDisabled(javax.faces.context.FacesContext,
109      *      javax.faces.component.UIComponent)
110      */
111     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
112     {
113         return super.isDisabled(facesContext, uiComponent);
114     }
115 
116     /**
117      * @see javax.faces.render.Renderer#decode(javax.faces.context.FacesContext,
118      *      javax.faces.component.UIComponent)
119      */
120     public void decode(FacesContext facesContext, UIComponent uiComponent)
121     {
122         if (uiComponent instanceof HtmlTreeCheckbox)
123         {
124             //nothing to decode
125         }
126         else
127         {
128             super.decode(facesContext, uiComponent);
129         }
130     }
131 }