001package jmri.jmrix.bidib.serialdriver.configurexml;
002
003//import com.pi4j.io.serial.Serial;
004import jmri.jmrix.PortAdapter;
005import jmri.jmrix.configurexml.AbstractSerialConnectionConfigXml;
006import jmri.jmrix.bidib.serialdriver.ConnectionConfig;
007import jmri.jmrix.bidib.serialdriver.SerialDriverAdapter;
008import org.bidib.jbidibc.messages.utils.ByteUtils;
009import org.jdom2.Element;
010
011/**
012 * Handle XML persistance of layout connections by persistening the
013 * SerialDriverAdapter (and connections). Note this is named as the XML version
014 * of a ConnectionConfig object, but it's actually persisting the
015 * SerialDriverAdapter.
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 Eckart Meyer Copyright (C) 2019
023 */
024public class ConnectionConfigXml extends AbstractSerialConnectionConfigXml {
025
026    public ConnectionConfigXml() {
027        super();
028    }
029
030    @Override
031    protected void getInstance() {
032        adapter = new SerialDriverAdapter();
033    }
034
035    @Override
036    protected void getInstance(Object object) {
037        adapter = ((ConnectionConfig) object).getAdapter();
038    }
039
040    @Override
041    protected void register() {
042        this.register(new ConnectionConfig(adapter));
043    }
044
045    @Override
046    protected void loadCommon(Element shared, Element perNode, PortAdapter adapter) {
047        unpackElement1(shared, perNode); //we must have those attributes before opening the line (open is in load after loadCommon but before unpackElement)
048        super.loadCommon(shared, perNode, adapter);
049    }
050    /**
051     * Write out the SerialNode objects too
052     *
053     * @param e Element being extended
054     */
055    @Override
056    protected void extendElement(Element e) {
057        SerialDriverAdapter a = (SerialDriverAdapter)adapter;
058        if (a.getUseAutoScan()) {
059            e.setAttribute("autoScan", "true");
060        }
061        else {
062            e.setAttribute("autoScan", "false");
063        }
064        if (a.getRootNodeUid() != null) {
065            e.setAttribute("rootNodeUID", ByteUtils.formatHexUniqueId(a.getRootNodeUid()));
066        }
067        if (!a.getPortNameFilter().isEmpty()) {
068            e.setAttribute("portNameFilter", a.getPortNameFilter());
069        }
070    }
071    
072    /**
073     * Same as unpackElement() in super class, but that one is called from load() too late.
074     * Get the additional parameters from XML.
075     * 
076     * @see #unpackElement
077     * @param shared selected Element
078     * @param perNode from super class
079     */
080    protected void unpackElement1(Element shared, Element perNode) {
081        SerialDriverAdapter a = (SerialDriverAdapter)adapter;
082        if (shared.getAttribute("autoScan") != null) {
083            a.setUseAutoScan( shared.getAttributeValue("autoScan").equals("true"));
084        }
085        if (shared.getAttribute("rootNodeUID") != null) {
086            a.setRootNodeUid(ByteUtils.parseHexUniqueId(shared.getAttributeValue("rootNodeUID")));
087        }
088        if (shared.getAttribute("portNameFilter") != null) {
089            a.setPortNameFilter(shared.getAttributeValue("portNameFilter"));
090        }
091    }
092}