001package jmri.util;
002
003import java.awt.FlowLayout;
004import java.awt.GridLayout;
005
006import javax.swing.*;
007
008import org.openide.util.lookup.ServiceProvider;
009
010import jmri.InstanceManager;
011// import jmri.swing.JTitledSeparator;
012import jmri.swing.PreferencesPanel;
013
014/**
015 * Preferences panel for HelpUtil
016 *
017 * @author Daniel Bergqvist Copyright 2021
018 */
019@ServiceProvider(service = PreferencesPanel.class)
020public class HelpUtilPreferencesPanel extends JPanel implements PreferencesPanel {
021
022    private final HelpUtilPreferences _preferences;
023
024    JRadioButton _openHelpOnFileRadioButton;
025    JRadioButton _openHelpOnlineRadioButton;
026    JRadioButton _openHelpOnJMRIWebServerRadioButton;
027    ButtonGroup _openHelpButtonGroup;
028
029    public HelpUtilPreferencesPanel() {
030        _preferences = InstanceManager.getDefault(HelpUtilPreferences.class);
031        initGUI();
032    }
033
034    private void initGUI() {
035        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
036        add(getHelpPanel());
037    }
038
039    /**
040     * Set the local prefs to match the GUI Local prefs are independent from the
041     * singleton instance prefs.
042     *
043     * @return true if set, false if values are unacceptable.
044     */
045    private boolean setValues() {
046        boolean didSet = true;
047        _preferences.setOpenHelpOnFile(_openHelpOnFileRadioButton.isSelected());
048        _preferences.setOpenHelpOnline(_openHelpOnlineRadioButton.isSelected());
049        _preferences.setOpenHelpOnJMRIWebServer(_openHelpOnJMRIWebServerRadioButton.isSelected());
050        return didSet;
051    }
052
053    private JPanel getHelpPanel() {
054        JPanel panel = new JPanel();
055
056        _openHelpButtonGroup = new ButtonGroup();
057
058        _openHelpOnFileRadioButton = new JRadioButton(Bundle.getMessage("Help_LabelOpenHelpOnFile"));
059        _openHelpOnFileRadioButton.setToolTipText(Bundle.getMessage("Help_ToolTipLabelOpenHelpOnFile"));
060        _openHelpButtonGroup.add(_openHelpOnFileRadioButton);
061
062        _openHelpOnlineRadioButton = new JRadioButton(Bundle.getMessage("Help_LabelOpenHelpOnline"));
063        _openHelpOnlineRadioButton.setToolTipText(Bundle.getMessage("Help_ToolTipLabelOpenHelpOnline"));
064        _openHelpButtonGroup.add(_openHelpOnlineRadioButton);
065
066        _openHelpOnJMRIWebServerRadioButton = new JRadioButton(Bundle.getMessage("Help_LabelOpenHelpOnJMRIWebServer"));
067        _openHelpOnJMRIWebServerRadioButton.setToolTipText(Bundle.getMessage("Help_ToolTipLabelOpenHelpOnJMRIWebServer"));
068        _openHelpButtonGroup.add(_openHelpOnJMRIWebServerRadioButton);
069
070        JPanel gridPanel = new JPanel(new GridLayout(0, 1));
071
072        gridPanel.add(_openHelpOnFileRadioButton);
073        gridPanel.add(_openHelpOnlineRadioButton);
074        gridPanel.add(_openHelpOnJMRIWebServerRadioButton);
075
076        _openHelpOnFileRadioButton.setSelected(_preferences.getOpenHelpOnFile());
077        _openHelpOnlineRadioButton.setSelected(_preferences.getOpenHelpOnline());
078        _openHelpOnJMRIWebServerRadioButton.setSelected(_preferences.getOpenHelpOnJMRIWebServer());
079
080        panel.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 0));
081        panel.add(gridPanel);
082
083        return panel;
084    }
085
086    @Override
087    public String getPreferencesItem() {
088        return "HELP"; // NOI18N
089    }
090
091    @Override
092    public String getPreferencesItemText() {
093        return Bundle.getMessage("MenuHelp"); // NOI18N
094    }
095
096    @Override
097    public String getTabbedPreferencesTitle() {
098        return getPreferencesItemText();
099    }
100
101    @Override
102    public String getLabelKey() {
103        return null;
104    }
105
106    @Override
107    public JComponent getPreferencesComponent() {
108        return this;
109    }
110
111    @Override
112    public boolean isPersistant() {
113        return false;
114    }
115
116    @Override
117    public String getPreferencesTooltip() {
118        return null;
119    }
120
121    @Override
122    public void savePreferences() {
123        if (setValues()) {
124            _preferences.save();
125        }
126    }
127
128    @Override
129    public boolean isDirty() {
130        return _preferences.isDirty();
131    }
132
133    @Override
134    public boolean isRestartRequired() {
135        return _preferences.isRestartRequired();
136    }
137
138    @Override
139    public boolean isPreferencesValid() {
140        return true; // no validity checking performed
141    }
142
143}