-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathLegupPreferences.java
More file actions
234 lines (190 loc) · 7.25 KB
/
LegupPreferences.java
File metadata and controls
234 lines (190 loc) · 7.25 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
package edu.rpi.legup.app;
import edu.rpi.legup.ui.color.ColorPreferences;
import java.io.File;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.Function;
import java.util.prefs.Preferences;
public class LegupPreferences {
private static LegupPreferences instance;
private static String savedPath = "";
private static final Preferences preferences =
Preferences.userNodeForPackage(LegupPreferences.class);
private static final Map<LegupPreference, Object> preferencesMap = new EnumMap<>(LegupPreference.class);
public enum LegupPreference {
WORK_DIRECTORY("work-directory", System.getProperty("user.dir"), o -> o),
START_FULL_SCREEN("start-full-screen", false, Boolean::parseBoolean),
AUTO_UPDATE("auto-update", true, Boolean::parseBoolean),
DARK_MODE("night-mode", false, Boolean::parseBoolean),
USE_CUSTOM_COLOR_THEME("use-custom-color-theme", false, Boolean::parseBoolean),
SHOW_MISTAKES("show-mistakes", true, Boolean::parseBoolean),
SHOW_ANNOTATIONS("show-annotations", true, Boolean::parseBoolean),
ALLOW_DEFAULT_RULES("allow-default-rules", false, Boolean::parseBoolean),
AUTO_GENERATE_CASES("auto-generate-cases", true, Boolean::parseBoolean),
IMMEDIATE_FEEDBACK("immediate-feedback", true, Boolean::parseBoolean),
COLOR_THEME_FILE("color-theme-file", System.getProperty("user.dir") + File.separator + ColorPreferences.LIGHT_COLOR_THEME_FILE_NAME, o -> o),
COLOR_BLIND("color-blind", false, Boolean::parseBoolean);
private final String id;
private final Object defaultValue;
/**
* Converts the object to a string so that it can be saved in preferences
*/
private final Function<Object, String> stringMapper;
/**
* Converts a string value to an object so it's more convenient to use in code
*/
private final Function<String, Object> stringToValueMapper;
@SuppressWarnings("unchecked")
<T> LegupPreference(
String id,
T defaultValue,
Function<T, String> stringMapper,
Function<String, T> stringToValueMapper
) {
this.id = id;
this.defaultValue = defaultValue;
this.stringMapper = (Function<Object, String>) stringMapper;
this.stringToValueMapper = (Function<String, Object>) stringToValueMapper;
}
<T> LegupPreference(String id, T defaultValue, Function<String, Object> stringToValueMapper) {
this(id, defaultValue, String::valueOf, stringToValueMapper);
}
public String id() {
return this.id;
}
public String defaultStringValue() {
return stringMapper.apply(defaultValue);
}
@SuppressWarnings("unused")
public Object defaultValue() {
return defaultValue;
}
public String stringValue() {
return stringMapper.apply(preferencesMap.get(this));
}
/**
* Convenience method to return the value of this preference cast to {@code clazz}
* @param clazz
* @return
* @param <T>
*/
public <T> T as(Class<T> clazz) {
return clazz.cast(stringToValueMapper.apply(stringValue()));
}
public boolean asBoolean() {
return as(Boolean.class);
}
public boolean asBoolean(String errorMessage) {
return as(Boolean.class, errorMessage);
}
public <T> T as(Class<T> clazz, String errorMessage) {
try {
return clazz.cast(defaultValue);
} catch (Exception e) {
throw new RuntimeException(errorMessage);
}
}
@SuppressWarnings("unchecked")
public <T> T convertToValue(String value) {
return (T) stringToValueMapper.apply(value);
}
public String convertToString(Object value) {
return stringMapper.apply(value);
}
}
private static Object getPreferenceOrDefault(LegupPreference preference) {
final String current = preferences.get(preference.id(), null);
if (current == null) {
return preference.defaultValue;
}
return current;
}
private static void addPreferenceFromDefault(LegupPreference preference) {
preferencesMap.put(preference, getPreferenceOrDefault(preference));
}
static {
for (final LegupPreference preference : LegupPreference.values()) {
addPreferenceFromDefault(preference);
}
}
/**
* Gets the legup preferences singleton instance.
*
* @return legup preferences
*/
public static LegupPreferences getInstance() {
if (instance == null) {
instance = new LegupPreferences();
}
return instance;
}
/**
* Private LegupPreferences Singleton Constructor
*/
private LegupPreferences() {
}
/**
* Gets the user preference by the string key
*
* @param preference the preference to get
* @return value of the preference
*/
private Object getUserPref(LegupPreference preference) {
return preferencesMap.get(preference);
}
/**
* Gets the user preference by the string key, value pair
*
* @param preference the preference to change
* @param value value of the preference
*/
public void setUserPref(LegupPreference preference, Object value) {
preferences.put(preference.id(), preference.convertToString(value));
preferencesMap.put(preference, value);
}
public boolean getUserPrefAsBool(LegupPreference preference) {
return preference.asBoolean("Cannot get user preference - " + preference.id());
}
public String getSavedPath() {
return savedPath;
}
public void setSavedPath(String path) {
savedPath = path;
}
public static boolean colorBlind() {
return LegupPreference.COLOR_BLIND.asBoolean();
}
public static boolean darkMode() {
return LegupPreference.DARK_MODE.asBoolean();
}
public static boolean showAnnotations() {
return LegupPreference.SHOW_ANNOTATIONS.asBoolean();
}
public static boolean allowDefaultRules() {
return LegupPreference.ALLOW_DEFAULT_RULES.asBoolean();
}
public static String workDirectory() {
return LegupPreference.WORK_DIRECTORY.stringValue();
}
public static boolean startFullScreen() {
return LegupPreference.START_FULL_SCREEN.asBoolean();
}
public static boolean autoUpdate() {
return LegupPreference.AUTO_UPDATE.asBoolean();
}
public static boolean showMistakes() {
return LegupPreference.SHOW_MISTAKES.asBoolean();
}
public static boolean autoGenerateCases() {
return LegupPreference.AUTO_GENERATE_CASES.asBoolean();
}
public static boolean immediateFeedback() {
return LegupPreference.IMMEDIATE_FEEDBACK.asBoolean();
}
public static boolean useCustomColorTheme() {
return LegupPreference.USE_CUSTOM_COLOR_THEME.asBoolean();
}
public static String colorThemeFile() {
return LegupPreference.COLOR_THEME_FILE.stringValue();
}
}