001package jmri.jmrix.secsi.simulator.configurexml; 002 003import java.util.List; 004import jmri.jmrix.configurexml.AbstractSerialConnectionConfigXml; 005import jmri.jmrix.secsi.SerialNode; 006import jmri.jmrix.secsi.SerialTrafficController; 007import jmri.jmrix.secsi.simulator.ConnectionConfig; 008import jmri.jmrix.secsi.simulator.SimulatorAdapter; 009import jmri.jmrix.secsi.SecsiSystemConnectionMemo; 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) ((SecsiSystemConnectionMemo)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) ((SecsiSystemConnectionMemo)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 = ((SecsiSystemConnectionMemo)adapter.getSystemConnectionMemo()).getTrafficController(); 083 // create node (they register themselves) 084 SerialNode node = new SerialNode(addr, type, tc); 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}