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

com.jidesoft.plaf.UIDefaultsLookup Maven / Gradle / Ivy

/*
 * @(#)UIManagerLookup.java 4/5/2007
 *
 * Copyright 2002 - 2007 JIDE Software Inc. All rights reserved.
 */

package com.jidesoft.plaf;

import com.jidesoft.converter.ObjectConverterManager;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.util.Locale;

/**
 * This class simply uses UIManager's get method to lookup the UIDefaults.
 * We used this everywhere in our code so that we have one central place
 * to find out which UIDefaults we are using.
 * Another good thing is you can use {@link #setTrace(boolean)} and {@link #setDebug(boolean)}
 * to turn on the trace so that it will print out which UIDefaults we are trying to get.
 */
public class UIDefaultsLookup {
    private static boolean _debug = false;
    private static boolean _trace = false;

    /**
     * Sets the debug mode. If debug mode is on, we will print out
     * any UIDefaults that the value is null.
     *
     * @param debug true or false.
     */
    public static void setDebug(boolean debug) {
        _debug = debug;
    }

    /**
     * Sets the trace mode. If trace mode is on, we will print out
     * any UIDefaults we are trying to get and its current value.
     *
     * @param trace true or false.
     */
    public static void setTrace(boolean trace) {
        _trace = trace;
    }

    public static Object get(Object key) {
        Object value = UIManager.get(key);
        log(value, key, null);
        return value;
    }

    public static Object get(Object key, Locale l) {
        Object value = UIManager.get(key, l);
        log(value, key, l);
        return value;
    }

    private static void log(Object value, Object key, Locale l) {
        if (_debug && value == null) {
            System.out.println("\"" + key + (l == null ? "" : l.toString()) + " \" ==> null ------------------------");
        }
        else if (_trace) {
            if (value == null) {
                System.out.println("\"" + key + (l == null ? "" : l.toString()) + " \" ==> null ------------------------");
            }
            else {
                System.out.println("\"" + key + (l == null ? "" : l.toString()) + " \" ==> " + value.getClass().getName() + "(" + ObjectConverterManager.toString(value) + ")");
            }
        }
    }

    /**
     * If the value of key is a Font return it,
     * otherwise return null.
     *
     * @param key the desired key
     * @return if the value for key is a Font,
     *         return the Font object; otherwise return
     *         null
     */
    public static Font getFont(Object key) {
        Object value = get(key);
        return (value instanceof Font) ? (Font) value : null;
    }


    /**
     * If the value of key for the given Locale
     * is a Font return it, otherwise return null.
     *
     * @param key the desired key
     * @param l   the desired locale
     * @return if the value for key and Locale
     *         is a Font,
     *         return the Font object; otherwise return
     *         null
     * @since 1.9.5.04
     */
    public static Font getFont(Object key, Locale l) {
        Object value = get(key, l);
        return (value instanceof Font) ? (Font) value : null;
    }

    /**
     * If the value of key is a Color return it,
     * otherwise return null.
     *
     * @param key the desired key
     * @return if the value for key is a Color,
     *         return the Color object; otherwise return
     *         null
     */
    public static Color getColor(Object key) {
        Object value = get(key);
        return (value instanceof Color) ? (Color) value : null;
    }


    /**
     * If the value of key for the given Locale
     * is a Color return it, otherwise return null.
     *
     * @param key the desired key
     * @param l   the desired locale
     * @return if the value for key and Locale
     *         is a Color,
     *         return the Color object; otherwise return
     *         null
     * @since 1.9.5.04
     */
    public static Color getColor(Object key, Locale l) {
        Object value = get(key, l);
        return (value instanceof Color) ? (Color) value : null;
    }


    /**
     * If the value of key is an Icon return it,
     * otherwise return null.
     *
     * @param key the desired key
     * @return if the value for key is an Icon,
     *         return the Icon object; otherwise return
     *         null
     */
    public static Icon getIcon(Object key) {
        Object value = get(key);
        return (value instanceof Icon) ? (Icon) value : null;
    }


    /**
     * If the value of key for the given Locale
     * is an Icon return it, otherwise return null.
     *
     * @param key the desired key
     * @param l   the desired locale
     * @return if the value for key and Locale
     *         is an Icon,
     *         return the Icon object; otherwise return
     *         null
     * @since 1.9.5.04
     */
    public static Icon getIcon(Object key, Locale l) {
        Object value = get(key, l);
        return (value instanceof Icon) ? (Icon) value : null;
    }


    /**
     * If the value of key is a Border return it,
     * otherwise return null.
     *
     * @param key the desired key
     * @return if the value for key is a Border,
     *         return the Border object; otherwise return
     *         null
     */
    public static Border getBorder(Object key) {
        Object value = get(key);
        return (value instanceof Border) ? (Border) value : null;
    }


