001    /////////////////////////////////////////////////
002    // This file is part of Sears project.
003    // Subtitle Editor And Re-Synch
004    // A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is available under sourceforge
013    // at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    ////////////////////////////////////////////////
017    package sears.tools;
018    
019    import java.util.Locale;
020    import java.util.ResourceBundle;
021    
022    /**
023     * Class SearsResourceBundle.
024     * <br><b>Summary:</b><br>
025     * This class permits to access to the string resources in the choosen locale.
026     */
027    public class SearsResourceBundle {
028        /**The locale choosen*/
029        private Locale locale;
030        /**The resource bundle that correspond to the locale.*/
031        private ResourceBundle resourceBundle;
032        /**The instance of SearsResourceBundle.*/
033        private static SearsResourceBundle instance;
034        /**The name of the resource bundles.*/
035        private static final String RESOURCE_NAME = "MessageBundle";
036        
037        /**
038         * Constructor SearsResourceBundle.
039         * <br><b>Summary:</b><br>
040         * Constructor of the class.
041         */
042        public SearsResourceBundle(){
043            this(new Locale("en", "US"));
044        }
045        
046        /**
047         * Constructor SearsResourceBundle.
048         * <br><b>Summary:</b><br>
049         * Constructor of the class.
050         */
051        public SearsResourceBundle(Locale locale){
052            instance = this;
053            setLocale(locale);
054        }
055        
056        /**
057         * Method setLocale.
058         * <br><b>Summary:</b><br>
059         * Change the locale to the specified one.
060         * @param locale        The new locale to set.
061         */
062        public static void setLocale(Locale locale){
063            instance.locale=locale;
064            instance.resourceBundle = ResourceBundle.getBundle(RESOURCE_NAME, locale);
065        }
066        
067        /**
068         * Method getResource.
069         * <br><b>Summary:</b><br>
070         * This method permits to return a resources associated to the given key in the resource bundle.
071         * It returns "", if key is not found in resource bundle.
072         * @param key               The key of the value to found.
073         * @return  <b>String</b>   The resources associated to the given key in the resource bundle.
074         */
075        public static String getResource(String key){
076            return getResource(key, "");
077        }
078        
079        /**
080         * Method getResource.
081         * <br><b>Summary:</b><br>
082         * This method permits to return a resources associated to the given key in the resource bundle.
083         * It returns the default value, if key is not found in resource bundle.
084         * @param key               The key of the value to found.
085         * @param defaultValue      The default value to return, if key is not found.
086         * @return  <b>String</b>   The resources associated to the given key in the resource bundle.
087         */
088        public static String getResource(String key, String defaultValue){
089            //The result of the method
090            String result = defaultValue;
091            //try to get the resource from resourceBundle.
092            result = instance.resourceBundle.getString(key);
093            //if we did not found the searched value, return default value.
094            if(result == null){
095                result = defaultValue;
096            }
097            //return the result
098            return result;
099        }
100        
101        /**
102         * Method getLocale.
103         * <br><b>Summary:</b><br>
104         * Return the Locale.
105         * @return  <b>Locale</b>       The Locale.
106         */
107        public static Locale getLocale(){
108            return instance.locale;
109        }
110    }