001package jmri.jmrix.loconet.demoport;
002
003import java.awt.GridBagConstraints;
004import java.awt.GridBagLayout;
005import java.util.*;
006
007import javax.swing.*;
008
009import jmri.InstanceManager;
010import jmri.SystemConnectionMemo;
011import jmri.jmrix.*;
012import jmri.jmrix.loconet.LocoNetSystemConnectionMemo;
013import jmri.util.swing.JmriPanel;
014
015/**
016 * Demo panel.
017 *
018 * @author Daniel Bergqvist (C) 2024
019 */
020class DemoPanel extends JmriPanel {
021
022    private final Map<SystemConnectionMemo, AbstractSerialPortController> memoMap = new HashMap<>();
023    private DemoSerialPort _demoSerialPort;
024    private LocoNetConnection _connection;
025    private JComboBox<LocoNetConnection> _locoNetConnection;
026    private JTextArea _textArea;
027    private boolean turnoutThrown1;
028    private boolean turnoutThrown2;
029
030    /** {@inheritDoc} */
031    @Override
032    public void initComponents() {
033
034        super.initComponents();
035
036        setLayout(new GridBagLayout());
037        GridBagConstraints c = new GridBagConstraints();
038        c.gridwidth = 1;
039        c.gridheight = 1;
040        c.gridx = 0;
041        c.gridy = 0;
042        c.anchor = GridBagConstraints.EAST;
043        add(new JLabel(Bundle.getMessage("DemoPanel_Connection")), c);
044
045        for (ConnectionConfig cc : InstanceManager.getDefault(ConnectionConfigManager.class)) {
046            if (cc.getAdapter() instanceof AbstractSerialPortController) {
047                AbstractSerialPortController pc = (AbstractSerialPortController) cc.getAdapter();
048                if (pc.isPortOpen()) {
049                    memoMap.put(cc.getAdapter().getSystemConnectionMemo(), pc);
050                }
051            }
052        }
053
054        _locoNetConnection = new JComboBox<>();
055        c.gridx = 1;
056        c.anchor = GridBagConstraints.WEST;
057        add(_locoNetConnection, c);
058
059        c.gridwidth = 2;
060        c.gridx = 0;
061        c.gridy = 1;
062        JButton buttonStartTest = new JButton(Bundle.getMessage("DemoPanel_ButtonStartTest"));
063        buttonStartTest.setEnabled(false);
064        add(buttonStartTest, c);
065
066        c.gridy = 2;
067        JButton buttonThrowTurnout1 = new JButton(Bundle.getMessage("DemoPanel_ButtonThrowTurnout1"));
068        buttonThrowTurnout1.addActionListener((e)->{
069            _demoSerialPort.throwTurnout(1, turnoutThrown1);
070            turnoutThrown1 = !turnoutThrown1;
071        });
072        buttonThrowTurnout1.setEnabled(false);
073        add(buttonThrowTurnout1, c);
074
075        c.gridy = 3;
076        JButton buttonThrowTurnout2 = new JButton(Bundle.getMessage("DemoPanel_ButtonThrowTurnout2"));
077        buttonThrowTurnout2.addActionListener((e)->{
078            _demoSerialPort.throwTurnout(2, turnoutThrown2);
079            turnoutThrown2 = !turnoutThrown2;
080        });
081        buttonThrowTurnout2.setEnabled(false);
082        add(buttonThrowTurnout2, c);
083
084        c.gridy = 4;
085        add(new JLabel(Bundle.getMessage("DemoPanel_LocoNetMonitor")), c);
086
087        c.gridy = 5;
088        _textArea = new JTextArea();
089        _textArea.setColumns(50);
090        _textArea.setRows(30);
091        add(_textArea, c);
092
093        _locoNetConnection.addActionListener((e)->{
094            _connection = _locoNetConnection.getItemAt(_locoNetConnection.getSelectedIndex());
095            buttonStartTest.setEnabled(true);
096        });
097        List<LocoNetSystemConnectionMemo> systemConnections =
098                jmri.InstanceManager.getList(LocoNetSystemConnectionMemo.class);
099        for (LocoNetSystemConnectionMemo memo : systemConnections) {
100            if (memoMap.get(memo) != null) {
101                LocoNetConnection conn = new LocoNetConnection(memo);
102                _locoNetConnection.addItem(conn);
103            }
104        }
105
106        buttonStartTest.addActionListener((e)->{
107            _demoSerialPort = new DemoSerialPort(this, _connection._memo);
108            _demoSerialPort.startDemo();
109            _locoNetConnection.setEnabled(false);
110            buttonStartTest.setEnabled(false);
111            buttonThrowTurnout1.setEnabled(true);
112            buttonThrowTurnout2.setEnabled(true);
113        });
114    }
115
116    public AbstractSerialPortController getPortController() {
117        if (_connection == null) {
118            return null;
119        }
120        return memoMap.get(_connection._memo);
121    }
122
123    public void addMessage(String msg) {
124        _textArea.append(msg);
125    }
126
127    /** {@inheritDoc} */
128    @Override
129    public String getTitle() {
130        return "Demo panel"; // this should come from a Bundle in your package
131    }
132
133    /** {@inheritDoc} */
134    @Override
135    public List<JMenu> getMenus() {
136        List<JMenu> menuList = new ArrayList<>();
137        return menuList;
138    }
139
140
141    private static class LocoNetConnection {
142
143        private final LocoNetSystemConnectionMemo _memo;
144
145        public LocoNetConnection(LocoNetSystemConnectionMemo memo) {
146            _memo = memo;
147        }
148
149        @Override
150        public String toString() {
151            return _memo.getUserName();
152        }
153    }
154
155}