001package jmri.configurexml.swing;
002
003import java.awt.FlowLayout;
004import java.awt.GridLayout;
005
006import javax.swing.*;
007
008import org.openide.util.lookup.ServiceProvider;
009
010import jmri.InstanceManager;
011import jmri.swing.PreferencesPanel;
012import jmri.configurexml.ShutdownPreferences;
013import jmri.configurexml.ShutdownPreferences.DialogDisplayOptions;
014
015/**
016 * Preferences panel for the shutdown options used by StoreAndCompare.
017 *
018 * @author Dave Sand Copyright 2022
019 */
020@ServiceProvider(service = PreferencesPanel.class)
021public class ShutdownPreferencesPanel extends JPanel implements PreferencesPanel {
022
023    private final ShutdownPreferences _preferences;
024
025    private JCheckBox _enableCheckBox;
026    private JComboBox<DialogDisplayOptions> _displayActionComboBox;
027    private JCheckBox _timebase;
028    private JCheckBox _sensorIconColor;
029
030    public ShutdownPreferencesPanel() {
031        _preferences = InstanceManager.getDefault(ShutdownPreferences.class);
032        initGUI();
033    }
034
035    private void initGUI() {
036        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
037        add(getPanel());
038    }
039
040    /**
041     * set the local prefs to match the GUI Local prefs are independent from the
042     * singleton instance prefs.
043     *
044     * @return true if set, false if values are unacceptable.
045     */
046    private boolean setValues() {
047        boolean didSet = true;
048        _preferences.setEnableStoreCheck(_enableCheckBox.isSelected());
049        _preferences.setIgnoreTimebase(_timebase.isSelected());
050        _preferences.setIgnoreSensorColors(_sensorIconColor.isSelected());
051        _preferences.setDisplayDialog((DialogDisplayOptions) _displayActionComboBox.getSelectedItem());
052        return didSet;
053    }
054
055    private JPanel getPanel() {
056        _enableCheckBox = new JCheckBox(Bundle.getMessage("LabelEnableCheckbox"));
057        _enableCheckBox.setSelected(_preferences.isStoreCheckEnabled());
058
059        _timebase = new JCheckBox(Bundle.getMessage("IgnoreTimebase"));
060        _timebase.setSelected(_preferences.isIgnoreTimebaseEnabled());
061
062        _sensorIconColor = new JCheckBox(Bundle.getMessage("IgnoreSensorColor"));
063        _sensorIconColor.setSelected(_preferences.isIgnoreSensorColorsEnabled());
064
065        _displayActionComboBox = new JComboBox<>();
066        for (DialogDisplayOptions opt : DialogDisplayOptions.values()) {
067            _displayActionComboBox.addItem(opt);
068            if (opt == _preferences.getDisplayDialog()) {
069                _displayActionComboBox.setSelectedItem(opt);
070            }
071        }
072
073        JPanel gridPanel = new JPanel(new GridLayout(0, 1));
074        gridPanel.add(_enableCheckBox);
075        gridPanel.add(_displayActionComboBox);
076        gridPanel.add(new JLabel());
077        gridPanel.add(new jmri.swing.JTitledSeparator(Bundle.getMessage("IgnoreSeparator")));
078        gridPanel.add(_timebase);
079        gridPanel.add(_sensorIconColor);
080
081        JPanel panel = new JPanel();
082        panel.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 0));
083        panel.add(gridPanel);
084
085        return panel;
086    }
087
088    @Override
089    public String getPreferencesItem() {
090        return "SHUTDOWN"; // NOI18N
091    }
092
093    @Override
094    public String getPreferencesItemText() {
095        return Bundle.getMessage("ShutdownMenu"); // NOI18N
096    }
097
098    @Override
099    public String getTabbedPreferencesTitle() {
100        return getPreferencesItemText();
101    }
102
103    @Override
104    public String getLabelKey() {
105        return Bundle.getMessage("ShutdownPrefLabel");
106    }
107
108    @Override
109    public JComponent getPreferencesComponent() {
110        return this;
111    }
112
113    @Override
114    public boolean isPersistant() {
115        return false;
116    }
117
118    @Override
119    public String getPreferencesTooltip() {
120        return null;
121    }
122
123    @Override
124    public void savePreferences() {
125        if (setValues()) {
126            _preferences.save();
127        }
128    }
129
130    @Override
131    public boolean isDirty() {
132        return _preferences.isDirty();
133    }
134
135    @Override
136    public boolean isRestartRequired() {
137        return _preferences.isRestartRequired();
138    }
139
140    @Override
141    public boolean isPreferencesValid() {
142        return true; // no validity checking performed
143    }
144
145    @Override
146    public int getSortOrder() {
147        return 450; // Place between "Start Up" and "Display"
148    }
149}