Module org.autogui

Class GuiSwingPreferences

java.lang.Object
org.autogui.swing.GuiSwingPreferences

public class GuiSwingPreferences extends Object
a preferences manager.

GUI properties as preferences

  1. define a custom GuiSwingPreferences.PreferencesByJsonEntry (or GuiSwingPreferences.Preferences).
           class PreferencesForX implements PreferencesByJsonEntry {
               protected int prop; //a GUI property
    
               public void applyTo(P guiComp) { //the method for setting the property of the component
                   guiComp.setProp(prop);
               }
    
               public void set(P guiComp) { //from the component to the prefs
                   prop = guiComp.getProp();
               }
    
               public String getKey() { return "$x"; } //the entry key
               public Object toJson() { Map m = new HashMap(); m.put("p", prop); return m; } //to a JSON entry
               public void setJson(Object j) {
                   if (j != null && j instanceof Map) { prop = (Integer) ((Map) j).get("p"); }
               }
           }
             
  2. a GUI component defines a listener for incremental updating of properties
           class PropUpdater implements XListener {
               GuiMappingContext context;
               boolean enabled = true;
               Consumer<GuiSwingPreferences.PreferencesUpdateEvent> updater;
               PreferencesForX prefs = new PreferencesForX();
    
               void changed(Event e) { //suppose an event handler
                  if (enabled) {
                   prefs.set(e.getComponent());
                   updater.accept(new PreferencesUpdateEvent(context, prefs);
                  }
               }
           }
            
    and the GUI component sets up the listener with implementing GuiSwingPreferences.PreferencesUpdateSupport
           class P implements ValuePane, PreferencesUpdateSupport {
                PropUpdater updater;
                P(GuiMappingContext c) {
                    ...
                     updater = new PropUpdater(c);
                     addXLister(updater);
                }
                public void setPreferencesUpdater(Consumer<PreferencesUpdateEvent> u) {
                     updater.updater = u;
                }
            }
             
  3. the GUI component overrides GuiSwingView.ValuePane.saveSwingPreferences(GuiPreferences) and GuiSwingView.ValuePane.loadSwingPreferences(GuiPreferences) for bulk loading/saving.
                public void loadSwingPreferences(GuiPreferences p) {
                    GuiSwingView.loadPreferencesDefault(this, p);
                    updater.prefs.loadFrom(p.getDescendant(context));
                    updater.enabled = false;
                    updater.prefs.applyTo(this);
                    updater.enabled = true;
                }
                public void saveSwingPreferences(GuiPreferences p) {
                    GuiSwingView.savePreferencesDefault(this, p);
                    updater.prefs.saveTo(p.getDescendant(context));
                }