@@ -11,57 +11,35 @@ class EmgSettings {
1111 values = new EmgSettingsValues ();
1212 }
1313
14- public boolean loadSettingsValues (String filename ) {
14+ public String getJson () {
15+ Gson gson = new GsonBuilder (). setPrettyPrinting(). create();
16+ return gson. toJson(values);
17+ }
18+
19+ public boolean loadSettingsFromJson (String json ) {
1520 try {
16- File file = new File (filename);
17- StringBuilder fileContents = new StringBuilder ((int )file. length());
18- Scanner scanner = new Scanner (file);
19- while (scanner. hasNextLine()) {
20- fileContents. append(scanner. nextLine() + System . lineSeparator());
21- }
2221 Gson gson = new Gson ();
23- EmgSettingsValues tempValues = gson. fromJson(fileContents. toString(), EmgSettingsValues . class);
22+ EmgSettingsValues tempValues = gson. fromJson(json, EmgSettingsValues . class);
23+
24+ // Validate channel count matches
2425 if (tempValues. window. length != channelCount) {
25- outputError(" Emg Settings: Loaded EMG Settings file has different number of channels than the current board." );
26+ outputError(" Emg Settings: Loaded EMG Settings JSON has different number of channels than the current board." );
2627 return false ;
2728 }
28- // Explicitely copy values over to avoid reference issues
29- // (e.g. values = tempValues "nukes" the old values object)
29+
30+ // Explicitly copy values to avoid reference issues
3031 values. window = tempValues. window;
3132 values. uvLimit = tempValues. uvLimit;
3233 values. creepIncreasing = tempValues. creepIncreasing;
3334 values. creepDecreasing = tempValues. creepDecreasing;
3435 values. minimumDeltaUV = tempValues. minimumDeltaUV;
3536 values. lowerThresholdMinimum = tempValues. lowerThresholdMinimum;
37+
38+ settingsWereLoaded = true ;
3639 return true ;
37- } catch (IOException e) {
40+ } catch (Exception e) {
3841 e. printStackTrace();
39- File f = new File (filename);
40- if (f. exists()) {
41- if (f. delete()) {
42- outputError(" Emg Settings: Could not load EMG settings from disk. Deleting this file..." );
43- } else {
44- outputError(" Emg Settings: Error deleting old/broken EMG settings file! Please make sure the GUI has proper read/write permissions." );
45- }
46- }
47- return false ;
48- }
49- }
50-
51- public String getJson () {
52- Gson gson = new GsonBuilder (). setPrettyPrinting(). create();
53- return gson. toJson(values);
54- }
55-
56- public boolean saveToFile (String filename ) {
57- String json = getJson();
58- try {
59- FileWriter writer = new FileWriter (filename);
60- writer. write(json);
61- writer. close();
62- return true ;
63- } catch (IOException e) {
64- e. printStackTrace();
42+ outputError(" EmgSettings: Could not load EMG settings from JSON string." );
6543 return false ;
6644 }
6745 }
@@ -76,69 +54,11 @@ class EmgSettings {
7654 return channelCount;
7755 }
7856
79- // Avoid error with popup being in another thread.
80- public void storeSettings () {
81- StringBuilder settingsFilename = new StringBuilder (directoryManager. getSettingsPath());
82- settingsFilename. append(" EmgSettings" );
83- settingsFilename. append(" _" );
84- settingsFilename. append(getChannelCount());
85- settingsFilename. append(" Channels.json" );
86- String filename = settingsFilename. toString();
87- File fileToSave = new File (filename);
88- FileChooser chooser = new FileChooser (
89- FileChooserMode . SAVE ,
90- " storeEmgSettings" ,
91- fileToSave,
92- " Save EMG settings to file" );
93- }
94-
95- // Avoid error with popup being in another thread.
96- public void loadSettings () {
97- StringBuilder settingsFilename = new StringBuilder (directoryManager. getSettingsPath());
98- settingsFilename. append(" EmgSettings" );
99- settingsFilename. append(" _" );
100- settingsFilename. append(getChannelCount());
101- settingsFilename. append(" Channels.json" );
102- String filename = settingsFilename. toString();
103- File fileToLoad = new File (filename);
104- FileChooser chooser = new FileChooser (
105- FileChooserMode . LOAD ,
106- " loadEmgSettings" ,
107- fileToLoad,
108- " Select EMG settings file to load" );
109-
110- }
111-
11257 public boolean getSettingsWereLoaded () {
11358 return settingsWereLoaded;
11459 }
11560
11661 public void setSettingsWereLoaded (boolean settingsWereLoaded ) {
11762 this . settingsWereLoaded = settingsWereLoaded;
11863 }
119- }
120-
121- // Used by button in the EMG UI. Must be global and public. Called in above loadSettings method.
122- public void loadEmgSettings (File selection ) {
123- if (selection == null ) {
124- output(" EMG Settings file not selected." );
125- } else {
126- if (dataProcessing. emgSettings. loadSettingsValues(selection. getAbsolutePath())) {
127- outputSuccess(" EMG Settings Loaded!" );
128- dataProcessing. emgSettings. setSettingsWereLoaded(true );
129- }
130- }
131- }
132-
133- // Used by button in the EMG UI. Must be global and public. Called in above storeSettings method.
134- public void storeEmgSettings (File selection ) {
135- if (selection == null ) {
136- output(" EMG Settings file not selected." );
137- } else {
138- if (dataProcessing. emgSettings. saveToFile(selection. getAbsolutePath())) {
139- outputSuccess(" EMG Settings Saved!" );
140- } else {
141- outputError(" Failed to save EMG Settings." );
142- }
143- }
14464}
0 commit comments