001package jmri.jmrit.logixng.util.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.NamedBeanAddressing;
006import jmri.jmrit.logixng.util.LogixNG_SelectBoolean;
007import jmri.jmrit.logixng.util.parser.ParserException;
008
009import org.jdom2.Element;
010
011/**
012 * Xml class for jmri.jmrit.logixng.util.LogixNG_SelectBoolean.
013 *
014 * @author Daniel Bergqvist (C) 2023
015 */
016public class LogixNG_SelectBooleanXml {
017
018    /**
019     * Default implementation for storing the contents of a LogixNG_SelectBoolean
020     *
021     * @param selectBoolean the LogixNG_SelectBoolean object
022     * @param tagName the name of the element
023     * @return Element containing the complete info
024     */
025    public Element store(LogixNG_SelectBoolean selectBoolean, 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(selectBoolean.getAddressing().name()));
032        intElement.addContent(new Element("value").addContent(selectBoolean.getValue() ? "yes" : "no"));
033        if (selectBoolean.getReference() != null && !selectBoolean.getReference().isEmpty()) {
034            intElement.addContent(new Element("reference").addContent(selectBoolean.getReference()));
035        }
036        var memory = selectBoolean.getMemory();
037        if (memory != null) {
038            intElement.addContent(new Element("memory").addContent(memory.getName()));
039        }
040        intElement.addContent(new Element("listenToMemory").addContent(selectBoolean.getListenToMemory() ? "yes" : "no"));
041        if (selectBoolean.getLocalVariable() != null && !selectBoolean.getLocalVariable().isEmpty()) {
042            intElement.addContent(new Element("localVariable").addContent(selectBoolean.getLocalVariable()));
043        }
044        if (selectBoolean.getFormula() != null && !selectBoolean.getFormula().isEmpty()) {
045            intElement.addContent(new Element("formula").addContent(selectBoolean.getFormula()));
046        }
047
048        if (selectBoolean.getAddressing() == NamedBeanAddressing.Table) {
049            intElement.addContent(selectTableXml.store(selectBoolean.getSelectTable(), "table"));
050        }
051
052        return intElement;
053    }
054
055    public void load(Element booleanElement, LogixNG_SelectBoolean selectBoolean)
056            throws JmriConfigureXmlException {
057
058        if (booleanElement != null) {
059
060            LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
061
062            try {
063                Element elem = booleanElement.getChild("addressing");
064                if (elem != null) {
065                    selectBoolean.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
066                }
067
068                elem = booleanElement.getChild("value");
069                if (elem != null) {
070                    selectBoolean.setValue("yes".equals(elem.getTextTrim()));
071                }
072
073                elem = booleanElement.getChild("reference");
074                if (elem != null) selectBoolean.setReference(elem.getTextTrim());
075
076                Element memoryName = booleanElement.getChild("memory");
077                if (memoryName != null) {
078                    Memory m = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName.getTextTrim());
079                    if (m != null) selectBoolean.setMemory(m);
080                    else selectBoolean.removeMemory();
081                }
082
083                Element listenToMemoryElem = booleanElement.getChild("listenToMemory");
084                if (listenToMemoryElem != null) {
085                    selectBoolean.setListenToMemory("yes".equals(listenToMemoryElem.getTextTrim()));
086                }
087
088                elem = booleanElement.getChild("localVariable");
089                if (elem != null) selectBoolean.setLocalVariable(elem.getTextTrim());
090
091                elem = booleanElement.getChild("formula");
092                if (elem != null) selectBoolean.setFormula(elem.getTextTrim());
093
094                if (booleanElement.getChild("table") != null) {
095                    selectTableXml.load(booleanElement.getChild("table"), selectBoolean.getSelectTable());
096                }
097
098            } catch (ParserException e) {
099                throw new JmriConfigureXmlException(e);
100            }
101        }
102    }
103
104}