KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > L10N


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004 Dave Brosius <dbrosius@users.sourceforge.net>
4  * Copyright (C) 2003,2004 University of Maryland
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20
21 /*
22  * FindBugsFrame.java
23  *
24  * Created on March 30, 2003, 12:05 PM
25  */

26
27 package edu.umd.cs.findbugs;
28
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.io.FileWriter JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.PrintWriter JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.MissingResourceException JavaDoc;
38 import java.util.ResourceBundle JavaDoc;
39
40 import javax.swing.AbstractButton JavaDoc;
41
42 import edu.umd.cs.findbugs.gui.AnnotatedString;
43
44 public class L10N {
45     private static final boolean DEBUG = SystemProperties.getBoolean("i18n.debug");
46     private static final boolean GENERATE_MISSING_KEYS = SystemProperties.getBoolean("i18n.generateMissingKeys");
47
48     private static ResourceBundle JavaDoc bundle;
49     private static ResourceBundle JavaDoc bundle_en;
50
51     private static PrintWriter JavaDoc extraProperties;
52     static {
53         try {
54             if (GENERATE_MISSING_KEYS) try {
55                 extraProperties = new PrintWriter JavaDoc(new FileWriter JavaDoc("/tmp/extra.properties"));
56             } catch (IOException JavaDoc e) {
57                 e.printStackTrace();
58             }
59             
60             bundle = ResourceBundle.getBundle("edu.umd.cs.findbugs.gui.bundle.findbugs");
61             bundle_en = ResourceBundle.getBundle("edu.umd.cs.findbugs.gui.bundle.findbugs", Locale.ENGLISH);
62
63                             
64         } catch (Exception JavaDoc mre) {
65         }
66     }
67
68
69     private L10N() {
70     }
71
72     private static String JavaDoc lookup(ResourceBundle JavaDoc b, String JavaDoc key) {
73         if (b == null || key == null ) throw new MissingResourceException JavaDoc(null,null,null);
74         
75         return b.getString(key);
76     }
77     public static String JavaDoc getLocalString(String JavaDoc key, String JavaDoc defaultString) {
78         if (key == null) return "TRANSLATE("+defaultString+")";
79         try {
80             return lookup(bundle, key);
81         } catch (MissingResourceException JavaDoc mre) {
82             try {
83                 String JavaDoc en = lookup(bundle_en, key);
84                 if (DEBUG) return "TRANSLATE("+en+")";
85                 else return en;
86             } catch (MissingResourceException JavaDoc mre2) {
87                 if (extraProperties != null) {
88                     extraProperties.println(key+"="+defaultString);
89                     extraProperties.flush();
90                 }
91                 String JavaDoc en = "Default("+defaultString+")";
92                 if (DEBUG) return "TRANSLATE("+en+")";
93                 else return en;
94         }
95         }
96     }
97
98     /**
99      * Localise the given AbstractButton, setting the text and optionally mnemonic
100      * Note that AbstractButton includes menus and menu items.
101      * @param button The button to localise
102      * @param key The key to look up in resource bundle
103      * @param defaultString default String to use if key not found
104      * @param setMnemonic whether or not to set the mnemonic. According to Sun's
105      * guidelines, default/cancel buttons should not have mnemonics
106      * but instead should use Return/Escape
107      */

108     public static void localiseButton(AbstractButton JavaDoc button, String JavaDoc key, String JavaDoc defaultString,
109                                 boolean setMnemonic) {
110         AnnotatedString as = new AnnotatedString(getLocalString(key, defaultString));
111         button.setText(as.toString());
112         int mnemonic;
113         if (setMnemonic &&
114             (mnemonic = as.getMnemonic()) != KeyEvent.VK_UNDEFINED) {
115             button.setMnemonic(mnemonic);
116             button.setDisplayedMnemonicIndex(as.getMnemonicIndex());
117         }
118     }
119 }
120
Popular Tags