Changeset 2053 in josm for trunk/src/org/openstreetmap/josm/data
- Timestamp:
- 2009-09-04T17:33:27+02:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r2038 r2053 119 119 } 120 120 121 public File getPreferenceFile() { 122 return new File(getPreferencesDirFile(), "preferences"); 123 } 124 121 125 public File getPluginsDirFile() { 122 126 return new File(getPreferencesDirFile(), "plugins"); … … 254 258 properties.put(key, value); 255 259 } 256 save(); 260 try { 261 save(); 262 } catch(IOException e){ 263 System.out.println(tr("Warning: failed to persist preferences to ''{0}''", getPreferenceFile().getAbsoluteFile())); 264 } 257 265 firePreferenceChanged(key, value); 258 266 return true; … … 287 295 * in log. 288 296 */ 289 public void save() { 297 public void save() throws IOException { 290 298 /* currently unused, but may help to fix configuration issues in future */ 291 299 properties.put("josm.version", AboutAction.getVersionString()); 292 try { 293 setSystemProperties(); 294 String prefFile = getPreferencesDir() + "preferences"; 295 296 // Backup old preferences 297 copyFile(new File(prefFile), new File(prefFile + "_backup")); 298 299 final PrintWriter out = new PrintWriter(new OutputStreamWriter( 300 new FileOutputStream(prefFile + "_tmp"), "utf-8"), false); 301 for (final Entry<String, String> e : properties.entrySet()) { 302 String s = defaults.get(e.getKey()); 303 /* don't save default values */ 304 if(s == null || !s.equals(e.getValue())) { 305 out.println(e.getKey() + "=" + e.getValue()); 306 } 307 } 308 out.close(); 309 310 File tmpFile = new File(prefFile + "_tmp"); 311 copyFile(tmpFile, new File(prefFile)); 312 tmpFile.delete(); 313 314 } catch (final IOException e) { 315 e.printStackTrace(); 316 // do not message anything, since this can be called from strange 317 // places. 318 } 300 301 setSystemProperties(); 302 File prefFile = new File(getPreferencesDirFile(), "preferences"); 303 304 // Backup old preferences if there are old preferences 305 if(prefFile.exists()) { 306 copyFile(prefFile, new File(prefFile + "_backup")); 307 } 308 309 final PrintWriter out = new PrintWriter(new OutputStreamWriter( 310 new FileOutputStream(prefFile + "_tmp"), "utf-8"), false); 311 for (final Entry<String, String> e : properties.entrySet()) { 312 String s = defaults.get(e.getKey()); 313 /* don't save default values */ 314 if(s == null || !s.equals(e.getValue())) { 315 out.println(e.getKey() + "=" + e.getValue()); 316 } 317 } 318 out.close(); 319 320 File tmpFile = new File(prefFile + "_tmp"); 321 copyFile(tmpFile, prefFile); 322 tmpFile.delete(); 319 323 } 320 324 … … 366 370 } 367 371 368 public void init(Boolean reset) 369 { 372 public void init(boolean reset){ 370 373 // get the preferences. 371 374 File prefDir = getPreferencesDirFile(); 372 375 if (prefDir.exists()) { 373 376 if(!prefDir.isDirectory()) { 377 System.err.println(tr("Warning: Failed to initialize preferences. Preference directory ''{0}'' isn't a directory.", prefDir.getAbsoluteFile())); 374 378 JOptionPane.showMessageDialog( 375 379 Main.parent, 376 tr(" Cannot openpreferences directory: {0}",Main.pref.getPreferencesDir()),380 tr("<html>Failed to initialize preferences.<br>Preference directory ''{0}'' isn't a directory.</html>", prefDir.getAbsoluteFile()), 377 381 tr("Error"), 378 382 JOptionPane.ERROR_MESSAGE … … 381 385 } 382 386 } else { 383 prefDir.mkdirs(); 384 } 385 386 if (!new File(getPreferencesDir()+"preferences").exists()) { 387 resetToDefault(); 388 } 389 387 if (! prefDir.mkdirs()) { 388 System.err.println(tr("Warning: Failed to initialize preferences. Failed to create missing preference directory: {0}", prefDir.getAbsoluteFile())); 389 JOptionPane.showMessageDialog( 390 Main.parent, 391 tr("<html>Failed to initialize preferences.<br>Failed to create missing preference directory: {0}</html>",prefDir.getAbsoluteFile()), 392 tr("Error"), 393 JOptionPane.ERROR_MESSAGE 394 ); 395 return; 396 } 397 } 398 399 File preferenceFile = getPreferenceFile(); 390 400 try { 391 if (reset) { 401 if (!preferenceFile.exists()) { 402 System.out.println(tr("Warning: Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile())); 392 403 resetToDefault(); 393 } else { 394 load(); 395 } 396 } catch (final IOException e1) { 397 e1.printStackTrace(); 398 String backup = getPreferencesDir() + "preferences.bak"; 404 save(); 405 } else if (reset) { 406 System.out.println(tr("Warning: Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile())); 407 resetToDefault(); 408 save(); 409 } 410 } catch(IOException e) { 411 e.printStackTrace(); 399 412 JOptionPane.showMessageDialog( 400 413 Main.parent, 401 tr("Preference s file had errors. Making backup of old one to {0}.", backup),414 tr("<html>Failed to initialize preferences.<br>Failed to reset preference file to default: {0}</html>",getPreferenceFile().getAbsoluteFile()), 402 415 tr("Error"), 403 416 JOptionPane.ERROR_MESSAGE 404 417 ); 405 new File(getPreferencesDir() + "preferences").renameTo(new File(backup)); 406 save(); 407 } 408 } 409 410 public final void resetToDefault() { 418 return; 419 } 420 try { 421 load(); 422 } catch (IOException e) { 423 e.printStackTrace(); 424 File backupFile = new File(prefDir,"preferences.bak"); 425 JOptionPane.showMessageDialog( 426 Main.parent, 427 tr("<html>Preferences file had errors.<br> Making backup of old one to <br>{0}<br> and creating a new default preference file.</html>", backupFile.getAbsoluteFile()), 428 tr("Error"), 429 JOptionPane.ERROR_MESSAGE 430 ); 431 preferenceFile.renameTo(backupFile); 432 try { 433 resetToDefault(); 434 save(); 435 } catch(IOException e1) { 436 e1.printStackTrace(); 437 System.err.println(tr("Warning: Failed to initialize preferences.Failed to reset preference file to default: {0}", getPreferenceFile())); 438 } 439 } 440 } 441 442 public final void resetToDefault(){ 411 443 properties.clear(); 412 444 put("layerlist.visible", true); … … 419 451 put("laf", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 420 452 } 421 save(); 453 } 454 455 public File getBookmarksFile() { 456 return new File(getPreferencesDir(),"bookmarks"); 422 457 } 423 458 424 459 public Collection<Bookmark> loadBookmarks() throws IOException { 425 File bookmarkFile = new File(getPreferencesDir()+"bookmarks");460 File bookmarkFile = getBookmarksFile(); 426 461 if (!bookmarkFile.exists()) { 427 462 bookmarkFile.createNewFile();
Note:
See TracChangeset
for help on using the changeset viewer.