001package jmri.jmrix.cmri.serial.sim;
002
003import javax.annotation.Nonnull;
004import javax.swing.BoxLayout;
005import javax.swing.JButton;
006import javax.swing.JPanel;
007
008import jmri.jmrix.cmri.CMRISystemConnectionMemo;
009import jmri.jmrix.cmri.serial.nodeconfigmanager.NodeConfigManagerAction;
010
011/**
012 * Definition of objects to handle configuring a layout connection via a C/MRI
013 * Simulator object.
014 *
015 * @author Bob Jacobsen Copyright (C) 2001, 2003, 2008
016 * @author Chuck Catania Copyright (C) 2017
017 */
018public class ConnectionConfig extends jmri.jmrix.AbstractSimulatorConnectionConfig {
019
020    /**
021     * Ctor for an object being created during load process; Swing init is
022     * deferred.
023     * @param p serial port adapter.
024     */
025    public ConnectionConfig(jmri.jmrix.SerialPortAdapter p) {
026        super(p);
027    }
028
029    /**
030     * Ctor for a connection configuration with no preexisting adapter.
031     * {@link #setInstance()} will fill the adapter member.
032     */
033    public ConnectionConfig() {
034        super();
035    }
036
037    /**
038     * {@inheritDoc}
039     */
040    @Override
041    public void loadDetails(JPanel details) {
042
043        setInstance();
044
045        // have to embed the usual one in a new JPanel
046
047        JPanel p = new JPanel();
048        super.loadDetails(p);
049
050        details.setLayout(new BoxLayout(details, BoxLayout.Y_AXIS));
051        details.add(p);
052
053        // add another button
054        JButton b = new JButton(Bundle.getMessage("ConfigureNodesTitle"));
055
056        details.add(b);
057
058        b.addActionListener(new NodeConfigManagerAction((CMRISystemConnectionMemo)adapter.getSystemConnectionMemo()));
059    }
060
061    @Override
062    public String name() {
063        return "C/MRI Simulator";
064    }
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    protected void setInstance() {
071        if(adapter == null ) {
072           adapter = new SimDriverAdapter();
073           adapter.configure(); // make sure the traffic controller
074                                // loads so that node details can be
075                                // saved.
076        }
077    }
078
079    /** {@inheritDoc} */
080    @Override
081    public Config getConfig() {
082        return ((CMRISystemConnectionMemo) getAdapter().getSystemConnectionMemo())
083                .getConfig();
084    }
085
086    /** {@inheritDoc} */
087    @Override
088    public void setConfig(@Nonnull Config config) {
089        if (config instanceof CMRISystemConnectionMemo.Config) {
090            var memo = (CMRISystemConnectionMemo) getAdapter().getSystemConnectionMemo();
091            memo.setConfig((CMRISystemConnectionMemo.Config) config);
092            memo.restoreConfig();
093        } else {
094            log.info("Can't set config. Expected {} but got {}",
095                    config.getClass().getName(),
096                    CMRISystemConnectionMemo.Config.class.getName());
097        }
098    }
099
100    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConnectionConfig.class);
101}