001package jmri.jmrix.grapevine.serialdriver.configurexml;
002
003import java.util.List;
004import jmri.jmrix.configurexml.AbstractSerialConnectionConfigXml;
005import jmri.jmrix.grapevine.SerialNode;
006import jmri.jmrix.grapevine.SerialTrafficController;
007import jmri.jmrix.grapevine.serialdriver.ConnectionConfig;
008import jmri.jmrix.grapevine.serialdriver.SerialDriverAdapter;
009import jmri.jmrix.grapevine.GrapevineSystemConnectionMemo;
010import org.jdom2.Element;
011
012/**
013 * Handle XML persistance of layout connections by persisting the
014 * SerialDriverAdapter (and connections). Note this is named as the XML version
015 * of a ConnectionConfig object, but it's actually persisting the
016 * 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, 2007
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) ((GrapevineSystemConnectionMemo)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) ((GrapevineSystemConnectionMemo)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(Object object) {
062        adapter = ((ConnectionConfig) object).getAdapter();
063    }
064
065    @Override
066    protected void getInstance() {
067        if (adapter == null ) {
068           adapter = new SerialDriverAdapter();
069        }
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            SerialTrafficController tc = ((GrapevineSystemConnectionMemo) adapter.getSystemConnectionMemo()).getTrafficController();
081
082            // create node (they register themselves)
083            SerialNode node = new SerialNode(addr, type, tc);
084
085            // Trigger initialization of this Node to reflect these parameters
086            tc.initializeSerialNode(node);
087        }
088    }
089
090    @Override
091    protected void register() {
092        this.register(new ConnectionConfig(adapter));
093    }
094
095}