001package jmri.jmrix.oaktree.simulator.configurexml;
002
003import java.util.List;
004import jmri.jmrix.configurexml.AbstractSerialConnectionConfigXml;
005import jmri.jmrix.oaktree.SerialNode;
006import jmri.jmrix.oaktree.simulator.ConnectionConfig;
007import jmri.jmrix.oaktree.simulator.SimulatorAdapter;
008import jmri.jmrix.oaktree.OakTreeSystemConnectionMemo;
009import org.jdom2.Element;
010
011/**
012 * Handle XML persistence of layout connections by persisting the
013 * SerialDriverAdapter (and connections).
014 * <p>
015 * Note this is named as the XML version of a ConnectionConfig object,
016 * but it's actually persisting the SerialDriverAdapter.
017 * <p>
018 * This class is invoked from jmrix.JmrixConfigPaneXml on write, as that class
019 * is the one actually registered. Reads are brought here directly via the class
020 * attribute in the XML.
021 *
022 * @author Bob Jacobsen Copyright (c) 2003 copied from NCE/Tams code
023 * @author kcameron Copyright (c) 2014
024 */
025public class ConnectionConfigXml extends AbstractSerialConnectionConfigXml {
026
027    public ConnectionConfigXml() {
028        super();
029    }
030
031    /**
032     * Write out the SerialNode objects too.
033     *
034     * @param e Element being extended
035     */
036    @Override
037    protected void extendElement(Element e) {
038        SerialNode node = (SerialNode) ((OakTreeSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController().getNode(0);
039        int index = 1;
040        while (node != null) {
041            // add node as an element
042            Element n = new Element("node");
043            n.setAttribute("name", "" + node.getNodeAddress());
044            e.addContent(n);
045            // add parameters to the node as needed
046            n.addContent(makeParameter("nodetype", "" + node.getNodeType()));
047
048            // look for the next node
049            node = (SerialNode) ((OakTreeSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController().getNode(index);
050            index++;
051        }
052    }
053
054    protected Element makeParameter(String name, String value) {
055        Element p = new Element("parameter");
056        p.setAttribute("name", name);
057        p.addContent(value);
058        return p;
059    }
060
061    @Override
062    protected void getInstance() {
063        if (adapter == null) {
064            adapter = new SimulatorAdapter();
065        }
066    }
067
068    @Override
069    protected void getInstance(Object object) {
070        adapter = ((ConnectionConfig) object).getAdapter();
071    }
072
073    @Override
074    protected void unpackElement(Element shared, Element perNode) {
075        List<Element> l = shared.getChildren("node");
076        for (int i = 0; i < l.size(); i++) {
077            Element n = l.get(i);
078            int addr = Integer.parseInt(n.getAttributeValue("name"));
079            int type = Integer.parseInt(findParmValue(n, "nodetype"));
080
081            // create node (they register themselves)
082            SerialNode node = new SerialNode(addr, type, (OakTreeSystemConnectionMemo)adapter.getSystemConnectionMemo());
083
084            // Trigger initialization of this Node to reflect these parameters
085            ((OakTreeSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController().initializeSerialNode(node);
086        }
087    }
088
089    @Override
090    protected void register() {
091        this.register(new ConnectionConfig(adapter));
092    }
093
094}