001 package net.sourceforge.mp3dings;
002
003 import java.awt.Color;
004 import java.awt.Font;
005 import java.awt.Insets;
006 import java.io.File;
007 import java.text.MessageFormat;
008 import javax.swing.UIManager;
009 import java.util.Enumeration;
010 import java.util.ArrayList;
011 import java.util.Locale;
012 import java.util.List;
013 import java.util.ResourceBundle;
014 import java.util.prefs.Preferences;
015 import java.util.logging.Level;
016 import java.util.logging.Logger;
017 import de.mospace.lang.Array;
018 import de.mospace.lang.FullTextString;
019
020 /** Provides static constants for the application. */
021 public final class Globals {
022 public static final String OK = UIManager.getString("OptionPane.okButtonText");
023 public static final String CANCEL = UIManager.getString("OptionPane.cancelButtonText");
024 private static final Logger logger = Logger.getLogger(Globals.class.getPackage().getName());
025 private static String currentLocale = Locale.getDefault().getLanguage();
026 public static final String VERSION = "2.0";
027 public static ResourceBundle LR =
028 ResourceBundle.getBundle("net.sourceforge.mp3dings.LanguageResource");
029 private static Preferences userprefs;
030 private final static String emptyString = "";
031 private final static FullTextString emptyFTS = new FullTextString(emptyString);
032 private static String DIR_HOME = null;
033 static {
034 String javaVersion = System.getProperty("java.version");
035 /* System.getenv throws a java.lang.Error in Java 1.4
036 * therefore we need to protect the call to System.getenv */
037 if(!
038 (javaVersion.equals("1.4") || javaVersion.startsWith("1.4."))
039 ){
040 try{
041 DIR_HOME = System.getenv("APPDATA");
042 } catch (Throwable ignore){
043 }
044 }
045 if(DIR_HOME == null){
046 DIR_HOME = System.getProperty("user.home") + File.separator + ".mp3dings";
047 } else {
048 DIR_HOME += File.separator + "Mp3dings";
049 }
050 logger.info("Reading/writing application data from/to " + DIR_HOME);
051 }
052 private final static String DIR_LIB = DIR_HOME + File.separator + "lib";
053 private final static String DIR_FILTER = DIR_HOME + File.separator + "filters";
054 private final static String DIR_BEANS = DIR_HOME + File.separator + "beans";
055
056 public static String BUILDID = getString("ABOUT_BOX_VERSION");
057
058 final private static Color[] colors;
059 final public static int COLOR_MAIN_BG = 0;
060 final public static int COLOR_TAB_BG = 1;
061 final public static int COLOR_TOOLB_BG = 2;
062 final public static int COLOR_PANEL_BG = 3;
063 final public static int COLOR_FILTERED = 4;
064 final public static int COLOR_UNFILTERED = 5;
065 static{
066 int[] defaultColors = {
067 -1184257, //mainBackground
068 -1381889, //tabsBackground
069 -197419, //toolbarBackground
070 -13159, //panelBackground
071 -16751052, //header foreground filtered
072 -16777215, //header foreground unfiltered
073 };
074 String[] prefColors = getPrefNode().get("Colors","").split(",");
075
076 final int nn = Math.min(prefColors.length, defaultColors.length);
077 colors = new Color[defaultColors.length];
078
079 for(int i=0; i<nn; i++){
080 try{
081 int c = Integer.parseInt(prefColors[i]);
082 defaultColors[i] = c;
083 colors[i] = new Color(c);
084 } catch (NumberFormatException ex){
085 colors[i] = new Color(defaultColors[i]);
086 }
087 }
088 for(int i = prefColors.length; i<defaultColors.length; i++){
089 colors[i] = new Color(defaultColors[i]);
090 }
091 getPrefNode().put("Colors", Array.join(defaultColors,','));
092 }
093
094 /* @param which one of HOME, LIB, FILTERS, BEANS */
095 public static String getDir(String which){
096 if(which.equals("HOME")){
097 return DIR_HOME;
098 } else if(which.equals("LIB")){
099 return DIR_LIB;
100 } else if(which.equals("FILTERS")){
101 return DIR_FILTER;
102 } else if(which.equals("BEANS")){
103 return DIR_BEANS;
104 } else {
105 return null;
106 }
107 }
108
109 private Globals(){
110 }
111
112 public static Color getColor(int id){
113 return colors[id];
114 }
115
116 public static void logStackTrace(Logger logger, Level level, Throwable ex) {
117 StackTraceElement elements[] = ex.getStackTrace();
118 StringBuffer sb = new StringBuffer('\n').append(ex.toString());
119 for (int i=0, n=elements.length; i<n; i++) {
120 sb.append(String.valueOf(elements[i])).append('\n');
121 }
122 logger.log(level, sb.toString());
123 }
124
125 public static void logStackTrace(Logger logger, Throwable ex) {
126 logStackTrace(logger, Level.WARNING, ex);
127 }
128
129 public static void setColors(int[] rgb){
130 for(int i = 0; i < rgb.length; i++){
131 colors[i] = new Color(rgb[i]);
132 }
133 getPrefNode().put("Colors", Array.join(rgb,','));
134 }
135
136 public static String getMessage(String key, Object arg){
137 return MessageFormat.format(Globals.getString(key), new Object[]{arg});
138 }
139
140 public static String getMessage(String key, int arg){
141 return getMessage(key, new Integer(arg));
142 }
143
144 public static String getMessage(String key, Object[] args){
145 return MessageFormat.format(Globals.getString(key), args);
146 }
147
148 private static void checkLocale(){
149 if(!Locale.getDefault().getLanguage().equals(currentLocale)){
150 LR = ResourceBundle.getBundle(
151 "net.sourceforge.mp3dings.LanguageResource");
152 currentLocale = Locale.getDefault().getLanguage();
153 }
154 }
155
156 public static Preferences getPrefNode(){
157 if(userprefs == null){
158 userprefs = Preferences.userRoot().node("mp3dings").node(VERSION);
159 }
160 return userprefs;
161 }
162
163
164 private static final Integer[] INTEGERS = new Integer[100];
165 static{
166 for(int i=0; i<INTEGERS.length; i++){
167 INTEGERS[i] = new Integer(i);
168 }
169 }
170 public static Integer asInteger(int i){
171 if(i < 100){
172 return INTEGERS[i];
173 } else {
174 return new Integer(i);
175 }
176 }
177
178 public static boolean resourceExists(String s){
179 checkLocale();
180 Enumeration e = LR.getKeys();
181 boolean found = false;
182 while(e.hasMoreElements() && !found){
183 found = ((String) e.nextElement()).equals(s);
184 }
185 return found;
186 }
187
188 public static String getString(String s){
189 checkLocale();
190 return LR.getString(s);
191 }
192
193 public static int[] getJavaVersion(){
194 final String javaversion = System.getProperty("java.version");
195 if(javaversion == null){
196 return null;
197 }
198 StringBuffer sb = new StringBuffer();
199 char[] jvchars = javaversion.toCharArray();
200 List list = new ArrayList(3);
201 for(int i=0; i<jvchars.length; i++){
202 char c = jvchars[i];
203 boolean hasDigits = true;
204 if(Character.isDigit(c)){
205 sb.append(c);
206 hasDigits = true;
207 } else if(hasDigits){
208 list.add(sb.toString());
209 sb.setLength(0);
210 hasDigits = false;
211 }
212 }
213 list.add(sb.toString());
214 final int n = list.size();
215 int[] result = new int[n];
216 for(int i=0; i<n; i++){
217 String vpart = (String) list.get(i);
218 result[i] = (vpart.length() == 0)? 0 : Integer.parseInt(vpart);
219 }
220 return result;
221 }
222
223 public static String[] getStringArray(String s){
224 checkLocale();
225 Object o_result = LR.getObject(s);
226 String[] s_result;
227 if(o_result instanceof String[]){
228 s_result = (String[]) o_result;
229 } else if(o_result instanceof String){
230 s_result = ((String) o_result).split(";");
231 } else {
232 s_result = LR.getStringArray(s);
233 }
234 return s_result;
235 }
236
237 public static int getKey(String s){
238 return (int) getString(s).charAt(0);
239 }
240
241 public static Integer getKeyObject(String s){
242 return new Integer(getKey(s));
243 }
244
245 public static Object getObject(String key){
246 checkLocale();
247 Object result = LR.getObject(key);
248 if(result instanceof String){
249 String s = (String) result;
250 if(s.length() == 1){
251 char c = s.charAt(0);
252 int ctype = Character.getType(c);
253 if(ctype == Character.UPPERCASE_LETTER ||
254 ctype == Character.DECIMAL_DIGIT_NUMBER)
255 {
256 result = new Integer((int) c);
257 }
258 }
259 }
260 return result;
261 }
262
263 public static Object getNullFor(Class c){
264 Object v = null;
265 if (c == String.class){
266 v = emptyString;
267 } else if (c == Boolean.class){
268 v = Boolean.FALSE;
269 } else if(c == FullTextString.class){
270 v = emptyFTS;
271 }
272 return v;
273 }
274
275 public final static Font SERIF_0_10 = new Font("Serif", 0, 10);
276 public final static Font SERIF_0_12 = new Font("Serif", 0, 12);
277 public final static Font MONOSPACE_0_12 = new Font("Monospaced", 0, 12);
278 public final static Font SANS_BOLD_12 = new Font("SansSerif", Font.BOLD, 12);
279 public final static Insets INSETS_0505 = new Insets(0, 5, 0, 5);
280 public final static Insets INSETS_0 = new Insets(0, 0, 0, 0);
281 public final static Insets INSETS_5 = new Insets(5, 5, 5, 5);
282
283 }