1 16 package com.google.gwt.i18n.client; 17 18 import com.google.gwt.core.client.JavaScriptObject; 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Map ; 25 import java.util.MissingResourceException ; 26 import java.util.Set ; 27 28 76 public final class Dictionary { 77 78 private static Map cache = new HashMap (); 79 private static final int MAX_KEYS_TO_SHOW = 20; 80 81 89 public static Dictionary getDictionary(String name) { 90 Dictionary target = (Dictionary) cache.get(name); 91 if (target == null) { 92 target = new Dictionary(name); 93 cache.put(name, target); 94 } 95 return target; 96 } 97 98 static void resourceErrorBadType(String name) { 99 throw new MissingResourceException ("'" + name 100 + "' is not a JavaScript object and cannot be used as a Dictionary", 101 null, name); 102 } 103 104 private JavaScriptObject dict; 105 106 private String label; 107 108 113 private Dictionary(String name) { 114 if (name == null || "".equals(name)) { 115 throw new IllegalArgumentException ( 116 "Cannot create a Dictionary with a null or empty name"); 117 } 118 this.label = "Dictionary " + name; 119 attach(name); 120 if (dict == null) { 121 throw new MissingResourceException ( 122 "Cannot find JavaScript object with the name '" + name + "'", name, 123 null); 124 } 125 } 126 127 138 public native String get(String key) ; 147 148 153 public Set keySet() { 154 HashSet s = new HashSet (); 155 addKeys(s); 156 return s; 157 } 158 159 public String toString() { 160 return label; 161 } 162 163 168 public Collection values() { 169 ArrayList s = new ArrayList (); 170 addValues(s); 171 return s; 172 } 173 174 void resourceError(String key) { 175 Collection s = this.keySet(); 176 String error = "Cannot find '" + key + "' in " + this; 177 if (s.size() < MAX_KEYS_TO_SHOW) { 178 error += "\n keys found: " + s; 179 } 180 throw new MissingResourceException (error, this.toString(), key); 181 } 182 183 private native void addKeys(HashSet s) ; 188 189 private native void addValues(ArrayList s) ; 195 196 private native void attach(String name); 206 } 207 | Popular Tags |