Skip to content

Commit 71b87e9

Browse files
committed
change how new prefs are loaded and platform-specific handling
1 parent 8ae5d67 commit 71b87e9

1 file changed

Lines changed: 53 additions & 10 deletions

File tree

app/src/processing/app/Preferences.java

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class Preferences {
5050
static final String PREFS_FILE = "preferences.txt"; //$NON-NLS-1$
5151

5252
static HashMap<String, String> defaults;
53-
static HashMap<String, String> table = new HashMap<String,String>();
53+
static HashMap<String, String> table = new HashMap<String, String>();
5454
static File preferencesFile;
5555

5656
static final String PROMPT_YES = Language.text("prompt.yes");
@@ -66,9 +66,6 @@ public class Preferences {
6666
static public int BUTTON_WIDTH =
6767
Integer.parseInt(Language.text("preferences.button.width"));
6868

69-
// /** height of the EditorHeader, EditorToolbar, and EditorStatus */
70-
// static final int GRID_SIZE = 32;
71-
7269
// Indents and spacing standards. These probably need to be modified
7370
// per platform as well, because Mac OS X is so huge, Windows is smaller,
7471
// and Linux is all over the map. Consider these deprecated.
@@ -90,6 +87,8 @@ static public void init() {
9087
"You'll need to reinstall Processing.", e);
9188
}
9289

90+
/* provisionally removed in 3.0a6, see changes in load()
91+
9392
// check for platform-specific properties in the defaults
9493
String platformExt = "." + PConstants.platformNames[PApplet.platform]; //$NON-NLS-1$
9594
int platformExtLength = platformExt.length();
@@ -109,9 +108,10 @@ static public void init() {
109108
String value = get(key);
110109
set(actualKey, value);
111110
}
111+
*/
112112

113-
// clone the hash table
114-
//defaults = (HashMap<String, String>) table.clone();
113+
// Clone the defaults, then override any them with the user's preferences.
114+
// This ensures that any new/added preference will be present.
115115
defaults = new HashMap<String, String>(table);
116116

117117
// other things that have to be set explicitly for the defaults
@@ -158,10 +158,19 @@ static protected String getPreferencesPath() {
158158
}
159159

160160

161-
// .................................................................
161+
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
162162

163163

164+
/**
165+
* Load a set of key/value pairs from a UTF-8 encoded file into 'table'.
166+
* For 3.0a6, this removes any platform-specific extensions from keys, so
167+
* that we don't have platform-specific entries in a user's preferences.txt
168+
* file, which would require all prefs to be changed twice, or risk being
169+
* overwritten by the unchanged platform-specific version on reload.
170+
*/
164171
static public void load(InputStream input) throws IOException {
172+
HashMap<String, String> platformSpecific = new HashMap<>();
173+
165174
String[] lines = PApplet.loadStrings(input); // Reads as UTF-8
166175
for (String line : lines) {
167176
if ((line.length() == 0) ||
@@ -172,13 +181,46 @@ static public void load(InputStream input) throws IOException {
172181
if (equals != -1) {
173182
String key = line.substring(0, equals).trim();
174183
String value = line.substring(equals + 1).trim();
175-
table.put(key, value);
184+
if (!isPlatformSpecific(key, value, platformSpecific)) {
185+
table.put(key, value);
186+
}
187+
}
188+
}
189+
// Now override the keys with any platform-specific defaults we've found.
190+
for (String key : platformSpecific.keySet()) {
191+
table.put(key, platformSpecific.get(key));
192+
}
193+
}
194+
195+
196+
/**
197+
* @param key original key (may include platform extension)
198+
* @param value
199+
* @param specific where to put the key/value pairs for *this* platform
200+
* @return true if a platform-specific key
201+
*/
202+
static protected boolean isPlatformSpecific(String key, String value,
203+
Map<String, String> specific) {
204+
for (String platform : PConstants.platformNames) {
205+
String ext = "." + platform;
206+
if (key.endsWith(ext)) {
207+
String thisPlatform = PConstants.platformNames[PApplet.platform];
208+
if (platform.equals(thisPlatform)) {
209+
key = key.substring(0, key.lastIndexOf(ext));
210+
// store this for later overrides
211+
specific.put(key, value);
212+
} else {
213+
// ignore platform-specific defaults for other platforms,
214+
// but return 'true' because it needn't be added to the big list
215+
}
216+
return true;
176217
}
177218
}
219+
return false;
178220
}
179221

180222

181-
// .................................................................
223+
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
182224

183225

184226
static protected void save() {
@@ -191,6 +233,7 @@ static protected void save() {
191233
PrintWriter writer = PApplet.createWriter(preferencesFile);
192234

193235
String[] keyList = table.keySet().toArray(new String[table.size()]);
236+
// Sorting is really helpful for debugging, diffing, and finding keys
194237
keyList = PApplet.sort(keyList);
195238
for (String key : keyList) {
196239
writer.println(key + "=" + table.get(key)); //$NON-NLS-1$
@@ -201,7 +244,7 @@ static protected void save() {
201244
}
202245

203246

204-
// .................................................................
247+
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
205248

206249

207250
// all the information from preferences.txt

0 commit comments

Comments
 (0)