1 21 22 package org.armedbear.j; 23 24 import java.awt.Color ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.util.ArrayList ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 import java.util.Properties ; 31 32 public final class Preferences 33 { 34 private Properties properties = new Properties (); 35 private ArrayList listeners; 36 37 public static final File getPreferencesFile() 38 { 39 return File.getInstance(Directories.getEditorDirectory(), "prefs"); 40 } 41 42 public synchronized void reload() 43 { 44 reloadInternal(); 45 firePreferencesChanged(); 46 } 47 48 private void reloadInternal() 49 { 50 File file = getPreferencesFile(); 51 if (file == null || !file.isFile()) { 52 properties = new Properties (); 54 return; 55 } 56 57 Properties temp = new Properties (); 60 try { 61 InputStream in = file.getInputStream(); 62 temp.load(in); 63 in.close(); 64 } 65 catch (IOException e) { 66 Log.error(e); 67 } 68 temp = canonicalize(temp); 70 71 String themeName = temp.getProperty(Property.THEME.key()); 72 if (themeName == null || themeName.length() == 0) { 73 properties = temp; 75 return; 76 } 77 78 String themePath = temp.getProperty(Property.THEME_PATH.key()); 79 80 properties = loadTheme(themeName, themePath); 82 83 properties.putAll(temp); 85 } 86 87 private static Properties canonicalize(Properties properties) 89 { 90 Properties newProperties = new Properties (); 91 for (Enumeration e = properties.keys(); e.hasMoreElements();) { 92 String key = (String ) e.nextElement(); 93 newProperties.put(key.toLowerCase(), properties.get(key)); 94 } 95 return newProperties; 96 } 97 98 public synchronized void killTheme() 100 { 101 Iterator it = properties.keySet().iterator(); 102 while (it.hasNext()) { 103 String key = (String ) it.next(); 104 if (key.startsWith("color.")) 105 it.remove(); 106 else if (key.indexOf(".color.") >= 0) 107 it.remove(); 108 else if (key.startsWith("style.")) 109 it.remove(); 110 else if (key.indexOf(".style.") >= 0) 111 it.remove(); 112 } 113 } 114 115 private static Properties loadTheme(String themeName, String themePath) 116 { 117 Properties properties = new Properties (); 118 File file = getThemeFile(themeName, themePath); 119 if (file != null && file.isFile()) { 120 try { 121 InputStream in = file.getInputStream(); 122 properties.load(in); 123 in.close(); 124 } 125 catch (IOException e) { 126 Log.error(e); 127 } 128 } 129 return canonicalize(properties); 130 } 131 132 private static File getThemeFile(String themeName, String themePath) 133 { 134 if (themeName == null) 135 return null; 136 themeName = stripQuotes(themeName); 137 138 if (Utilities.isFilenameAbsolute(themeName)) 141 return File.getInstance(themeName); 142 143 if (themePath != null) { 145 Path path = new Path(stripQuotes(themePath)); 146 String [] array = path.list(); 147 if (array != null) { 148 for (int i = 0; i < array.length; i++) { 149 File dir = File.getInstance(array[i]); 150 if (dir != null && dir.isDirectory()) { 151 File themeFile = File.getInstance(dir, themeName); 152 if (themeFile != null && themeFile.isFile()) 153 return themeFile; 154 } 155 } 156 } 157 } 158 159 String classPath = System.getProperty("java.class.path"); 161 if (classPath != null) { 162 Path path = new Path(classPath); 163 String [] array = path.list(); 164 if (array == null) 165 return null; 166 final File userDir = File.getInstance(System.getProperty("user.dir")); 167 for (int i = 0; i < array.length; i++) { 168 String pathComponent = array[i]; 169 if (pathComponent.endsWith("src")) { 170 File srcDir = File.getInstance(pathComponent); 172 if (srcDir != null && srcDir.isDirectory()) { 173 File parentDir = srcDir.getParentFile(); if (parentDir != null && parentDir.isDirectory()) { 175 File themeDir = File.getInstance(parentDir, "themes"); if (themeDir != null && themeDir.isDirectory()) { 177 File themeFile = File.getInstance(themeDir, themeName); 178 if (themeFile != null && themeFile.isFile()) 179 return themeFile; 180 } 181 } 182 } 183 } else { 184 String suffix = "j.jar"; 185 if (pathComponent.endsWith(suffix)) { 186 String prefix = pathComponent.substring(0, pathComponent.length() - suffix.length()); 188 File prefixDir; 190 if (prefix.length() == 0) { 191 prefixDir = userDir; 193 } else { 194 prefixDir = File.getInstance(userDir, prefix); 196 } 197 if (prefixDir != null && prefixDir.isDirectory()) { 198 File themeDir = File.getInstance(prefixDir, "themes"); 200 if (themeDir != null && themeDir.isDirectory()) { 202 File themeFile = File.getInstance(themeDir, themeName); 203 if (themeFile != null && themeFile.isFile()) 204 return themeFile; 205 } 206 } 207 } 208 } 209 } 210 } 211 return null; 212 } 213 214 public synchronized void setProperty(Property property, String value) 215 { 216 properties.setProperty(property.key(), value); 217 } 218 219 public synchronized void setProperty(String key, String value) 220 { 221 properties.setProperty(key.toLowerCase(), value); 222 } 223 224 public synchronized void removeProperty(String key) 225 { 226 properties.remove(key.toLowerCase()); 227 } 228 229 public synchronized String getStringProperty(Property property) 231 { 232 String value = getProperty(property.key()); 233 if (value != null) 234 return stripQuotes(value); 235 else 236 return (String ) property.getDefaultValue(); } 238 239 public synchronized String getStringProperty(String key) 241 { 242 String value = getProperty(key); 243 if (value != null) 244 return stripQuotes(value); 245 else 246 return null; 247 } 248 249 public synchronized boolean getBooleanProperty(Property property) 250 { 251 String value = getProperty(property.key()); 252 if (value != null) { 253 value = value.trim(); 254 if (value.equals("true") || value.equals("1")) 255 return true; 256 if (value.equals("false") || value.equals("0")) 257 return false; 258 } 259 return ((Boolean )property.getDefaultValue()).booleanValue(); 260 } 261 262 public synchronized boolean getBooleanProperty(Property property, boolean defaultValue) 263 { 264 return getBooleanProperty(property.key(), defaultValue); 265 } 266 267 public synchronized boolean getBooleanProperty(String key, boolean defaultValue) 268 { 269 String value = getProperty(key); 270 if (value != null) { 271 value = value.trim(); 272 if (value.equals("true") || value.equals("1")) 273 return true; 274 if (value.equals("false") || value.equals("0")) 275 return false; 276 } 277 return defaultValue; 278 } 279 280 public synchronized int getIntegerProperty(Property property) 281 { 282 String value = getProperty(property.key()); 283 if (value != null) { 284 value = value.trim(); 285 if (value.length() > 0) { 286 if (value.charAt(0) == '+') 288 value = value.substring(1).trim(); 289 try { 290 return Integer.parseInt(value); 291 } 292 catch (NumberFormatException e) {} 293 } 294 } 295 return ((Integer )property.getDefaultValue()).intValue(); 296 } 297 298 public synchronized Color getColorProperty(String key) 299 { 300 String value = getStringProperty(key); 301 if (value != null) 302 return Utilities.getColor(value); 303 return null; 304 } 305 306 private String getProperty(String key) 307 { 308 return properties.getProperty(key.toLowerCase()); 309 } 310 311 private static String stripQuotes(String s) 312 { 313 final int length = s.length(); 314 if (length >= 2) { 315 if (s.charAt(0) == '"' && s.charAt(length-1) == '"') 316 return s.substring(1, length-1); 317 else if (s.charAt(0) == '\'' && s.charAt(length-1) == '\'') 318 return s.substring(1, length-1); 319 } 320 return s.trim(); 322 } 323 324 public synchronized void addPreferencesChangeListener(PreferencesChangeListener listener) 325 { 326 if (listeners == null) 327 listeners = new ArrayList (); 328 listeners.add(listener); 329 } 330 331 public synchronized void firePreferencesChanged() 332 { 333 if (listeners != null) 334 for (int i = 0; i < listeners.size(); i++) 335 ((PreferencesChangeListener)listeners.get(i)).preferencesChanged(); 336 } 337 } 338 | Popular Tags |