001package jmri.jmrit.logixng.util.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.NamedBeanAddressing;
006import jmri.jmrit.logixng.util.LogixNG_SelectInteger;
007import jmri.jmrit.logixng.util.parser.ParserException;
008
009import org.jdom2.Element;
010
011/**
012 * Xml class for jmri.jmrit.logixng.util.LogixNG_SelectInteger.
013 *
014 * @author Daniel Bergqvist (C) 2022
015 */
016public class LogixNG_SelectIntegerXml {
017
018    /**
019     * Default implementation for storing the contents of a LogixNG_SelectEnum
020     *
021     * @param selectInt the LogixNG_SelectInteger object
022     * @param tagName the name of the element
023     * @return Element containing the complete info
024     */
025    public Element store(LogixNG_SelectInteger selectInt, String tagName) {
026
027        LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
028
029        Element intElement = new Element(tagName);
030
031        intElement.addContent(new Element("addressing").addContent(selectInt.getAddressing().name()));
032        intElement.addContent(new Element("value").addContent(Integer.toString(selectInt.getValue())));
033        if (selectInt.getReference() != null && !selectInt.getReference().isEmpty()) {
034            intElement.addContent(new Element("reference").addContent(selectInt.getReference()));
035        }
036        var memory = selectInt.getMemory();
037        if (memory != null) {
038            intElement.addContent(new Element("memory").addContent(memory.getName()));
039        }
040        intElement.addContent(new Element("listenToMemory").addContent(selectInt.getListenToMemory() ? "yes" : "no"));
041        if (selectInt.getLocalVariable() != null && !selectInt.getLocalVariable().isEmpty()) {
042            intElement.addContent(new Element("localVariable").addContent(selectInt.getLocalVariable()));
043        }
044        if (selectInt.getFormula() != null && !selectInt.getFormula().isEmpty()) {
045            intElement.addContent(new Element("formula").addContent(selectInt.getFormula()));
046        }
047
048        if (selectInt.getAddressing() == NamedBeanAddressing.Table) {
049            intElement.addContent(selectTableXml.store(selectInt.getSelectTable(), "table"));
050        }
051
052        return intElement;
053    }
054
055    public void load(Element intElement, LogixNG_SelectInteger selectInt)
056            throws JmriConfigureXmlException {
057
058        if (intElement != null) {
059
060            LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
061
062            try {
063                Element elem = intElement.getChild("addressing");
064                if (elem != null) {
065                    selectInt.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
066                }
067
068                elem = intElement.getChild("value");
069                if (elem != null) {
070                    selectInt.setValue(Integer.parseInt(elem.getTextTrim()));
071                }
072
073                elem = intElement.getChild("reference");
074                if (elem != null) selectInt.setReference(elem.getTextTrim());
075
076                Element memoryName = intElement.getChild("memory");
077                if (memoryName != null) {
078                    Memory m = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName.getTextTrim());
079                    if (m != null) selectInt.setMemory(m);
080                    else selectInt.removeMemory();
081                }
082
083                Element listenToMemoryElem = intElement.getChild("listenToMemory");
084                if (listenToMemoryElem != null) {
085                    selectInt.setListenToMemory("yes".equals(listenToMemoryElem.getTextTrim()));
086                }
087
088                elem = intElement.getChild("localVariable");
089                if (elem != null) selectInt.setLocalVariable(elem.getTextTrim());
090
091                elem = intElement.getChild("formula");
092                if (elem != null) selectInt.setFormula(elem.getTextTrim());
093
094                if (intElement.getChild("table") != null) {
095                    selectTableXml.load(intElement.getChild("table"), selectInt.getSelectTable());
096                }
097
098            } catch (ParserException e) {
099                throw new JmriConfigureXmlException(e);
100            }
101        }
102    }
103
104    /**
105     * This method is for backward compability up to and including 4.99.4.Remove this class after 5.0.
106     *
107     * @param shared the shared element
108     * @param selectInt the LogixNG_SelectEnum
109     * @param addressingElementName the name of the element of the addressing
110     * @param valueElementName the name of the element of the integer
111     * @param referenceElementName the name of the element of the reference
112     * @param localVariableElementName the name of the element of the local variable
113     * @param formulaElementName the name of the element of the formula
114     * @throws JmriConfigureXmlException if an exception occurs
115     */
116    public void loadLegacy(
117            Element shared,
118            LogixNG_SelectInteger selectInt,
119            String addressingElementName,
120            String valueElementName,
121            String referenceElementName,
122            String localVariableElementName,
123            String formulaElementName)
124            throws JmriConfigureXmlException {
125
126        Element name = shared.getChild(valueElementName);
127        if (name != null) {
128            selectInt.setValue(Integer.parseInt(name.getTextTrim()));
129        }
130
131        try {
132            Element elem = shared.getChild(addressingElementName);
133            if (addressingElementName != null && elem != null) {
134                selectInt.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
135            }
136
137            elem = shared.getChild(referenceElementName);
138            if (referenceElementName != null && elem != null) selectInt.setReference(elem.getTextTrim());
139
140            elem = shared.getChild(localVariableElementName);
141            if (localVariableElementName != null && elem != null) selectInt.setLocalVariable(elem.getTextTrim());
142
143            elem = shared.getChild(formulaElementName);
144            if (formulaElementName != null && elem != null) selectInt.setFormula(elem.getTextTrim());
145
146        } catch (ParserException e) {
147            throw new JmriConfigureXmlException(e);
148        }
149    }
150
151}