001package org.jdesktop.swingx.renderer; 002 003import java.util.Locale; 004import java.util.Map; 005 006import org.jdesktop.swingx.plaf.UIManagerExt; 007import org.jdesktop.swingx.util.Contract; 008 009/** 010 * A StringValue which looks up localized String representations for objects. 011 */ 012public class LocalizableStringValue implements StringValue { 013 014 private Map<Object, String> lookup; 015 016 private Locale locale; 017 018 private String prefix; 019 020 /** 021 * Instantiates a LocaleStringValue which looks up localized String 022 * representations for items in the map using the JComponent defaultLocale. 023 * 024 * @param lookup a map containing Entries of objects and a string key to 025 * look up its string representation in the UIManager 026 */ 027 public LocalizableStringValue(Map<Object, String> lookup) { 028 this(lookup, null, null); 029 } 030 031 /** 032 * Instantiates a LocaleStringValue which looks up localized String 033 * representations for items in the map using the given Locale. 034 * 035 * @param lookup a map containing Entries of objects and a string key to 036 * look up its string representation in the UIManager 037 * @param locale the locale to lookup the localized strings, may be null to 038 * denote using JComponent.defaultLocale 039 */ 040 public LocalizableStringValue(Map<Object, String> lookup, Locale locale) { 041 this(lookup, null, locale); 042 } 043 044 /** 045 * Instantiates a LocaleStringValue which looks up localized String 046 * representations for items in the map using the JComponent defaultLocale. 047 * 048 * @param lookup a map containing Entries of objects and a string key to 049 * look up its string representation in the UIManager 050 * @param prefix a common prefix for all string keys in the map, may be null 051 * to denote that the keys should be use as are 052 */ 053 public LocalizableStringValue(Map<Object, String> lookup, String prefix) { 054 this(lookup, prefix, null); 055 } 056 057 /** 058 * Instantiates a LocaleStringValue which looks up localized String 059 * representations for items in the map using the given Locale. 060 * 061 * @param lookup a map containing Entries of objects and a string key to 062 * look up its string representation in the UIManager 063 * @param prefix a common prefix for all string keys in the map, may be null 064 * to denote that the keys should be use as are 065 * @param locale the locale to lookup the localized strings, may be null to 066 * denote using JComponent.defaultLocale 067 */ 068 public LocalizableStringValue(Map<Object, String> lookup, String prefix, 069 Locale locale) { 070 this.lookup = Contract.asNotNull(lookup, "map must not be null"); 071 this.prefix = prefix; 072 setLocale(locale); 073 } 074 075 /** 076 * 077 * @inherited <p> 078 * 079 * Implemented to lookup the value's localized string 080 * representation, if contained in the lookup map. Returns 081 * toString if not. 082 * 083 */ 084 @Override 085 public String getString(Object value) { 086 String key = lookup.get(value); 087 if (key != null) { 088 if (prefix != null) { 089 key = prefix + key; 090 } 091 String text = UIManagerExt.getString(key, getLocale()); 092 if (text != null) 093 return text; 094 } 095 return StringValues.TO_STRING_UI.getString(value); 096 } 097 098 // -------------------- implement Localizable 099 100 /** 101 * Sets the Locale to use for lookup of localized string representation. 102 * 103 * @param locale the locale to lookup the localized strings, may be null to 104 * denote using Locale's default. 105 */ 106 public final void setLocale(Locale locale) { 107 this.locale = locale; 108 } 109 110 /** 111 * Returns the Locale to use for lookup, guaranteed to be not null. If 112 * the initial setting had been null, returns current Locale's default. 113 * 114 * @return the Locale used for lookup. 115 */ 116 public Locale getLocale() { 117 return locale != null ? locale : Locale.getDefault(); 118 } 119}