001package jmri.jmrix.dccpp.dccppovertcp;
002
003import java.awt.event.ActionEvent;
004import java.util.Set;
005import javax.annotation.Nonnull;
006import javax.swing.BoxLayout;
007import javax.swing.JButton;
008import javax.swing.JCheckBox;
009import javax.swing.JLabel;
010import javax.swing.JPanel;
011import javax.swing.JSpinner;
012import javax.swing.SpinnerNumberModel;
013import javax.swing.event.ChangeEvent;
014import jmri.InstanceInitializer;
015import jmri.InstanceManager;
016import jmri.InstanceManagerAutoInitialize;
017import jmri.implementation.AbstractInstanceInitializer;
018import org.openide.util.lookup.ServiceProvider;
019
020/**
021 * Frame displaying and programming a DCCppovertcp server.
022 * <p>
023 * Some of the message formats used in this class are Copyright Digitrax, Inc.
024 * and used with permission as part of the JMRI project. That permission does
025 * not extend to uses in other software products. If you wish to use this code,
026 * algorithm or these message formats outside of JMRI, please contact Digitrax
027 * Inc for separate permission.
028 *
029 * @author Bob Jacobsen Copyright (C) 2003, 2004
030 * @author Alex Shepherd Copyright (C) 2006
031 * @author Mark Underwood Copyright (C) 2015
032 */
033public class ServerFrame extends jmri.util.JmriJFrame implements ServerListner, InstanceManagerAutoInitialize {
034
035    private ServerFrame() {
036        super("DCCppOverTcp Server");
037
038        super.getContentPane().setLayout(new BoxLayout(super.getContentPane(), BoxLayout.Y_AXIS));
039
040        portNumber = new JSpinner();
041        portNumberModel = new SpinnerNumberModel(65535, 1, 65535, 1);
042        portNumber.setModel(portNumberModel);
043
044        portNumber.setFocusable(false);
045
046        // add GUI items
047        JPanel panel = new JPanel();
048        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
049        panel.add(autoStartCheckBox);
050        panel.add(portNumberLabel);
051        panel.add(portNumber);
052        super.getContentPane().add(panel);
053
054        panel = new JPanel();
055        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
056        panel.add(startButton);
057        panel.add(stopButton);
058        panel.add(saveButton);
059        super.getContentPane().add(panel);
060
061        panel = new JPanel();
062        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
063        panel.add(statusLabel);
064        super.getContentPane().add(panel);
065
066        startButton.addActionListener((ActionEvent a) -> {
067            InstanceManager.getDefault(Server.class).enable();
068        });
069
070        stopButton.addActionListener((ActionEvent a) -> {
071            InstanceManager.getDefault(Server.class).disable();
072        });
073
074        saveButton.addActionListener((ActionEvent a) -> {
075            InstanceManager.getDefault(Server.class).setAutoStart(autoStartCheckBox.isSelected());
076            InstanceManager.getDefault(Server.class).setPortNumber(((Integer) portNumber.getValue()));
077            InstanceManager.getDefault(Server.class).saveSettings();
078        });
079
080        autoStartCheckBox.addActionListener((ActionEvent a) -> {
081            saveButton.setEnabled(true);
082        });
083
084        portNumber.addChangeListener((ChangeEvent e) -> {
085            saveButton.setEnabled(true);
086        });
087
088        super.pack();
089    }
090
091    @Override
092    public void windowClosing(java.awt.event.WindowEvent e) {
093        setVisible(false);
094        InstanceManager.getDefault(Server.class).setStateListner(null);
095        InstanceManager.deregister(this, ServerFrame.class);
096        dispose();
097        super.windowClosing(e);
098    }
099
100    private void updateServerStatus() {
101        Server server = InstanceManager.getDefault(Server.class);
102        autoStartCheckBox.setSelected(server.getAutoStart());
103        autoStartCheckBox.setEnabled(!server.isEnabled());
104
105        portNumber.setValue(server.getPortNumber());
106        portNumber.setEnabled(!server.isEnabled());
107        portNumberLabel.setEnabled(!server.isEnabled());
108
109        startButton.setEnabled(!server.isEnabled());
110        stopButton.setEnabled(server.isEnabled());
111        saveButton.setEnabled(server.isSettingChanged());
112        updateClientStatus(server);
113    }
114
115    private void updateClientStatus(Server s) {
116        statusLabel.setText(Bundle.getMessage("StatusLabel", (s.isEnabled() ? Bundle.getMessage("Running") : Bundle.getMessage("Stopped")), s.getClientCount()));
117        // combined status and count in 1 field, like LnTcpServer
118    }
119
120    @Override
121    public void notifyServerStateChanged(Server s) {
122        javax.swing.SwingUtilities.invokeLater(() -> {
123            updateServerStatus();
124        });
125    }
126
127    @Override
128    public void notifyClientStateChanged(Server s) {
129        javax.swing.SwingUtilities.invokeLater(() -> {
130            updateClientStatus(s);
131        });
132    }
133
134    JSpinner portNumber;
135    SpinnerNumberModel portNumberModel;
136    JLabel portNumberLabel = new JLabel(Bundle.getMessage("MakeLabel", Bundle.getMessage("LabelPort")));
137    private final JLabel statusLabel = new JLabel(Bundle.getMessage("StatusLabel", Bundle.getMessage("Stopped"), 0));
138    JCheckBox autoStartCheckBox = new JCheckBox(Bundle.getMessage("LabelStartup"));
139    JButton startButton = new JButton(Bundle.getMessage("StartServer"));
140    JButton stopButton = new JButton(Bundle.getMessage("StopServer"));
141    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
142
143    @Override
144    public void initialize() {
145        Server server = InstanceManager.getDefault(Server.class);
146        server.setStateListner(this);
147        server.updateServerStateListener();
148        server.updateClinetStateListener();
149    }
150
151    @ServiceProvider(service = InstanceInitializer.class)
152    public static class Initializer extends AbstractInstanceInitializer {
153
154        @Override
155        @Nonnull
156        public <T> Object getDefault(Class<T> type) {
157            if (type.equals(ServerFrame.class)) {
158                return new ServerFrame();
159            }
160            return super.getDefault(type);
161        }
162
163        @Override
164        @Nonnull
165        public Set<Class<?>> getInitalizes() {
166            Set<Class<?>> set = super.getInitalizes();
167            set.add(ServerFrame.class);
168            return set;
169        }
170    }
171
172}