001package jmri.jmrix.can.cbus.swing.simulator;
002
003import javax.swing.BoxLayout;
004import java.awt.event.ActionEvent;
005import java.util.ArrayList;
006import javax.swing.BorderFactory;
007import javax.swing.JButton;
008import javax.swing.JComboBox;
009import javax.swing.JLabel;
010import javax.swing.JPanel;
011import jmri.jmrix.can.cbus.simulator.CbusDummyCS;
012import jmri.util.swing.ComboBoxToolTipRenderer;
013
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017/**
018 * Pane for viewing and setting simulated network objects.
019 * @see SimulatorPane
020 * @author Steve Young Copyright (C) 2018 2019
021 * @since 4.15.2
022 */
023public class CsPane extends JPanel {
024    
025    private final CbusDummyCS _cs;
026    private JComboBox<String> _selectCs;
027    private int _id;
028    private int _type;
029    private int _numSessions;
030    private JButton _resetCs;
031    private JLabel _sessionText;
032    private ArrayList<String> tooltips;
033    
034    public CsPane(CbusDummyCS cs ) {
035        super();
036        _cs=cs;
037        if ( _cs != null ) {
038            init();
039        }
040    }
041        
042    private void init() {
043        _type = _cs.getDummyType();
044        _numSessions=_cs.getNumberSessions();
045        _sessionText = new JLabel();
046        _sessionText.setToolTipText(Bundle.getMessage("ActiveSess"));
047        
048        _selectCs = new JComboBox<>();
049        _selectCs.setEditable(false);
050        
051        ComboBoxToolTipRenderer renderer = new ComboBoxToolTipRenderer();
052        _selectCs.setRenderer(renderer);
053        
054        updateSessionTotal();
055        
056        _cs.setPane(this);
057        tooltips = new ArrayList<>();
058        String getSelected="";
059        
060        for (int i = 0; i < _cs.csTypes.size(); i++) {
061            String option = _cs.csTypes.get(i);
062            _selectCs.addItem(option);
063            tooltips.add(_cs.csTypesTip.get(i));
064            if ( i == _type ){
065                getSelected = option;
066            }
067        }
068        
069        _selectCs.setSelectedItem(getSelected);
070        _selectCs.addActionListener ((ActionEvent e) -> {
071            String chosen = (String)_selectCs.getSelectedItem();
072            for (int i = 0; i < _cs.csTypes.size(); i++) {
073                if (_cs.csTypes.get(i).equals(chosen)) {
074                    log.debug("chosen {} {}",i,chosen);
075                    _cs.setDummyType(i);
076                }
077            }
078        });
079        renderer.setTooltips(tooltips);
080        
081        _resetCs = new JButton(Bundle.getMessage("Reset"));
082        
083        JPanel topPane = new JPanel();
084        topPane.add(_selectCs);
085        topPane.add(_sessionText);
086        topPane.add(_resetCs);
087
088        DirectionPane dp = new DirectionPane(_cs);
089        
090        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
091        setBorder(BorderFactory.createEtchedBorder());
092        
093        add(topPane);
094        add(dp);
095        
096        _resetCs.addActionListener ((ActionEvent e) -> {
097            _cs.resetCS();
098        });
099    }
100    
101    void updateSessionTotal(){
102        _sessionText.setText("<html> <h2> " + _numSessions + " </h2> </html>");
103    }
104    
105    public void setNumSessions(int num){
106        _numSessions=num;
107        updateSessionTotal();
108    }
109    
110    public int getId(){
111        return _id;
112    }
113
114    private final static Logger log = LoggerFactory.getLogger(CsPane.class);
115}