Skip to content

Commit 074fff7

Browse files
committed
Add functional save/load widget settings with simpler method calls - Test driven with W_Template
1 parent 073205d commit 074fff7

File tree

5 files changed

+55
-38
lines changed

5 files changed

+55
-38
lines changed

OpenBCI_GUI/W_Template.pde

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,42 +40,32 @@ class W_Template extends WidgetWithSettings {
4040
localCP5.setAutoDraw(false);
4141

4242
createWidgetTemplateButton();
43-
4443
}
4544

4645
@Override
4746
protected void initWidgetSettings() {
4847
super.initWidgetSettings();
4948
// Widget Settings are used to store the state of the widget.
5049
// This is where you can set the default values for your dropdowns and other settings.
51-
widgetSettings.set(TemplateDropdown1.class, TemplateDropdown1.ITEM_A);
52-
widgetSettings.set(TemplateDropdown2.class, TemplateDropdown2.ITEM_C);
53-
widgetSettings.set(TemplateDropdown3.class, TemplateDropdown3.ITEM_F);
54-
widgetSettings.saveDefaults();
55-
56-
// Get the list of Strings for the dropdowns. These are the options that will be displayed in the dropdowns.
57-
List<String> dropdown1List = EnumHelper.getEnumStrings(TemplateDropdown1.class);
58-
List<String> dropdown2List = EnumHelper.getEnumStrings(TemplateDropdown2.class);
59-
List<String> dropdown3List = EnumHelper.getEnumStrings(TemplateDropdown3.class);
60-
61-
// Get the current settings for the dropdowns. This is where you can retrieve the values that were just set.
62-
int dropdown1Index = widgetSettings.get(TemplateDropdown1.class).getIndex();
63-
int dropdown2Index = widgetSettings.get(TemplateDropdown2.class).getIndex();
64-
int dropdown3Index = widgetSettings.get(TemplateDropdown3.class).getIndex();
50+
widgetSettings.set(TemplateDropdown1.class, TemplateDropdown1.ITEM_A)
51+
.set(TemplateDropdown2.class, TemplateDropdown2.ITEM_C)
52+
.set(TemplateDropdown3.class, TemplateDropdown3.ITEM_F)
53+
.saveDefaults();
6554

6655
// This is the protocol for setting up dropdowns.
6756
// Note that these 3 dropdowns correspond to the 3 global functions below.
6857
// You just need to make sure the "id" (the 1st String) has the same name as the corresponding function.
69-
addDropdown("widgetTemplateDropdown1", "Drop 1", dropdown1List, dropdown1Index);
70-
addDropdown("widgetTemplateDropdown2", "Drop 2", dropdown2List, dropdown2Index);
71-
addDropdown("widgetTemplateDropdown3", "Drop 3", dropdown3List, dropdown3Index);
58+
// Arguments: Class, String id, String label
59+
initDropdown(TemplateDropdown1.class, "widgetTemplateDropdown1", "Drop 1");
60+
initDropdown(TemplateDropdown2.class, "widgetTemplateDropdown2", "Drop 2");
61+
initDropdown(TemplateDropdown3.class, "widgetTemplateDropdown3", "Drop 3");
7262
}
7363

7464
@Override
7565
protected void applySettings() {
76-
//FIX ME
77-
println("!!!!!!!!!!!!!!!!!!!!!!!!!Applying settings for " + widgetTitle);
78-
//widgetSettings.applySettings();
66+
updateDropdownLabel(TemplateDropdown1.class, "widgetTemplateDropdown1");
67+
updateDropdownLabel(TemplateDropdown2.class, "widgetTemplateDropdown2");
68+
updateDropdownLabel(TemplateDropdown3.class, "widgetTemplateDropdown3");
7969
}
8070

