001package jmri.jmrit.logixng.util.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.NamedBeanAddressing;
006import jmri.jmrit.logixng.util.LogixNG_SelectDouble;
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_SelectDoubleXml {
017
018    /**
019     * Default implementation for storing the contents of a LogixNG_SelectEnum
020     *
021     * @param selectDouble 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_SelectDouble selectDouble, String tagName) {
026
027        LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
028
029        Element doubleElement = new Element(tagName);
030
031        doubleElement.addContent(new Element("addressing").addContent(selectDouble.getAddressing().name()));
032        doubleElement.addContent(new Element("value").addContent(selectDouble.formatValue(selectDouble.getValue())));
033        if (selectDouble.getReference() != null && !selectDouble.getReference().isEmpty()) {
034            doubleElement.addContent(new Element("reference").addContent(selectDouble.getReference()));
035        }
036        var memory = selectDouble.getMemory();
037        if (memory != null) {
038            doubleElement.addContent(new Element("memory").addContent(memory.getName()));
039        }
040        // Not used anymore. It's kept to not change old tables and panels files
041        doubleElement.addContent(new Element("listenToMemory").addContent("no"));
042        if (selectDouble.getLocalVariable() != null && !selectDouble.getLocalVariable().isEmpty()) {
043            doubleElement.addContent(new Element("localVariable").addContent(selectDouble.getLocalVariable()));
044        }
045        if (selectDouble.getFormula() != null && !selectDouble.getFormula().isEmpty()) {
046            doubleElement.addContent(new Element("formula").addContent(selectDouble.getFormula()));
047        }
048
049        if (selectDouble.getAddressing() == NamedBeanAddressing.Table) {
050            doubleElement.addContent(selectTableXml.store(selectDouble.getSelectTable(), "table"));
051        }
052
053        return doubleElement;
054    }
055
056    public void load(Element doubleElement, LogixNG_SelectDouble selectDouble)
057            throws JmriConfigureXmlException {
058
059        if (doubleElement != null) {
060
061            LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
062
063            try {
064                Element elem = doubleElement.getChild("addressing");
065                if (elem != null) {
066                    selectDouble.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
067                }
068
069                elem = doubleElement.getChild("value");
070                if (elem != null) {
071                    selectDouble.setValue(Double.parseDouble(elem.getTextTrim()));
072                }
073
074                elem = doubleElement.getChild("reference");
075                if (elem != null) selectDouble.setReference(elem.getTextTrim());
076
077                Element memoryName = doubleElement.getChild("memory");
078                if (memoryName != null) {
079                    Memory m = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName.getTextTrim());
080                    if (m != null) selectDouble.setMemory(m);
081                    else selectDouble.removeMemory();
082                }
083
084                elem = doubleElement.getChild("localVariable");
085                if (elem != null) selectDouble.setLocalVariable(elem.getTextTrim());
086
087                elem = doubleElement.getChild("formula");
088                if (elem != null) selectDouble.setFormula(elem.getTextTrim());
089
090                if (doubleElement.getChild("table") != null) {
091                    selectTableXml.load(doubleElement.getChild("table"), selectDouble.getSelectTable());
092                }
093
094            } catch (ParserException e) {
095                throw new JmriConfigureXmlException(e);
096            }
097        }
098    }
099
100}