-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathSettings.java
More file actions
296 lines (231 loc) · 7.79 KB
/
Settings.java
File metadata and controls
296 lines (231 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2004-11 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app;
import java.awt.*;
import java.io.*;
import java.util.*;
import processing.app.ui.Toolkit;
import processing.core.*;
/**
* Storage class for theme settings. This was separated from the Preferences
* class for 1.0 so that the coloring wouldn't conflict with previous releases
* and to make way for future ability to customize.
*/
public class Settings {
/**
* Copy of the defaults in case the user mangles a preference.
* It's necessary to keep a copy of the defaults around, because the user may
* have replaced a setting on their own. In the past, we used to load the
* defaults, then replace those with what was in the user's preferences file.
* Problem is, if something like a font entry in the user's file no longer
* parses properly, we need to be able to get back to a clean version of that
* setting so that we can recover.
*/
Map<String, String> defaults;
/** Table of attributes/values. */
Map<String, String> table = new HashMap<>();
/** Associated file for this settings data. */
File file;
public Settings(File file) throws IOException {
this.file = file;
if (file.exists()) {
load();
}
// clone the hash table
defaults = new HashMap<>(table);
}
public void load() {
load(file);
}
public void load(File additions) {
String[] lines = PApplet.loadStrings(additions);
if (lines != null) {
for (String line : lines) {
if ((line.length() == 0) ||
(line.charAt(0) == '#')) continue;
// this won't properly handle = signs being in the text
int equals = line.indexOf('=');
if (equals != -1) {
String key = line.substring(0, equals).trim();
String value = line.substring(equals + 1).trim();
table.put(key, value);
}
}
} else {
Messages.err(additions + " could not be read");
}
// check for platform-specific properties in the defaults
String platformExt = "." + Platform.getName();
int platformExtLength = platformExt.length();
for (String key : table.keySet()) {
if (key.endsWith(platformExt)) {
// this is a key specific to a particular platform
String actualKey = key.substring(0, key.length() - platformExtLength);
String value = get(key);
table.put(actualKey, value);
}
}
}
public void save() {
save(file); // save back to the original file
}
public void save(File outputFile) {
PrintWriter writer = PApplet.createWriter(outputFile);
String[] keyList = table.keySet().toArray(new String[0]);
// Sorting is really helpful for debugging, diffing, and finding keys
keyList = PApplet.sort(keyList);
for (String key : keyList) {
writer.println(key + "=" + table.get(key));
}
writer.flush();
writer.close();
}
public String get(String attribute) {
return table.get(attribute);
}
public String getDefault(String attribute) {
return defaults.get(attribute);
}
public void set(String attribute, String value) {
table.put(attribute, value);
}
public boolean getBoolean(String attribute) {
String value = get(attribute);
if (value == null) {
System.err.println("Boolean not found: " + attribute);
return false;
}
return Boolean.parseBoolean(value);
}
public void setBoolean(String attribute, boolean value) {
set(attribute, value ? "true" : "false");
}
public int getInteger(String attribute) {
String value = get(attribute);
if (value == null) {
System.err.println("Integer not found: " + attribute);
return 0;
}
return Integer.parseInt(value);
}
@SuppressWarnings("unused")
public void setInteger(String key, int value) {
set(key, String.valueOf(value));
}
/**
* Parse a color from a Settings file. Values are hexadecimal in either
* #RRGGBB (for opaque) or 0xAARRGGBB format (to include alpha).
*/
public Color getColor(String attribute) {
Color outgoing = null;
String s = get(attribute);
if (s != null) {
try {
if (s.startsWith("#")) {
// parse a 6-digit hex color
outgoing = new Color(Integer.parseInt(s.substring(1), 16));
} else if (s.startsWith("0x")) {
int v = Integer.parseInt(s.substring(2), 16);
outgoing = new Color(v, true);
}
} catch (Exception ignored) { }
}
if (outgoing == null) {
System.err.println("Could not parse color " + s + " for " + attribute);
}
return outgoing;
}
public void setColor(String attr, Color what) {
set(attr, "#" + PApplet.hex(what.getRGB() & 0xffffff, 6));
}
// identical version found in Preferences.java
public Font getFont(String attr) {
try {
boolean replace = false;
String value = get(attr);
if (value == null) {
// use the default font instead
value = getDefault(attr);
replace = true;
}
String[] pieces = PApplet.split(value, ',');
if (pieces.length != 3) {
value = getDefault(attr);
pieces = PApplet.split(value, ',');
replace = true;
}
String name = pieces[0];
int style = Font.PLAIN; // equals zero
if (pieces[1].contains("bold")) { //$NON-NLS-1$
style |= Font.BOLD;
}
if (pieces[1].contains("italic")) { //$NON-NLS-1$
style |= Font.ITALIC;
}
int size = PApplet.parseInt(pieces[2], 12);
size = Toolkit.zoom(size);
// replace bad font with the default from lib/preferences.txt
if (replace) {
set(attr, value);
}
if (!name.startsWith("processing.")) {
return new Font(name, style, size);
} else {
if (pieces[0].equals("processing.sans")) {
return Toolkit.getSansFont(size, style);
} else if (pieces[0].equals("processing.mono")) {
return Toolkit.getMonoFont(size, style);
}
}
} catch (Exception e) {
// Adding try/catch block because this may be where
// a lot of startup crashes are happening.
Messages.log("Error with font " + get(attr) + " for attribute " + attr);
}
return new Font("Dialog", Font.PLAIN, 12);
}
public String remove(String key) {
return table.remove(key);
}
/**
* The day of reckoning: save() a file if it has entries, or delete
* if the file exists but the table no longer has any entries.
*/
public void reckon() {
if (table.isEmpty()) {
if (file.exists() && !file.delete()) {
System.err.println("Could not remove empty " + file);
}
} else {
save();
}
}
public boolean isEmpty() {
return table.isEmpty();
}
public void print() {
String[] keys = table.keySet().toArray(new String[0]);
Arrays.sort(keys);
for (String key : keys) {
System.out.println(key + " = " + table.get(key));
}
}
public Map<String, String> getMap() {
return table;
}
}