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        doubleElement.addContent(new Element("listenToMemory").addContent(selectDouble.getListenToMemory() ? "yes" : "no"));
041        if (selectDouble.getLocalVariable() != null && !selectDouble.getLocalVariable().isEmpty()) {
042            doubleElement.addContent(new Element("localVariable").addContent(selectDouble.getLocalVariable()));
043        }
044        if (selectDouble.getFormula() != null && !selectDouble.getFormula().isEmpty()) {
045            doubleElement.addContent(new Element("formula").addContent(selectDouble.getFormula()));
046        }
047
048        if (selectDouble.getAddressing() == NamedBeanAddressing.Table) {
049            doubleElement.addContent(selectTableXml.store(selectDouble.getSelectTable(), "table"));
050        }
051
052        return doubleElement;
053    }
054
055    public void load(Element doubleElement, LogixNG_SelectDouble selectDouble)
056            throws JmriConfigureXmlException {
057
058        if (doubleElement != null) {
059
060            LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
061
062            try {
063                Element elem = doubleElement.getChild("addressing");
064                if (elem != null) {
065                    selectDouble.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
066                }
067
068                elem = doubleElement.getChild("value");
069                if (elem != null) {
070                    selectDouble.setValue(Double.parseDouble(elem.getTextTrim()));
071                }
072
073                elem = doubleElement.getChild("reference");
074                if (elem != null) selectDouble.setReference(elem.getTextTrim());
075
076                Element memoryName = doubleElement.getChild("memory");
077                if (memoryName != null) {
078                    Memory m = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName.getTextTrim());
079                    if (m != null) selectDouble.setMemory(m);
080                    else selectDouble.removeMemory();
081                }
082
083                Element listenToMemoryElem = doubleElement.getChild("listenToMemory");
084                if (listenToMemoryElem != null) {
085                    selectDouble.setListenToMemory("yes".equals(listenToMemoryElem.getTextTrim()));
086                }
087
088                elem = doubleElement.getChild("localVariable");
089                if (elem != null) selectDouble.setLocalVariable(elem.getTextTrim());
090
091                elem = doubleElement.getChild("formula");
092                if (elem != null) selectDouble.setFormula(elem.getTextTrim());
093
094                if (doubleElement.getChild("table") != null) {
095                    selectTableXml.load(doubleElement.getChild("table"), selectDouble.getSelectTable());
096                }
097
098            } catch (ParserException e) {
099                throw new JmriConfigureXmlException(e);
100            }
101        }
102    }
103
104}