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.shared.renderkit.html;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UISelectMany;
27  import javax.faces.component.UISelectOne;
28  import javax.faces.component.behavior.ClientBehavior;
29  import javax.faces.component.behavior.ClientBehaviorHolder;
30  import javax.faces.component.html.HtmlSelectManyMenu;
31  import javax.faces.component.html.HtmlSelectOneMenu;
32  import javax.faces.context.FacesContext;
33  import javax.faces.convert.Converter;
34  import javax.faces.convert.ConverterException;
35  
36  import org.apache.myfaces.shared.renderkit.RendererUtils;
37  import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
38  
39  /**
40   * X-CHECKED: tlddoc of h:selectManyListbox
41   */
42  public class HtmlMenuRendererBase
43          extends HtmlSelectableRendererBase
44  {
45      //private static final Log log = LogFactory.getLog(HtmlMenuRenderer.class);
46  
47      public void encodeEnd(FacesContext facesContext, UIComponent component)
48              throws IOException
49      {
50          RendererUtils.checkParamValidity(facesContext, component, null);
51          
52          Map<String, List<ClientBehavior>> behaviors = null;
53          if (component instanceof ClientBehaviorHolder)
54          {
55              behaviors = ((ClientBehaviorHolder) component).getClientBehaviors();
56              if (!behaviors.isEmpty())
57              {
58                  ResourceUtils.renderDefaultJsfJsInlineIfNecessary(
59                          facesContext, facesContext.getResponseWriter());
60              }
61          }
62  
63          if (component instanceof UISelectMany)
64          {
65              renderMenu(facesContext,
66                                           (UISelectMany)component,
67                                           isDisabled(facesContext, component),
68                                           getConverter(facesContext, component));
69          }
70          else if (component instanceof UISelectOne)
71          {
72              renderMenu(facesContext,
73                                           (UISelectOne)component,
74                                           isDisabled(facesContext, component),
75                                           getConverter(facesContext, component));
76          }
77          else
78          {
79              throw new IllegalArgumentException("Unsupported component class " + component.getClass().getName());
80          }
81      }
82      
83      protected void renderMenu(FacesContext facesContext,
84              UISelectOne selectOne, boolean disabled, Converter converter)
85              throws IOException
86      {
87          internalRenderSelect(facesContext, selectOne, disabled, 1, false,
88                  converter);
89      }
90  
91      protected void renderMenu(FacesContext facesContext,
92              UISelectMany selectMany, boolean disabled, Converter converter)
93              throws IOException
94      {
95          internalRenderSelect(facesContext, selectMany, disabled, 1, true,
96                  converter);
97      }
98  
99      protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
100     {
101         //TODO: overwrite in extended HtmlMenuRenderer and check for enabledOnUserRole
102         if (uiComponent instanceof HtmlSelectManyMenu)
103         {
104             return ((HtmlSelectManyMenu)uiComponent).isDisabled();
105         }
106         else if (uiComponent instanceof HtmlSelectOneMenu)
107         {
108             return ((HtmlSelectOneMenu)uiComponent).isDisabled();
109         }
110         else
111         {
112             return org.apache.myfaces.shared.renderkit.RendererUtils.getBooleanAttribute(uiComponent, 
113                     org.apache.myfaces.shared.renderkit.html.HTML.DISABLED_ATTR, false);
114         }
115     }
116 
117     public void decode(FacesContext facesContext, UIComponent uiComponent)
118     {
119         org.apache.myfaces.shared.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, null);
120 
121         if (uiComponent instanceof UISelectMany)
122         {
123             HtmlRendererUtils.decodeUISelectMany(facesContext, uiComponent);
124         }
125         else if (uiComponent instanceof UISelectOne)
126         {
127             HtmlRendererUtils.decodeUISelectOne(facesContext, uiComponent);
128         }
129         else
130         {
131             throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
132         }
133         if (uiComponent instanceof ClientBehaviorHolder &&
134                 !HtmlRendererUtils.isDisabled(uiComponent))
135         {
136             HtmlRendererUtils.decodeClientBehaviors(facesContext, uiComponent);
137         }
138     }
139 
140     public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue)
141          throws ConverterException
142     {
143         org.apache.myfaces.shared.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, null);
144 
145         if (uiComponent instanceof UISelectMany)
146         {
147             return org.apache.myfaces.shared.renderkit.RendererUtils.getConvertedUISelectManyValue(facesContext,
148                                                                (UISelectMany)uiComponent,
149                                                                submittedValue);
150         }
151         else if (uiComponent instanceof UISelectOne)
152         {
153             return org.apache.myfaces.shared.renderkit.RendererUtils.getConvertedUISelectOneValue(facesContext,
154                                                            (UISelectOne)uiComponent,
155                                                            submittedValue);
156         }
157         else
158         {
159             throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
160         }
161     }
162     
163     /**
164      * Gets the converter for the given component rendered by this renderer.
165      * @param facesContext
166      * @param component
167      * @return
168      */
169     protected Converter getConverter(FacesContext facesContext,
170             UIComponent component)
171     {
172         if (component instanceof UISelectMany)
173         {
174             return HtmlRendererUtils.findUISelectManyConverterFailsafe(facesContext, 
175                     (UISelectMany) component);
176         }
177         else if (component instanceof UISelectOne)
178         {
179             return HtmlRendererUtils.findUIOutputConverterFailSafe(facesContext, component);
180         }
181         return null;
182     }
183 
184 }