KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > LocaleSupport


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 package org.netbeans.editor;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.MissingResourceException JavaDoc;
26
27 /**
28 * All the strings that should be localized will go through this place.
29 * Multiple custom localizers can be registered.
30 *
31 * @deprecated this class is deprecated as the number of bundle queries is proportional
32  * to the number of registered localizers which is not optimal from the performance
33  * point of view. The queries through this class should be replaced
34  * by queries to individual bundles.
35 *
36 * @author Miloslav Metelka
37 * @version 1.00
38 */

39
40 public class LocaleSupport {
41     
42     private static final boolean debug
43             = Boolean.getBoolean("netbeans.debug.editor.localesupport");
44     
45 // Fix of #36754 - localizers memory consumption
46
// private static final String NULL_STRING = new String();
47

48     /** Cache for the retrieved strings */
49     // Fix of #36754 - localizers memory consumption
50
// private static final HashMap cache = new HashMap(503);
51

52     private static Localizer[] localizers = new Localizer[0];
53
54     /** Add a new localizer to the localizer array. The array of localizers
55     * is tracked from the lastly added localizer to the firstly added one
56     * until the translation for the given key is found.
57     * @param localizer localizer to add to the localizer array.
58     */

59     public static void addLocalizer(Localizer localizer) {
60         ArrayList JavaDoc ll = new ArrayList JavaDoc(Arrays.asList(localizers));
61         ll.add(localizer);
62         Localizer[] la = new Localizer[ll.size()];
63         ll.toArray(la);
64         localizers = la;
65
66 // cache.clear();
67
}
68
69     /** Remove the existing localizer from the localizer array.
70     * @param localizer localizer to remove.
71     */

72     public static void removeLocalizer(Localizer localizer) {
73         ArrayList JavaDoc ll = new ArrayList JavaDoc(Arrays.asList(localizers));
74         ll.remove(localizer);
75         Localizer[] la = new Localizer[ll.size()];
76         ll.toArray(la);
77         localizers = la;
78
79 // cache.clear();
80
}
81
82     /** Get the localized string for the given key using the registered
83     * localizers.
84     * @param key key to translate to localized string.
85     * @return localized string or null if there's no localization.
86     */

87     public static synchronized String JavaDoc getString(String JavaDoc key) {
88         // Fix of #36754 - localizers memory consumption
89
String JavaDoc ret = null;
90         // String ret = (String)cache.get(key);
91
//if (ret == null) {
92
int i;
93             for (i = localizers.length - 1; i >= 0; i--) {
94
95                 // Try to find a return value
96
try {
97                     ret = localizers[i].getString(key);
98                 } catch (MissingResourceException JavaDoc e) { // localizers are often bundles
99
ret = null;
100                 }
101
102                 if (ret != null) {
103                     break;
104                 }
105             }
106
107 // if (ret == null) {
108
// ret = NULL_STRING;
109
// }
110
// cache.put(key, ret);
111
//}
112

113 // return (ret != NULL_STRING) ? ret : null;
114
if (debug) {
115             String JavaDoc inLocalizerString = (i >= 0)
116                 ? " found in localizer=" + localizers[i] // NOI18N
117
: ""; // NOI18N
118
/*DEBUG*/System.err.println("LocaleSupport.getString(): key=\"" + key + // NOI18N
119
"\", value=\"" + ret + "\"" + inLocalizerString);
120             /*DEBUG*/Thread.dumpStack();
121         }
122         return ret;
123     }
124
125     /** Get the localized string or the default value if no translation
126     * for the given key exists.
127     * @param key key to translate to localized string.
128     * @param defaultValue default value to be returned in case no localized
129     * string is found for the given key.
130     */

131     public static String JavaDoc getString(String JavaDoc key, String JavaDoc defaultValue) {
132         String JavaDoc ret = getString(key);
133         return (ret != null) ? ret : defaultValue;
134     }
135     
136     /** Get the localized character or the default value if no translation
137      * for the given key exists. This method is mainly usable for getting
138      * localized mnemonics.
139      * @param key key to translate to localized character.
140      * @param defaultValue default value to be returned in case no localized
141      * character is found for the given key.
142      */

143     public static char getChar( String JavaDoc key, char defaultValue ) {
144         String JavaDoc value = getString( key );
145         if( value == null || value.length() < 1 ) return defaultValue;
146         return value.charAt( 0 );
147     }
148
149     /** Translates the keys to the localized strings. There can be multiple localizers
150     * registered in the locale support.
151     */

152     public interface Localizer {
153
154         /** Translate the key to the localized string.
155         * @param key key to translate to the localized string.
156         */

157         public String JavaDoc getString(String JavaDoc key);
158
159     }
160
161 }
162
Popular Tags