001package jmri.jmrit.display.logixng.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.DigitalActionManager;
006import jmri.jmrit.logixng.NamedBeanAddressing;
007import jmri.jmrit.display.layoutEditor.LayoutTurnout;
008import jmri.jmrit.display.logixng.ActionLayoutTurnout;
009import jmri.jmrit.logixng.util.parser.ParserException;
010
011import org.jdom2.Element;
012
013/**
014 * Handle XML configuration for ActionLayoutTurnout objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright (C) 2022
018 */
019public class ActionLayoutTurnoutXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
020
021    public ActionLayoutTurnoutXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of a ActionLayoutTurnout
026     *
027     * @param o Object to store, of type TripleTurnoutSignalHead
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032        ActionLayoutTurnout p = (ActionLayoutTurnout) o;
033
034        Element element = new Element("DisplayActionLayoutTurnout");
035        element.setAttribute("class", this.getClass().getName());
036        element.addContent(new Element("systemName").addContent(p.getSystemName()));
037
038        storeCommon(p, element);
039
040        String layoutEditorName = p.getLayoutEditorName();
041        if (layoutEditorName != null) {
042            element.addContent(new Element("layoutEditorName").addContent(layoutEditorName));
043        }
044
045        LayoutTurnout layoutTurnout = p.getLayoutTurnout();
046        if (layoutTurnout != null) {
047            element.addContent(new Element("layoutTurnout")
048                    .addContent(layoutTurnout.getTurnoutName()));
049        }
050
051        element.addContent(new Element("addressing").addContent(p.getAddressing().name()));
052        element.addContent(new Element("reference").addContent(p.getReference()));
053        element.addContent(new Element("localVariable").addContent(p.getLocalVariable()));
054        element.addContent(new Element("formula").addContent(p.getFormula()));
055
056        element.addContent(new Element("stateAddressing").addContent(p.getStateAddressing().name()));
057        element.addContent(new Element("operation").addContent(p.getOperation().name()));
058        element.addContent(new Element("stateReference").addContent(p.getStateReference()));
059        element.addContent(new Element("stateLocalVariable").addContent(p.getStateLocalVariable()));
060        element.addContent(new Element("stateFormula").addContent(p.getStateFormula()));
061
062        return element;
063    }
064
065    @Override
066    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {     // Test class that inherits this class throws exception
067        String sys = getSystemName(shared);
068        String uname = getUserName(shared);
069        ActionLayoutTurnout h = new ActionLayoutTurnout(sys, uname);
070
071        loadCommon(h, shared);
072
073        Element elem = shared.getChild("layoutEditorName");
074        if (elem != null) {
075            h.setLayoutEditor(elem.getTextTrim());
076        }
077
078        elem = shared.getChild("layoutTurnout");
079        if (elem != null) {
080            h.setLayoutTurnout(elem.getTextTrim());
081        }
082
083        try {
084            elem = shared.getChild("addressing");
085            if (elem != null) {
086                h.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
087            }
088
089            elem = shared.getChild("reference");
090            if (elem != null) h.setReference(elem.getTextTrim());
091
092            elem = shared.getChild("localVariable");
093            if (elem != null) h.setLocalVariable(elem.getTextTrim());
094
095            elem = shared.getChild("formula");
096            if (elem != null) h.setFormula(elem.getTextTrim());
097
098
099            elem = shared.getChild("stateAddressing");
100            if (elem != null) {
101                h.setStateAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
102            }
103
104            Element isControlling = shared.getChild("operation");
105            if (isControlling != null) {
106                h.setOperation(ActionLayoutTurnout.Operation.valueOf(isControlling.getTextTrim()));
107            }
108
109            elem = shared.getChild("stateReference");
110            if (elem != null) h.setStateReference(elem.getTextTrim());
111
112            elem = shared.getChild("stateLocalVariable");
113            if (elem != null) h.setStateLocalVariable(elem.getTextTrim());
114
115            elem = shared.getChild("stateFormula");
116            if (elem != null) h.setStateFormula(elem.getTextTrim());
117
118        } catch (ParserException e) {
119            throw new JmriConfigureXmlException(e);
120        }
121
122        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
123        return true;
124    }
125
126//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionLayoutTurnoutXml.class);
127}