KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > selectOneCountry > SelectOneCountry


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.selectOneCountry;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.TreeMap JavaDoc;
23
24 import javax.faces.component.UISelectItem;
25 import javax.faces.component.UIViewRoot;
26 import javax.faces.context.FacesContext;
27 import javax.faces.el.ValueBinding;
28 import javax.faces.model.SelectItem;
29
30 import org.apache.myfaces.component.html.ext.HtmlSelectOneMenu;
31
32 /**
33  * @author Sylvain Vieujot (latest modification by $Author: svieujot $)
34  * @version $Revision: 1.1 $ $Date: 2005/02/09 13:05:20 $
35  * $Log: SelectOneCountry.java,v $
36  * Revision 1.1 2005/02/09 13:05:20 svieujot
37  * new x:selectOneCountry component
38  *
39  */

40 public class SelectOneCountry extends HtmlSelectOneMenu {
41     public static final String JavaDoc COMPONENT_TYPE = "org.apache.myfaces.SelectOneCountry";
42     private static final String JavaDoc DEFAULT_RENDERER_TYPE = "org.apache.myfaces.SelectOneCountryRenderer";
43     
44     private Integer JavaDoc _maxLength = null;
45     
46     public SelectOneCountry() {
47         setRendererType(DEFAULT_RENDERER_TYPE);
48     }
49     
50     public Integer JavaDoc getMaxLength() {
51         if (_maxLength != null) return _maxLength;
52         ValueBinding vb = getValueBinding("length");
53         return vb != null ? (Integer JavaDoc)vb.getValue(getFacesContext()) : null;
54     }
55     public void setMaxLength(Integer JavaDoc maxLength) {
56         _maxLength = maxLength;
57     }
58         
59     public Object JavaDoc saveState(FacesContext context) {
60         Object JavaDoc values[] = new Object JavaDoc[2];
61         values[0] = super.saveState(context);
62         values[1] = _maxLength;
63         return values;
64     }
65
66     public void restoreState(FacesContext context, Object JavaDoc state) {
67         Object JavaDoc values[] = (Object JavaDoc[])state;
68         super.restoreState(context, values[0]);
69         _maxLength = (Integer JavaDoc)values[1];
70     }
71     
72     // -------- Over ridden UIComponent methods -----------
73

74     public void encodeChildren(FacesContext context){
75         // noop
76
}
77     
78     public int getChildCount(){
79         return Locale.getISOCountries().length;
80     }
81     
82     public List JavaDoc getChildren(){
83         String JavaDoc[] availableCountries = Locale.getISOCountries();
84         
85         Locale JavaDoc currentLocale;
86         
87         FacesContext facesContext = FacesContext.getCurrentInstance();
88         UIViewRoot viewRoot = facesContext.getViewRoot();
89         if( viewRoot != null )
90             currentLocale = viewRoot.getLocale();
91         else
92             currentLocale = facesContext.getApplication().getDefaultLocale();
93
94         
95         TreeMap JavaDoc map = new TreeMap JavaDoc();
96         // TreeMap is sorted according to the keys' natural order
97

98         for(int i=0; i<availableCountries.length; i++){
99             String JavaDoc countryCode = availableCountries[i];
100             Locale JavaDoc tmp = new Locale JavaDoc(countryCode, countryCode);
101             map.put(tmp.getDisplayCountry(currentLocale), countryCode);
102         }
103         
104         List JavaDoc countriesSelectItems = new ArrayList JavaDoc(availableCountries.length);
105         
106         int maxDescriptionLength = _maxLength==null ? Integer.MAX_VALUE : _maxLength.intValue();
107         if( maxDescriptionLength < 5 )
108             maxDescriptionLength = 5;
109         
110         for(Iterator JavaDoc i = map.keySet().iterator(); i.hasNext(); ){
111             String JavaDoc countryName = (String JavaDoc) i.next();
112             String JavaDoc countryCode = (String JavaDoc) map.get( countryName );
113             String JavaDoc label;
114             if( countryName.length() <= maxDescriptionLength )
115                 label = countryName;
116             else{
117                 label = countryName.substring(0, maxDescriptionLength-3)+"...";
118             }
119             
120             UISelectItem selectItem = new UISelectItem();
121             selectItem.setValue( new SelectItem(countryCode, label) );
122             
123             countriesSelectItems.add( selectItem );
124         }
125         
126         return countriesSelectItems;
127     }
128 }
Popular Tags