/*
 * @(#)ResourceBunldeUtil.java  1.4  2005-01-04
 *
 * Copyright (c) 2000 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package ch.randelshofer.util;

import java.util.ResourceBundle;
import java.util.NoSuchElementException;
import java.util.MissingResourceException;
import javax.swing.*;
import java.text.MessageFormat;
import java.net.URL;
import java.util.Formatter;

/**
 * This is a convenience wrapper for accessing resources stored in a ResourceBundle.
 *
 * @author  Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland
 * @version  1.4 2005-01-04   Methods configureAction and configureMenu added.
 * <br>      1.3 2001-10-10   The default resource name changed from 'name_Metal' 
 *                            to 'name'.
 * <br>      1.2 2001-07-23   Adaptation to JDK 1.3 in progress.
 * <br>      1.0 2000-06-10   Created.
 */
public class ResourceBundleUtil {
    /** The wrapped resource bundle. */
    private ResourceBundle resource;

    /**
     * Creates a new ResouceBundleUtil which wraps
     * the provided resource bundle.
     */
    public ResourceBundleUtil(ResourceBundle r) {
        resource = r;
    }
    

	/**
	 * Get a String from the ResourceBundle.
	 * <br>Convenience method to save casting.
	 *
	 * @param key The key of the property.
	 * @return The value of the property. Returns the key
	 *          if the property is missing.
	 */
    public String getString(String key) {
		try {
			return resource.getString(key);
		} catch (MissingResourceException e) {
			return key;
		}
    }
	/**
	 * Get an image icon from the ResourceBundle.
	 * <br>Convenience method .
	 *
	 * @param key The key of the property.
	 * @return The value of the property. Returns null
	 *          if the property is missing.
	 */
	public ImageIcon getImageIcon(String key, Class baseClass) {
		try {
			String rsrcName = resource.getString(key);
			if (rsrcName.equals("")) {
				return null;
			}
			URL url = baseClass.getResource(rsrcName);
			return (url == null) ? null : new ImageIcon(url);
		} catch (MissingResourceException e) {
			return null;
		}
	}

	/**
	 * Get a Mnemonic from the ResourceBundle.
	 * <br>Convenience method.
	 *
	 * @param key The key of the property.
	 * @return The first char of the value of the property.
	 *          Returns '\0' if the property is missing.
	 */
    public char getMnemonic(String key) {
        String s = resource.getString(key);
        return (s == null || s.length() == 0) ? '\0' : s.charAt(0);
    }
	/**
	 * Get a Mnemonic from the ResourceBundle.
	 * <br>Convenience method.
	 *
	 * @param key The key of the property. This method appends "Mnem" to the key.
	 * @return The first char of the value of the property.
	 *          Returns '\0' if the property is missing.
	 */
    public char getMnem(String key) {
        String s = resource.getString(key+"Mnem");
        return (s == null || s.length() == 0) ? '\0' : s.charAt(0);
    }

	/**
	 * Get a KeyStroke from the ResourceBundle.
	 * <BR>Convenience method.
	 *
	 * @param key The key of the property.
	 * @return <code>javax.swing.KeyStroke.getKeyStroke(value)</code>.
	 *          Returns null if the property is missing.
	 */
    public KeyStroke getKeyStroke(String key) {
        KeyStroke ks = null;
        try {
            String s = resource.getString(key);
            ks = (s == null) ? (KeyStroke) null : KeyStroke.getKeyStroke(s);
        } catch (NoSuchElementException e) {
        }
        return ks;
    }
	/**
	 * Get a KeyStroke from the ResourceBundle.
	 * <BR>Convenience method.
	 *
	 * @param key The key of the property. This method adds "Acc" to the key.
	 * @return <code>javax.swing.KeyStroke.getKeyStroke(value)</code>.
	 *          Returns null if the property is missing.
	 */
    public KeyStroke getAcc(String key) {
        KeyStroke ks = null;
        try {
            String s = resource.getString(key+"Acc");
            ks = (s == null) ? (KeyStroke) null : KeyStroke.getKeyStroke(s);
        } catch (NoSuchElementException e) {
        }
        return ks;
    }
    
    /**
     * Returns a formatted string using javax.text.MessageFormat.
     * @param key
     * @param arguments
     * @return formatted String
     */
    public String getFormatted(String key, Object... arguments) {
        return MessageFormat.format(resource.getString(key), arguments);
    }
    /**
     * Returns a formatted string using java.util.Formatter().
     * @param key
     * @param arguments
     * @return formatted String
     */
    public String format(String key, Object... arguments) {
        //return String.format(resource.getLocale(), resource.getString(key), arguments);
        return new Formatter(resource.getLocale()).format(resource.getString(key), arguments).toString();
    }
    

    /**
     * Get the appropriate ResourceBundle subclass.
     * The baseName is extended by the Swing Look and Feel ID
     * and by the Locale code.
     *
     * The default Look and Feel ID is Metal.
     *
     * @see java.util.ResourceBundle
     */
    public static ResourceBundleUtil getLAFBundle(String baseName)
    throws MissingResourceException {
        ResourceBundleUtil r;
        try {
            r = new ResourceBundleUtil(
                ResourceBundle.getBundle(baseName + "_" + UIManager.getLookAndFeel().getID()
                )
            );
        } catch (MissingResourceException e) {
            r = new ResourceBundleUtil(
                ResourceBundle.getBundle(baseName)
            );
        }
        return r;
    }
    
    public void configureAction(Action action, String argument) {
        action.putValue(Action.NAME, getString(argument));
        action.putValue(Action.ACCELERATOR_KEY, getAcc(argument));
        action.putValue(Action.MNEMONIC_KEY, new Integer(getMnem(argument)));
        }
    
    public void configureMenu(JMenuItem menu, String argument) {
        menu.setText(getString(argument));
        if (! (menu instanceof JMenu)) {
        menu.setAccelerator(getAcc(argument));
        }
        menu.setMnemonic(getMnem(argument));
        }
}
