Module org.autogui

Class GuiSwingPreferences

java.lang.Object
org.autogui.swing.GuiSwingPreferences
All Implemented Interfaces:
GuiPreferences.PreferencesStoreChangeListener

public class GuiSwingPreferences extends Object implements GuiPreferences.PreferencesStoreChangeListener
a preferences manager.

GUI properties as preferences

  1. define a custom GuiSwingPrefsSupports.PreferencesByJsonEntry (or GuiSwingPrefsSupports.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<GuiSwingPrefsSupports.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 GuiSwingPrefsSupports.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. define a new visitor method in GuiSwingPrefsApplyOptions for applying prefs;
                   default public void apply(PropUpdater updater, GuiPreferences prefs, P pane)  {
                       updater.prefs.applyTo(pane);
                   }
               
    this method also need to be overriden in GuiSwingPrefsEditor for prefs settings pane. GuiSwingPrefsEditor.addToContentPaneIfFirst(GuiPreferences, Supplier) can be usable.
                  public void apply(PropUpdater updater, GuiPreferences prefs, P pane)  {
                       addToContentPaneIfFirst(prefs, PreferencesForX::new));
                  }
               
  4. the GUI component overrides GuiSwingView.ValuePane.saveSwingPreferences(GuiPreferences) and GuiSwingView.ValuePane.loadSwingPreferences(GuiPreferences, GuiSwingPrefsApplyOptions) for bulk loading/saving.
                public void loadSwingPreferences(GuiPreferences p, GuiSwingPrefsApplyOptions options) {
                    GuiSwingView.loadPreferencesDefault(this, p, options);
                    updater.prefs.loadFrom(p.getDescendant(context));
                    updater.enabled = false;
                    options.apply(updater, updaterprefs, this);
                    updater.enabled = true;
                }
                public void saveSwingPreferences(GuiPreferences p) {
                    GuiSwingView.savePreferencesDefault(this, p);
                    updater.prefs.saveTo(p.getDescendant(context));
                }