001package jmri.jmrix.acela.configurexml;
002
003import jmri.InstanceManager;
004import jmri.SignalHead;
005import jmri.jmrix.acela.AcelaAddress;
006import jmri.jmrix.acela.AcelaNode;
007import jmri.jmrix.acela.AcelaSignalHead;
008import jmri.jmrix.acela.AcelaSystemConnectionMemo;
009import org.jdom2.Attribute;
010import org.jdom2.Element;
011
012/**
013 * Handle XML configuration for AcelaSignalHead objects. based upon example of
014 * Grapevine by Bob Jacobsen
015 *
016 * @author Bob Coleman Copyright: Copyright (c) 2009
017 */
018public class AcelaSignalHeadXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
019
020    AcelaSystemConnectionMemo _memo = null;
021
022    public AcelaSignalHeadXml() {
023       _memo = InstanceManager.getDefault(AcelaSystemConnectionMemo.class);
024    }
025
026    /**
027     * Default implementation for storing the contents of an Acela
028     * AcelaSignalHead
029     *
030     * @param o Object to store, of type AcelaSignalHead
031     * @return Element containing the complete info
032     */
033    @Override
034    public Element store(Object o) {
035        AcelaSignalHead p = (AcelaSignalHead) o;
036
037        Element element = new Element("signalhead");
038        element.setAttribute("class", this.getClass().getName());
039
040        // include contents
041        element.setAttribute("systemName", p.getSystemName());
042        String tu;
043        tu = p.getUserName();
044        if (tu != null) {
045            element.setAttribute("userName", tu);
046        }
047        AcelaNode sh = AcelaAddress.getNodeFromSystemName(p.getSystemName(), _memo);
048        int rawaddr = AcelaAddress.getBitFromSystemName(p.getSystemName(), _memo.getSystemPrefix());
049        String shtype = sh.getOutputSignalHeadTypeString(rawaddr);
050        element.setAttribute("signalheadType", shtype);
051
052        storeCommon(p, element);
053
054        return element;
055    }
056
057    @Override
058    public boolean load(Element shared, Element perNode) {
059        String sys = shared.getAttribute("systemName").getValue();
060        Attribute a = shared.getAttribute("userName");
061        SignalHead h;
062        if (a == null) {
063            h = new AcelaSignalHead(sys, _memo);
064        } else {
065            h = new AcelaSignalHead(sys, a.getValue(), _memo);
066        }
067
068        Attribute t = shared.getAttribute("signalheadType");
069        String shtype;
070        if (t == null) {
071            shtype = "UKNOWN";
072        } else {
073            shtype = t.getValue();
074        }
075
076        loadCommon(h, shared);
077
078        InstanceManager.getDefault(jmri.SignalHeadManager.class).register(h);
079
080        AcelaNode sh = AcelaAddress.getNodeFromSystemName(sys, _memo);
081        int rawaddr = AcelaAddress.getBitFromSystemName(sys, _memo.getSystemPrefix());
082        sh.setOutputSignalHeadTypeString(rawaddr, shtype);
083
084        return true;
085    }
086
087//    private final static Logger log = LoggerFactory.getLogger(AcelaSignalHeadXml.class);
088}