001package jmri.implementation.configurexml;
002
003import java.util.List;
004import jmri.InstanceManager;
005import jmri.NamedBeanHandle;
006import jmri.SignalHead;
007import jmri.Turnout;
008import jmri.implementation.QuadOutputSignalHead;
009import org.jdom2.Element;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Handle XML configuration for QuadOutputSignalHead objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2009
017 */
018public class QuadOutputSignalHeadXml extends TripleTurnoutSignalHeadXml {
019
020    public QuadOutputSignalHeadXml() {
021    }
022
023    /**
024     * Default implementation for storing the contents of a QuadOutputSignalHead.
025     *
026     * @param o Object to store, of type TripleTurnoutSignalHead
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        QuadOutputSignalHead p = (QuadOutputSignalHead) o;
032
033        Element element = new Element("signalhead");
034        element.setAttribute("class", this.getClass().getName());
035
036        element.addContent(new Element("systemName").addContent(p.getSystemName()));
037
038        storeCommon(p, element);
039
040        element.addContent(addTurnoutElement(p.getGreen(), "green"));
041        element.addContent(addTurnoutElement(p.getYellow(), "yellow"));
042        element.addContent(addTurnoutElement(p.getRed(), "red"));
043        element.addContent(addTurnoutElement(p.getLunar(), "lunar"));
044
045        return element;
046    }
047
048    @Override
049    public boolean load(Element shared, Element perNode) {
050        List<Element> l = shared.getChildren("turnoutname");
051        if (l.size() == 0) {
052            l = shared.getChildren("turnout");
053        }
054        NamedBeanHandle<Turnout> green = loadTurnout(l.get(0));
055        NamedBeanHandle<Turnout> yellow = loadTurnout(l.get(1));
056        NamedBeanHandle<Turnout> red = loadTurnout(l.get(2));
057        NamedBeanHandle<Turnout> lunar = loadTurnout(l.get(3));
058
059        // put it together
060        String sys = getSystemName(shared);
061        String uname = getUserName(shared);
062        SignalHead h;
063        if (uname == null) {
064            h = new QuadOutputSignalHead(sys, green, yellow, red, lunar);
065        } else {
066            h = new QuadOutputSignalHead(sys, uname, green, yellow, red, lunar);
067        }
068
069        loadCommon(h, shared);
070
071        SignalHead existingBean =
072                InstanceManager.getDefault(jmri.SignalHeadManager.class)
073                        .getBySystemName(sys);
074
075        if ((existingBean != null) && (existingBean != h)) {
076            log.error("systemName is already registered: {}", sys);
077        } else {
078            InstanceManager.getDefault(jmri.SignalHeadManager.class).register(h);
079        }
080
081        return true;
082    }
083
084    private final static Logger log = LoggerFactory.getLogger(QuadOutputSignalHeadXml.class);
085
086}