001package jmri.jmris.srcp;
002
003import javax.swing.Box;
004import javax.swing.BoxLayout;
005import javax.swing.JComponent;
006import javax.swing.JFrame;
007import javax.swing.JLabel;
008import javax.swing.JPanel;
009import javax.swing.JSpinner;
010import javax.swing.SpinnerNumberModel;
011import javax.swing.event.ChangeEvent;
012
013import jmri.swing.JTitledSeparator;
014import jmri.swing.PreferencesPanel;
015import jmri.util.swing.JmriJOptionPane;
016
017import org.openide.util.lookup.ServiceProvider;
018
019/**
020 * @author Randall Wood Copyright (C) 2012
021 */
022@ServiceProvider(service = PreferencesPanel.class)
023public class JmriSRCPServerPreferencesPanel extends JPanel implements PreferencesPanel {
024
025    private JSpinner port;
026    private JmriSRCPServerPreferences preferences;
027    private JFrame parentFrame = null;
028
029    public JmriSRCPServerPreferencesPanel() {
030        this.preferences = new JmriSRCPServerPreferences();
031        this.preferences.apply(JmriSRCPServerManager.getJmriSRCPServerPreferences());
032        initGUI();
033        setGUI();
034    }
035
036    public JmriSRCPServerPreferencesPanel(JFrame f) {
037        this();
038        parentFrame = f;
039    }
040
041    private void initGUI() {
042        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
043        add(new JTitledSeparator(Bundle.getMessage("ServerSectionTitle")));
044        add(portPanel());
045        add(new JTitledSeparator(Bundle.getMessage("SRCPSectionTitle")));
046        add(Box.createVerticalGlue());
047    }
048
049    private void setGUI() {
050        port.setValue(preferences.getPort());
051    }
052
053    /**
054     * set the local prefs to match the GUI Local prefs are independent from the
055     * singleton instance prefs.
056     *
057     * @return true if set, false if values are unacceptable.
058     */
059    private boolean setValues() {
060        boolean didSet = true;
061        int portNum;
062        try {
063            portNum = (Integer) port.getValue();
064        } catch (NumberFormatException NFE) { //  Not a number
065            portNum = 0;
066        }
067        if ((portNum < 1) || (portNum > 65535)) { //  Invalid port value
068            JmriJOptionPane.showMessageDialog(this,
069                    Bundle.getMessage("InvalidPortWarningMessage"),
070                    Bundle.getMessage("InvalidPortWarningTitle"),
071                    JmriJOptionPane.WARNING_MESSAGE);
072            didSet = false;
073        } else {
074            preferences.setPort(portNum);
075        }
076        return didSet;
077    }
078
079    /**
080     * Update the singleton instance of prefs, then mark (isDirty) that the
081     * values have changed and needs to save to xml file.
082     */
083    protected void applyValues() {
084        this.setValues();
085    }
086
087    protected void cancelValues() {
088        java.awt.Container ancestor = getTopLevelAncestor();
089        if (ancestor != null && ancestor instanceof JFrame) {
090            ((JFrame) ancestor).setVisible(false);
091        }
092    }
093
094    private JPanel portPanel() {
095        JPanel panel = new JPanel();
096        port = new JSpinner(new SpinnerNumberModel(preferences.getPort(), 1, 65535, 1));
097        ((JSpinner.DefaultEditor) port.getEditor()).getTextField().setEditable(true);
098        port.setEditor(new JSpinner.NumberEditor(port, "#"));
099        this.port.addChangeListener((ChangeEvent e) -> {
100            this.setValues();
101        });
102        this.port.setToolTipText(Bundle.getMessage("PortToolTip"));
103        panel.add(port);
104        panel.add(new JLabel(Bundle.getMessage("LabelPort")));
105        return panel;
106    }
107
108    @Override
109    public String getPreferencesItem() {
110        return Bundle.getMessage("PreferencesItem");
111    }
112
113    @Override
114    public String getPreferencesItemText() {
115        return Bundle.getMessage("PreferencesItemTitle");
116    }
117
118    @Override
119    public String getTabbedPreferencesTitle() {
120        return Bundle.getMessage("PreferencesTabTitle");
121    }
122
123    @Override
124    public String getLabelKey() {
125        return null;
126    }
127
128    @Override
129    public JComponent getPreferencesComponent() {
130        return this;
131    }
132
133    @Override
134    public boolean isPersistant() {
135        return false;
136    }
137
138    @Override
139    public String getPreferencesTooltip() {
140        return Bundle.getMessage("PreferencesTooltip");
141    }
142
143    @Override
144    public void savePreferences() {
145        if (this.setValues()) {
146            JmriSRCPServerManager.getJmriSRCPServerPreferences().apply(this.preferences);
147            JmriSRCPServerManager.getJmriSRCPServerPreferences().save();
148            if (this.parentFrame != null) {
149                this.parentFrame.dispose();
150            }
151        }
152    }
153
154    @Override
155    public boolean isDirty() {
156        return this.preferences.compareValuesDifferent(JmriSRCPServerManager.getJmriSRCPServerPreferences())
157                || JmriSRCPServerManager.getJmriSRCPServerPreferences().isDirty();
158    }
159
160    @Override
161    public boolean isRestartRequired() {
162        return JmriSRCPServerManager.getJmriSRCPServerPreferences().isRestartRequired();
163    }
164
165    /**
166     * Indicate that the preferences are valid.
167     *
168     * @return true if the preferences are valid, false otherwise
169     */
170    @Override
171    public boolean isPreferencesValid() {
172        return true;
173    }
174
175}