1 19 24 25 package org.netbeans.modules.j2ee.sun.share; 26 27 import java.nio.charset.Charset ; 28 29 import java.util.Collection ; 30 import java.util.Comparator ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.ResourceBundle ; 34 import java.util.SortedMap ; 35 import java.util.TreeMap ; 36 37 import java.text.MessageFormat ; 38 39 import java.beans.PropertyChangeSupport ; 40 import java.beans.PropertyChangeListener ; 41 42 51 public class CharsetMapping implements Comparable { 52 53 private static final ResourceBundle webappBundle = ResourceBundle.getBundle( 54 "org.netbeans.modules.j2ee.sun.share.Bundle"); 56 private Charset charset; 57 private String chosenAlias; 58 private boolean showAliases; 59 private String displayText; 60 private boolean textOutOfDate; 61 62 66 public CharsetMapping(final Charset c) { 67 this(c, c.displayName(), true); 68 } 69 70 76 public CharsetMapping(final Charset c, boolean sa) { 77 this(c, c.displayName(), sa); 78 } 79 80 85 public CharsetMapping(final Charset c, String alias) { 86 this(c, alias, false); 87 } 88 89 private CharsetMapping(final Charset c, String alias, boolean sa) { 90 charset = c; 91 chosenAlias = alias; 92 showAliases = sa; 93 displayText = buildDisplayText(); 94 } 95 96 101 public boolean equals(Object o) { 102 boolean result = false; 103 104 if(o instanceof CharsetMapping) { 105 CharsetMapping cm = (CharsetMapping) o; 106 result = chosenAlias.equals(cm.getAlias()); 107 } 108 109 return result; 110 } 111 112 116 public int hashCode() { 117 return charset.hashCode(); 118 } 119 120 126 public String toString() { 127 if(textOutOfDate) { 128 displayText = buildDisplayText(); 129 } 130 131 return displayText; 132 } 133 134 138 public Charset getCharset() { 139 return charset; 140 } 141 142 146 public String getAlias() { 147 return chosenAlias; 148 } 149 150 153 public void updateDisplayText() { 154 textOutOfDate = true; 155 } 156 157 private String buildDisplayText() { 158 String result = chosenAlias; 159 160 if(showAliases) { 161 StringBuffer aliasList = new StringBuffer (200); 162 163 for(Iterator iter = charset.aliases().iterator(); iter.hasNext(); ) { 164 aliasList.append((String ) iter.next()); 165 if(iter.hasNext()) { 166 aliasList.append(", "); } 168 } 169 170 Object [] args = new Object [] { chosenAlias, aliasList.toString() }; 171 172 result = MessageFormat.format( 173 webappBundle.getString("LBL_CharsetComboBoxDisplayText"), args); } 175 176 if(result == null || result.length() == 0) { 177 result = webappBundle.getString("LBL_UnnamedCharset"); } 179 180 textOutOfDate = false; 181 182 return result; 183 } 184 185 191 public int compareTo(Object obj) { 192 int result = -1; 193 194 if(obj instanceof CharsetMapping) { 195 CharsetMapping targetMapping = (CharsetMapping) obj; 201 result = chosenAlias.compareTo(targetMapping.getAlias()); 202 } 203 204 return result; 205 } 206 207 private static boolean useAliases = false; 208 private static SortedMap sortedCanonicalCharsetMappings = null; 209 210 private static SortedMap getSortedCanonicalCharsetMappings() { 211 if(sortedCanonicalCharsetMappings == null) { 212 SortedMap charsets = Charset.availableCharsets(); 213 sortedCanonicalCharsetMappings = new TreeMap (); 214 215 for(Iterator iter = charsets.entrySet().iterator(); iter.hasNext(); ) { 216 Map.Entry entry = (Map.Entry ) iter.next(); 217 CharsetMapping mapping = new CharsetMapping((Charset ) entry.getValue()); 218 sortedCanonicalCharsetMappings.put(mapping.getAlias(), mapping); 219 } 220 } 221 222 return sortedCanonicalCharsetMappings; 223 } 224 225 private static SortedMap sortedAliasCharsetMappings = null; 226 227 private static SortedMap getSortedAliasCharsetMappings() { 228 if(sortedAliasCharsetMappings == null) { 229 SortedMap charsets = Charset.availableCharsets(); 230 sortedAliasCharsetMappings = new TreeMap (); 231 232 for(Iterator iter = charsets.entrySet().iterator(); iter.hasNext(); ) { 233 Map.Entry entry = (Map.Entry ) iter.next(); 234 Charset charset = (Charset ) entry.getValue(); 235 CharsetMapping mapping = new CharsetMapping(charset, false); 236 sortedAliasCharsetMappings.put(mapping.getAlias(), mapping); 237 238 for(Iterator aliasIter = charset.aliases().iterator(); aliasIter.hasNext(); ) { 239 String alias = (String ) aliasIter.next(); 240 CharsetMapping aliasMapping = new CharsetMapping(charset, alias); 241 sortedAliasCharsetMappings.put(alias, aliasMapping); 242 } 243 } 244 } 245 246 return sortedAliasCharsetMappings; 247 } 248 249 256 public static SortedMap getSortedAvailableCharsetMappings() { 257 SortedMap result; 258 259 if(useAliases) { 260 if(sortedAliasCharsetMappings == null){ 261 sortedAliasCharsetMappings = getSortedAliasCharsetMappings(); 262 } 263 result = sortedAliasCharsetMappings; 264 } else { 265 if(sortedCanonicalCharsetMappings == null){ 266 sortedCanonicalCharsetMappings = getSortedCanonicalCharsetMappings(); 267 } 268 result = sortedCanonicalCharsetMappings; 269 } 270 271 return result; 272 } 273 274 279 public static CharsetMapping getCharsetMapping(Charset c) { 280 return (CharsetMapping) getSortedAvailableCharsetMappings().get(c.name()); 281 } 282 283 288 public static CharsetMapping getCharsetMapping(String name) { 289 CharsetMapping result = null; 290 291 if(name != null) { 292 try { 293 Charset charset = Charset.forName(name); 294 295 if(charset != null) { 296 result = (CharsetMapping) getSortedAvailableCharsetMappings().get(charset.name()); 297 } 298 } catch(Exception ex) { 299 } 301 } 302 303 return result; 304 } 305 306 322 323 326 327 public static final String CHARSET_DISPLAY_TYPE = "CharsetDisplayType"; 328 public static final Integer CHARSET_CANONICAL = new Integer (0); 329 public static final Integer CHARSET_ALIAS_ASIDE = new Integer (1); 330 public static final Integer CHARSET_ALIAS_SELECTION = new Integer (2); 331 332 private static Integer displayOption = CHARSET_ALIAS_ASIDE; 333 private static java.beans.PropertyChangeSupport propSupport = new PropertyChangeSupport (CharsetMapping.class);; 334 335 public static void addPropertyChangeListener(PropertyChangeListener listener) { 336 propSupport.addPropertyChangeListener(listener); 337 } 338 339 public static void removePropertyChangeListener(PropertyChangeListener listener) { 340 propSupport.removePropertyChangeListener(listener); 341 } 342 343 public static void setDisplayOption(Integer option) { 344 Integer newDisplayOption = getDisplayOptionEnum(option); 345 346 if(newDisplayOption != null && newDisplayOption != displayOption) { 347 Integer oldDisplayOption = displayOption; 348 displayOption = newDisplayOption; 349 updateInternalState(); 350 351 propSupport.firePropertyChange(CHARSET_DISPLAY_TYPE, oldDisplayOption, displayOption); 352 } 353 } 354 355 private static void updateInternalState() { 356 if(displayOption == CHARSET_CANONICAL) { 357 useAliases = false; 358 if(sortedCanonicalCharsetMappings == null){ 359 sortedCanonicalCharsetMappings = getSortedCanonicalCharsetMappings(); 360 } 361 Collection mappings = sortedCanonicalCharsetMappings.values(); 362 for(Iterator iter = mappings.iterator(); iter.hasNext(); ) { 363 CharsetMapping mapping = (CharsetMapping) iter.next(); 364 mapping.showAliases = false; 365 mapping.updateDisplayText(); 366 } 367 } else if(displayOption == CHARSET_ALIAS_ASIDE) { 368 useAliases = false; 369 if(sortedCanonicalCharsetMappings == null){ 370 sortedCanonicalCharsetMappings = getSortedCanonicalCharsetMappings(); 371 } 372 Collection mappings = sortedCanonicalCharsetMappings.values(); 373 for(Iterator iter = mappings.iterator(); iter.hasNext(); ) { 374 CharsetMapping mapping = (CharsetMapping) iter.next(); 375 mapping.showAliases = true; 376 mapping.updateDisplayText(); 377 } 378 } else if(displayOption == CHARSET_ALIAS_SELECTION) { 379 useAliases = true; 380 } 381 } 382 383 public static Integer getDisplayOption() { 384 return displayOption; 385 } 386 387 private static Integer getDisplayOptionEnum(Integer option) { 388 Integer result = null; 389 390 if(option != null) { 391 if(CHARSET_CANONICAL.compareTo(option) == 0) { 392 result = CHARSET_CANONICAL; 393 } else if(CHARSET_ALIAS_ASIDE.compareTo(option) == 0) { 394 result = CHARSET_ALIAS_ASIDE; 395 } else if(CHARSET_ALIAS_SELECTION.compareTo(option) == 0) { 396 result = CHARSET_ALIAS_SELECTION; 397 } 398 } 399 400 return result; 401 } 402 } 403 | Popular Tags |