001package jmri.jmrit.logixng.util.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.NamedBeanAddressing;
006import jmri.jmrit.logixng.util.LogixNG_SelectEnum;
007import jmri.jmrit.logixng.util.parser.ParserException;
008
009import org.jdom2.Element;
010
011/**
012 * Xml class for jmri.jmrit.logixng.util.LogixNG_SelectEnum.
013 *
014 * @param <E> the type of enum
015 *
016 * @author Daniel Bergqvist (C) 2022
017 */
018public class LogixNG_SelectEnumXml<E extends Enum<?>> {
019
020    /**
021     * Default implementation for storing the contents of a LogixNG_SelectEnum
022     *
023     * @param selectEnum the LogixNG_SelectTable object
024     * @param tagName the name of the element
025     * @return Element containing the complete info
026     */
027    public Element store(LogixNG_SelectEnum<E> selectEnum, String tagName) {
028
029        LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
030
031        Element enumElement = new Element(tagName);
032
033        enumElement.addContent(new Element("addressing").addContent(selectEnum.getAddressing().name()));
034        enumElement.addContent(new Element("enum").addContent(selectEnum.getEnum().name()));
035        if (selectEnum.getReference() != null && !selectEnum.getReference().isEmpty()) {
036            enumElement.addContent(new Element("reference").addContent(selectEnum.getReference()));
037        }
038        var memory = selectEnum.getMemory();
039        if (memory != null) {
040            enumElement.addContent(new Element("memory").addContent(memory.getName()));
041        }
042        enumElement.addContent(new Element("listenToMemory").addContent(selectEnum.getListenToMemory() ? "yes" : "no"));
043        if (selectEnum.getLocalVariable() != null && !selectEnum.getLocalVariable().isEmpty()) {
044            enumElement.addContent(new Element("localVariable").addContent(selectEnum.getLocalVariable()));
045        }
046        if (selectEnum.getFormula() != null && !selectEnum.getFormula().isEmpty()) {
047            enumElement.addContent(new Element("formula").addContent(selectEnum.getFormula()));
048        }
049
050        if (selectEnum.getAddressing() == NamedBeanAddressing.Table) {
051            enumElement.addContent(selectTableXml.store(selectEnum.getSelectTable(), "table"));
052        }
053
054        return enumElement;
055    }
056
057    public void load(Element enumElement, LogixNG_SelectEnum<E> selectEnum)
058            throws JmriConfigureXmlException {
059
060        if (enumElement != null) {
061
062            LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
063
064            try {
065                Element elem = enumElement.getChild("addressing");
066                if (elem != null) {
067                    selectEnum.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
068                }
069
070                elem = enumElement.getChild("enum");
071                if (elem != null) {
072                    selectEnum.setEnum(selectEnum.getEnum(elem.getTextTrim()));
073                }
074
075                elem = enumElement.getChild("reference");
076                if (elem != null) selectEnum.setReference(elem.getTextTrim());
077
078                Element memoryName = enumElement.getChild("memory");
079                if (memoryName != null) {
080                    Memory m = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName.getTextTrim());
081                    if (m != null) selectEnum.setMemory(m);
082                    else selectEnum.removeMemory();
083                }
084
085                Element listenToMemoryElem = enumElement.getChild("listenToMemory");
086                if (listenToMemoryElem != null) {
087                    selectEnum.setListenToMemory("yes".equals(listenToMemoryElem.getTextTrim()));
088                }
089
090                elem = enumElement.getChild("localVariable");
091                if (elem != null) selectEnum.setLocalVariable(elem.getTextTrim());
092
093                elem = enumElement.getChild("formula");
094                if (elem != null) selectEnum.setFormula(elem.getTextTrim());
095
096                if (enumElement.getChild("table") != null) {
097                    selectTableXml.load(enumElement.getChild("table"), selectEnum.getSelectTable());
098                }
099
100            } catch (ParserException e) {
101                throw new JmriConfigureXmlException(e);
102            }
103        }
104    }
105
106    /**
107     * This method is for backward compability up to and including 4.99.4.Remove this class after 5.0.
108     *
109     * @param shared the shared element
110     * @param selectEnum the LogixNG_SelectEnum
111     * @param addressingElementName the name of the element of the addressing, for example "state"
112     * @param enumElementName the name of the element of the enum, for example "state"
113     * @param referenceElementName the name of the element of the reference, for example "state"
114     * @param localVariableElementName the name of the element of the local variable, for example "state"
115     * @param formulaElementName the name of the element of the formula, for example "state"
116     * @throws JmriConfigureXmlException if an exception occurs
117     */
118    public void loadLegacy(
119            Element shared,
120            LogixNG_SelectEnum<E> selectEnum,
121            String addressingElementName,
122            String enumElementName,
123            String referenceElementName,
124            String localVariableElementName,
125            String formulaElementName)
126            throws JmriConfigureXmlException {
127
128        Element name = shared.getChild(enumElementName);
129        if (name != null) {
130            selectEnum.setEnum(selectEnum.getEnum(name.getTextTrim()));
131        }
132
133        try {
134            Element elem;
135            if (addressingElementName != null) {
136                elem = shared.getChild(addressingElementName);
137                if (elem != null) {
138                    selectEnum.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
139                }
140            }
141
142            if (referenceElementName != null) {
143                elem = shared.getChild(referenceElementName);
144                if (elem != null) selectEnum.setReference(elem.getTextTrim());
145            }
146
147            if (localVariableElementName != null) {
148                elem = shared.getChild(localVariableElementName);
149                if (elem != null) selectEnum.setLocalVariable(elem.getTextTrim());
150            }
151
152            if (formulaElementName != null) {
153                elem = shared.getChild(formulaElementName);
154                if (elem != null) selectEnum.setFormula(elem.getTextTrim());
155            }
156        } catch (ParserException e) {
157            throw new JmriConfigureXmlException(e);
158        }
159    }
160
161}