001package jmri.jmrix.oaktree.serialdriver.configurexml;
002
003import java.util.List;
004import jmri.jmrix.configurexml.AbstractSerialConnectionConfigXml;
005import jmri.jmrix.oaktree.SerialNode;
006import jmri.jmrix.oaktree.OakTreeSystemConnectionMemo;
007import jmri.jmrix.oaktree.serialdriver.ConnectionConfig;
008import jmri.jmrix.oaktree.serialdriver.SerialDriverAdapter;
009import org.jdom2.Element;
010
011/**
012 * Handle XML persistance 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, 2006
023 */
024public class ConnectionConfigXml extends AbstractSerialConnectionConfigXml {
025
026    public ConnectionConfigXml() {
027        super();
028    }
029
030    /**
031     * Write out the SerialNode objects too.
032     *
033     * @param e Element being extended
034     */
035    @Override
036    protected void extendElement(Element e) {
037        SerialNode node = (SerialNode) ((OakTreeSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController().getNode(0);
038        int index = 1;
039        while (node != null) {
040            // add node as an element
041            Element n = new Element("node");
042            n.setAttribute("name", "" + node.getNodeAddress());
043            e.addContent(n);
044            // add parameters to the node as needed
045            n.addContent(makeParameter("nodetype", "" + node.getNodeType()));
046
047            // look for the next node
048            node = (SerialNode) ((OakTreeSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController().getNode(index);
049            index++;
050        }
051    }
052
053    protected Element makeParameter(String name, String value) {
054        Element p = new Element("parameter");
055        p.setAttribute("name", name);
056        p.addContent(value);
057        return p;
058    }
059
060    @Override
061    protected void getInstance() {
062        if (adapter == null) {
063           adapter = new SerialDriverAdapter();
064        }
065    }
066
067    @Override
068    protected void getInstance(Object object) {
069        adapter = ((ConnectionConfig) object).getAdapter();
070    }
071
072    @Override
073    protected void unpackElement(Element shared, Element perNode) {
074        List<Element> l = shared.getChildren("node");
075        for (int i = 0; i < l.size(); i++) {
076            Element n = l.get(i);
077            int addr = Integer.parseInt(n.getAttributeValue("name"));
078            int type = Integer.parseInt(findParmValue(n, "nodetype"));
079
080            // create node (they register themselves)
081            SerialNode node = new SerialNode(addr, type, (OakTreeSystemConnectionMemo)adapter.getSystemConnectionMemo());
082
083            // Trigger initialization of this Node to reflect these parameters
084            ((OakTreeSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController().initializeSerialNode(node);
085        }
086    }
087
088    @Override
089    protected void register() {
090        this.register(new ConnectionConfig(adapter));
091    }
092
093}