001package jmri.jmrix.loconet.configurexml;
002
003import java.util.List;
004import jmri.InstanceManager;
005import jmri.SignalHead;
006import jmri.jmrix.loconet.SE8cSignalHead;
007import jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML;
008import org.jdom2.DataConversionException;
009import org.jdom2.Element;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Handle XML configuration for loconet.SE8cSignalHead objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2003, 2008
017 */
018public class SE8cSignalHeadXml extends AbstractNamedBeanManagerConfigXML {
019
020    public SE8cSignalHeadXml() {
021    }
022
023    /**
024     * Default implementation for storing the contents of a SE8cSignalHead
025     *
026     * @param o Object to store, of type SE8cSignalHead
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        SE8cSignalHead p = (SE8cSignalHead) o;
032
033        Element element = new Element("signalhead"); // NOI18N
034        element.setAttribute("class", this.getClass().getName()); // NOI18N
035
036        // include contents
037        element.setAttribute("systemName", p.getSystemName()); // NOI18N
038        element.addContent(new Element("systemName").addContent(p.getSystemName())); // NOI18N
039
040        storeCommon(p, element);
041
042        // store the turnout number, not a name, as that's needed when recreating
043        element.addContent(addTurnoutElement(p.getNumber()));
044
045        return element;
046    }
047
048    Element addTurnoutElement(int number) {
049        Element el = new Element("turnout"); // NOI18N
050        el.setAttribute("systemName", "" + number); // NOI18N
051
052        return el;
053    }
054
055    @Override
056    public boolean load(Element shared, Element perNode) {
057        List<Element> l = shared.getChildren("turnout"); // NOI18N
058        int turnout = loadTurnout(l.get(0));
059        // put it together
060        String uname = getUserName(shared);
061        SignalHead h;
062        if (uname == null) {
063            h = new jmri.implementation.SE8cSignalHead(turnout);
064        } else {
065            h = new jmri.implementation.SE8cSignalHead(turnout, uname);
066        }
067
068        loadCommon(h, shared);
069
070        // replace if already present
071        SignalHead sh = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(h.getSystemName());
072        if (sh != null) InstanceManager.getDefault(jmri.SignalHeadManager.class).deregister(sh);
073
074        InstanceManager.getDefault(jmri.SignalHeadManager.class).register(h);
075        return true;
076    }
077
078    int loadTurnout(Object o) {
079        Element e = (Element) o;
080
081        // in this case, the systemName attribute is a number
082        try {
083            return e.getAttribute("systemName").getIntValue(); // NOI18N
084        } catch (DataConversionException ex) {
085            log.warn("Can't read turnout number for SE8cSignalHead because of exception", ex); // NOI18N
086            return 0;
087        }
088    }
089
090    @Override
091    public void load(Element element, Object o) {
092        log.error("Invalid method called"); // NOI18N
093    }
094
095    private final static Logger log = LoggerFactory.getLogger(SE8cSignalHeadXml.class);
096}