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.util.ArrayList;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.Set;
27  import java.util.TreeMap;
28  
29  import javax.faces.component.UISelectItems;
30  import javax.faces.component.UIViewRoot;
31  import javax.faces.context.FacesContext;
32  import javax.faces.model.SelectItem;
33  
34  import org.apache.myfaces.component.html.ext.HtmlSelectOneMenu;
35  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
36  
37  /**
38   * A localized list of languages choose box. The value binds to the 
39   * language ISO 639 code (lowercase). This is the same code as 
40   * for java.util.Locale.getLanguage(). The official codes 
41   * list is available here : 
42   * 
43   * http://www.loc.gov/standards/iso639-2/englangn.html 
44   * 
45   * Unless otherwise specified, all attributes accept static values or EL expressions.
46   * 
47   * @JSFComponent
48   *   name = "t:selectOneLanguage"
49   *   class = "org.apache.myfaces.custom.selectOneLanguage.SelectOneLanguage"
50   *   tagClass = "org.apache.myfaces.custom.selectOneLanguage.SelectOneLanguageTag"
51   * @since 1.1.7
52   * @author Sylvain Vieujot (latest modification by $Author: lu4242 $)
53   * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (Wed, 03 Sep 2008) $
54   */
55  public abstract class AbstractSelectOneLanguage extends HtmlSelectOneMenu {
56      public static final String COMPONENT_TYPE = "org.apache.myfaces.SelectOneLanguage";
57      private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.SelectOneLanguageRenderer";
58  
59      private Integer _maxLength = null;
60      
61      private String _emptySelection = null;
62  
63      public AbstractSelectOneLanguage() {
64          setRendererType(DEFAULT_RENDERER_TYPE);
65      }
66  
67      /**
68       * Integer equals to the maximum number of characters in the language name.
69       * 
70       * @JSFProperty
71       */
72      public abstract Integer getMaxLength();
73      
74      /**
75       * Label and value to be used when displaying an empty selection
76       * 
77       * @JSFProperty
78       */
79      public abstract String getEmptySelection();
80  
81      private Set getFilterSet(){
82          List selectItems = RendererUtils.getSelectItemList( this );
83          Set set = new HashSet( selectItems.size() );
84  
85          for (Iterator i = selectItems.iterator(); i.hasNext(); )
86              set.add( ((SelectItem)i.next()).getValue().toString().toLowerCase() );
87  
88          return set;
89      }
90  
91      protected List getLanguagesChoicesAsSelectItemList(){
92          //return RendererUtils.getSelectItemList(component);
93  
94          Set filterSet = getFilterSet();
95  
96          String[] availableLanguages = Locale.getISOLanguages();
97  
98          Locale currentLocale;
99  
100         FacesContext facesContext = FacesContext.getCurrentInstance();
101         UIViewRoot viewRoot = facesContext.getViewRoot();
102         if( viewRoot != null )
103             currentLocale = viewRoot.getLocale();
104         else
105             currentLocale = facesContext.getApplication().getDefaultLocale();
106 
107 
108         TreeMap map = new TreeMap();
109         // TreeMap is sorted according to the keys' natural order
110 
111         for(int i=0; i<availableLanguages.length; i++){
112             String languageCode = availableLanguages[i];
113             if( ! filterSet.isEmpty() && ! filterSet.contains(languageCode))
114                 continue;
115             Locale tmp = new Locale(languageCode);
116             map.put(tmp.getDisplayLanguage(currentLocale), languageCode);
117         }
118 
119         List languagesSelectItems = new ArrayList( map.size() );
120         if(getEmptySelection() != null)
121             languagesSelectItems.add(new SelectItem("", getEmptySelection()));
122 
123         Integer maxLength = getMaxLength();
124         int maxDescriptionLength = maxLength==null ? Integer.MAX_VALUE : maxLength.intValue();
125         if( maxDescriptionLength < 5 )
126             maxDescriptionLength = 5;
127 
128         for(Iterator i = map.keySet().iterator(); i.hasNext(); ){
129             String languageName = (String) i.next();
130             String languageCode = (String) map.get( languageName );
131             String label;
132             if( languageName.length() <= maxDescriptionLength )
133                 label = languageName;
134             else
135                 label = languageName.substring(0, maxDescriptionLength-3)+"...";
136 
137             languagesSelectItems.add( new SelectItem(languageCode, label) );
138         }
139 
140         return languagesSelectItems;
141     }
142 
143     protected void validateValue(FacesContext context, Object value) {
144         UISelectItems selectItems = new UISelectItems();
145         selectItems.setTransient(true);
146         selectItems.setValue(getLanguagesChoicesAsSelectItemList());
147         getChildren().add(selectItems);
148         
149         super.validateValue(context,value);
150     }
151 }