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        // Not used anymore. It's kept to not change old tables and panels files
041        intElement.addContent(new Element("listenToMemory").addContent("no"));
042        if (selectInt.getLocalVariable() != null && !selectInt.getLocalVariable().isEmpty()) {
043            intElement.addContent(new Element("localVariable").addContent(selectInt.getLocalVariable()));
044        }
045        if (selectInt.getFormula() != null && !selectInt.getFormula().isEmpty()) {
046            intElement.addContent(new Element("formula").addContent(selectInt.getFormula()));
047        }
048
049        if (selectInt.getAddressing() == NamedBeanAddressing.Table) {
050            intElement.addContent(selectTableXml.store(selectInt.getSelectTable(), "table"));
051        }
052
053        return intElement;
054    }
055
056    public void load(Element intElement, LogixNG_SelectInteger selectInt)
057            throws JmriConfigureXmlException {
058
059        if (intElement != null) {
060
061            LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
062
063            try {
064                Element elem = intElement.getChild("addressing");
065                if (elem != null) {
066                    selectInt.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
067                }
068
069                elem = intElement.getChild("value");
070                if (elem != null) {
071                    selectInt.setValue(Integer.parseInt(elem.getTextTrim()));
072                }
073
074                elem = intElement.getChild("reference");
075                if (elem != null) selectInt.setReference(elem.getTextTrim());
076
077                Element memoryName = intElement.getChild("memory");
078                if (memoryName != null) {
079                    Memory m = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName.getTextTrim());
080                    if (m != null) selectInt.setMemory(m);
081                    else selectInt.removeMemory();
082                }
083
084                elem = intElement.getChild("localVariable");
085                if (elem != null) selectInt.setLocalVariable(elem.getTextTrim());
086
087                elem = intElement.getChild("formula");
088                if (elem != null) selectInt.setFormula(elem.getTextTrim());
089
090                if (intElement.getChild("table") != null) {
091                    selectTableXml.load(intElement.getChild("table"), selectInt.getSelectTable());
092                }
093
094            } catch (ParserException e) {
095                throw new JmriConfigureXmlException(e);
096            }
097        }
098    }
099
100    /**
101     * This method is for backward compability up to and including 4.99.4.Remove this class after 5.0.
102     *
103     * @param shared the shared element
104     * @param selectInt the LogixNG_SelectEnum
105     * @param addressingElementName the name of the element of the addressing
106     * @param valueElementName the name of the element of the integer
107     * @param referenceElementName the name of the element of the reference
108     * @param localVariableElementName the name of the element of the local variable
109     * @param formulaElementName the name of the element of the formula
110     * @throws JmriConfigureXmlException if an exception occurs
111     */
112    public void loadLegacy(
113            Element shared,
114            LogixNG_SelectInteger selectInt,
115            String addressingElementName,
116            String valueElementName,
117            String referenceElementName,
118            String localVariableElementName,
119            String formulaElementName)
120            throws JmriConfigureXmlException {
121
122        Element name = shared.getChild(valueElementName);
123        if (name != null) {
124            selectInt.setValue(Integer.parseInt(name.getTextTrim()));
125        }
126
127        try {
128            Element elem = shared.getChild(addressingElementName);
129            if (addressingElementName != null && elem != null) {
130                selectInt.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
131            }
132
133            elem = shared.getChild(referenceElementName);
134            if (referenceElementName != null && elem != null) selectInt.setReference(elem.getTextTrim());
135
136            elem = shared.getChild(localVariableElementName);
137            if (localVariableElementName != null && elem != null) selectInt.setLocalVariable(elem.getTextTrim());
138
139            elem = shared.getChild(formulaElementName);
140            if (formulaElementName != null && elem != null) selectInt.setFormula(elem.getTextTrim());
141
142        } catch (ParserException e) {
143            throw new JmriConfigureXmlException(e);
144        }
145    }
146
147}