|
21 | 21 |
|
22 | 22 | package processing.app; |
23 | 23 |
|
24 | | -import java.io.BufferedReader; |
25 | | -import java.io.File; |
26 | | -import java.io.FileReader; |
27 | | -import java.util.Properties; |
| 24 | +import java.io.IOException; |
| 25 | + |
28 | 26 |
|
29 | 27 | /** |
30 | | - * Storage class for user preferences and environment settings. |
31 | | - * <P> |
32 | | - * This class does not use the Properties class because .properties files use |
33 | | - * ISO 8859-1 encoding, which is highly likely to be a problem when trying to |
34 | | - * save sketch folders and locations. Like the rest of Processing, we use UTF8. |
35 | | - * <p> |
36 | | - * We don't use the Java Preferences API because it would entail writing to |
37 | | - * the registry (on Windows), or an obscure file location (on Mac OS X) and |
38 | | - * make it far more difficult (impossible) to remove the preferences.txt to |
39 | | - * reset them (when they become corrupt), or to find the the file to make |
40 | | - * edits for numerous obscure preferences that are not part of the preferences |
41 | | - * window. If we added a generic editor (e.g. about:config in Mozilla) for |
42 | | - * such things, we could start using the Java Preferences API. But wow, that |
43 | | - * sounds like a lot of work. Not unlike writing this paragraph. |
| 28 | + * Minimal stand-in for the app's Preferences so the preprocessor can be |
| 29 | + * used without the PDE. Delegates to the standalone |
| 30 | + * {@link processing.utils.Preferences}, which bundles the default settings, |
| 31 | + * so preferences resolve even when no preferences.txt exists yet. |
44 | 32 | */ |
45 | 33 | public class Preferences { |
46 | | - static public String get(String attribute /*, String defaultValue */) { |
47 | | - try { |
48 | | - var settingsFile = Base.getSettingsFile("preferences.txt"); |
49 | | - var reader = new BufferedReader(new FileReader(settingsFile)); |
50 | | - |
51 | | - var settings = new Properties(); |
52 | | - settings.load(reader); |
53 | | - reader.close(); |
54 | | - |
55 | | - return settings.getProperty(attribute); |
56 | | - }catch (Exception e) { |
57 | | - return null; |
58 | | - } |
59 | | - } |
60 | | - static public boolean getBoolean(String attribute) { |
61 | | - String value = get(attribute); |
62 | | - return Boolean.parseBoolean(value); |
63 | | - } |
64 | | - static public int getInteger(String attribute /*, int defaultValue*/) { |
65 | | - return Integer.parseInt(get(attribute)); |
| 34 | + |
| 35 | + static private void ensureInitialized() { |
| 36 | + if (!processing.utils.Preferences.isInitialized()) { |
| 37 | + try { |
| 38 | + processing.utils.Preferences.init(); |
| 39 | + } catch (IOException e) { |
| 40 | + // The bundled defaults are enough to preprocess with; an unreadable |
| 41 | + // preferences.txt shouldn't stop a build. |
| 42 | + System.err.println("Could not read preferences: " + e.getMessage()); |
| 43 | + } |
66 | 44 | } |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + static public String get(String attribute) { |
| 49 | + ensureInitialized(); |
| 50 | + return processing.utils.Preferences.get(attribute); |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + static public boolean getBoolean(String attribute) { |
| 55 | + ensureInitialized(); |
| 56 | + return processing.utils.Preferences.getBoolean(attribute); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + static public int getInteger(String attribute) { |
| 61 | + ensureInitialized(); |
| 62 | + return processing.utils.Preferences.getInteger(attribute); |
| 63 | + } |
67 | 64 | } |
0 commit comments