View Javadoc
1 package org.neuclear.commons.swing; 2 3 /* 4 * The NeuClear Project and it's libraries are 5 * (c) 2002-2004 Antilles Software Ventures SA 6 * For more information see: http://neuclear.org 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23 import java.util.Locale; 24 import java.util.ResourceBundle; 25 import java.util.prefs.BackingStoreException; 26 import java.util.prefs.Preferences; 27 28 /*** 29 * User: pelleb 30 * Date: Apr 16, 2004 31 * Time: 11:46:54 AM 32 */ 33 public class Messages { 34 /*** 35 */ 36 private Messages() { 37 this(Messages.class, null, "cryptodialogs"); 38 } 39 40 private Messages(Class ref, Messages parent, String name) { 41 this.name = name; 42 this.parent = parent; 43 System.setProperty("file.encoding", "UTF-8"); 44 System.out.println("encoding: " + System.getProperty("file.encoding")); 45 this.bundle = ResourceBundle.getBundle(name, getLocale(), ref.getClassLoader()); 46 } 47 48 public final String getString(String key) { 49 final String value = bundle.getString(key); 50 if (value == null && parent != null) 51 return parent.getString(key); 52 return value; 53 } 54 55 private final String name; 56 private final Messages parent; 57 private final ResourceBundle bundle; 58 59 private static Messages messages; 60 private static Object lock = new Object(); 61 62 public static Messages getMessages() { 63 synchronized (lock) { 64 if (messages == null) 65 messages = new Messages(); 66 } 67 return messages; 68 } 69 70 public static void updateMessageRoot(Class ref, String name) { 71 synchronized (lock) { 72 messages = new Messages(ref, messages, name); 73 } 74 75 } 76 77 public static String getTitle(String id) { 78 return getComponentText(id, "title"); 79 } 80 81 public static String getDescription(String id) { 82 return getComponentText(id, "desc"); 83 } 84 85 public static String getProcessTitle(String id) { 86 return getTitle(id + ".process"); 87 } 88 89 public static String getProcessDescription(String id) { 90 return getDescription(id + ".process"); 91 } 92 93 public static String getOK(String id) { 94 final String text = getComponentText(id, "ok.title"); 95 if (text != null) 96 return text; 97 else 98 return getText("ok"); 99 } 100 101 public static String getOKToolTip(String id) { 102 final String text = getComponentText(id, "ok.desc"); 103 if (text != null) 104 return text; 105 else 106 return getOK("ok"); 107 } 108 109 public static String getComponentText(String id, String part) { 110 return getText(id + "." + part); 111 } 112 113 public static String getText(String id) { 114 return getMessages().getString(id); 115 } 116 117 public static String getText(Class ref, String id) { 118 return getMessages().getString(id); 119 } 120 121 public static String getText(Object object, String id) { 122 return getText(object.getClass(), id); 123 } 124 125 126 private static Preferences getPrefs() { 127 return Preferences.userNodeForPackage(Messages.class); 128 } 129 130 public static Locale getLocale() { 131 Locale deflocale = Locale.getDefault(); 132 Preferences prefs = getPrefs(); 133 // String cc = prefs.get(CC, deflocale.getCountry()); 134 String lang = prefs.get(LANG, deflocale.getLanguage()); 135 if (deflocale.getLanguage().equals(lang)) { 136 return deflocale; 137 } 138 // String variant=prefs.get("LANGUAGE",deflocale.getVariant()); 139 140 final Locale locale = new Locale(lang); 141 Locale.setDefault(locale); 142 return locale; 143 } 144 145 public static void updateLocale(String language, String country) { 146 Preferences prefs = getPrefs(); 147 Locale.setDefault(new Locale(language, country)); 148 prefs.put(LANG, language); 149 prefs.put(CC, country); 150 try { 151 prefs.flush(); 152 } catch (BackingStoreException e) { 153 System.err.println(e.getLocalizedMessage()); 154 } 155 } 156 157 public static void updateLocale(String language) { 158 Preferences prefs = getPrefs(); 159 Locale.setDefault(new Locale(language)); 160 prefs.put(LANG, language); 161 try { 162 prefs.flush(); 163 } catch (BackingStoreException e) { 164 System.err.println(e.getLocalizedMessage()); 165 } 166 } 167 168 private static final String CC = "CC"; 169 private static final String LANG = "LANG"; 170 171 172 }

This page was automatically generated by Maven