001package jmri.jmris.simpleserver;
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 SimpleServerPreferencesPanel extends JPanel implements PreferencesPanel {
024
025    private JSpinner port;
026    private SimpleServerPreferences preferences;
027    private JFrame parentFrame = null;
028
029    public SimpleServerPreferencesPanel() {
030        this.preferences = new SimpleServerPreferences();
031        this.preferences.apply(SimpleServerManager.getSimpleServerPreferences());
032        initGUI();
033        setGUI();
034    }
035
036    public SimpleServerPreferencesPanel(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("SimpleServerSectionTitle")));
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 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) -> this.setValues());
100        this.port.setToolTipText(Bundle.getMessage("PortToolTip"));
101        panel.add(port);
102        panel.add(new JLabel(Bundle.getMessage("LabelPort")));
103        return panel;
104    }
105
106    @Override
107    public String getPreferencesItem() {
108        return Bundle.getMessage("PreferencesItem");
109    }
110
111    @Override
112    public String getPreferencesItemText() {
113        return Bundle.getMessage("PreferencesItemTitle");
114    }
115
116    @Override
117    public String getTabbedPreferencesTitle() {
118        return Bundle.getMessage("PreferencesTabTitle");
119    }
120
121    @Override
122    public String getLabelKey() {
123        return null;
124    }
125
126    @Override
127    public JComponent getPreferencesComponent() {
128        return this;
129    }
130
131    @Override
132    public boolean isPersistant() {
133        return false;
134    }
135
136    @Override
137    public String getPreferencesTooltip() {
138        return Bundle.getMessage("PreferencesTooltip");
139    }
140
141    @Override
142    public void savePreferences() {
143        if (this.setValues()) {
144            SimpleServerManager.getSimpleServerPreferences().apply(this.preferences);
145            SimpleServerManager.getSimpleServerPreferences().save();
146            if (this.parentFrame != null) {
147                this.parentFrame.dispose();
148            }
149        }
150    }
151
152    @Override
153    public boolean isDirty() {
154        return this.preferences.compareValuesDifferent(SimpleServerManager.getSimpleServerPreferences())
155                || SimpleServerManager.getSimpleServerPreferences().isDirty();
156    }
157
158    @Override
159    public boolean isRestartRequired() {
160        return SimpleServerManager.getSimpleServerPreferences().isRestartRequired();
161    }
162
163    /**
164     * Indicate that the preferences are valid.
165     *
166     * @return true if the preferences are valid, false otherwise
167     */
168    @Override
169    public boolean isPreferencesValid() {
170        return true;
171    }
172
173}