()
| 210 | |
| 211 | |
| 212 | static public void save() { |
| 213 | // On startup it'll be null, don't worry about it. It's trying to update |
| 214 | // the prefs for the open sketch before Preferences.init() has been called. |
| 215 | if (preferencesFile != null) { |
| 216 | try { |
| 217 | File dir = preferencesFile.getParentFile(); |
| 218 | File preferencesTemp = File.createTempFile("preferences", ".txt", dir); |
| 219 | preferencesTemp.setWritable(true, false); |
| 220 | |
| 221 | // Fix for 0163 to properly use Unicode when writing preferences.txt |
| 222 | PrintWriter writer = PApplet.createWriter(preferencesTemp); |
| 223 | |
| 224 | String[] keyList = table.keySet().toArray(new String[table.size()]); |
| 225 | // Sorting is really helpful for debugging, diffing, and finding keys |
| 226 | keyList = PApplet.sort(keyList); |
| 227 | for (String key : keyList) { |
| 228 | writer.println(key + "=" + table.get(key)); //$NON-NLS-1$ |
| 229 | } |
| 230 | writer.flush(); |
| 231 | writer.close(); |
| 232 | |
| 233 | // Rename preferences.txt to preferences.old |
| 234 | File oldPreferences = new File(dir, "preferences.old"); |
| 235 | if (oldPreferences.exists()) { |
| 236 | if (!oldPreferences.delete()) { |
| 237 | throw new IOException("Could not delete preferences.old"); |
| 238 | } |
| 239 | } |
| 240 | if (preferencesFile.exists() && |
| 241 | !preferencesFile.renameTo(oldPreferences)) { |
| 242 | throw new IOException("Could not replace preferences.old"); |
| 243 | } |
| 244 | // Make the temporary file into the real preferences |
| 245 | if (!preferencesTemp.renameTo(preferencesFile)) { |
| 246 | throw new IOException("Could not move preferences file into place"); |
| 247 | } |
| 248 | |
| 249 | } catch (IOException e) { |
| 250 | Messages.showWarning("Preferences", |
| 251 | "Could not save the Preferences file.", e); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | |
| 257 | // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
no test coverage detected