|
3 | 3 | /* |
4 | 4 | Part of the Processing project - http://processing.org |
5 | 5 |
|
6 | | - Copyright (c) 2004-10 Ben Fry and Casey Reas |
| 6 | + Copyright (c) 2004-11 Ben Fry and Casey Reas |
7 | 7 | Copyright (c) 2001-04 Massachusetts Institute of Technology |
8 | 8 |
|
9 | 9 | This program is free software; you can redistribute it and/or modify |
|
37 | 37 | * and to make way for future ability to customize. |
38 | 38 | */ |
39 | 39 | public class Settings { |
40 | | - /** Copy of the defaults in case the user mangles a preference. */ |
| 40 | + /** |
| 41 | + * Copy of the defaults in case the user mangles a preference. |
| 42 | + * It's necessary to keep a copy of the defaults around, because the user may |
| 43 | + * have mangled a setting on their own. In the past, we used to load the |
| 44 | + * defaults, then replace those with what was in the user's preferences file. |
| 45 | + * Problem is, if something like a font entry in the user's file no longer |
| 46 | + * parses properly, we need to be able to get back to a clean version of that |
| 47 | + * setting so we can recover. |
| 48 | + */ |
41 | 49 | HashMap<String,String> defaults; |
42 | | - /** Table of attributes/values for the theme. */ |
| 50 | + |
| 51 | + /** Table of attributes/values. */ |
43 | 52 | HashMap<String,String> table = new HashMap<String,String>();; |
| 53 | + |
44 | 54 | /** Associated file for this settings data. */ |
45 | 55 | File file; |
46 | 56 |
|
47 | 57 |
|
48 | 58 | public Settings(File file) throws IOException { |
49 | 59 | this.file = file; |
50 | | - load(); |
51 | | - |
52 | | - // other things that have to be set explicitly for the defaults |
53 | | - setColor("run.window.bgcolor", SystemColor.control); |
54 | | - |
| 60 | + |
| 61 | + if (file.exists()) { |
| 62 | + load(); |
| 63 | + } |
| 64 | + |
55 | 65 | // clone the hash table |
56 | 66 | defaults = (HashMap<String,String>) table.clone(); |
57 | 67 | } |
@@ -86,19 +96,23 @@ public void load() { |
86 | 96 | } |
87 | 97 |
|
88 | 98 |
|
| 99 | + public void save() { |
| 100 | + PrintWriter writer = PApplet.createWriter(file); |
| 101 | + |
| 102 | + for (String key : table.keySet()) { |
| 103 | + writer.println(key + "=" + table.get(key)); |
| 104 | + } |
| 105 | + |
| 106 | + writer.flush(); |
| 107 | + writer.close(); |
| 108 | + } |
| 109 | + |
| 110 | + |
89 | 111 | public String get(String attribute) { |
90 | 112 | return table.get(attribute); |
91 | 113 | } |
92 | 114 |
|
93 | 115 |
|
94 | | - /** |
95 | | - * It's necessary to keep a copy of the defaults around, because the user may |
96 | | - * have mangled a setting on their own. In the past, we used to load the |
97 | | - * defaults, then replace those with what was in the user's preferences file. |
98 | | - * Problem is, if something like a font entry in the user's file no longer |
99 | | - * parses properly, we need to be able to get back to a clean version of that |
100 | | - * setting so we can recover. |
101 | | - */ |
102 | 116 | public String getDefault(String attribute) { |
103 | 117 | return defaults.get(attribute); |
104 | 118 | } |
|
0 commit comments