Changeset 2053 in josm


Ignore:
Timestamp:
2009-09-04T17:33:27+02:00 (15 years ago)
Author:
Gubaer
Message:

fixed #2322: Regression in r2038 (patches "JOSM destroys its preferences, when disk is full")

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r2038 r2053  
    119119    }
    120120
     121    public File getPreferenceFile() {
     122        return new File(getPreferencesDirFile(), "preferences");
     123    }
     124
    121125    public File getPluginsDirFile() {
    122126        return new File(getPreferencesDirFile(), "plugins");
     
    254258                properties.put(key, value);
    255259            }
    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            }
    257265            firePreferenceChanged(key, value);
    258266            return true;
     
    287295     * in log.
    288296     */
    289     public void save() {
     297    public void save() throws IOException {
    290298        /* currently unused, but may help to fix configuration issues in future */
    291299        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();
    319323    }
    320324
     
    366370    }
    367371
    368     public void init(Boolean reset)
    369     {
     372    public void init(boolean reset){
    370373        // get the preferences.
    371374        File prefDir = getPreferencesDirFile();
    372375        if (prefDir.exists()) {
    373376            if(!prefDir.isDirectory()) {
     377                System.err.println(tr("Warning: Failed to initialize preferences. Preference directory ''{0}'' isn't a directory.", prefDir.getAbsoluteFile()));
    374378                JOptionPane.showMessageDialog(
    375379                        Main.parent,
    376                         tr("Cannot open preferences directory: {0}",Main.pref.getPreferencesDir()),
     380                        tr("<html>Failed to initialize preferences.<br>Preference directory ''{0}'' isn't a directory.</html>", prefDir.getAbsoluteFile()),
    377381                        tr("Error"),
    378382                        JOptionPane.ERROR_MESSAGE
     
    381385            }
    382386        } 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();
    390400        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()));
    392403                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();
    399412            JOptionPane.showMessageDialog(
    400413                    Main.parent,
    401                     tr("Preferences 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()),
    402415                    tr("Error"),
    403416                    JOptionPane.ERROR_MESSAGE
    404417            );
    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(){
    411443        properties.clear();
    412444        put("layerlist.visible", true);
     
    419451            put("laf", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    420452        }
    421         save();
     453    }
     454
     455    public File getBookmarksFile() {
     456        return new File(getPreferencesDir(),"bookmarks");
    422457    }
    423458
    424459    public Collection<Bookmark> loadBookmarks() throws IOException {
    425         File bookmarkFile = new File(getPreferencesDir()+"bookmarks");
     460        File bookmarkFile = getBookmarksFile();
    426461        if (!bookmarkFile.exists()) {
    427462            bookmarkFile.createNewFile();
  • trunk/src/org/openstreetmap/josm/gui/BookmarkList.java

    r2017 r2053  
    1414import org.openstreetmap.josm.Main;
    1515import org.openstreetmap.josm.data.Preferences;
     16
     17import sun.security.action.GetBooleanAction;
    1618
    1719/**
     
    4446            JOptionPane.showMessageDialog(
    4547                    Main.parent,
    46                     tr("<html>Could not read bookmarks.<br>{0}</html>", e.getMessage()),
     48                    tr("<html>Could not read bookmarks from<br>''{0}''<br>Error was: {1}</html>",
     49                            Main.pref.getBookmarksFile(),
     50                            e.getMessage()
     51                    ),
    4752                    tr("Error"),
    4853                    JOptionPane.ERROR_MESSAGE
  • trunk/src/org/openstreetmap/josm/gui/MainApplet.java

    r2017 r2053  
    1010import java.awt.event.KeyEvent;
    1111import java.io.File;
     12import java.io.IOException;
    1213import java.net.URL;
    1314import java.util.Arrays;
     
    3637        public UploadPreferencesAction() {
    3738            super(tr("Upload Preferences"), "upload-preferences", tr("Upload the current preferences to the server"),
    38             Shortcut.registerShortcut("applet:uploadprefs", tr("Upload Preferences"), KeyEvent.VK_U, Shortcut.GROUP_HOTKEY), true);
     39                    Shortcut.registerShortcut("applet:uploadprefs", tr("Upload Preferences"), KeyEvent.VK_U, Shortcut.GROUP_HOTKEY), true);
    3940        }
    4041        public void actionPerformed(ActionEvent e) {
     
    7071        for (String[] s : paramInfo) {
    7172            Collection<String> p = readParameter(s[0], args.get(s[0]));
    72             if (p != null)
     73            if (p != null) {
    7374                args.put(s[0], p);
     75            }
    7476        }
    7577        if (!args.containsKey("geometry") && getParameter("width") != null && getParameter("height") != null) {
     
    98100        Main.pref = new ServerSidePreferences(getCodeBase());
    99101        ((ServerSidePreferences)Main.pref).download(username, password);
    100 
    101102        Main.preConstructorInit(args);
    102103        Main.parent = this;
     
    124125        String param = getParameter(s);
    125126        if (param != null) {
    126             if (v == null)
     127            if (v == null) {
    127128                v = new LinkedList<String>();
     129            }
    128130            v.addAll(Arrays.asList(param.split(";")));
    129131        }
Note: See TracChangeset for help on using the changeset viewer.