001package jmri.jmrit.logixng.actions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.DigitalActionManager;
006import jmri.jmrit.logixng.NamedBeanAddressing;
007import jmri.jmrit.logixng.actions.ActionLight;
008import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectEnumXml;
009import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
010import jmri.jmrit.logixng.util.parser.ParserException;
011
012import org.jdom2.Element;
013
014/**
015 * Handle XML configuration for ActionLightXml objects.
016 *
017 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
018 * @author Daniel Bergqvist Copyright (C) 2019
019 */
020public class ActionLightXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
021
022    public ActionLightXml() {
023    }
024
025    /**
026     * Default implementation for storing the contents of a SE8cSignalHead
027     *
028     * @param o Object to store, of type TripleLightSignalHead
029     * @return Element containing the complete info
030     */
031    @Override
032    public Element store(Object o) {
033        ActionLight p = (ActionLight) o;
034
035        Element element = new Element("ActionLight");
036        element.setAttribute("class", this.getClass().getName());
037        element.addContent(new Element("systemName").addContent(p.getSystemName()));
038
039        storeCommon(p, element);
040
041        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Light>();
042        var selectEnumXml = new LogixNG_SelectEnumXml<ActionLight.LightState>();
043
044        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
045        element.addContent(selectEnumXml.store(p.getSelectEnum(), "state"));
046
047        element.addContent(new Element("dataAddressing").addContent(p.getDataAddressing().name()));
048        element.addContent(new Element("dataReference").addContent(p.getDataReference()));
049        element.addContent(new Element("dataLocalVariable").addContent(p.getDataLocalVariable()));
050        element.addContent(new Element("dataFormula").addContent(p.getDataFormula()));
051
052        if (p.getLightValue() > 0) {
053            element.addContent(new Element("lightValue").addContent(Integer.toString(p.getLightValue())));
054        }
055
056        return element;
057    }
058
059    @Override
060    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
061        String sys = getSystemName(shared);
062        String uname = getUserName(shared);
063        ActionLight h = new ActionLight(sys, uname);
064
065        loadCommon(h, shared);
066
067        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Light>();
068        var selectEnumXml = new LogixNG_SelectEnumXml<ActionLight.LightState>();
069
070        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
071        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "light");
072
073        selectEnumXml.load(shared.getChild("state"), h.getSelectEnum());
074        selectEnumXml.loadLegacy(
075                shared, h.getSelectEnum(),
076                "stateAddressing",
077                "lightState",
078                "stateReference",
079                "stateLocalVariable",
080                "stateFormula");
081
082        try {
083            Element elem = shared.getChild("dataAddressing");
084            if (elem != null) {
085                h.setDataAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
086            }
087
088            elem = shared.getChild("dataReference");
089            if (elem != null) h.setDataReference(elem.getTextTrim());
090
091            elem = shared.getChild("dataLocalVariable");
092            if (elem != null) h.setDataLocalVariable(elem.getTextTrim());
093
094            elem = shared.getChild("dataFormula");
095            if (elem != null) h.setDataFormula(elem.getTextTrim());
096
097
098            elem = shared.getChild("lightValue");
099            if (elem != null) {
100                try {
101                    h.setLightValue(Integer.parseInt(elem.getTextTrim()));
102                } catch (NumberFormatException ex) {
103                    h.setLightValue(0);
104                }
105            }
106
107        } catch (ParserException e) {
108            throw new JmriConfigureXmlException(e);
109        }
110
111        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
112        return true;
113    }
114
115//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionLightXml.class);
116}