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.selectOneLanguage;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Locale;
24  import java.util.Set;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.context.ResponseWriter;
29  import javax.faces.convert.Converter;
30  
31  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
32  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
33  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
34  import org.apache.myfaces.renderkit.html.ext.HtmlMenuRenderer;
35  
36  /**
37   * @JSFRenderer
38   *   renderKitId = "HTML_BASIC"
39   *   family = "javax.faces.SelectOne"
40   *   type = "org.apache.myfaces.SelectOneLanguageRenderer"
41   * 
42   * @author Sylvain Vieujot (latest modification by $Author: skitching $)
43   * @version $Revision: 673833 $ $Date: 2005-05-11 12:14:23 -0400 (Wed, 11 May 2005) $
44   */
45  public class SelectOneLanguageRenderer extends HtmlMenuRenderer {
46      
47      public void encodeEnd(FacesContext facesContext, UIComponent component)
48      throws IOException
49      {
50          RendererUtils.checkParamValidity(facesContext, component, null);
51  
52          SelectOneLanguage selectOneLanguage = (SelectOneLanguage) component;
53          ResponseWriter writer = facesContext.getResponseWriter();
54          
55          if(HtmlRendererUtils.isDisplayValueOnly(component))
56          {
57              //HtmlRendererUtils.renderDisplayValueOnlyForSelects(facesContext, component);
58              writer.startElement(HTML.SPAN_ELEM, selectOneLanguage);
59              HtmlRendererUtils.writeIdIfNecessary(writer, selectOneLanguage, facesContext);
60          
61              String[] supportedAttributes = {HTML.STYLE_CLASS_ATTR, HTML.STYLE_ATTR};
62              HtmlRendererUtils.renderHTMLAttributes(writer, selectOneLanguage, supportedAttributes);
63              
64              String languageCode = selectOneLanguage.getValue().toString();
65              String languageName = new Locale(languageCode).getDisplayLanguage( facesContext.getViewRoot().getLocale() );
66              
67              writer.write( languageName );
68              
69              writer.endElement(HTML.SPAN_ELEM);
70              return;
71          }
72          
73          writer.startElement(HTML.SELECT_ELEM, component);
74          HtmlRendererUtils.writeIdIfNecessary(writer, selectOneLanguage, facesContext);
75          writer.writeAttribute(HTML.NAME_ATTR, selectOneLanguage.getClientId(facesContext), null);
76  
77          List selectItemList = selectOneLanguage.getLanguagesChoicesAsSelectItemList();
78          Converter converter = HtmlRendererUtils.findUIOutputConverterFailSafe(facesContext, selectOneLanguage);
79  
80          writer.writeAttribute(HTML.SIZE_ATTR, "1", null);
81  
82          HtmlRendererUtils.renderHTMLAttributes(writer, selectOneLanguage, HTML.SELECT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
83          if ( isDisabled(facesContext, selectOneLanguage) ) {
84              writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null);
85          }
86  
87          Set lookupSet = HtmlRendererUtils.getSubmittedOrSelectedValuesAsSet(false, selectOneLanguage, facesContext, converter);
88  
89          HtmlRendererUtils.renderSelectOptions(facesContext, selectOneLanguage, converter, lookupSet, selectItemList);
90          // bug #970747: force separate end tag
91          writer.writeText("", null);
92          writer.endElement(HTML.SELECT_ELEM);
93      }
94  }