001    package com.sptci.echo2demo;
002    
003    import java.util.Locale;
004    import java.util.MissingResourceException;
005    import java.util.ResourceBundle;
006    
007    import nextapp.echo2.app.ApplicationInstance;
008    
009    /**
010     * A utility class that is used to load and retrieve the localised
011     * configurable properties for the UI components.  This class needs
012     * an active <code>ApplicationInstance</code> to be properly
013     * initialised.
014     *
015     * <p>Copyright 2006 Sans Pareil Technologies, Inc.</p>
016     * @author Rakesh Vidyadharan 2006-01-21
017     * @version $Id: Configuration.java,v 1.1.1.1 2006/02/06 00:44:09 rakesh Exp $
018     */
019    public class Configuration
020    {
021      /**
022       * The name of the resource bundle to load.
023       *
024       * {@value}
025       */
026      private static final String BUNDLE_NAME = 
027        "META-INF.resource.localisation.Configuration";
028    
029      /**
030       * The resource bundle that represents the appropriate properties 
031       * file.
032       */
033      private static ResourceBundle resource = null; 
034    
035      /** 
036       * Cannot be instantiated.
037       */
038      private Configuration() {}
039        
040      /**
041       * Returns localised configured text for the key.
042       * 
043       * @param key The key of the text to be returned
044       * @return The appropriate localised text (if the key is not defined,
045       *   the string "!key!" is returned)
046       */
047      public static final String getString( String key )
048      {
049        try 
050        {
051          Locale locale = ApplicationInstance.getActive().getLocale();
052    
053          if ( resource == null )
054          {
055            resource = ResourceBundle.getBundle( BUNDLE_NAME, locale );
056          }
057    
058          return resource.getString(key);
059        } 
060        catch ( MissingResourceException mrex ) 
061        {
062          System.err.format( "No resource configured in %s for key %s%n",
063              BUNDLE_NAME, key );
064          mrex.printStackTrace();
065          return '!' + key + '!';
066        }
067      }
068        
069      /**
070       * Returns configured integer value for the key.
071       * 
072       * @param key The key of the integer value to be returned
073       * @return The appropriate value (if the key is not defined,
074       *   the value 0 is returned)
075       */
076      public static final int getInt( String key )
077      {
078        try 
079        {
080          Locale locale = ApplicationInstance.getActive().getLocale();
081    
082          if ( resource == null )
083          {
084            resource = ResourceBundle.getBundle( BUNDLE_NAME, locale );
085          }
086    
087          return Integer.parseInt( resource.getString( key ) );
088        } 
089        catch ( MissingResourceException mrex ) 
090        {
091          System.err.format( "No resource configured in %s for key %s%n",
092              BUNDLE_NAME, key );
093          mrex.printStackTrace();
094          return 0;
095        }
096      }
097    }