8171
public void update(){

OpenBCI_GUI/Widget.pde

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ abstract class WidgetWithSettings extends Widget {
319319
}
320320

321321
public void setWidgetSettings(WidgetSettings _widgetSettings) {
322-
//FIX ME - DO I NEED THIS?
323322
widgetSettings = _widgetSettings;
323+
applySettings();
324324
}
325325

326326
public WidgetSettings getWidgetSettings() {
@@ -330,6 +330,17 @@ abstract class WidgetWithSettings extends Widget {
330330
protected void initWidgetSettings() {
331331
widgetSettings = new WidgetSettings(getWidgetTitle());
332332
}
333+
334+
protected <T extends Enum<T> & IndexingInterface> void initDropdown(Class<T> enumClass, String id, String label) {
335+
List<String> options = EnumHelper.getEnumStrings(enumClass);
336+
int currentIndex = widgetSettings.get(enumClass).getIndex();
337+
addDropdown(id, label, options, currentIndex);
338+
}
339+
340+
protected <T extends Enum<T> & IndexingInterface> void updateDropdownLabel(Class<T> enumClass, String controllerId) {
341+
String value = widgetSettings.get(enumClass).getString();
342+
cp5_widget.getController(controllerId).getCaptionLabel().setText(value);
343+
}
333344

334345
protected abstract void applySettings();
335346
}

OpenBCI_GUI/WidgetManager.pde

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,21 @@ class WidgetManager {
345345
continue;
346346
}
347347

348+
WidgetWithSettings widgetWithSettings = (WidgetWithSettings) widget;
348349
String widgetTitle = widget.getWidgetTitle();
349-
if (json.hasKey(widgetTitle)) {
350-
String settingsJson = json.getString(widgetTitle, "");
351-
((WidgetWithSettings) widget).getWidgetSettings().fromJSON(settingsJson);
352-
} else {
350+
if (!json.hasKey(widgetTitle)) {
353351
println("WidgetManager:loadWidgetSettingsFromJson: No settings found for " + widgetTitle);
352+
continue;
353+
}
354+
355+
String settingsJson = json.getString(widgetTitle, "");
356+
WidgetSettings widgetSettings = widgetWithSettings.getWidgetSettings();
357+
boolean success = widgetSettings.loadFromJSON(settingsJson);
358+
if (!success) {
359+
println("WidgetManager:loadWidgetSettingsFromJson: Failed to load settings for " + widgetTitle);
360+
continue;
354361
}
362+
widgetWithSettings.applySettings();
355363
}
356364
}
357365
};

OpenBCI_GUI/WidgetSettings.pde

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ class WidgetSettings {
1717

1818
/**
1919
* Store a setting using enum class as key
20+
* @return this WidgetSettings instance for method chaining
2021
*/
21-
public <T extends Enum<?>> void set(Class<T> enumClass, T value) {
22+
public <T extends Enum<?>> WidgetSettings set(Class<T> enumClass, T value) {
2223
settings.put(enumClass.getName(), value);
24+
return this;
2325
}
2426

2527
/**
@@ -28,9 +30,9 @@ class WidgetSettings {
2830
*
2931
* @param enumClass The enum class to look up values
3032
* @param index The index of the enum constant to set
31-
* @return true if successful, false if the index is out of bounds
33+
* @return this WidgetSettings instance for method chaining
3234
*/
33-
public <T extends Enum<?>> boolean setByIndex(Class<T> enumClass, int index) {
35+
public <T extends Enum<?>> WidgetSettings setByIndex(Class<T> enumClass, int index) {
3436
T[] enumConstants = enumClass.getEnumConstants();
3537

3638
// Check if index is valid
@@ -39,12 +41,12 @@ class WidgetSettings {
3941
T value = enumConstants[index];
4042
// Set it using the regular set method
4143
set(enumClass, value);
42-
return true;
44+
} else {
45+
// Index was out of bounds
46+
println("Warning: Invalid index " + index + " for enum " + enumClass.getName());
4347
}
4448

45-
// Index was out of bounds
46-
println("Warning: Invalid index " + index + " for enum " + enumClass.getName());
47-
return false;
49+
return this;
4850
}
4951

5052
/**
@@ -77,16 +79,20 @@ class WidgetSettings {
7779

7880
/**
7981
* Save current settings as defaults
82+
* @return this WidgetSettings instance for method chaining
8083
*/
81-
public void saveDefaults() {
84+
public WidgetSettings saveDefaults() {
8285
defaults = new HashMap<String, Enum<?>>(settings);
86+
return this;
8387
}
8488

8589
/**
8690
* Restore to default settings
91+
* @return this WidgetSettings instance for method chaining
8792
*/
88-
public void restoreDefaults() {
93+
public WidgetSettings restoreDefaults() {
8994
settings = new HashMap<String, Enum<?>>(defaults);
95+
return this;
9096
}
9197

9298
/**
@@ -112,9 +118,11 @@ class WidgetSettings {
112118
}
113119

114120
/**
115-
* Load settings from JSON string
116-
*/
117-
public boolean fromJSON(String jsonString) {
121+
* Attempts to load settings from a JSON string
122+
* @param jsonString The JSON string containing settings
123+
* @return true if settings were loaded successfully, false otherwise
124+
*/
125+
public boolean loadFromJSON(String jsonString) {
118126
try {
119127
JSONObject json = parseJSONObject(jsonString);
120128
if (json == null) return false;

OpenBCI_GUI/WidgetWithSettings.pde

Whitespace-only changes.

0 commit comments

Comments
 (0)