001package jmri.configurexml;
002
003import java.util.prefs.Preferences;
004
005import jmri.InstanceManagerAutoDefault;
006import jmri.beans.PreferencesBean;
007import jmri.profile.ProfileManager;
008import jmri.profile.ProfileUtils;
009
010/**
011 * Preferences for Shutdown used by StoreAndCompare
012 *
013 * @author Dave Sand Copyright 2022
014 */
015public final class ShutdownPreferences extends PreferencesBean implements InstanceManagerAutoDefault {
016
017    public static final String ENABLE_STORE_CHECK = "enableStoreCheck";
018    public static final String IGNORE_TIMEBASE = "ignoreTimebase";
019    public static final String IGNORE_SENSORCOLORS = "ignoreSensorColors";
020    public static final String DISPLAY_DIALOG = "displayDialog";
021
022    private boolean _enableStoreCheck = false;
023    private boolean _ignoreTimebase = false;
024    private boolean _ignoreSensorColors = false;
025    private DialogDisplayOptions _displayDialog = DialogDisplayOptions.ShowDialog;
026
027
028    public ShutdownPreferences() {
029        super(ProfileManager.getDefault().getActiveProfile());
030        Preferences sharedPreferences = ProfileUtils.getPreferences(
031                super.getProfile(), this.getClass(), true);
032        this.readPreferences(sharedPreferences);
033    }
034
035    private void readPreferences(Preferences sharedPreferences) {
036        _enableStoreCheck = sharedPreferences.getBoolean(ENABLE_STORE_CHECK, true);
037        _ignoreTimebase = sharedPreferences.getBoolean(IGNORE_TIMEBASE, false);
038        _ignoreSensorColors = sharedPreferences.getBoolean(IGNORE_SENSORCOLORS, false);
039        var display = sharedPreferences.get(DISPLAY_DIALOG, "ShowDialog");
040        _displayDialog = DialogDisplayOptions.valueOf(display);
041        setIsDirty(false);
042    }
043
044    public boolean compareValuesDifferent(ShutdownPreferences prefs) {
045        if (isStoreCheckEnabled() != prefs.isStoreCheckEnabled()) {
046            return true;
047        }
048        if (isIgnoreTimebaseEnabled() != prefs.isIgnoreTimebaseEnabled()) {
049            return true;
050        }
051        if (isIgnoreSensorColorsEnabled() != prefs.isIgnoreSensorColorsEnabled()) {
052            return true;
053        }
054        if (!getDisplayDialog().equals(prefs.getDisplayDialog())) {
055            return true;
056        }
057        return false;
058    }
059
060    public void apply(ShutdownPreferences prefs) {
061        setEnableStoreCheck(prefs.isStoreCheckEnabled());
062        setIgnoreTimebase(prefs.isIgnoreTimebaseEnabled());
063        setIgnoreSensorColors(prefs.isIgnoreSensorColorsEnabled());
064        setDisplayDialog(prefs.getDisplayDialog());
065    }
066
067    public void save() {
068        Preferences sharedPreferences = ProfileUtils.getPreferences(this.getProfile(), this.getClass(), true);
069        sharedPreferences.putBoolean(ENABLE_STORE_CHECK, this.isStoreCheckEnabled());
070        sharedPreferences.putBoolean(IGNORE_TIMEBASE, this.isIgnoreTimebaseEnabled());
071        sharedPreferences.putBoolean(IGNORE_SENSORCOLORS, this.isIgnoreSensorColorsEnabled());
072        sharedPreferences.put(DISPLAY_DIALOG, this.getDisplayDialog().name());
073        setIsDirty(false);
074    }
075
076    public void setEnableStoreCheck(boolean value) {
077        _enableStoreCheck = value;
078        setIsDirty(true);
079    }
080
081    public boolean isStoreCheckEnabled() {
082        return _enableStoreCheck;
083    }
084
085    public void setIgnoreTimebase(boolean value) {
086        _ignoreTimebase = value;
087        setIsDirty(true);
088    }
089
090    public boolean isIgnoreTimebaseEnabled() {
091        return _ignoreTimebase;
092    }
093
094    public void setIgnoreSensorColors(boolean value) {
095        _ignoreSensorColors = value;
096        setIsDirty(true);
097    }
098
099    public boolean isIgnoreSensorColorsEnabled() {
100        return _ignoreSensorColors;
101    }
102
103    public void setDisplayDialog(DialogDisplayOptions value) {
104        _displayDialog = value;
105        setIsDirty(true);
106    }
107
108    public DialogDisplayOptions getDisplayDialog() {
109        return _displayDialog;
110    }
111
112    public enum DialogDisplayOptions {
113
114        ShowDialog(Bundle.getMessage("OptionDisplay")),
115        SkipDialog(Bundle.getMessage("OptionSkip"));
116
117        private final String _description;
118
119        private DialogDisplayOptions(String description) {
120            _description = description;
121        }
122
123        @Override
124        public String toString() {
125            return _description;
126        }
127    }
128
129
130//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(StoreAndComparePreferences.class);
131}