001package jmri.jmrix.lenz.xnetsimulator.configurexml;
002
003import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
004import jmri.jmrix.SerialPortAdapter;
005import jmri.jmrix.configurexml.AbstractConnectionConfigXml;
006import jmri.jmrix.lenz.xnetsimulator.ConnectionConfig;
007import jmri.jmrix.lenz.xnetsimulator.XNetSimulatorAdapter;
008import org.jdom2.Element;
009
010/**
011 * Handle XML persistance of layout connections by persistening the
012 * XNetSimulatorAdapter (and connections). Note this is named as the XML version
013 * of a ConnectionConfig object, but it's actually persisting the
014 * XNetSimulatorAdapter.
015 * <p>
016 * This class is invoked from jmrix.JmrixConfigPaneXml on write, as that class
017 * is the one actually registered. Reads are brought here directly via the class
018 * attribute in the XML.
019 *
020 * @author Bob Jacobsen Copyright: Copyright (c) 2003
021 * @author Paul Bender Copyright: Copyright (c) 2009
022 */
023public class ConnectionConfigXml extends AbstractConnectionConfigXml {
024
025    public ConnectionConfigXml() {
026        super();
027    }
028
029    protected SerialPortAdapter adapter;
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    @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "if adapter is not initialized already, it is initialized by the getInstance() call")
050    @Override
051    public boolean load(Element shared, Element perNode) {
052        boolean result = true;
053        // start the "connection"
054        getInstance();
055
056        loadCommon(shared, perNode, adapter);
057
058        // register, so can be picked up next time
059        register();
060
061        if (adapter.getDisabled()) {
062            unpackElement(shared, perNode);
063            return result;
064        }
065
066        adapter.configure();
067
068        return result;
069    }
070
071    @Override
072    protected void getInstance() {
073        if (adapter == null) {
074            adapter = new XNetSimulatorAdapter();
075        }
076    }
077
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    @Override
088    protected void dispose() {
089        adapter.dispose();
090    }
091
092}