    /**
     * If the value of key for the given Locale
     * is a Border return it, otherwise return null.
     *
     * @param key the desired key
     * @param l   the desired locale
     * @return if the value for key and Locale
     *         is a Border,
     *         return the Border object; otherwise return
     *         null
     * @since 1.9.5.04
     */
    public static Border getBorder(Object key, Locale l) {
        Object value = get(key, l);
        return (value instanceof Border) ? (Border) value : null;
    }


    /**
     * If the value of key is a String return it,
     * otherwise return null.
     *
     * @param key the desired key
     * @return if the value for key is a String,
     *         return the String object; otherwise return
     *         null
     */
    public static String getString(Object key) {
        Object value = get(key);
        return (value instanceof String) ? (String) value : null;
    }

    /**
     * If the value of key for the given Locale
     * is a String return it, otherwise return null.
     *
     * @param key the desired key
     * @param l   the desired Locale
     * @return if the value for key for the given
     *         Locale is a String,
     *         return the String object; otherwise return
     *         null
     * @since 1.9.5.04
     */
    public static String getString(Object key, Locale l) {
        Object value = get(key, l);
        return (value instanceof String) ? (String) value : null;
    }

    /**
     * If the value of key is an Integer return its
     * integer value, otherwise return 0.
     *
     * @param key the desired key
     * @return if the value for key is an Integer,
     *         return its value, otherwise return 0
     */
    public static int getInt(Object key) {
        Object value = get(key);
        return (value instanceof Integer) ? (Integer) value : 0;
    }


    /**
     * If the value of key for the given Locale
     * is an Integer return its integer value, otherwise return 0.
     *
     * @param key the desired key
     * @param l   the desired locale
     * @return if the value for key and Locale
     *         is an Integer,
     *         return its value, otherwise return 0
     * @since 1.9.5.04
     */
    public static int getInt(Object key, Locale l) {
        Object value = get(key, l);
        return (value instanceof Integer) ? (Integer) value : 0;
    }


    /**
     * If the value of key is boolean, return the
     * boolean value, otherwise return false.
     *
     * @param key an Object specifying the key for the desired boolean value
     * @return if the value of key is boolean, return the
     *         boolean value, otherwise return false.
     * @since 1.9.5.04
     */
    public static boolean getBoolean(Object key) {
        Object value = get(key);
        return (value instanceof Boolean) ? (Boolean) value : false;
    }


    /**
     * If the value of key for the given Locale
     * is boolean, return the boolean value, otherwise return false.
     *
     * @param key an Object specifying the key for the desired boolean value
     * @param l   the desired locale
     * @return if the value for key and Locale
     *         is boolean, return the
     *         boolean value, otherwise return false.
     * @since 1.9.5.04
     */
    public static boolean getBoolean(Object key, Locale l) {
        Object value = get(key, l);
        return (value instanceof Boolean) ? (Boolean) value : false;
    }


    /**
     * If the value of key is an Insets return it,
     * otherwise return null.
     *
     * @param key the desired key
     * @return if the value for key is an Insets,
     *         return the Insets object; otherwise return
     *         null
     */
    public static Insets getInsets(Object key) {
        Object value = get(key);
        return (value instanceof Insets) ? (Insets) value : null;
    }


    /**
     * If the value of key for the given Locale
     * is an Insets return it, otherwise return null.
     *
     * @param key the desired key
     * @param l   the desired locale
     * @return if the value for key and Locale
     *         is an Insets,
     *         return the Insets object; otherwise return
     *         null
     * @since 1.9.5.04
     */
    public static Insets getInsets(Object key, Locale l) {
        Object value = get(key, l);
        return (value instanceof Insets) ? (Insets) value : null;
    }


    /**
     * If the value of key is a Dimension return it,
     * otherwise return null.
     *
     * @param key the desired key
     * @return if the value for key is a Dimension,
     *         return the Dimension object; otherwise return
     *         null
     */
    public static Dimension getDimension(Object key) {
        Object value = get(key);
        return (value instanceof Dimension) ? (Dimension) value : null;
    }


    /**
     * If the value of key for the given Locale
     * is a Dimension return it, otherwise return null.
     *
     * @param key the desired key
     * @param l   the desired locale
     * @return if the value for key and Locale
     *         is a Dimension,
     *         return the Dimension object; otherwise return
     *         null
     * @since 1.9.5.04
     */
    public static Dimension getDimension(Object key, Locale l) {
        Object value = get(key, l);
        return (value instanceof Dimension) ? (Dimension) value : null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy