001 package com.sptci.echo2;
002
003 import java.util.Locale;
004 import java.util.HashMap;
005 import java.util.MissingResourceException;
006 import java.util.ResourceBundle;
007 import java.util.logging.Level;
008 import java.util.logging.Logger;
009
010 import nextapp.echo2.app.ApplicationInstance;
011
012 /**
013 * A utility class that is used to load and retrieve the localised
014 * configurable dimensions for UI components. This class needs an
015 * active <code>ApplicationInstance</code> to be properly initialised.
016 *
017 * <p>Copyright 2006 Sans Pareil Technologies, Inc.</p>
018 * @author Rakesh Vidyadharan 2006-11-23
019 * @version $Id: Dimensions.java 3026 2007-03-02 14:02:28Z rakesh $
020 */
021 public class Dimensions
022 {
023 /**
024 * The name of the resource bundle to load.
025 *
026 * {@value}
027 */
028 private static final String BUNDLE_NAME =
029 "META-INF.resource.localisation.Dimensions";
030
031 /**
032 * The logger used to log errors.
033 */
034 private static final Logger logger =
035 Logger.getLogger( Dimensions.class.getName() );
036
037 /**
038 * A container for storing the resource bundles that represent the
039 * appropriate properties.
040 */
041 private static final HashMap<Locale, ResourceBundle> resource =
042 new HashMap<Locale, ResourceBundle>();
043
044 /**
045 * Cannot be instantiated.
046 */
047 private Dimensions() {}
048
049 /**
050 * Returns configured integer value for the key for the specified
051 * source object. Look for a key that is named after the fully
052 * qualified class name of the source object.
053 *
054 * @see #getInt( String )
055 * @param source The object whose configured localised value is
056 * to be retrieved.
057 * @param key The key of the integer value to be returned
058 * @return The appropriate value (if the key is not defined,
059 * the value 0 is returned)
060 */
061 public static final int getInt( Object source, String key )
062 {
063 return getInt( source.getClass().getName() + "." + key );
064 }
065
066 /**
067 * Returns configured integer value for the key.
068 *
069 * @param key The key of the integer value to be returned
070 * @return The appropriate value (if the key is not defined,
071 * the value 0 is returned)
072 */
073 public static final int getInt( String key )
074 {
075 int result = 0;
076 try
077 {
078 result = Integer.parseInt( fetchResourceBundle().getString( key ) );
079 }
080 catch ( MissingResourceException mrex )
081 {
082 logger.warning( "No resource configured in " +
083 BUNDLE_NAME + " for key " + key );
084 }
085
086 return result;
087 }
088
089 /**
090 * Fetch the appropriate <code>ResourceBundle</code> based upon the
091 * <code>Locale</code> from {@link #resource}.
092 *
093 * @return ResourceBundle The appropriate resource bundle for the
094 * locale.
095 */
096 private static ResourceBundle fetchResourceBundle()
097 {
098 Locale locale = Locale.getDefault();
099
100 if ( ApplicationInstance.getActive() != null )
101 {
102 locale = ApplicationInstance.getActive().getLocale();
103 }
104
105 ResourceBundle bundle = resource.get( locale );
106 if ( bundle == null )
107 {
108 bundle = ResourceBundle.getBundle( BUNDLE_NAME, locale );
109 resource.put( locale, bundle );
110 }
111
112 return bundle;
113 }
114 }