KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > customizers > webapp > LocaleMapping


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * LocaleMapping.java
21  *
22  * Created on December 10, 2003, 3:22 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.webapp;
26
27 import java.util.Comparator JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30 import java.util.SortedMap JavaDoc;
31 import java.util.TreeMap JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33
34 import java.text.MessageFormat JavaDoc;
35
36 /** Object for nice usage of Locales in comboboxes, sorted lists, etc.
37  * Provides same equality properties as Locale (but with LocaleMapping)
38  * but a better "display name" via toString().
39  *
40  * There are also several static utility methods for finding and creating
41  * Locales and LocaleMappings.
42  *
43  * @author Peter Williams
44  */

45 public class LocaleMapping implements Comparable JavaDoc {
46     
47     private static final ResourceBundle JavaDoc webappBundle = ResourceBundle.getBundle(
48         "org.netbeans.modules.j2ee.sun.share.configbean.customizers.webapp.Bundle"); // NOI18N
49

50     private Locale JavaDoc locale;
51     private String JavaDoc displayText;
52     private boolean textOutOfDate;
53
54     /** Create a mapping for a locale
55      *
56      * @param l The locale for this mapping
57      */

58     public LocaleMapping(final Locale JavaDoc l) {
59         locale = l;
60         displayText = buildDisplayText();
61     }
62
63     /** equals() maps to Locale.equals()
64      *
65      * @return true/false based on whether the embedded locale objects compare
66      * as equal.
67      */

68     public boolean equals(Object JavaDoc o) {
69         boolean result = false;
70
71         if(o instanceof LocaleMapping) {
72             LocaleMapping lm = (LocaleMapping) o;
73             result = locale.equals(lm.getLocale());
74         }
75
76         return result;
77     }
78
79     /** hashCode() maps to Locale.hashCode()
80      *
81      * @return the hashcode
82      */

83     public int hashCode() {
84         return locale.hashCode();
85     }
86
87     /** A more readable display string
88      *
89      * @return A descriptive string
90      */

91     public String JavaDoc toString() {
92         if(textOutOfDate) {
93             displayText = buildDisplayText();
94         }
95
96         return displayText;
97     }
98
99     /** The locale
100      *
101      * @return the locale this is a mapping for
102      */

103     public Locale JavaDoc getLocale() {
104         return locale;
105     }
106
107     /** Force the display text to be recalculated. Recalculation won't happen
108      * until next time text is requested.
109      */

110     public void updateDisplayText() {
111         textOutOfDate = true;
112     }
113
114     private String JavaDoc buildDisplayText() {
115         Object JavaDoc [] args = new Object JavaDoc [] { locale.toString(), locale.getDisplayName() };
116         String JavaDoc result = MessageFormat.format(
117             webappBundle.getString("LBL_LocaleComboBoxDisplayText"), args); // NOI18N
118

119         if(result == null || result.length() == 0) {
120             result = webappBundle.getString("LBL_UnnamedLocale"); // NOI18N
121
}
122
123         textOutOfDate = false;
124
125         return result;
126     }
127
128     /** For sorted collections. We compare the string representations of the
129      * embedded locale.
130      *
131      * @param obj the LocaleMapping to compare to
132      * @return result of comparison (negative, 0, or positive depending on match)
133      */

134     public int compareTo(Object JavaDoc obj) {
135         int result = -1;
136
137         if(obj instanceof LocaleMapping) {
138             LocaleMapping targetMapping = (LocaleMapping) obj;
139             result = locale.toString().compareTo(targetMapping.getLocale().toString());
140         }
141
142         return result;
143     }
144
145     private static SortedMap JavaDoc sortedLocaleMappings = getSortedAvailableLocaleMappings();
146
147     /** Return a sorted map containg mappings for all locales supported by the
148      * current JVM.
149      *
150      * @return SortedMap containing LocaleMapping objects
151      */

152     public static SortedMap JavaDoc getSortedAvailableLocaleMappings() {
153         if(sortedLocaleMappings == null) {
154             Locale JavaDoc [] isoInstalledLocales = Locale.getAvailableLocales();
155
156             sortedLocaleMappings = new TreeMap JavaDoc(new LocaleComparator());
157             for(int i = 0; i < isoInstalledLocales.length; i++) {
158                 sortedLocaleMappings.put(isoInstalledLocales[i], new LocaleMapping(isoInstalledLocales[i]));
159             }
160         }
161
162         return sortedLocaleMappings;
163     }
164
165     /** Retrieve the LocaleMapping object matching this locale.
166      *
167      * @param l Locale to search for.
168      * @return LocaleMapping matching the passed in locale. Null if not found.
169      */

170     public static LocaleMapping getLocaleMapping(Locale JavaDoc l) {
171         return (LocaleMapping) sortedLocaleMappings.get(l);
172     }
173
174     /** Retrieve the LocaleMapping object matching the locale string
175      *
176      * @param ls String representing a locale in AA_AA_AA format (e.g. en_US)
177      * @return LocaleMapping matching the passed in locale. Null if not found.
178      */

179     public static LocaleMapping getLocaleMapping(String JavaDoc ls) {
180         return (LocaleMapping) sortedLocaleMappings.get(getLocale(ls));
181     }
182
183     /** Construct a locale from a locale string. We break up the string and use
184      * the correct Locale constructor.
185      *
186      * @param localeSpec String representing a locale in AA_AA_AA format (e.g. en_US)
187      * @return A new Locale object representing the passed in locale or null if
188      * string was blank or empty.
189      */

190     public static Locale JavaDoc getLocale(String JavaDoc localeSpec) {
191         Locale JavaDoc result = null;
192
193         if(localeSpec != null) {
194             // !PW Split locale string into it's component parts, as needed for
195
// Locale constructor
196
String JavaDoc [] parts = localeSpec.split("_", 3); // NOI18N
197

198             if(parts.length >= 1) { // LANGUAGE ONLY
199
String JavaDoc language = parts[0];
200                 if(language == null) {
201                     language = ""; // NOI18N
202
}
203
204                 if(parts.length >= 2) { // LANGUAGE, COUNTRY
205
String JavaDoc country = parts[1];
206                     if(country == null) {
207                         country = ""; // NOI18N
208
}
209
210                     if(parts.length >= 3) { // LANGUAGE, COUNTRY, VARIANT
211
String JavaDoc variant = parts[2];
212                         if(variant == null) {
213                             variant = ""; // NOI18N
214
}
215                         // three arguments
216
result = new Locale JavaDoc(language, country, variant);
217                     } else {
218                         // two arguments
219
result = new Locale JavaDoc(language, country);
220                     }
221                 } else {
222                     // one argument
223
result = new Locale JavaDoc(language);
224                 }
225             }
226         }
227
228         return result;
229     }
230
231     /** Comparator used to compare two LocaleMappings for equivalency. Used by
232      * the SortedMap of Locales maintained by LocaleMapping.
233      */

234     public static class LocaleComparator implements Comparator JavaDoc {
235         /** Compare's two object for equivalency. In this case, both are expected
236          * to be instances of LocaleMapping and will compare based on their
237          * string representation (e.g. en_US).
238          *
239          * @param o1 First object to compare
240          * @param o2 Second object to compare
241          * @return negative, zero, or positive based on string representation.
242          */

243         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
244             int result = -1;
245
246             if(o1 instanceof Locale JavaDoc && o2 instanceof Locale JavaDoc) {
247                 Locale JavaDoc l1 = (Locale JavaDoc) o1;
248                 Locale JavaDoc l2 = (Locale JavaDoc) o2;
249
250                 result = l1.toString().compareTo(l2.toString());
251             }
252
253             return result;
254         }
255     }
256 }
257
Popular Tags