001package jmri.jmrix.can.adapters.loopback.configurexml;
002
003import jmri.jmrix.PortAdapter;
004import jmri.jmrix.can.adapters.loopback.ConnectionConfig;
005import jmri.jmrix.can.adapters.loopback.Port;
006import jmri.jmrix.configurexml.AbstractSerialConnectionConfigXml;
007import org.jdom2.Element;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Handle XML persistance of layout connections by persistening the CAN
013 * simulator (and connections).
014 * <p>
015 * This class is invoked from jmrix.JmrixConfigPaneXml on write, as that class
016 * is the one actually registered. Reads are brought here directly via the class
017 * attribute in the XML.
018 *
019 * @author Bob Jacobsen Copyright: Copyright (c) 2008, 2010
020 */
021public class ConnectionConfigXml extends AbstractSerialConnectionConfigXml {
022
023    public ConnectionConfigXml() {
024        super();
025    }
026
027    /**
028     * A simulated connection needs no extra information, so we reimplement the
029     * superclass method to just write the necessary parts.
030     *
031     * @param o object to store
032     * @return formatted element containing no attributes except the class name
033     */
034    @Override
035    public Element store(Object o) {
036
037        adapter = ((ConnectionConfig) o).getAdapter();
038        Element e = new Element("connection");
039
040        if (adapter.getCurrentPortName() != null) { // port not functional in loopback Sim, hidden in UI. Remove in store?
041            e.setAttribute("port", adapter.getCurrentPortName());
042        } else {
043            e.setAttribute("port", Bundle.getMessage("noneSelected"));
044        }
045        if (adapter.getManufacturer() != null) {
046            e.setAttribute("manufacturer", adapter.getManufacturer());
047        }
048        if (adapter.getSystemConnectionMemo() != null) {
049            e.setAttribute("userName", adapter.getSystemConnectionMemo().getUserName());
050            e.setAttribute("systemPrefix", adapter.getSystemConnectionMemo().getSystemPrefix());
051        }
052        if (adapter.getDisabled()) {
053            e.setAttribute("disabled", "yes");
054        } else {
055            e.setAttribute("disabled", "no");
056        }
057
058        saveOptions(e, adapter);
059        
060        setOutputInterval(adapter, e);
061
062        e.setAttribute("class", this.getClass().getName());
063
064        extendElement(e);
065
066        return e;
067    }
068
069    @Override
070    public boolean load(Element shared, Element perNode) {
071        boolean result = true;
072        getInstance();
073        // simulator has fewer options in the XML, so implement
074        // just needed ones       
075        if (adapter.getSystemConnectionMemo() != null) {
076            if (shared.getAttribute("userName") != null) {
077                adapter.getSystemConnectionMemo().setUserName(shared.getAttribute("userName").getValue());
078            }
079
080            if (shared.getAttribute("systemPrefix") != null) {
081                adapter.getSystemConnectionMemo().setSystemPrefix(shared.getAttribute("systemPrefix").getValue());
082            }
083        }
084        if (shared.getAttribute("option1") != null) {
085            String option1Setting = shared.getAttribute("option1").getValue();
086            adapter.configureOption1(option1Setting);
087        }
088
089        if (shared.getAttribute("manufacturer") != null) {
090            String mfg = shared.getAttribute("manufacturer").getValue();
091            adapter.setManufacturer(mfg);
092        }
093        if (shared.getAttribute("port") != null) { // port not functional in loopback Sim, hidden in UI. Remove in load?
094            String portName = shared.getAttribute("port").getValue();
095            adapter.setPort(portName);
096        }
097
098        if (shared.getAttribute("disabled") != null) {
099            String yesno = shared.getAttribute("disabled").getValue();
100            if ((yesno != null) && (!yesno.isEmpty())) {
101                if (yesno.equals("no")) {
102                    adapter.setDisabled(false);
103                } else if (yesno.equals("yes")) {
104                    adapter.setDisabled(true);
105                }
106            }
107        }
108        loadOptions(shared.getChild("options"), perNode.getChild("options"), adapter);
109        // register, so can be picked up next time
110        register();
111
112        if (adapter.getDisabled()) {
113            unpackElement(shared, perNode);
114            return result;
115        }
116        adapter.configure();
117        
118        if (perNode.getAttribute("turnoutInterval") != null) {
119            adapter.getSystemConnectionMemo().setOutputInterval(Integer.parseInt(perNode.getAttribute("turnoutInterval").getValue()));
120        }
121
122        return result;
123    }
124
125    @Override
126    protected void getInstance() {
127        adapter = new Port();
128    }
129
130    @Override
131    protected void getInstance(Object object) {
132        adapter = ((ConnectionConfig) object).getAdapter();
133    }
134
135    @Override
136    protected void register() {
137        this.register(new ConnectionConfig(adapter));
138        log.info("CAN Simulator Started");
139    }
140
141    @Override
142    protected void loadOptions(Element shared, Element perNode, PortAdapter adapter) {
143        super.loadOptions(shared, perNode, adapter);
144
145        jmri.jmrix.openlcb.configurexml.ConnectionConfigXml.maybeLoadOlcbProfileSettings(
146                shared.getParentElement(), perNode.getParentElement(), adapter);
147    }
148
149    @Override
150    protected void extendElement(Element e) {
151        jmri.jmrix.openlcb.configurexml.ConnectionConfigXml.maybeSaveOlcbProfileSettings(
152                e, adapter);
153    }
154
155    // initialize logging
156    private final static Logger log = LoggerFactory.getLogger(ConnectionConfigXml.class);
157
158}