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.jmrit.logixng.actions.IfThenElse;
009import jmri.profile.ProfileManager;
010import jmri.profile.ProfileUtils;
011
012/**
013 * Preferences for LogixNG
014 *
015 * @author Daniel Bergqvist Copyright 2018
016 */
017public final class DefaultLogixNGPreferences extends PreferencesBean implements LogixNGPreferences {
018
019    private static final String START_LOGIXNG_ON_LOAD = "startLogixNGOnStartup";
020    private static final String INSTALL_DEBUGGER = "installDebugger";
021    private static final String SHOW_SYSTEM_USER_NAMES = "showSystemUserNames";
022    private static final String ERROR_HANDLING_TYPE = "errorHandlingType";
023    private static final String TREE_EDITOR_HIGHLIGHT_ROW = "treeEditorHighlightRow";
024    private static final String SHOW_SYSTEM_NAME_IN_EXCEPTION = "showSystemNameInExceptions";
025    private static final String STRICT_TYPING_GLOBAL_VARIABLES = "strictTypingGlobalVariables";
026    private static final String STRICT_TYPING_LOCAL_VARIABLES = "strictTypingLocalVariables";
027    private static final String IF_THEN_ELSE_EXECUTE_TYPE_DEFAULT = "ifThenElseExecuteTypeDefault";
028    private static final String LOG_ALL_BEFORE = "logAllBefore";
029    private static final String LOG_ALL_AFTER = "logAllAfter";
030
031    private boolean _startLogixNGOnLoad = true;
032    private boolean _showSystemUserNames = false;
033    private boolean _installDebugger = true;
034    private ErrorHandlingType _errorHandlingType = ErrorHandlingType.ShowDialogBox;
035    private boolean _treeEditorHighlightRow = false;
036    private boolean _showSystemNameInException = false;
037    private boolean _strictTypingGlobalVariables = false;
038    private boolean _strictTypingLocalVariables = false;
039    private IfThenElse.ExecuteType _ifThenElseExecuteTypeDefault = IfThenElse.ExecuteType.ExecuteOnChange;
040    private boolean _logAllBefore = false;
041    private boolean _logAllAfter = false;
042
043
044    public DefaultLogixNGPreferences() {
045        super(ProfileManager.getDefault().getActiveProfile());
046//        System.out.format("LogixNG preferences%n");
047        Preferences sharedPreferences = ProfileUtils.getPreferences(
048                super.getProfile(), this.getClass(), true);
049        this.readPreferences(sharedPreferences);
050    }
051
052    private void readPreferences(Preferences sharedPreferences) {
053        _startLogixNGOnLoad = sharedPreferences.getBoolean(START_LOGIXNG_ON_LOAD, _startLogixNGOnLoad);
054        _installDebugger = sharedPreferences.getBoolean(INSTALL_DEBUGGER, _installDebugger);
055        _showSystemUserNames = sharedPreferences.getBoolean(SHOW_SYSTEM_USER_NAMES, _showSystemUserNames);
056        _errorHandlingType = ErrorHandlingType.valueOf(
057                sharedPreferences.get(ERROR_HANDLING_TYPE, _errorHandlingType.name()));
058        _treeEditorHighlightRow = sharedPreferences.getBoolean(TREE_EDITOR_HIGHLIGHT_ROW, _treeEditorHighlightRow);
059        _showSystemNameInException = sharedPreferences.getBoolean(SHOW_SYSTEM_NAME_IN_EXCEPTION, _showSystemNameInException);
060        _strictTypingGlobalVariables = sharedPreferences.getBoolean(STRICT_TYPING_GLOBAL_VARIABLES, _strictTypingGlobalVariables);
061        _strictTypingLocalVariables = sharedPreferences.getBoolean(STRICT_TYPING_LOCAL_VARIABLES, _strictTypingLocalVariables);
062        _ifThenElseExecuteTypeDefault = IfThenElse.ExecuteType.valueOf(
063                sharedPreferences.get(IF_THEN_ELSE_EXECUTE_TYPE_DEFAULT, _ifThenElseExecuteTypeDefault.name()));
064        _logAllBefore = sharedPreferences.getBoolean(LOG_ALL_BEFORE, _logAllBefore);
065        _logAllAfter = sharedPreferences.getBoolean(LOG_ALL_AFTER, _logAllAfter);
066
067        setIsDirty(false);
068    }
069
070    @Override
071    public boolean compareValuesDifferent(LogixNGPreferences prefs) {
072        if (getStartLogixNGOnStartup() != prefs.getStartLogixNGOnStartup()) {
073            return true;
074        }
075        if (getInstallDebugger() != prefs.getInstallDebugger()) {
076            return true;
077        }
078        if (getShowSystemUserNames() != prefs.getShowSystemUserNames()) {
079            return true;
080        }
081        if (getTreeEditorHighlightRow() != prefs.getTreeEditorHighlightRow()) {
082            return true;
083        }
084        if (getShowSystemNameInException() != prefs.getShowSystemNameInException()) {
085            return true;
086        }
087        if (getStrictTypingGlobalVariables() != prefs.getStrictTypingGlobalVariables()) {
088            return true;
089        }
090        if (getStrictTypingLocalVariables() != prefs.getStrictTypingLocalVariables()) {
091            return true;
092        }
093        if (getIfThenElseExecuteTypeDefault() != prefs.getIfThenElseExecuteTypeDefault()) {
094            return true;
095        }
096        if (getLogAllBefore() != prefs.getLogAllBefore()) {
097            return true;
098        }
099        if (getLogAllAfter() != prefs.getLogAllAfter()) {
100            return true;
101        }
102        return (getErrorHandlingType() != prefs.getErrorHandlingType());
103    }
104
105    @Override
106    public void apply(LogixNGPreferences prefs) {
107        setStartLogixNGOnStartup(prefs.getStartLogixNGOnStartup());
108        setInstallDebugger(prefs.getInstallDebugger());
109        setShowSystemUserNames(prefs.getShowSystemUserNames());
110        this.setErrorHandlingType(prefs.getErrorHandlingType());
111        setTreeEditorHighlightRow(prefs.getTreeEditorHighlightRow());
112        setShowSystemNameInException(prefs.getShowSystemNameInException());
113        setStrictTypingGlobalVariables(prefs.getStrictTypingGlobalVariables());
114        setStrictTypingLocalVariables(prefs.getStrictTypingLocalVariables());
115        setIfThenElseExecuteTypeDefault(prefs.getIfThenElseExecuteTypeDefault());
116        setLogAllBefore(prefs.getLogAllBefore());
117        setLogAllAfter(prefs.getLogAllAfter());
118    }
119
120    @Override
121    public void save() {
122        Preferences sharedPreferences = ProfileUtils.getPreferences(this.getProfile(), this.getClass(), true);
123        sharedPreferences.putBoolean(START_LOGIXNG_ON_LOAD, this.getStartLogixNGOnStartup());
124        sharedPreferences.putBoolean(INSTALL_DEBUGGER, this.getInstallDebugger());
125        sharedPreferences.putBoolean(SHOW_SYSTEM_USER_NAMES, this.getShowSystemUserNames());
126        sharedPreferences.put(ERROR_HANDLING_TYPE, this.getErrorHandlingType().name());
127        sharedPreferences.putBoolean(TREE_EDITOR_HIGHLIGHT_ROW, this.getTreeEditorHighlightRow());
128        sharedPreferences.putBoolean(SHOW_SYSTEM_NAME_IN_EXCEPTION, this.getShowSystemNameInException());
129        sharedPreferences.putBoolean(STRICT_TYPING_GLOBAL_VARIABLES, this.getStrictTypingGlobalVariables());
130        sharedPreferences.putBoolean(STRICT_TYPING_LOCAL_VARIABLES, this.getStrictTypingLocalVariables());
131        sharedPreferences.put(IF_THEN_ELSE_EXECUTE_TYPE_DEFAULT, this.getIfThenElseExecuteTypeDefault().name());
132        sharedPreferences.putBoolean(LOG_ALL_BEFORE, this.getLogAllBefore());
133        sharedPreferences.putBoolean(LOG_ALL_AFTER, this.getLogAllAfter());
134        setIsDirty(false);
135    }
136
137    @Override
138    public void setStartLogixNGOnStartup(boolean value) {
139        _startLogixNGOnLoad = value;
140        setIsDirty(true);
141    }
142
143    @Override
144    public boolean getStartLogixNGOnStartup() {
145        return _startLogixNGOnLoad;
146    }
147
148    @Override
149    public void setShowSystemUserNames(boolean value) {
150        _showSystemUserNames = value;
151        setIsDirty(true);
152    }
153
154    @Override
155    public boolean getShowSystemUserNames() {
156        return _showSystemUserNames;
157    }
158
159    @Override
160    public void setInstallDebugger(boolean value) {
161        _installDebugger = value;
162        setIsDirty(true);
163    }
164
165    @Override
166    public boolean getInstallDebugger() {
167        return _installDebugger;
168    }
169
170    @Override
171    public void setErrorHandlingType(ErrorHandlingType type) {
172        _errorHandlingType = type;
173        setIsDirty(true);
174    }
175
176    @Override
177    public ErrorHandlingType getErrorHandlingType() {
178        return _errorHandlingType;
179    }
180
181    @Override
182    public void setTreeEditorHighlightRow(boolean value) {
183        _treeEditorHighlightRow = value;
184        setIsDirty(true);
185    }
186
187    @Override
188    public boolean getTreeEditorHighlightRow() {
189        return _treeEditorHighlightRow;
190    }
191
192    @Override
193    public void setShowSystemNameInException(boolean value) {
194        _showSystemNameInException = value;
195        setIsDirty(true);
196    }
197
198    @Override
199    public boolean getShowSystemNameInException() {
200        return _showSystemNameInException;
201    }
202
203    @Override
204    public void setStrictTypingGlobalVariables(boolean value) {
205        _strictTypingGlobalVariables = value;
206        setIsDirty(true);
207    }
208
209    @Override
210    public boolean getStrictTypingGlobalVariables() {
211        return _strictTypingGlobalVariables;
212    }
213
214    @Override
215    public void setStrictTypingLocalVariables(boolean value) {
216        _strictTypingLocalVariables = value;
217        setIsDirty(true);
218    }
219
220    @Override
221    public boolean getStrictTypingLocalVariables() {
222        return _strictTypingLocalVariables;
223    }
224
225    @Override
226    public void setIfThenElseExecuteTypeDefault(IfThenElse.ExecuteType value) {
227        _ifThenElseExecuteTypeDefault = value;
228        setIsDirty(true);
229    }
230
231    @Override
232    public IfThenElse.ExecuteType getIfThenElseExecuteTypeDefault() {
233        return _ifThenElseExecuteTypeDefault;
234    }
235
236    @Override
237    public void setLogAllBefore(boolean value) {
238        _logAllBefore = value;
239        setIsDirty(true);
240    }
241
242    @Override
243    public boolean getLogAllBefore() {
244        return _logAllBefore;
245    }
246
247    @Override
248    public void setLogAllAfter(boolean value) {
249        _logAllAfter = value;
250        setIsDirty(true);
251    }
252
253    @Override
254    public boolean getLogAllAfter() {
255        return _logAllAfter;
256    }
257
258//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogixNGPreferences.class);
259}