1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jarduino.gui;
18
19 import java.io.File;
20 import java.nio.charset.Charset;
21 import java.nio.charset.StandardCharsets;
22 import java.util.prefs.BackingStoreException;
23 import java.util.prefs.Preferences;
24
25 import net.sourceforge.jarduino.ArduinoException;
26
27
28
29
30 public final class ArduinoPreferences {
31
32
33
34 private static final String ERROR_SAVE = "Failed to save preference";
35
36
37
38
39 private static final String DEFAULT = "DefaultDBC";
40
41
42
43
44 private static final String SKETCHDIR = "SketchDir";
45
46
47
48
49 private static final String LIBDIR = "LibraryDir";
50
51
52
53
54 private static final String CHARSET = "CharSet";
55
56
57
58
59 private ArduinoPreferences() {
60 }
61
62
63
64
65
66 static File getDefaultLocation() {
67 final Preferences myPreferences = deriveHandle();
68 final String myLocation = myPreferences.get(DEFAULT, null);
69 return myLocation == null ? null : new File(myLocation);
70 }
71
72
73
74
75
76
77 static ArduinoException storeDefaultLocation(final File pLocation) {
78
79 try {
80 final Preferences myPreferences = deriveHandle();
81 myPreferences.put(DEFAULT, pLocation.getAbsolutePath());
82 myPreferences.flush();
83 return null;
84
85 } catch (BackingStoreException e) {
86 return new ArduinoException(ERROR_SAVE, e);
87 }
88 }
89
90
91
92
93
94 static File getSketchLocation() {
95 final Preferences myPreferences = deriveHandle();
96 final String myLocation = myPreferences.get(SKETCHDIR, null);
97 return myLocation == null ? null : new File(myLocation);
98 }
99
100
101
102
103
104
105 static ArduinoException storeSketchLocation(final File pLocation) {
106
107 try {
108 final Preferences myPreferences = deriveHandle();
109 myPreferences.put(SKETCHDIR, pLocation.getAbsolutePath());
110 myPreferences.flush();
111 return null;
112
113 } catch (BackingStoreException e) {
114 return new ArduinoException(ERROR_SAVE, e);
115 }
116 }
117
118
119
120
121
122 static File getLibraryLocation() {
123 final Preferences myPreferences = deriveHandle();
124 final String myLocation = myPreferences.get(LIBDIR, null);
125 return myLocation == null ? null : new File(myLocation);
126 }
127
128
129
130
131
132
133 static ArduinoException storeLibraryLocation(final File pLocation) {
134
135 try {
136 final Preferences myPreferences = deriveHandle();
137 myPreferences.put(LIBDIR, pLocation.getAbsolutePath());
138 myPreferences.flush();
139 return null;
140
141 } catch (BackingStoreException e) {
142 return new ArduinoException(ERROR_SAVE, e);
143 }
144 }
145
146
147
148
149
150 static Charset getCharSet() {
151 final Preferences myPreferences = deriveHandle();
152 final String myName = myPreferences.get(CHARSET, StandardCharsets.ISO_8859_1.name());
153 return myName.equals(StandardCharsets.UTF_8.name())
154 ? StandardCharsets.UTF_8
155 : StandardCharsets.ISO_8859_1;
156 }
157
158
159
160
161
162
163 static ArduinoException storeCharSet(final Charset pCharSet) {
164
165 try {
166 final Preferences myPreferences = deriveHandle();
167 myPreferences.put(CHARSET, pCharSet.name());
168 myPreferences.flush();
169 return null;
170
171 } catch (BackingStoreException e) {
172 return new ArduinoException(ERROR_SAVE, e);
173 }
174 }
175
176
177
178
179
180 private static Preferences deriveHandle() {
181
182 final Class<?> myClass = ArduinoPreferences.class;
183 String myName = myClass.getCanonicalName();
184
185
186 final String myPackage = myClass.getPackage().getName();
187
188
189 myName = myName.substring(myPackage.length() + 1);
190
191
192 final Preferences myHandle = Preferences.userNodeForPackage(myClass);
193 return myHandle.node(myName);
194 }
195 }