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