KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > corbaclient > 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.shark.corbaclient;
12
13 import java.net.*;
14 import java.util.*;
15
16 /**
17 * Implements the static methods that are used to implement
18 * multilanguage support, and to create some resources as
19 * menubar, toolbars and button panels.
20 */

21 public class ResourceManager {
22 private static final String JavaDoc resourcePath=
23          "org.enhydra.shark.corbaclient.resources.SharkClient";
24
25    public static Locale defaultLocale;
26    public static ResourceBundle defaultResource;
27    public static Locale choosenLocale;
28    public static ResourceBundle choosenResource;
29
30
31    static {
32       try {
33          // default is english
34
defaultLocale = new Locale("en");
35          defaultResource = ResourceBundle.
36             getBundle(resourcePath,new Locale(""));
37
38          // chose the default system settings at the start
39
choosenLocale = Locale.getDefault();
40          choosenResource = ResourceBundle.getBundle(resourcePath,choosenLocale);
41       }
42       catch (MissingResourceException mre) {
43          System.err.println(resourcePath+".properties not found");
44          System.exit(1);
45       }
46    }
47
48    /**
49     * Gets a resource string from the resource bundle.<p> Resource bundle
50     * represents the <i>property file</i>. For example, if property file
51     * contains something like this:<BR><CENTER>menubar=file edit help</CENTER>
52     * method call getResourceString("menubar") will give the string
53     * <i>file edit help</i> as a result. <BR> This method reads information
54     * from property file. If can't find desired resource, returns <b>null</b>.
55     * @param nm name of the resource to fetch.
56     * @return String value of named resource.
57     */

58    public static String JavaDoc getLanguageDependentString (String JavaDoc nm) {
59       String JavaDoc str;
60       try {
61          str=choosenResource.getString(nm);
62       } catch (MissingResourceException mre) {
63          try {
64             str=defaultResource.getString(nm);
65          } catch (MissingResourceException mre1) {
66             str = null;
67          }
68       }
69       return str;
70    }
71
72    /**
73     * Gets the url from a resource string.
74     * @param key the string key to the url in the resource bundle.
75     * @return the resource location.
76     * @see java.lang.Class#getResource
77     */

78    public static URL getResource (String JavaDoc key) {
79       String JavaDoc name = getLanguageDependentString(key);
80       if (name != null) {
81          URL url = ResourceManager.class.getClassLoader().getResource(name);
82          return url;
83       }
84       return null;
85    }
86
87
88    public static void setDefault () {
89       choosenResource=defaultResource;
90       choosenLocale=defaultLocale;
91    }
92
93    /** Returns the default resource bundle.
94     *
95     */

96    public static ResourceBundle getDefaultResource() {
97       return defaultResource;
98    }
99
100    /** Returns the current locale.
101     *
102     */

103    public static ResourceBundle getChoosenResource() {
104       return choosenResource;
105    }
106
107    /** Returns the default locale.
108     *
109     */

110    public static Locale getDefaultLocale() {
111       return defaultLocale;
112    }
113
114    /** Returns the current locale.
115     *
116     */

117    public static Locale getChoosenLocale() {
118       return choosenLocale;
119    }
120
121    /**
122     * Sets the new resource and locale to be used.
123     */

124    public static void setChoosen (String JavaDoc language) throws MissingResourceException {
125       choosenLocale = new Locale(language);
126       String JavaDoc bundle=resourcePath+"_"+language;
127       choosenResource = ResourceBundle.getBundle(bundle);
128    }
129
130    /**
131     * Sets the new resource and locale to be used.
132     */

133    public static void setChoosen (String JavaDoc language,String JavaDoc country) throws MissingResourceException {
134       choosenLocale = new Locale(language,country);
135       String JavaDoc bundle=resourcePath+"_"+language+"_"+country;
136       choosenResource = ResourceBundle.getBundle(bundle);
137    }
138
139    /**
140     * Sets the new resource and locale to be used.
141     */

142    public static void setChoosen (String JavaDoc language,String JavaDoc country,String JavaDoc variant) throws MissingResourceException {
143       choosenLocale = new Locale(language,country,variant);
144       String JavaDoc bundle=resourcePath+"_"+language+"_"+country+"_"+variant;
145       choosenResource = ResourceBundle.getBundle(bundle);
146    }
147
148 }
149
150
Popular Tags