001package jmri.jmrit.logixng.tools.swing;
002
003import jmri.jmrit.logixng.LogixNGPreferences;
004import jmri.jmrit.logixng.implementation.DefaultLogixNGPreferences;
005
006import java.awt.FlowLayout;
007import java.awt.GridLayout;
008
009import javax.swing.*;
010
011import org.openide.util.lookup.ServiceProvider;
012
013import jmri.InstanceManager;
014import jmri.jmrit.logixng.MaleSocket.ErrorHandlingType;
015import jmri.jmrit.logixng.actions.IfThenElse;
016// import jmri.swing.JTitledSeparator;
017import jmri.swing.PreferencesPanel;
018import jmri.util.swing.JComboBoxUtil;
019
020/**
021 * Preferences panel for LogixNG
022 *
023 * @author Daniel Bergqvist Copyright 2018
024 */
025@ServiceProvider(service = PreferencesPanel.class)
026public class LogixNGPreferencesPanel extends JPanel implements PreferencesPanel {
027
028    private final DefaultLogixNGPreferences preferences;
029
030    JCheckBox _startLogixNGOnLoadCheckBox;
031    JCheckBox _installDebuggerCheckBox;
032    JCheckBox _logAllBeforeCheckBox;
033    JCheckBox _logAllAfterCheckBox;
034    JCheckBox _showSystemUserNamesCheckBox;
035    JCheckBox _treeEditorHighlightRow;
036    JCheckBox _showSystemNameInException;
037    JCheckBox _strictTypingGlobalVariables;
038    JCheckBox _strictTypingLocalVariables;
039    private JComboBox<ErrorHandlingType> _errorHandlingComboBox;
040    private JComboBox<IfThenElse.ExecuteType> _ifThenElseExecuteTypeDefault;
041
042
043    public LogixNGPreferencesPanel() {
044        LogixNGPreferences prefs = InstanceManager.getDefault(LogixNGPreferences.class);
045        if (!(prefs instanceof DefaultLogixNGPreferences)) {
046            throw new RuntimeException("LogixNGPreferences is not of type DefaultLogixNGPreferences");
047        }
048        preferences = (DefaultLogixNGPreferences)prefs;
049        initGUI();
050    }
051
052    private void initGUI() {
053        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
054//        add(new JTitledSeparator(Bundle.getMessage("TitleStartupSettingsPanel")));
055        add(getStartupPanel());
056    }
057
058    /**
059     * set the local prefs to match the GUI Local prefs are independent from the
060     * singleton instance prefs.
061     *
062     * @return true if set, false if values are unacceptable.
063     */
064    private boolean setValues() {
065        boolean didSet = true;
066        preferences.setStartLogixNGOnStartup(_startLogixNGOnLoadCheckBox.isSelected());
067        preferences.setInstallDebugger(_installDebuggerCheckBox.isSelected());
068        preferences.setLogAllBefore(_logAllBeforeCheckBox.isSelected());
069        preferences.setLogAllAfter(_logAllAfterCheckBox.isSelected());
070        preferences.setShowSystemUserNames(_showSystemUserNamesCheckBox.isSelected());
071        preferences.setTreeEditorHighlightRow(_treeEditorHighlightRow.isSelected());
072        preferences.setErrorHandlingType(_errorHandlingComboBox.getItemAt(
073                _errorHandlingComboBox.getSelectedIndex()));
074        preferences.setShowSystemNameInException(_showSystemNameInException.isSelected());
075        preferences.setStrictTypingGlobalVariables(_strictTypingGlobalVariables.isSelected());
076        preferences.setStrictTypingLocalVariables(_strictTypingLocalVariables.isSelected());
077        preferences.setIfThenElseExecuteTypeDefault(_ifThenElseExecuteTypeDefault.getItemAt(
078                _ifThenElseExecuteTypeDefault.getSelectedIndex()));
079        return didSet;
080    }
081
082    private JPanel getStartupPanel() {
083        JPanel panel = new JPanel();
084
085        _startLogixNGOnLoadCheckBox = new JCheckBox(Bundle.getMessage("LabelStartLogixNGOnLoad"));
086        _startLogixNGOnLoadCheckBox.setToolTipText(Bundle.getMessage("ToolTipStartLogixNGOnLoad"));
087
088        _installDebuggerCheckBox = new JCheckBox(Bundle.getMessage("LabelInstallDebugger"));
089        _installDebuggerCheckBox.setToolTipText(Bundle.getMessage("ToolTipLabelInstallDebugger"));
090
091        _logAllBeforeCheckBox = new JCheckBox(Bundle.getMessage("LabelLogAllBefore"));
092        _logAllBeforeCheckBox.setToolTipText(Bundle.getMessage("ToolTipLabelLogAllBefore"));
093
094        _logAllAfterCheckBox = new JCheckBox(Bundle.getMessage("LabelLogAllAfter"));
095        _logAllAfterCheckBox.setToolTipText(Bundle.getMessage("ToolTipLabelLogAllAfter"));
096
097        _showSystemUserNamesCheckBox = new JCheckBox(Bundle.getMessage("LabelShowSystemUserNames"));
098        _showSystemUserNamesCheckBox.setToolTipText(Bundle.getMessage("ToolTipLabeShowSystemUserNames"));
099
100        _treeEditorHighlightRow = new JCheckBox(Bundle.getMessage("LabelTreeEditorHighlightRow"));
101        _treeEditorHighlightRow.setToolTipText(Bundle.getMessage("ToolTipTreeEditorHighlightRow"));
102
103        _showSystemNameInException = new JCheckBox(Bundle.getMessage("LabelShowSystemNameInException"));
104        _showSystemNameInException.setToolTipText(Bundle.getMessage("ToolTipShowSystemNameInException"));
105
106        _strictTypingGlobalVariables = new JCheckBox(Bundle.getMessage("LabelStrictTypingGlobalVariables"));
107        _strictTypingGlobalVariables.setToolTipText(Bundle.getMessage("ToolTipStrictTypingGlobalVariables"));
108
109        _strictTypingLocalVariables = new JCheckBox(Bundle.getMessage("LabelStrictTypingLocalVariables"));
110        _strictTypingLocalVariables.setToolTipText(Bundle.getMessage("ToolTipStrictTypingLocalVariables"));
111
112        JPanel gridPanel = new JPanel(new GridLayout(0, 1));
113
114        gridPanel.add(_startLogixNGOnLoadCheckBox);
115        gridPanel.add(_installDebuggerCheckBox);
116        gridPanel.add(_logAllBeforeCheckBox);
117        gridPanel.add(_logAllAfterCheckBox);
118        gridPanel.add(_showSystemUserNamesCheckBox);
119        gridPanel.add(_treeEditorHighlightRow);
120        gridPanel.add(Box.createVerticalStrut(2));
121        gridPanel.add(_strictTypingGlobalVariables);
122        gridPanel.add(_strictTypingLocalVariables);
123        gridPanel.add(Box.createVerticalStrut(2));
124        gridPanel.add(_showSystemNameInException);
125
126        _startLogixNGOnLoadCheckBox.setSelected(preferences.getStartLogixNGOnStartup());
127        _installDebuggerCheckBox.setSelected(preferences.getInstallDebugger());
128        _logAllBeforeCheckBox.setSelected(preferences.getLogAllBefore());
129        _logAllAfterCheckBox.setSelected(preferences.getLogAllAfter());
130        _showSystemUserNamesCheckBox.setSelected(preferences.getShowSystemUserNames());
131        _treeEditorHighlightRow.setSelected(preferences.getTreeEditorHighlightRow());
132        _showSystemNameInException.setSelected(preferences.getShowSystemNameInException());
133        _strictTypingGlobalVariables.setSelected(preferences.getStrictTypingGlobalVariables());
134        _strictTypingLocalVariables.setSelected(preferences.getStrictTypingLocalVariables());
135
136        gridPanel.add(Box.createVerticalStrut(2));
137        gridPanel.add(new JLabel(Bundle.getMessage("LabelDefaultErrorHandling")));
138        _errorHandlingComboBox = new JComboBox<>();
139        for (ErrorHandlingType type : ErrorHandlingType.values()) {
140            // ErrorHandlingType.Default cannot be used as default
141            if (type != ErrorHandlingType.Default) {
142                _errorHandlingComboBox.addItem(type);
143                if (preferences.getErrorHandlingType() == type) {
144                    _errorHandlingComboBox.setSelectedItem(type);
145                }
146            }
147        }
148        JComboBoxUtil.setupComboBoxMaxRows(_errorHandlingComboBox);
149        gridPanel.add(_errorHandlingComboBox);
150
151
152        gridPanel.add(Box.createVerticalStrut(2));
153        gridPanel.add(new JLabel(Bundle.getMessage("LabelDefaultIfThenElseExecution")));
154        _ifThenElseExecuteTypeDefault = new JComboBox<>();
155        for (IfThenElse.ExecuteType type : IfThenElse.ExecuteType.values()) {
156            _ifThenElseExecuteTypeDefault.addItem(type);
157            if (preferences.getIfThenElseExecuteTypeDefault() == type) {
158                _ifThenElseExecuteTypeDefault.setSelectedItem(type);
159            }
160        }
161        JComboBoxUtil.setupComboBoxMaxRows(_ifThenElseExecuteTypeDefault);
162        gridPanel.add(_ifThenElseExecuteTypeDefault);
163
164        panel.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 0));
165        panel.add(gridPanel);
166
167        return panel;
168    }
169
170    @Override
171    public String getPreferencesItem() {
172        return "LOGIXNG"; // NOI18N
173    }
174
175    @Override
176    public String getPreferencesItemText() {
177        return Bundle.getMessage("MenuLogixNG"); // NOI18N
178    }
179
180    @Override
181    public String getTabbedPreferencesTitle() {
182        return getPreferencesItemText();
183    }
184
185    @Override
186    public String getLabelKey() {
187        return null;
188    }
189
190    @Override
191    public JComponent getPreferencesComponent() {
192        return this;
193    }
194
195    @Override
196    public boolean isPersistant() {
197        return false;
198    }
199
200    @Override
201    public String getPreferencesTooltip() {
202        return null;
203    }
204
205    @Override
206    public void savePreferences() {
207        if (setValues()) {
208            preferences.save();
209        }
210    }
211
212    @Override
213    public boolean isDirty() {
214        return preferences.isDirty();
215    }
216
217    @Override
218    public boolean isRestartRequired() {
219        return preferences.isRestartRequired();
220    }
221
222    @Override
223    public boolean isPreferencesValid() {
224        return true; // no validity checking performed
225    }
226
227}