package com.sptci.echo2demo;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import nextapp.echo2.app.ApplicationInstance;

/**
 * A utility class that is used to load and retrieve the localised
 * configurable properties for the UI components.  This class needs
 * an active <code>ApplicationInstance</code> to be properly
 * initialised.
 *
 * <p>Copyright 2006 Sans Pareil Technologies, Inc.</p>
 * @author Rakesh Vidyadharan 2006-01-21
 * @version $Id: Configuration.java,v 1.1.1.1 2006/02/06 00:44:09 rakesh Exp $
 */
public class Configuration
{
  /**
   * The name of the resource bundle to load.
   *
   * {@value}
   */
  private static final String BUNDLE_NAME = 
    "META-INF.resource.localisation.Configuration";

  /**
   * The resource bundle that represents the appropriate properties 
   * file.
   */
  private static ResourceBundle resource = null; 

  /** 
   * Cannot be instantiated.
   */
  private Configuration() {}
    
  /**
   * Returns localised configured text for the key.
   * 
   * @param key The key of the text to be returned
   * @return The appropriate localised text (if the key is not defined,
   *   the string "!key!" is returned)
   */
  public static final String getString( String key )
  {
    try 
    {
      Locale locale = ApplicationInstance.getActive().getLocale();

      if ( resource == null )
      {
        resource = ResourceBundle.getBundle( BUNDLE_NAME, locale );
      }

      return resource.getString(key);
    } 
    catch ( MissingResourceException mrex ) 
    {
      System.err.format( "No resource configured in %s for key %s%n",
          BUNDLE_NAME, key );
      mrex.printStackTrace();
      return '!' + key + '!';
    }
  }
    
  /**
   * Returns configured integer value for the key.
   * 
   * @param key The key of the integer value to be returned
   * @return The appropriate value (if the key is not defined,
   *   the value 0 is returned)
   */
  public static final int getInt( String key )
  {
    try 
    {
      Locale locale = ApplicationInstance.getActive().getLocale();

      if ( resource == null )
      {
        resource = ResourceBundle.getBundle( BUNDLE_NAME, locale );
      }

      return Integer.parseInt( resource.getString( key ) );
    } 
    catch ( MissingResourceException mrex ) 
    {
      System.err.format( "No resource configured in %s for key %s%n",
          BUNDLE_NAME, key );
      mrex.printStackTrace();
      return 0;
    }
  }
}
