001: /**
002: * $RCSfile: VAGlobals.java,v $
003: * @creation 01/02/00
004: * @modification $Date: 2005/03/16 19:55:26 $
005: * Bug-fixing and additional features by Antonio Petrelli
006: */package com.memoire.vainstall;
007:
008: import java.awt.Color;
009: import java.text.MessageFormat;
010: import java.util.Hashtable;
011: import java.util.Locale;
012: import java.util.MissingResourceException;
013: import java.util.ResourceBundle;
014: import java.util.StringTokenizer;
015: import java.util.Vector;
016:
017: import javax.swing.UIManager;
018:
019: /**
020: * @version $Id: VAGlobals.java,v 1.27 2005/03/16 19:55:26 deniger Exp $
021: * @author Axel von Arnim
022: */
023:
024: public class VAGlobals {
025: public final static boolean DEBUG = "yes".equals(System
026: .getProperty("DEBUG"));
027:
028: public final static String NAME = "VAInstall";
029: public final static String VERSION = "0.23a";
030: public final static String AUTHOR = "Axel von Arnim";
031: public final static String COPYRIGHT = "2000-2002";
032: public final static String LICENSE = "GPL2";
033: public final static String HTTP = "http://www.ifrance.com/vonarnim/vainstall";
034: public final static String EMAIL = "vonarnim@club-internet.fr";
035: public final static Class BASE_CLASS = new VAGlobals().getClass();
036:
037: public final static int UNKNOWN = 0;
038: public final static int INSTALL = 1;
039: public final static int UNINSTALL = 2;
040: public final static int UPDATE = 3;
041: public static int OPERATION = UNKNOWN;
042:
043: public static String UI_MODE;
044: public static boolean UI_BLUESCREEN;
045: public static Color UI_BLUESCREEN_COLOR;
046: public static String IMAGE;
047: public static boolean USE_FULL_JAVA_PATH;
048: public static boolean SHORTCUTS_IN_INSTALLDIR;
049: public static String DEST_PATH;
050: public static String APP_NAME;
051: public static String APP_VERSION;
052:
053: public static String JNI_DLL_FILE;
054:
055: public static String LINK_SECTION_NAME;
056: public static String LINK_SECTION_ICON;
057: public static String LINK_ENTRY_NAME;
058: public static String LINK_ENTRY_ICON;
059: public static boolean CREATE_UNINSTALL_SHORTCUT;
060:
061: /**
062: * Supported languages
063: */
064: public static String[][] languages = {
065: { "danish", "Danish", "da", "DK" },
066: { "german", "Deutsch", "de", "DE" },
067: { "english", "English", "en", "UK" },
068: { "french", "Français", "fr", "FR" },
069: { "italian", "Italian", "it", "IT" },
070: { "japanese", "Japanese", "ja", "JP" } };
071: private static String[] allSupportedLanguages;
072:
073: private static String[] supportedLanguages;
074:
075: /**
076: * The locale that is used now
077: */
078: private static Locale locale;
079:
080: /**
081: * The language that is used now
082: */
083: private static String currentLanguage;
084:
085: /**
086: * Cached resources
087: */
088: private static Hashtable resourceList = null;
089:
090: // AVA : deprecated
091: // private static Properties transTable=null;
092:
093: public static void printDebug(String msg) {
094: if (DEBUG)
095: System.err.println(msg);
096: }
097:
098: public static String i18n(String key) {
099: String s = null;
100: try {
101: s = getResource("com.memoire.vainstall.Language", key);
102: } catch (MissingResourceException e) {
103: }
104: if (s == null)
105: try {
106: s = getResource("com.memoire.vainstall.gui.Language",
107: key);
108: } catch (MissingResourceException e) {
109: }
110: if (s == null)
111: try {
112: s = getResource("com.memoire.vainstall.xui.Language",
113: key);
114: } catch (MissingResourceException e) {
115: }
116: if (s == null)
117: try {
118: s = getResource("com.memoire.vainstall.tui.Language",
119: key);
120: } catch (MissingResourceException e) {
121: }
122: // last try failed -> print key
123: if (s == null)
124: s = key;
125: return s;
126: }
127:
128: public static String i18n(String key, Object[] params) {
129: String s = i18n(key);
130: if (s != null && params != null) {
131: s = MessageFormat.format(s, params);
132: }
133: return s;
134: }
135:
136: /* AVA : deprecated
137: public static String getString(String s)
138: {
139: Locale l=Locale.getDefault();
140: if("en".equals(l.getLanguage())) return s;
141: if( transTable==null ) {
142: transTable=new Properties();
143: try {
144: InputStream fin=BASE_CLASS.getResourceAsStream("vai_"+l.getLanguage()+".lang");
145: if( fin!=null ) {
146: transTable.load(fin);
147: fin.close();
148: }
149: } catch( IOException e ) { printDebug("error while reading vai_"+l.getLanguage()+".lang"); }
150: }
151: String res=transTable.getProperty(s);
152: if( (res==null)||("".equals(res.trim())) ) res=s;
153: return res;
154: }
155: */
156:
157: /**
158: * Get a String resource from a resourcebundle.
159: * @param baseName String The name of the resource class ex. 'com.memorie.vainstall.Language'
160: * @param key String The keyword to find
161: * @return String
162: */
163: public static String getResource(String baseName, String key)
164: throws MissingResourceException {
165:
166: if ((locale == null) || DEBUG) {
167: locale = new Locale("en", "UK");
168: }
169:
170: if (resourceList == null) {
171: resourceList = new java.util.Hashtable();
172: }
173:
174: // check to see if we have cached
175: if (resourceList.contains(baseName) == false) {
176: // throws java.util.MissingResourceException
177: ResourceBundle resource = ResourceBundle.getBundle(
178: baseName, locale);
179:
180: // we got the resource bundle
181: resourceList.put(baseName, resource);
182: return resource.getString(key);
183: } else {
184: return ((ResourceBundle) resourceList.get(baseName))
185: .getString(key);
186: }
187:
188: } // endmethod
189:
190: /**
191: * Get a 'int' resource from a resourcebundle.
192: * @param baseName String The name of the resource class ex. 'com.memorie.vainstall.Language'
193: * @param key String The keyword to find
194: * @return int
195: */
196: public static int getResourceInt(String baseName, String key)
197: throws java.util.MissingResourceException {
198:
199: if (locale == null) {
200: locale = new Locale("en", "UK");
201: }
202:
203: if (resourceList == null) {
204: resourceList = new java.util.Hashtable();
205: }
206:
207: // check to see if we have cached
208: if (resourceList.contains(baseName) == false) {
209: // throws java.util.MissingResourceException
210: ResourceBundle resource = ResourceBundle.getBundle(
211: baseName, locale);
212:
213: // we got the resource bundle
214: resourceList.put(baseName, resource);
215:
216: Integer value = (Integer) resource.getObject(key);
217: return (int) value.intValue();
218: } else {
219: Integer value = (Integer) ((ResourceBundle) resourceList
220: .get(baseName)).getObject(key);
221: return (int) value.intValue();
222: }
223:
224: } // endmethod
225:
226: /**
227: * Set the language as defined in the documentation.
228: * We translate the name to a 'controlled' Java Locale.
229: * @param language String The language as defined in the documentation
230: * english, french, danish etc.
231: * @return void
232: */
233: public static void setLanguage(String language) {
234: // clear resourcelist cache
235: if (resourceList != null) {
236: resourceList.clear();
237: }
238:
239: if (language.toLowerCase().indexOf("choose") != -1) {
240: // 'choose ' keywork alone equals that all supported languages
241: // can be choosen
242: if (language.equalsIgnoreCase("choose") == true) {
243: setLanguage("default");
244: return;
245: }
246:
247: Vector languageList = new Vector();
248: String[] supportedLangs = getAllSupportedLanguages();
249:
250: Locale l = Locale.getDefault();
251: for (int i = 0; i < languages.length; i++) {
252: if (l.getLanguage().equals(languages[i][2]) == true) {
253: currentLanguage = languages[i][0];
254: }
255: }
256:
257: StringTokenizer nizer = new StringTokenizer(language, ",");
258: while (nizer.hasMoreTokens() == true) {
259: String element = (String) nizer.nextToken();
260: element = element.trim().toLowerCase();
261:
262: for (int i = 0; i < languages.length; i++) {
263: if (element.equals(languages[i][0]) == true) {
264:
265: if (element.equals(currentLanguage) == true) {
266: languageList.insertElementAt(
267: languages[i][0], 0);
268: } else {
269: languageList.addElement(languages[i][0]);
270: }
271: }
272: }
273: }
274:
275: supportedLanguages = new String[languageList.size()];
276: for (int i = 0; i < languageList.size(); i++) {
277: for (int j = 0; j < languages.length; j++) {
278: if (languageList.elementAt(i).equals(
279: languages[j][0]) == true) {
280: supportedLanguages[i] = (String) languages[j][1];
281: }
282: }
283: }
284: setLanguage("default");
285: return;
286: }
287:
288: /* If 'default' then we try to initially
289: look at the default language.
290: If it's one of the supported we use that.
291: During install we use the default language if supported
292: until the user selects one.
293: English is the default language.
294: */
295: if (language.equals("default") == true) {
296: Locale l = Locale.getDefault();
297: String shortLanguage = l.getLanguage();
298:
299: for (int i = 0; i < languages.length; i++) {
300: if (shortLanguage.equals(languages[i][2]) == true) {
301: currentLanguage = languages[i][0];
302: locale = new Locale(languages[i][2],
303: languages[i][3]);
304: }
305: }
306:
307: } else {
308: // One of the known languages
309: for (int i = 0; i < languages.length; i++) {
310: if (language.equals(languages[i][0]) == true) {
311: currentLanguage = languages[i][0];
312: locale = new Locale(languages[i][2],
313: languages[i][3]);
314: }
315: }
316: }
317:
318: // update UIManager
319: UIManager.put("OptionPane.yesButtonText", getResource(
320: "com.memoire.vainstall.Language",
321: "Common_OptionPane.yesButtonText"));
322: UIManager.put("OptionPane.noButtonText", getResource(
323: "com.memoire.vainstall.Language",
324: "Common_OptionPane.noButtonText"));
325: UIManager.put("OptionPane.okButtonText", getResource(
326: "com.memoire.vainstall.Language",
327: "Common_OptionPane.okButtonText"));
328: UIManager.put("OptionPane.cancelButtonText", getResource(
329: "com.memoire.vainstall.Language",
330: "Common_OptionPane.cancelButtonText"));
331:
332: }
333:
334: public static String[] getSupportedLanguages() {
335: if (supportedLanguages == null) {
336: supportedLanguages = new String[languages.length];
337: for (int i = 0; i < languages.length; i++) {
338: supportedLanguages[i] = languages[i][1];
339: }
340: }
341: return supportedLanguages;
342: }
343:
344: public static String[] getAllSupportedLanguages() {
345: if (allSupportedLanguages == null) {
346: allSupportedLanguages = new String[languages.length];
347: for (int i = 0; i < languages.length; i++) {
348: allSupportedLanguages[i] = languages[i][1];
349: }
350: }
351: return allSupportedLanguages;
352: }
353:
354: public static String getCurrentLanguage() {
355: return currentLanguage;
356: }
357:
358: /**
359: * Return an index that corresponds to the value of getSupportedLanguages
360: * or getAllSupportedLanguages that matches the default locale.
361: * If an exact match for the locale is not present,the value returned is
362: * the first language in the list that matches the language of the default
363: * locale.
364: *
365: */
366: public static int suggestLanguage() {
367: Locale syslocale = Locale.getDefault();
368: String syslang = syslocale.getLanguage();
369: String syscountry = syslocale.getCountry();
370: int langonly = -1;
371: for (int idx = 0; idx < languages.length; idx++) {
372: if (syslang.equals(languages[idx][2])) {
373: if (syscountry.equals(languages[idx][3])) {
374: return idx;
375: } else {
376: if (langonly == -1)
377: langonly = idx;
378: }
379: }
380: }
381: if (langonly == -1)
382: langonly = 0;
383: return langonly;
384: }
385:
386: }
|