KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > ResourceManager


1 /* ResourceManager.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  *
9  */

10
11 package org.enhydra.jawe;
12
13 import org.enhydra.jawe.xml.*;
14
15 import java.awt.*;
16 import java.util.*;
17 import java.io.*;
18 import java.text.*;
19 import java.net.URL JavaDoc;
20 import javax.accessibility.*;
21
22 /**
23 * Implements the static methods that are used to implement
24 * multilanguage support.
25 */

26 public class ResourceManager {
27    private static Locale defaultLocale;
28    private static ResourceBundle defaultResourceBoundle;
29    private static Locale choosenLocale;
30    private static ResourceBundle choosenResourceBundle;
31
32    private static Properties properties=new Properties();
33
34    static {
35       try {
36          // default is english
37
defaultLocale = new Locale("");
38          defaultResourceBoundle = ResourceBundle.
39             getBundle(JaWEConstants.RESOURCE_PATH,defaultLocale);
40
41          // chose the default system settings at the start
42
String JavaDoc startingLocale=JaWEConfig.getInstance().getStartingLocale();
43          if (startingLocale!=null && startingLocale.length()>0) {
44             if (!startingLocale.equals("default")) {
45                choosenLocale=new Locale(startingLocale);
46             } else {
47                choosenLocale=defaultLocale;
48             }
49          } else {
50             choosenLocale=Locale.getDefault();
51          }
52          if (!startingLocale.equals("default")) {
53             choosenResourceBundle = ResourceBundle.getBundle(JaWEConstants.RESOURCE_PATH,choosenLocale);
54          } else {
55             choosenResourceBundle = defaultResourceBoundle;
56          }
57       } catch (MissingResourceException mre) {
58          System.err.println(JaWEConstants.RESOURCE_PATH+".properties not found");
59          System.exit(1);
60       }
61
62       try {
63          java.net.URL JavaDoc u=ResourceManager.class.getClassLoader().
64                getResource("org/enhydra/jawe/resources/JaWE.res");
65          InputStream is=(InputStream)u.getContent();
66          properties.load(is);
67       } catch (Exception JavaDoc ex) {}
68       XMLUtil.setDefaultResources(defaultResourceBoundle);
69       XMLUtil.setChoosenResources(choosenResourceBundle);
70       XMLUtil.setProperties(properties);
71    }
72
73    /**
74     * Gets a language dependent string from the resource bundle.<p> Resource bundle
75     * represents the <i>property file</i>. For example, if property file
76     * contains something like this:<BR><CENTER>menubar=file edit help</CENTER>
77     * method call getLanguageDependentString("menubar") will give the string
78     * <i>file edit help</i> as a result. <BR> This method reads information
79     * from property file. If can't find desired resource, returns <b>null</b>.
80     * @param nm name of the resource to fetch.
81     * @return String value of named resource.
82     */

83    public static String JavaDoc getLanguageDependentString (String JavaDoc nm) {
84       String JavaDoc str;
85       try {
86          str=choosenResourceBundle.getString(nm);
87       } catch (MissingResourceException mre) {
88          try {
89             str=defaultResourceBoundle.getString(nm);
90          } catch (MissingResourceException mre1) {
91             str = null;
92          }
93       }
94       return str;
95    }
96
97    public static String JavaDoc getResourceString (String JavaDoc nm) {
98       String JavaDoc str=null;
99       try {
100          str=properties.getProperty(nm);
101       } catch (Exception JavaDoc ex) {}
102       return str;
103    }
104
105    /**
106     * Gets the url from a resource string.
107     * @param key the string key to the url in the properties.
108     * @return the resource location.
109     * @see java.lang.Class#getResource
110     */

111    public static URL JavaDoc getResource (String JavaDoc key) {
112       String JavaDoc name=properties.getProperty(key);
113       if (name != null) {
114          URL JavaDoc url = ResourceManager.class.getClassLoader().getResource(name);
115          return url;
116       }
117       return null;
118    }
119
120
121    public static void setDefault () {
122       choosenResourceBundle=defaultResourceBoundle;
123       choosenLocale=defaultLocale;
124       XMLUtil.setChoosenResources(choosenResourceBundle);
125    }
126
127    public static void setSystem () {
128       choosenLocale=Locale.getDefault();
129       choosenResourceBundle = ResourceBundle.getBundle(JaWEConstants.RESOURCE_PATH,choosenLocale);
130       XMLUtil.setChoosenResources(choosenResourceBundle);
131    }
132
133    /** Returns the default resource bundle.
134     *
135     */

136    public static ResourceBundle getDefaultResourceBundle() {
137       return defaultResourceBoundle;
138    }
139
140    /** Returns the current locale.
141     *
142     */

143    public static ResourceBundle getChoosenResourceBundle() {
144       return choosenResourceBundle;
145    }
146
147    /** Returns the default locale.
148     *
149     */

150    public static Locale getDefaultLocale() {
151       return defaultLocale;
152    }
153
154    /** Returns the current locale.
155     *
156     */

157    public static Locale getChoosenLocale() {
158       return choosenLocale;
159    }
160
161    /**
162     * Sets the new resource and locale to be used.
163     */

164    public static void setChoosen (Locale loc) throws MissingResourceException {
165       Locale previousLocale=choosenLocale;
166       try {
167          choosenLocale=loc;
168          choosenResourceBundle = ResourceBundle.getBundle(JaWEConstants.RESOURCE_PATH,loc);
169          XMLUtil.setChoosenResources(choosenResourceBundle);
170       } catch (Exception JavaDoc ex) {
171          choosenLocale=previousLocale;
172       }
173    }
174 }
175
Popular Tags