001package jmri.jmrix.dccpp.simulator.configurexml;
002
003import jmri.jmrix.configurexml.AbstractSerialConnectionConfigXml;
004//import jmri.jmrix.dccpp.configurexml.AbstractDCCppSerialConnectionConfigXml;
005import jmri.jmrix.dccpp.simulator.ConnectionConfig;
006import jmri.jmrix.dccpp.simulator.DCCppSimulatorAdapter;
007import org.jdom2.Element;
008
009import javax.annotation.Nonnull;
010
011/**
012 * Handle XML persistence of layout connections by persisting the
013 * DCCppSimulatorAdapter (and connections). Note this is named as the XML version
014 * of a ConnectionConfig object, but it's actually persisting the
015 * DCCppSimulatorAdapter.
016 * <p>
017 * This class is invoked from jmrix.JmrixConfigPaneXml on write, as that class
018 * is the one actually registered. Reads are brought here directly via the class
019 * attribute in the XML.
020 *
021 * @author Bob Jacobsen Copyright: Copyright (c) 2003
022 * @author Paul Bender Copyright: Copyright (c) 2009
023 * @author Mark Underwood Copyright: Copyright (c) 2015
024 */
025public class ConnectionConfigXml extends AbstractSerialConnectionConfigXml { //AbstractDCCppSerialConnectionConfigXml {
026
027    public ConnectionConfigXml() {
028        super();
029    }
030
031    /**
032     * A Simulator connection needs no extra information, so we reimplement the
033     * superclass method to just write the necessary parts.
034     *
035     * @return Formatted element containing no attributes except the class name
036     */
037    @Override
038    public Element store(Object o) {
039        getInstance(o);
040
041        Element e = new Element("connection");
042        storeCommon(e, adapter);
043
044        e.setAttribute("class", this.getClass().getName());
045
046        return e;
047    }
048
049    @Override
050    public boolean load(@Nonnull Element shared, Element perNode) {
051        boolean result = true;
052        // start the "connection"
053        getInstance();
054
055        loadCommon(shared, perNode, adapter);
056
057        // register, so can be picked up next time
058        register();
059
060        if (adapter.getDisabled()) {
061            unpackElement(shared, perNode);
062            return result;
063        }
064
065        adapter.configure();
066
067        return result;
068    }
069
070    @Override
071    protected void getInstance() {
072        if (adapter == null) {
073            adapter = new DCCppSimulatorAdapter();
074        }
075    }
076
077    @Override
078    protected void getInstance(Object object) {
079        adapter = ((ConnectionConfig) object).getAdapter();
080    }
081
082    @Override
083    protected void register() {
084        this.register(new ConnectionConfig(adapter));
085    }
086
087}