@@ -22,13 +22,17 @@ public class UIPreferences {
22
22
23
23
public UIPreferences (Class <?> javaClass ) {
24
24
prefs = Preferences .userNodeForPackage (javaClass );
25
+ defineDefaultValues ();
25
26
if (!isBackingStoreAccessible ()) {
26
27
Logger .error ("User preferences could not be accessed, using default values!" );
27
28
} else {
28
29
addMissingValues ();
29
30
}
30
31
}
31
32
33
+ protected void defineDefaultValues () {
34
+ }
35
+
32
36
public boolean isBackingStoreAccessible () {
33
37
try {
34
38
prefs .keys ();
@@ -39,19 +43,19 @@ public boolean isBackingStoreAccessible() {
39
43
}
40
44
}
41
45
42
- public void addMissingValues () {
46
+ private void addMissingValues () {
43
47
try {
44
48
Set <String > prefKeys = new HashSet <>(Arrays .asList (prefs .keys ()));
45
49
List <String > allKeys = defaultValueMap .keySet ().stream ().sorted ().toList ();
46
50
for (String key : allKeys ) {
47
51
if (!prefKeys .contains (key )) {
48
52
Object defaultValue = defaultValueMap .get (key );
49
53
storeValue (key , defaultValue );
50
- Logger .info ("Added missing entry '{}'='{}'" , key , defaultValue );
54
+ Logger .info ("Added missing preference '{}'='{}'" , key , defaultValue );
51
55
}
52
56
}
53
57
} catch (BackingStoreException x ) {
54
- Logger .error ("Could not access preferences to add missing keys" );
58
+ Logger .error ("Could not access preferences store to add missing keys" );
55
59
Logger .error (x );
56
60
}
57
61
}
@@ -81,7 +85,7 @@ public void storeDefaultColor(String key, Color color) {
81
85
}
82
86
83
87
public Color getColor (String colorKey ) {
84
- String colorValue = prefs .get (colorKey , getDefaultValue (colorKey ));
88
+ String colorValue = prefs .get (colorKey , getDefaultValue (colorKey , String . class ));
85
89
return Color .web (colorValue );
86
90
}
87
91
@@ -112,21 +116,24 @@ public void storeDefaultFont(String key, Font font) {
112
116
113
117
public Font getFont (String key ) {
114
118
String familyKey = key + ".family" ;
115
- String family = prefs .get (familyKey , getDefaultValue (familyKey ));
119
+ String family = prefs .get (familyKey , getDefaultValue (familyKey , String . class ));
116
120
117
121
String styleKey = key + ".style" ;
118
- String style = prefs .get (styleKey , getDefaultValue (styleKey ));
122
+ String style = prefs .get (styleKey , getDefaultValue (styleKey , String . class ));
119
123
120
124
String sizeKey = key + ".size" ;
121
- Object defaultSize = getDefaultValue (sizeKey );
122
- float size = prefs .getFloat (sizeKey , defaultSize instanceof Float ? ((Float ) defaultSize ) : 8 );
125
+ float size = prefs .getFloat (sizeKey , getDefaultValue (sizeKey , Float .class ));
123
126
124
127
return Font .font (family , FontWeight .findByName (style ), size );
125
128
}
126
129
127
- @ SuppressWarnings ("unchecked" )
128
- public <T > T getDefaultValue (String key ) {
129
- return (T ) defaultValueMap .get (key );
130
+ public <T > T getDefaultValue (String key , Class <T > type ) {
131
+ Object defaultValue = defaultValueMap .get (key );
132
+ if (type .isInstance (defaultValue )) {
133
+ return type .cast (defaultValue );
134
+ }
135
+ throw new IllegalArgumentException ("There is no default preference value of type %s for key '%s'"
136
+ .formatted (type .getSimpleName (), defaultValue ));
130
137
}
131
138
132
139
private Object getValue (String key ) {
0 commit comments