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