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