001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor;
015:
016: import java.util.ArrayList;
017: import java.util.Arrays;
018: import java.util.HashMap;
019: import java.util.MissingResourceException;
020:
021: /**
022: * All the strings that should be localized will go through this place. Multiple
023: * custom localizers can be registered. The strings already retrieved are cached
024: * for the faster performance.
025: *
026: * @author Miloslav Metelka
027: * @version 1.00
028: */
029:
030: public class LocaleSupport {
031:
032: private static final String NULL_STRING = new String();
033:
034: /** Cache for the retrieved strings */
035: private static final HashMap cache = new HashMap(503);
036:
037: private static Localizer[] localizers = new Localizer[0];
038:
039: /**
040: * Add a new localizer to the localizer array. The array of localizers is
041: * tracked from the lastly added localizer to the firstly added one until
042: * the translation for the given key is found.
043: *
044: * @param localizer
045: * localizer to add to the localizer array.
046: */
047: public static void addLocalizer(Localizer localizer) {
048: ArrayList ll = new ArrayList(Arrays.asList(localizers));
049: ll.add(localizer);
050: Localizer[] la = new Localizer[ll.size()];
051: ll.toArray(la);
052: localizers = la;
053:
054: cache.clear();
055: }
056:
057: /**
058: * Remove the existing localizer from the localizer array.
059: *
060: * @param localizer
061: * localizer to remove.
062: */
063: public static void removeLocalizer(Localizer localizer) {
064: ArrayList ll = new ArrayList(Arrays.asList(localizers));
065: ll.remove(localizer);
066: Localizer[] la = new Localizer[ll.size()];
067: ll.toArray(la);
068: localizers = la;
069:
070: cache.clear();
071: }
072:
073: /**
074: * Get the localized string for the given key using the registered
075: * localizers.
076: *
077: * @param key
078: * key to translate to localized string.
079: * @return localized string or null if there's no localization.
080: */
081: public static synchronized String getString(String key) {
082: String ret = (String) cache.get(key);
083: if (ret == null) {
084: for (int i = localizers.length - 1; i >= 0; i--) {
085:
086: // Try to find a return value
087: try {
088: ret = localizers[i].getString(key);
089: } catch (MissingResourceException e) { // localizers are often
090: // bundles
091: ret = null;
092: }
093:
094: if (ret != null) {
095: break;
096: }
097: }
098:
099: if (ret == null) {
100: ret = NULL_STRING;
101: }
102: cache.put(key, ret);
103: }
104:
105: return (ret != NULL_STRING) ? ret : null;
106: }
107:
108: /**
109: * Get the localized string or the default value if no translation for the
110: * given key exists.
111: *
112: * @param key
113: * key to translate to localized string.
114: * @param defaultValue
115: * default value to be returned in case no localized string is
116: * found for the given key.
117: */
118: public static String getString(String key, String defaultValue) {
119: String ret = getString(key);
120: return (ret != null) ? ret : defaultValue;
121: }
122:
123: /**
124: * Get the localized character or the default value if no translation for
125: * the given key exists. This method is mainly usable for getting localized
126: * mnemonics.
127: *
128: * @param key
129: * key to translate to localized character.
130: * @param defaultValue
131: * default value to be returned in case no localized character is
132: * found for the given key.
133: */
134: public static char getChar(String key, char defaultValue) {
135: String value = getString(key);
136: if (value == null || value.length() < 1)
137: return defaultValue;
138: return value.charAt(0);
139: }
140:
141: /**
142: * Translates the keys to the localized strings. There can be multiple
143: * localizers registered in the locale support.
144: */
145: public interface Localizer {
146:
147: /**
148: * Translate the key to the localized string.
149: *
150: * @param key
151: * key to translate to the localized string.
152: */
153: public String getString(String key);
154:
155: }
156:
157: }
|