001package jmri.jmrix.can.cbus.swing.simulator;
002
003import java.awt.event.ActionEvent;
004import java.util.ArrayList;
005
006import javax.swing.*;
007
008import jmri.jmrix.can.CanSystemConnectionMemo;
009import jmri.jmrix.can.cbus.node.CbusNodeCanListener;
010import jmri.jmrix.can.cbus.simulator.*;
011import jmri.util.swing.ComboBoxToolTipRenderer;
012
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016/**
017 * Pane for viewing and setting simulated network objects.
018 * <p>
019 * Methods are subject to change and should not be relied on at present.
020 *
021 * @author Steve Young Copyright (C) 2019
022 * @since 4.15.2
023 */
024public class NdPane extends JPanel {
025    
026    private JComboBox<String> _selectNd;
027    private CbusDummyNode _node;
028    private JButton _flimButton;
029    private JLabel _sessionText;
030    private DirectionPane directionPane;
031    private final CanSystemConnectionMemo _memo;
032    
033    public NdPane(CbusDummyNode nd, CanSystemConnectionMemo sysmemo ) {
034        super();
035        _node = nd;
036        _memo = sysmemo;
037        init();
038    }
039        
040    private void init() {
041
042        _sessionText = new JLabel();
043
044        _selectNd = new JComboBox<>();
045        _selectNd.setEditable(false);
046
047        ComboBoxToolTipRenderer renderer = new ComboBoxToolTipRenderer();
048        _selectNd.setRenderer(renderer);
049
050        ArrayList<String> tooltips = new ArrayList<>();
051
052        _selectNd.addItem("None");
053        tooltips.add("Select a module to start the Simulation.");
054
055        CbusSimulatedModuleProvider.getInstancesCollection().forEach(module -> {
056            log.debug("found SPI {}", module.getModuleType());
057            _selectNd.addItem(module.getModuleType());
058            tooltips.add(module.getToolTipText());
059            if ( module.matchesManuAndModuleId(_node) ) {
060                _selectNd.setSelectedItem(module.getModuleType());
061            }
062        });
063
064        renderer.setTooltips(tooltips);
065        _selectNd.addActionListener(this::moduleSelectorChanged);
066
067        _flimButton = new JButton("FLiM"); // NOI18N
068        _flimButton.addActionListener ((ActionEvent e) -> {
069            _node.flimButton();
070        });
071
072        JPanel topPane = new JPanel();
073        topPane.add(_selectNd);
074        topPane.add(_sessionText);
075        topPane.add(_flimButton);
076
077        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
078        setBorder(BorderFactory.createEtchedBorder());
079
080        add(topPane);
081
082        directionPane = new DirectionPane(null);
083        add( directionPane );
084
085        updateNodeGui();
086    }
087
088    private void moduleSelectorChanged(ActionEvent e) {
089
090        String chosen = (String)_selectNd.getSelectedItem();
091        log.debug("Selected module {} {}",chosen,e);
092        
093        CbusSimulator sim = jmri.InstanceManager.getNullableDefault(CbusSimulator.class);
094
095        if (_node != null) {
096            // todo - use memo, not instancemanager
097            if ( sim != null ) {
098                sim.removeNode(_node);
099            } else {
100                log.warn("No Simulator Running to deregister Node {}",_node);
101            }
102            _node.dispose();
103            _node = null;
104        }
105
106        CbusSimulatedModuleProvider providerNode = CbusSimulatedModuleProvider.getProviderByName(chosen);
107        if ( providerNode != null ) {
108            _node = providerNode.getNewDummyNode( _memo, 0);
109
110            // todo - use memo, not instancemanager
111            if ( sim != null ) {
112                sim.addNode(_node);
113            } else {
114                log.warn("No Simulator Running to register Node {}",_node);
115            }
116        }
117        updateNodeGui();
118    }
119
120    public void updateNodeGui(){
121        _flimButton.setEnabled( _node != null );
122        if ( _node != null ) {
123            _node.setPane(this); // todo - move to property lkistener for node number changes
124            
125            CbusNodeCanListener ncl = _node.getCanListener();
126            if ( ncl instanceof CbusSimCanListener ) {
127                directionPane.setSimCanListener( (CbusSimCanListener) ncl);
128            }
129           _sessionText.setText("<html> <h2> " + _node.getNodeNumber() + " </h2> </html>");
130        } else {
131            directionPane.setSimCanListener(null);
132            _sessionText.setText("<html> <h2>   </h2> </html>");
133        }
134        
135    }
136    
137    private final static Logger log = LoggerFactory.getLogger(NdPane.class);
138    
139}