001package jmri.jmrix.can.cbus.swing.simulator;
002
003import java.awt.event.ActionEvent;
004import java.util.ArrayList;
005import java.util.List;
006
007import javax.annotation.Nonnull;
008import javax.swing.*;
009
010import jmri.jmrix.can.CanSystemConnectionMemo;
011import jmri.jmrix.can.cbus.CbusConfigurationManager;
012import jmri.jmrix.can.cbus.simulator.CbusSimulator;
013
014// import org.slf4j.Logger;
015// import org.slf4j.LoggerFactory;
016
017/**
018 * Pane for viewing and setting simulated network objects.
019 *
020 * @see CsPane
021 * @see EvResponderPane
022 * @see NdPane
023 * @see DirectionPane
024 * @author Steve Young Copyright (C) 2018 2019
025 * @since 4.15.2
026 */
027public class SimulatorPane extends jmri.jmrix.can.swing.CanPanel {
028
029    private CbusSimulator _sim;
030
031    private JPanel p1;
032    private JPanel _csPanes;
033    private JPanel _ndPanes;
034    private JPanel _evPanes;
035    private Boolean _disposeSimOnWindowClose = false;
036    private JScrollPane mainScroll;
037
038    /**
039     * {@inheritDoc}
040     */
041    @Override
042    public void initComponents(@Nonnull CanSystemConnectionMemo memo) {
043        super.initComponents(memo);
044        _sim = memo.get(CbusSimulator.class);
045        init();
046    }
047
048    /**
049     * {@inheritDoc}
050     */
051    @Override
052    public String getTitle() {
053        return prependConnToString(Bundle.getMessage("MenuItemNetworkSim"));
054    }
055
056    public SimulatorPane() {
057        super();
058    }
059
060    private void init() {
061
062        _disposeSimOnWindowClose=false;
063
064        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
065
066        p1 = new JPanel();
067        p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
068
069        _csPanes = new JPanel();
070        _evPanes = new JPanel();
071        _ndPanes = new JPanel();
072        _csPanes.setLayout(new BoxLayout(_csPanes, BoxLayout.Y_AXIS));
073        _evPanes.setLayout(new BoxLayout(_evPanes, BoxLayout.Y_AXIS));
074        _ndPanes.setLayout(new BoxLayout(_ndPanes, BoxLayout.Y_AXIS));
075
076        _csPanes.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("CmndStations")));
077        _evPanes.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("ResponseEvents")));
078        _ndPanes.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("CbusNodes")));
079
080        for ( int i=0 ; ( i < _sim.getNumCS() ) ; i++ ) {
081            CsPane thispane = new CsPane(_sim.getCS(i)); // id , type
082            _csPanes.add(thispane);
083            thispane.setVisible(true);
084        }
085
086        for ( int i=0 ; ( i < _sim.getNumNd() ) ; i++ ) {
087            NdPane thispanend = new NdPane(_sim.getNd(i), memo); // id , type
088            _ndPanes.add(thispanend);
089            thispanend.setVisible(true);
090        }
091        // and add an Empty pane
092        NdPane thispanend = new NdPane(null, memo); // id , type
093        _ndPanes.add(thispanend);
094        thispanend.setVisible(true);
095
096        for ( int i=0 ; ( i < _sim.getNumEv() ) ; i++ ) {
097            EvResponderPane thispane = new EvResponderPane(_sim.getEv(i)); // id , mode
098            _evPanes.add(thispane);
099            thispane.setVisible(true);
100        }
101
102        _csPanes.setVisible(true);
103        _evPanes.setVisible(true);
104        _ndPanes.setVisible(true);
105
106        p1.add(_csPanes);
107        p1.add(_ndPanes);
108        p1.add(_evPanes);
109
110        mainScroll = new JScrollPane (p1);
111        this.add(mainScroll);
112    }
113
114    /**
115     * Creates a Menu List
116     */
117    @Override
118    public List<JMenu> getMenus() {
119        List<JMenu> menuList = new ArrayList<>();
120        JMenu optionsMenu = new JMenu(Bundle.getMessage("OptionsMenu"));
121        JMenu addMenu = new JMenu(Bundle.getMessage("MenuAdd"));
122
123        JCheckBoxMenuItem closeSimOnDispose = new JCheckBoxMenuItem(Bundle.getMessage("StopSimWinClose"));
124        closeSimOnDispose.setSelected(false);
125        closeSimOnDispose.addActionListener ((ActionEvent e) -> {
126            _disposeSimOnWindowClose = closeSimOnDispose.isSelected();
127        });
128
129        JMenuItem newCs = new JMenuItem(Bundle.getMessage("CommandStation"));
130        newCs.addActionListener ((ActionEvent e) -> {
131            CsPane thispane = new CsPane(_sim.getNewCS());
132            _csPanes.add(thispane);
133            revalidate();
134        });
135
136        JMenuItem newEv = new JMenuItem(Bundle.getMessage("EventResponders"));
137        newEv.addActionListener ((ActionEvent e) -> {
138            EvResponderPane thispane = new EvResponderPane(_sim.getNewEv());
139            _evPanes.add(thispane);
140            revalidate();
141        });
142
143        JMenuItem newNd = new JMenuItem(Bundle.getMessage("CbusNode"));
144        newNd.addActionListener ((ActionEvent e) -> {
145            NdPane thispanend = new NdPane( null, memo);
146            _ndPanes.add(thispanend);
147            revalidate();
148        });
149
150
151        optionsMenu.add(closeSimOnDispose);
152        addMenu.add(newCs);
153        addMenu.add(newEv);
154        addMenu.add(newNd);
155
156        menuList.add(optionsMenu);
157        menuList.add(addMenu);
158        return menuList;
159    }
160
161    /**
162     * {@inheritDoc}
163     */
164    @Override
165    public String getHelpTarget() {
166        return "package.jmri.jmrix.can.cbus.swing.simulator.SimulatorPane";
167    }
168
169    /**
170     * {@inheritDoc}
171     */
172    @Override
173    public void dispose() {
174        super.dispose();
175        if ( memo != null && _disposeSimOnWindowClose ) {
176            memo.get(CbusConfigurationManager.class)
177                .disposeOf(_sim, CbusSimulator.class);
178        }
179    }
180
181    /**
182     * Nested class to create one of these using old-style defaults.
183     */
184    static public class Default extends jmri.jmrix.can.swing.CanNamedPaneAction {
185
186        public Default() {
187            super(Bundle.getMessage("MenuItemNetworkSim"),
188                    new jmri.util.swing.sdi.JmriJFrameInterface(),
189                    SimulatorPane.class.getName(),
190                    jmri.InstanceManager.getDefault(CanSystemConnectionMemo.class));
191        }
192    }
193    // private final static Logger log = LoggerFactory.getLogger(SimulatorPane.class);
194}