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