001: /* ResourceManager.java
002: *
003: * Authors:
004: * Stefanovic Nenad chupo@iis.ns.ac.yu
005: * Bojanic Sasa sasaboy@neobee.net
006: * Puskas Vladimir vpuskas@eunet.yu
007: * Pilipovic Goran zboniek@uns.ac.yu
008: *
009: */
010:
011: package org.enhydra.shark.swingclient;
012:
013: import java.net.*;
014: import java.util.*;
015:
016: /**
017: * Implements the static methods that are used to implement
018: * multilanguage support, and to create some resources as
019: * menubar, toolbars and button panels.
020: */
021: public class ResourceManager {
022: private static final String resourcePath = "org.enhydra.shark.swingclient.resources.SharkClient";
023:
024: public static Locale defaultLocale;
025: public static ResourceBundle defaultResource;
026: public static Locale choosenLocale;
027: public static ResourceBundle choosenResource;
028:
029: static {
030: try {
031: // default is english
032: defaultLocale = new Locale("en");
033: defaultResource = ResourceBundle.getBundle(resourcePath,
034: new Locale(""));
035:
036: // chose the default system settings at the start
037: choosenLocale = Locale.getDefault();
038: choosenResource = ResourceBundle.getBundle(resourcePath,
039: choosenLocale);
040: } catch (MissingResourceException mre) {
041: System.err.println(resourcePath + ".properties not found");
042: System.exit(1);
043: }
044: }
045:
046: /**
047: * Gets a resource string from the resource bundle.<p> Resource bundle
048: * represents the <i>property file</i>. For example, if property file
049: * contains something like this:<BR><CENTER>menubar=file edit help</CENTER>
050: * method call getResourceString("menubar") will give the string
051: * <i>file edit help</i> as a result. <BR> This method reads information
052: * from property file. If can't find desired resource, returns <b>null</b>.
053: * @param nm name of the resource to fetch.
054: * @return String value of named resource.
055: */
056: public static String getLanguageDependentString(String nm) {
057: String str;
058: try {
059: str = choosenResource.getString(nm);
060: } catch (MissingResourceException mre) {
061: try {
062: str = defaultResource.getString(nm);
063: } catch (MissingResourceException mre1) {
064: str = null;
065: }
066: }
067: return str;
068: }
069:
070: /**
071: * Gets the url from a resource string.
072: * @param key the string key to the url in the resource bundle.
073: * @return the resource location.
074: * @see java.lang.Class#getResource
075: */
076: public static URL getResource(String key) {
077: String name = getLanguageDependentString(key);
078: if (name != null) {
079: URL url = ResourceManager.class.getClassLoader()
080: .getResource(name);
081: return url;
082: }
083: return null;
084: }
085:
086: public static void setDefault() {
087: choosenResource = defaultResource;
088: choosenLocale = defaultLocale;
089: }
090:
091: /** Returns the default resource bundle.
092: *
093: */
094: public static ResourceBundle getDefaultResource() {
095: return defaultResource;
096: }
097:
098: /** Returns the current locale.
099: *
100: */
101: public static ResourceBundle getChoosenResource() {
102: return choosenResource;
103: }
104:
105: /** Returns the default locale.
106: *
107: */
108: public static Locale getDefaultLocale() {
109: return defaultLocale;
110: }
111:
112: /** Returns the current locale.
113: *
114: */
115: public static Locale getChoosenLocale() {
116: return choosenLocale;
117: }
118:
119: /**
120: * Sets the new resource and locale to be used.
121: */
122: public static void setChoosen(String language)
123: throws MissingResourceException {
124: choosenLocale = new Locale(language);
125: String bundle = resourcePath + "_" + language;
126: choosenResource = ResourceBundle.getBundle(bundle);
127: }
128:
129: /**
130: * Sets the new resource and locale to be used.
131: */
132: public static void setChoosen(String language, String country)
133: throws MissingResourceException {
134: choosenLocale = new Locale(language, country);
135: String 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 language, String country,
143: String variant) throws MissingResourceException {
144: choosenLocale = new Locale(language, country, variant);
145: String bundle = resourcePath + "_" + language + "_" + country
146: + "_" + variant;
147: choosenResource = ResourceBundle.getBundle(bundle);
148: }
149:
150: }
|