All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jdesktop.swingx.renderer.LocalizableStringValue Maven / Gradle / Ivy

There is a newer version: 1.7.2
Show newest version
package org.jdesktop.swingx.renderer;

import org.jdesktop.swingx.plaf.UIManagerExt;
import org.jdesktop.swingx.util.Contract;

import java.util.Locale;
import java.util.Map;

/**
 * A StringValue which looks up localized String representations for objects.
 */
public class LocalizableStringValue implements StringValue {

    private final Map lookup;

    private Locale locale;

    private final String prefix;

    /**
     * Instantiates a LocaleStringValue which looks up localized String
     * representations for items in the map using the JComponent defaultLocale.
     *
     * @param lookup a map containing Entries of objects and a string key to
     *               look up its string representation in the UIManager
     */
    public LocalizableStringValue(Map lookup) {
        this(lookup, null, null);
    }

    /**
     * Instantiates a LocaleStringValue which looks up localized String
     * representations for items in the map using the given Locale.
     *
     * @param lookup a map containing Entries of objects and a string key to
     *               look up its string representation in the UIManager
     * @param locale the locale to lookup the localized strings, may be null to
     *               denote using JComponent.defaultLocale
     */
    public LocalizableStringValue(Map lookup, Locale locale) {
        this(lookup, null, locale);
    }

    /**
     * Instantiates a LocaleStringValue which looks up localized String
     * representations for items in the map using the JComponent defaultLocale.
     *
     * @param lookup a map containing Entries of objects and a string key to
     *               look up its string representation in the UIManager
     * @param prefix a common prefix for all string keys in the map, may be null
     *               to denote that the keys should be use as are
     */
    public LocalizableStringValue(Map lookup, String prefix) {
        this(lookup, prefix, null);
    }

    /**
     * Instantiates a LocaleStringValue which looks up localized String
     * representations for items in the map using the given Locale.
     *
     * @param lookup a map containing Entries of objects and a string key to
     *               look up its string representation in the UIManager
     * @param prefix a common prefix for all string keys in the map, may be null
     *               to denote that the keys should be use as are
     * @param locale the locale to lookup the localized strings, may be null to
     *               denote using JComponent.defaultLocale
     */
    public LocalizableStringValue(Map lookup, String prefix, Locale locale) {
        this.lookup = Contract.asNotNull(lookup, "map must not be null");
        this.prefix = prefix;
        setLocale(locale);
    }

    /**
     * {@inheritDoc}
     * 

* Implemented to lookup the value's localized string * representation, if contained in the lookup map. Returns * toString if not. */ @Override public String getString(Object value) { String key = lookup.get(value); if (key != null) { if (prefix != null) { key = prefix + key; } String text = UIManagerExt.getString(key, getLocale()); if (text != null) return text; } return StringValues.TO_STRING_UI.getString(value); } // -------------------- implement Localizable /** * Sets the Locale to use for lookup of localized string representation. * * @param locale the locale to lookup the localized strings, may be null to * denote using Locale's default. */ public final void setLocale(Locale locale) { this.locale = locale; } /** * Returns the Locale to use for lookup, guaranteed to be not null. If * the initial setting had been null, returns current Locale's default. * * @return the Locale used for lookup. */ public Locale getLocale() { return locale != null ? locale : Locale.getDefault(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy