001package jmri.implementation.configurexml;
002
003import java.util.List;
004import jmri.InstanceManager;
005import jmri.NamedBeanHandle;
006import jmri.SignalHead;
007import jmri.Turnout;
008import jmri.implementation.TripleOutputSignalHead;
009import org.jdom2.Element;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Handle XML configuration for TripleOutputSignalHead objects.
015 *
016 * @author Suzie Tall based on work by Bob Jacobson
017 * @author Bob Jacobsen Copyright: Copyright (c) 2003, 2008
018 */
019public class TripleOutputSignalHeadXml extends DoubleTurnoutSignalHeadXml {
020
021    public TripleOutputSignalHeadXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of a
026     * TripleOutputSignalHead.
027     *
028     * @param o Object to store, of type TripleOutputSignalHead
029     * @return Element containing the complete info
030     */
031    @Override
032    public Element store(Object o) {
033        TripleOutputSignalHead p = (TripleOutputSignalHead) o;
034
035        Element element = new Element("signalhead");
036        element.setAttribute("class", this.getClass().getName());
037
038        // include contents
039        element.addContent(new Element("systemName").addContent(p.getSystemName()));
040
041        storeCommon(p, element);
042
043        element.addContent(addTurnoutElement(p.getGreen(), "green"));
044        element.addContent(addTurnoutElement(p.getBlue(), "blue"));
045        element.addContent(addTurnoutElement(p.getRed(), "red"));
046
047        return element;
048    }
049
050    @Override
051    public boolean load(Element shared, Element perNode) {
052        List<Element> l = shared.getChildren("turnoutname");
053        if (l.size() == 0) {
054            l = shared.getChildren("turnout");
055        }
056        NamedBeanHandle<Turnout> green = loadTurnout(l.get(0));
057        NamedBeanHandle<Turnout> blue = loadTurnout(l.get(1));
058        NamedBeanHandle<Turnout> red = loadTurnout(l.get(2));
059
060        // put it together
061        String sys = getSystemName(shared);
062        String uname = getUserName(shared);
063        SignalHead h;
064        if (uname == null) {
065            h = new TripleOutputSignalHead(sys, green, blue, red);
066        } else {
067            h = new TripleOutputSignalHead(sys, uname, green, blue, red);
068        }
069
070        loadCommon(h, shared);
071
072        SignalHead existingBean = 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    @Override
085    public void load(Element element, Object o) {
086        log.error("Invalid method called");
087    }
088
089    private final static Logger log = LoggerFactory.getLogger(TripleOutputSignalHeadXml.class);
090
091}