001package jmri.jmrit.logixng.implementation;
002
003import java.util.prefs.Preferences;
004
005import jmri.beans.PreferencesBean;
006import jmri.jmrit.logixng.LogixNGPreferences;
007import jmri.jmrit.logixng.MaleSocket.ErrorHandlingType;
008import jmri.profile.ProfileManager;
009import jmri.profile.ProfileUtils;
010
011/**
012 * Preferences for LogixNG
013 *
014 * @author Daniel Bergqvist Copyright 2018
015 */
016public final class DefaultLogixNGPreferences extends PreferencesBean implements LogixNGPreferences {
017
018    private static final String START_LOGIXNG_ON_LOAD = "startLogixNGOnStartup";
019    private static final String INSTALL_DEBUGGER = "installDebugger";
020    private static final String SHOW_SYSTEM_USER_NAMES = "showSystemUserNames";
021    private static final String ERROR_HANDLING_TYPE = "errorHandlingType";
022    private static final String TREE_EDITOR_HIGHLIGHT_ROW = "treeEditorHighlightRow";
023    private static final String SHOW_SYSTEM_NAME_IN_EXCEPTION = "showSystemNameInExceptions";
024
025    private boolean _startLogixNGOnLoad = true;
026    private boolean _showSystemUserNames = false;
027    private boolean _installDebugger = true;
028    private ErrorHandlingType _errorHandlingType = ErrorHandlingType.ShowDialogBox;
029    private boolean _treeEditorHighlightRow = false;
030    private boolean _showSystemNameInException = false;
031
032
033    public DefaultLogixNGPreferences() {
034        super(ProfileManager.getDefault().getActiveProfile());
035//        System.out.format("LogixNG preferences%n");
036        Preferences sharedPreferences = ProfileUtils.getPreferences(
037                super.getProfile(), this.getClass(), true);
038        this.readPreferences(sharedPreferences);
039    }
040
041    private void readPreferences(Preferences sharedPreferences) {
042        _startLogixNGOnLoad = sharedPreferences.getBoolean(START_LOGIXNG_ON_LOAD, _startLogixNGOnLoad);
043        _installDebugger = sharedPreferences.getBoolean(INSTALL_DEBUGGER, _installDebugger);
044        _showSystemUserNames = sharedPreferences.getBoolean(SHOW_SYSTEM_USER_NAMES, _showSystemUserNames);
045        _errorHandlingType = ErrorHandlingType.valueOf(
046                sharedPreferences.get(ERROR_HANDLING_TYPE, _errorHandlingType.name()));
047        _treeEditorHighlightRow = sharedPreferences.getBoolean(TREE_EDITOR_HIGHLIGHT_ROW, _treeEditorHighlightRow);
048        _showSystemNameInException = sharedPreferences.getBoolean(SHOW_SYSTEM_NAME_IN_EXCEPTION, _showSystemNameInException);
049
050        setIsDirty(false);
051    }
052
053    @Override
054    public boolean compareValuesDifferent(LogixNGPreferences prefs) {
055        if (getStartLogixNGOnStartup() != prefs.getStartLogixNGOnStartup()) {
056            return true;
057        }
058        if (getInstallDebugger() != prefs.getInstallDebugger()) {
059            return true;
060        }
061        if (getShowSystemUserNames() != prefs.getShowSystemUserNames()) {
062            return true;
063        }
064        if (getTreeEditorHighlightRow() != prefs.getTreeEditorHighlightRow()) {
065            return true;
066        }
067        if (getShowSystemNameInException() != prefs.getShowSystemNameInException()) {
068            return true;
069        }
070        return (getErrorHandlingType() != prefs.getErrorHandlingType());
071    }
072
073    @Override
074    public void apply(LogixNGPreferences prefs) {
075        setStartLogixNGOnStartup(prefs.getStartLogixNGOnStartup());
076        setInstallDebugger(prefs.getInstallDebugger());
077        setShowSystemUserNames(prefs.getShowSystemUserNames());
078        this.setErrorHandlingType(prefs.getErrorHandlingType());
079        setTreeEditorHighlightRow(prefs.getTreeEditorHighlightRow());
080        setShowSystemNameInException(prefs.getShowSystemNameInException());
081    }
082
083    @Override
084    public void save() {
085        Preferences sharedPreferences = ProfileUtils.getPreferences(this.getProfile(), this.getClass(), true);
086        sharedPreferences.putBoolean(START_LOGIXNG_ON_LOAD, this.getStartLogixNGOnStartup());
087        sharedPreferences.putBoolean(INSTALL_DEBUGGER, this.getInstallDebugger());
088        sharedPreferences.putBoolean(SHOW_SYSTEM_USER_NAMES, this.getShowSystemUserNames());
089        sharedPreferences.put(ERROR_HANDLING_TYPE, this.getErrorHandlingType().name());
090        sharedPreferences.putBoolean(TREE_EDITOR_HIGHLIGHT_ROW, this.getTreeEditorHighlightRow());
091        sharedPreferences.putBoolean(SHOW_SYSTEM_NAME_IN_EXCEPTION, this.getShowSystemNameInException());
092        setIsDirty(false);
093    }
094
095    @Override
096    public void setStartLogixNGOnStartup(boolean value) {
097        _startLogixNGOnLoad = value;
098        setIsDirty(true);
099    }
100
101    @Override
102    public boolean getStartLogixNGOnStartup() {
103        return _startLogixNGOnLoad;
104    }
105
106    @Override
107    public void setShowSystemUserNames(boolean value) {
108        _showSystemUserNames = value;
109        setIsDirty(true);
110    }
111
112    @Override
113    public boolean getShowSystemUserNames() {
114        return _showSystemUserNames;
115    }
116
117    @Override
118    public void setInstallDebugger(boolean value) {
119        _installDebugger = value;
120        setIsDirty(true);
121    }
122
123    @Override
124    public boolean getInstallDebugger() {
125        return _installDebugger;
126    }
127
128    @Override
129    public void setErrorHandlingType(ErrorHandlingType type) {
130        _errorHandlingType = type;
131        setIsDirty(true);
132    }
133
134    @Override
135    public ErrorHandlingType getErrorHandlingType() {
136        return _errorHandlingType;
137    }
138
139    @Override
140    public void setTreeEditorHighlightRow(boolean value) {
141        _treeEditorHighlightRow = value;
142        setIsDirty(true);
143    }
144
145    @Override
146    public boolean getTreeEditorHighlightRow() {
147        return _treeEditorHighlightRow;
148    }
149
150    @Override
151    public void setShowSystemNameInException(boolean value) {
152        _showSystemNameInException = value;
153        setIsDirty(true);
154    }
155
156    @Override
157    public boolean getShowSystemNameInException() {
158        return _showSystemNameInException;
159    }
160
161//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogixNGPreferences.class);
162}